Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Use async methods for connecting to tcpclient.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakshmi Priya Sekar committed Oct 10, 2016
1 parent bd2910e commit ba7b373
Showing 1 changed file with 54 additions and 13 deletions.
67 changes: 54 additions & 13 deletions src/System.Net.Mail/src/System/Net/Mail/SmtpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,25 @@ internal X509CertificateCollection ClientCertificates

internal void InitializeConnection(string host, int port)
{
_tcpClient.ConnectAsync(host, port).GetAwaiter().GetResult();
_tcpClient.Connect(host, port);
_networkStream = _tcpClient.GetStream();
}

internal IAsyncResult BeginInitializeConnection(string host, int port, AsyncCallback callback, object state)
{
return _tcpClient.BeginConnect(host, port, callback, state);
}

internal void EndInitializeConnection(IAsyncResult result)
{
_tcpClient.EndConnect(result);
_networkStream = _tcpClient.GetStream();
}

internal IAsyncResult BeginGetConnection(ContextAwareResult outerResult, AsyncCallback callback, object state, string host, int port)
{
ConnectAndHandshakeAsyncResult result = new ConnectAndHandshakeAsyncResult(this, host, port, outerResult, callback, state);
result.GetConnection(false);
result.GetConnection();
return result;
}

Expand Down Expand Up @@ -559,31 +570,61 @@ internal static void End(IAsyncResult result)
}
}

internal void GetConnection(bool synchronous)
internal void GetConnection()
{
if (GlobalLog.IsEnabled)
{
GlobalLog.Enter("ConnectAndHandshakeAsyncResult#" + LoggingHash.HashString(this) + "::Connect: sync=" + (synchronous ? "true" : "false"));
GlobalLog.Enter("ConnectAndHandshakeAsyncResult#" + LoggingHash.HashString(this) + "::Connect:");
}
if (_connection._isConnected)
{
throw new InvalidOperationException(SR.SmtpAlreadyConnected);
}

_connection.InitializeConnection(_host, _port);
InitializeConnection();
}

if (GlobalLog.IsEnabled)
private void InitializeConnection()
{
IAsyncResult result = _connection.BeginInitializeConnection(_host, _port, InitializeConnectionCallback, this);
if (result.CompletedSynchronously)
{
GlobalLog.Print("ConnectAndHandshakeAsyncResult#" + LoggingHash.HashString(this) + "::Connect returned" + LoggingHash.HashString(this));
}
_connection.EndInitializeConnection(result);
if (GlobalLog.IsEnabled)
{
GlobalLog.Print("ConnectAndHandshakeAsyncResult#" + LoggingHash.HashString(this) + "::Connect returned" + LoggingHash.HashString(this));
}

try
{
Handshake();
try
{
Handshake();
}
catch (Exception e)
{
InvokeCallback(e);
}
}
catch (Exception e)
}

private static void InitializeConnectionCallback(IAsyncResult result)
{
if (!result.CompletedSynchronously)
{
InvokeCallback(e);
ConnectAndHandshakeAsyncResult thisPtr = (ConnectAndHandshakeAsyncResult)result.AsyncState;
thisPtr._connection.EndInitializeConnection(result);
if (GlobalLog.IsEnabled)
{
GlobalLog.Print("ConnectAndHandshakeAsyncResult#" + LoggingHash.HashString(thisPtr) + "::Connect returned" + LoggingHash.HashString(thisPtr));
}

try
{
thisPtr.Handshake();
}
catch (Exception e)
{
thisPtr.InvokeCallback(e);
}
}
}

Expand Down

0 comments on commit ba7b373

Please sign in to comment.