Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit ba7b373

Browse files
author
Lakshmi Priya Sekar
committed
Use async methods for connecting to tcpclient.
1 parent bd2910e commit ba7b373

File tree

1 file changed

+54
-13
lines changed

1 file changed

+54
-13
lines changed

src/System.Net.Mail/src/System/Net/Mail/SmtpConnection.cs

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,25 @@ internal X509CertificateCollection ClientCertificates
153153

154154
internal void InitializeConnection(string host, int port)
155155
{
156-
_tcpClient.ConnectAsync(host, port).GetAwaiter().GetResult();
156+
_tcpClient.Connect(host, port);
157+
_networkStream = _tcpClient.GetStream();
158+
}
159+
160+
internal IAsyncResult BeginInitializeConnection(string host, int port, AsyncCallback callback, object state)
161+
{
162+
return _tcpClient.BeginConnect(host, port, callback, state);
163+
}
164+
165+
internal void EndInitializeConnection(IAsyncResult result)
166+
{
167+
_tcpClient.EndConnect(result);
157168
_networkStream = _tcpClient.GetStream();
158169
}
159170

160171
internal IAsyncResult BeginGetConnection(ContextAwareResult outerResult, AsyncCallback callback, object state, string host, int port)
161172
{
162173
ConnectAndHandshakeAsyncResult result = new ConnectAndHandshakeAsyncResult(this, host, port, outerResult, callback, state);
163-
result.GetConnection(false);
174+
result.GetConnection();
164175
return result;
165176
}
166177

@@ -559,31 +570,61 @@ internal static void End(IAsyncResult result)
559570
}
560571
}
561572

562-
internal void GetConnection(bool synchronous)
573+
internal void GetConnection()
563574
{
564575
if (GlobalLog.IsEnabled)
565576
{
566-
GlobalLog.Enter("ConnectAndHandshakeAsyncResult#" + LoggingHash.HashString(this) + "::Connect: sync=" + (synchronous ? "true" : "false"));
577+
GlobalLog.Enter("ConnectAndHandshakeAsyncResult#" + LoggingHash.HashString(this) + "::Connect:");
567578
}
568579
if (_connection._isConnected)
569580
{
570581
throw new InvalidOperationException(SR.SmtpAlreadyConnected);
571582
}
572583

573-
_connection.InitializeConnection(_host, _port);
584+
InitializeConnection();
585+
}
574586

575-
if (GlobalLog.IsEnabled)
587+
private void InitializeConnection()
588+
{
589+
IAsyncResult result = _connection.BeginInitializeConnection(_host, _port, InitializeConnectionCallback, this);
590+
if (result.CompletedSynchronously)
576591
{
577-
GlobalLog.Print("ConnectAndHandshakeAsyncResult#" + LoggingHash.HashString(this) + "::Connect returned" + LoggingHash.HashString(this));
578-
}
592+
_connection.EndInitializeConnection(result);
593+
if (GlobalLog.IsEnabled)
594+
{
595+
GlobalLog.Print("ConnectAndHandshakeAsyncResult#" + LoggingHash.HashString(this) + "::Connect returned" + LoggingHash.HashString(this));
596+
}
579597

580-
try
581-
{
582-
Handshake();
598+
try
599+
{
600+
Handshake();
601+
}
602+
catch (Exception e)
603+
{
604+
InvokeCallback(e);
605+
}
583606
}
584-
catch (Exception e)
607+
}
608+
609+
private static void InitializeConnectionCallback(IAsyncResult result)
610+
{
611+
if (!result.CompletedSynchronously)
585612
{
586-
InvokeCallback(e);
613+
ConnectAndHandshakeAsyncResult thisPtr = (ConnectAndHandshakeAsyncResult)result.AsyncState;
614+
thisPtr._connection.EndInitializeConnection(result);
615+
if (GlobalLog.IsEnabled)
616+
{
617+
GlobalLog.Print("ConnectAndHandshakeAsyncResult#" + LoggingHash.HashString(thisPtr) + "::Connect returned" + LoggingHash.HashString(thisPtr));
618+
}
619+
620+
try
621+
{
622+
thisPtr.Handshake();
623+
}
624+
catch (Exception e)
625+
{
626+
thisPtr.InvokeCallback(e);
627+
}
587628
}
588629
}
589630

0 commit comments

Comments
 (0)