/// <summary> /// Default constructor. /// </summary> public Server() { m_pClient = new TCP_Client(); m_pLockSynchronizer = m_pClient; }
/// <summary> /// Starts operation processing. /// </summary> /// <param name="owner">Owner TCP client.</param> /// <returns>Returns true if asynchronous operation in progress or false if operation completed synchronously.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>owner</b> is null reference.</exception> internal bool Start(TCP_Client owner) { if(owner == null){ throw new ArgumentNullException("owner"); } m_pTcpClient = owner; SetState(AsyncOP_State.Active); try{ m_pSslStream = new SslStream(m_pTcpClient.m_pTcpStream.SourceStream,false,this.RemoteCertificateValidationCallback); m_pSslStream.BeginAuthenticateAsClient("dummy",this.BeginAuthenticateAsClientCompleted,null); } catch(Exception x){ m_pException = x; SetState(AsyncOP_State.Completed); } // Set flag rise CompletedAsync event flag. The event is raised when async op completes. // If already completed sync, that flag has no effect. lock(m_pLock){ m_RiseCompleted = true; return m_State == AsyncOP_State.Active; } }
/// <summary> /// Starts operation processing. /// </summary> /// <param name="owner">Owner TCP client.</param> /// <returns>Returns true if asynchronous operation in progress or false if operation completed synchronously.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>owner</b> is null reference.</exception> internal bool Start(TCP_Client owner) { if(owner == null){ throw new ArgumentNullException("owner"); } m_pTcpClient = owner; SetState(AsyncOP_State.Active); try{ // Create socket. if(m_pRemoteEP.AddressFamily == AddressFamily.InterNetwork){ m_pSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); m_pSocket.ReceiveTimeout = m_pTcpClient.m_Timeout; m_pSocket.SendTimeout = m_pTcpClient.m_Timeout; } else if(m_pRemoteEP.AddressFamily == AddressFamily.InterNetworkV6){ m_pSocket = new Socket(AddressFamily.InterNetworkV6,SocketType.Stream,ProtocolType.Tcp); m_pSocket.ReceiveTimeout = m_pTcpClient.m_Timeout; m_pSocket.SendTimeout = m_pTcpClient.m_Timeout; } // Bind socket to the specified end point. if(m_pLocalEP != null){ m_pSocket.Bind(m_pLocalEP); } m_pTcpClient.LogAddText("Connecting to " + m_pRemoteEP.ToString() + "."); // Start connecting. m_pSocket.BeginConnect(m_pRemoteEP,this.BeginConnectCompleted,null); } catch(Exception x){ m_pException = x; CleanupSocketRelated(); m_pTcpClient.LogAddException("Exception: " + x.Message,x); SetState(AsyncOP_State.Completed); return false; } // Set flag rise CompletedAsync event flag. The event is raised when async op completes. // If already completed sync, that flag has no effect. lock(m_pLock){ m_RiseCompleted = true; return m_State == AsyncOP_State.Active; } }
/// <summary> /// Cleans up any resource being used. /// </summary> public void Dispose() { if(m_State == AsyncOP_State.Disposed){ return; } SetState(AsyncOP_State.Disposed); m_pException = null; m_pLocalEP = null; m_pRemoteEP = null; m_SSL = false; m_pCertCallback = null; m_pTcpClient = null; m_pSocket = null; m_pStream = null; this.CompletedAsync = null; }
/// <summary> /// Is called when EHLO/HELO command has completed. /// </summary> /// <param name="op">Asynchronous operation.</param> /// <exception cref="ArgumentNullException">Is raised when <b>op</b> is null reference.</exception> private void ConnectCompleted(TCP_Client.ConnectAsyncOP op) { if(op == null){ throw new ArgumentNullException("op"); } try{ // Connect failed. if(op.Error != null){ try{ // Release IP usage. m_pServer.RemoveIpUsage(m_pActiveTarget.Target.Address); m_pActiveTarget = null; // Connect failed, if there are more target IPs, try next one. if(!this.IsDisposed && !this.IsConnected && m_pTargets.Count > 0){ BeginConnect(); } else{ Dispose(op.Error); } } catch(Exception x1){ Dispose(x1); } } // Connect suceeded. else{ // Do EHLO/HELO. string hostName = string.IsNullOrEmpty(m_pLocalBindInfo.HostName) ? Dns.GetHostName() : m_pLocalBindInfo.HostName; SMTP_Client.EhloHeloAsyncOP ehloOP = new SMTP_Client.EhloHeloAsyncOP(hostName); ehloOP.CompletedAsync += delegate(object s,EventArgs<SMTP_Client.EhloHeloAsyncOP> e){ EhloCommandCompleted(ehloOP); }; if(!m_pSmtpClient.EhloHeloAsync(ehloOP)){ EhloCommandCompleted(ehloOP); } } } catch(Exception x){ Dispose(x); } }
/// <summary> /// Starts flow processing. /// </summary> internal void Start() { // Move processing to thread pool. AutoResetEvent startLock = new AutoResetEvent(false); ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object state){ lock(m_pLock){ startLock.Set(); // TCP / TLS client, connect to remote end point. if(!m_IsServer && m_Transport != SIP_Transport.UDP){ try{ TCP_Client client = new TCP_Client(); client.Connect(m_pLocalEP,m_pRemoteEP,m_Transport == SIP_Transport.TLS); m_pTcpSession = client; BeginReadHeader(); } catch{ Dispose(); } } } })); startLock.WaitOne(); startLock.Close(); }