/// <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; m_pSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); } 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; m_pSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); } // 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> /// 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; 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); } // NOTE: If operation completed synchronously, then this.State is AsyncOP_State.Completed and we don't have pending operation. return this.State != AsyncOP_State.Completed; }
/// <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; }