internal void GetConnection(bool synchronous) { if (this.connection.isConnected) { throw new InvalidOperationException(SR.GetString("SmtpAlreadyConnected")); } SmtpPooledStream pooledStream = (SmtpPooledStream)this.connection.connectionPool.GetConnection(this, synchronous ? null : m_ConnectionCreatedCallback, this.connection.Timeout); if (pooledStream != null) { try { while ((pooledStream.creds != null) && (pooledStream.creds != this.connection.credentials)) { this.connection.connectionPool.PutConnection(pooledStream, pooledStream.Owner, this.connection.Timeout, false); pooledStream = (SmtpPooledStream)this.connection.connectionPool.GetConnection(this, synchronous ? null : m_ConnectionCreatedCallback, this.connection.Timeout); if (pooledStream == null) { return; } } pooledStream.creds = this.connection.credentials; pooledStream.Owner = this.connection; lock (this.connection) { this.connection.pooledStream = pooledStream; } this.Handshake(); } catch (Exception exception) { base.InvokeCallback(exception); } } }
private static void ConnectionCreatedCallback(object request, object state) { GlobalLog.Enter("ConnectAndHandshakeAsyncResult#" + ValidationHelper.HashString(request) + "::ConnectionCreatedCallback"); ConnectAndHandshakeAsyncResult ConnectAndHandshakeAsyncResult = (ConnectAndHandshakeAsyncResult)request; if (state is Exception) { ConnectAndHandshakeAsyncResult.InvokeCallback((Exception)state); return; } SmtpPooledStream pooledStream = (SmtpPooledStream)(PooledStream)state; try { while (pooledStream.creds != null && pooledStream.creds != ConnectAndHandshakeAsyncResult.connection.credentials) { GlobalLog.Print("ConnectAndHandshakeAsyncResult#" + ValidationHelper.HashString(request) + "::Connect pooledStream has wrong creds " + ValidationHelper.HashString(pooledStream)); ConnectAndHandshakeAsyncResult.connection.connectionPool.PutConnection(pooledStream, pooledStream.Owner, ConnectAndHandshakeAsyncResult.connection.Timeout, false); pooledStream = (SmtpPooledStream)ConnectAndHandshakeAsyncResult.connection.connectionPool.GetConnection((object)ConnectAndHandshakeAsyncResult, ConnectAndHandshakeAsyncResult.m_ConnectionCreatedCallback, ConnectAndHandshakeAsyncResult.connection.Timeout); if (pooledStream == null) { GlobalLog.Leave("ConnectAndHandshakeAsyncResult#" + ValidationHelper.HashString(request) + "::Connect returning asynchronously"); return; } } if (Logging.On) { Logging.Associate(Logging.Web, ConnectAndHandshakeAsyncResult.connection, pooledStream); } pooledStream.Owner = ConnectAndHandshakeAsyncResult.connection; //needs to be updated for gc reasons pooledStream.creds = ConnectAndHandshakeAsyncResult.connection.credentials; lock (ConnectAndHandshakeAsyncResult.connection) { //if we were cancelled while getting the connection, we should close and return if (ConnectAndHandshakeAsyncResult.connection.isClosed) { ConnectAndHandshakeAsyncResult.connection.connectionPool.PutConnection(pooledStream, pooledStream.Owner, ConnectAndHandshakeAsyncResult.connection.Timeout, false); GlobalLog.Print("ConnectAndHandshakeAsyncResult#" + ValidationHelper.HashString(request) + "::ConnectionCreatedCallback Connect was aborted " + ValidationHelper.HashString(pooledStream)); ConnectAndHandshakeAsyncResult.InvokeCallback(null); return; } ConnectAndHandshakeAsyncResult.connection.pooledStream = pooledStream; } ConnectAndHandshakeAsyncResult.Handshake(); } catch (Exception e) { ConnectAndHandshakeAsyncResult.InvokeCallback(e); } GlobalLog.Leave("ConnectAndHandshakeAsyncResult#" + ValidationHelper.HashString(request) + "::ConnectionCreatedCallback pooledStream#" + ValidationHelper.HashString(pooledStream)); }
private static void ConnectionCreatedCallback(object request, object state) { SmtpConnection.ConnectAndHandshakeAsyncResult owningObject = (SmtpConnection.ConnectAndHandshakeAsyncResult)request; if (state is Exception) { owningObject.InvokeCallback((Exception)state); } else { SmtpPooledStream pooledStream = (SmtpPooledStream)((PooledStream)state); try { while ((pooledStream.creds != null) && (pooledStream.creds != owningObject.connection.credentials)) { owningObject.connection.connectionPool.PutConnection(pooledStream, pooledStream.Owner, owningObject.connection.Timeout, false); pooledStream = (SmtpPooledStream)owningObject.connection.connectionPool.GetConnection(owningObject, m_ConnectionCreatedCallback, owningObject.connection.Timeout); if (pooledStream == null) { return; } } if (Logging.On) { Logging.Associate(Logging.Web, owningObject.connection, pooledStream); } pooledStream.Owner = owningObject.connection; pooledStream.creds = owningObject.connection.credentials; lock (owningObject.connection) { if (owningObject.connection.isClosed) { owningObject.connection.connectionPool.PutConnection(pooledStream, pooledStream.Owner, owningObject.connection.Timeout, false); owningObject.InvokeCallback(null); return; } owningObject.connection.pooledStream = pooledStream; } owningObject.Handshake(); } catch (Exception exception) { owningObject.InvokeCallback(exception); } } }
internal void GetConnection(bool synchronous) { GlobalLog.Enter("ConnectAndHandshakeAsyncResult#" + ValidationHelper.HashString(this) + "::Connect: [....]=" + (synchronous ? "true" : "false")); if (connection.isConnected) { throw new InvalidOperationException(SR.GetString(SR.SmtpAlreadyConnected)); } SmtpPooledStream pooledStream = (SmtpPooledStream)connection.connectionPool.GetConnection((object)this, (synchronous ? null : m_ConnectionCreatedCallback), connection.Timeout); GlobalLog.Print("ConnectAndHandshakeAsyncResult#" + ValidationHelper.HashString(this) + "::Connect returned" + ValidationHelper.HashString(this)); if (pooledStream != null) { try { while (pooledStream.creds != null && pooledStream.creds != connection.credentials) { GlobalLog.Print("ConnectAndHandshakeAsyncResult#" + ValidationHelper.HashString(this) + "::Connect pooledStream has wrong creds " + ValidationHelper.HashString(pooledStream)); connection.connectionPool.PutConnection(pooledStream, pooledStream.Owner, connection.Timeout, false); pooledStream = (SmtpPooledStream)connection.connectionPool.GetConnection((object)this, (synchronous ? null : m_ConnectionCreatedCallback), connection.Timeout); if (pooledStream == null) { GlobalLog.Leave("ConnectAndHandshakeAsyncResult#" + ValidationHelper.HashString(this) + "::Connect returning asynchronously"); return; } } pooledStream.creds = connection.credentials; pooledStream.Owner = this.connection; //needs to be updated for gc reasons lock (connection) { connection.pooledStream = pooledStream; } Handshake(); } catch (Exception e) { InvokeCallback(e); } } GlobalLog.Leave("ConnectAndHandshakeAsyncResult#" + ValidationHelper.HashString(this) + "::Connect pooledStream#" + ValidationHelper.HashString(pooledStream)); }