Пример #1
0
 public void Disconnect()
 {
     if (client != null)
     {
         lock (lockobj)
         {
             if (client != null)
             {
                 client.Close();
                 client = null;
                 if (_receiver != null)
                 {
                     _receiver.Close();
                 }
                 _receiver = null;
                 if (_sender != null)
                 {
                     _sender.Close();
                 }
                 _sender = null;
             }
             Status = status.Disconnect;
         }
     }
 }
Пример #2
0
 public override void DisConnect()
 {
     try {
         if (m_cSocket != null)
         {
             if (m_cAsyncResult != null)
             {
                 m_cSocket.EndConnect(m_cAsyncResult);
             }
             if (Status == SocketClientStatus.Connected)
             {
                 m_cSocket.Shutdown(SocketShutdown.Both);
             }
         }
     }catch (Exception e)
     {
         CLog.LogError(e.Message + "\n" + e.StackTrace);
     }
     finally
     {
         try
         {
             if (m_cSocket != null)
             {
                 m_cSocket.Close();
             }
         }
         catch (Exception e)
         {
             CLog.LogError(e.Message + "\n" + e.StackTrace);
         }
         if (m_cSender != null)
         {
             m_cSender.Dispose();
             m_cSender = null;
         }
         if (m_cReceiver != null)
         {
             m_cReceiver.Dispose();
             m_cReceiver = null;
         }
         m_cSocket      = null;
         m_cAsyncResult = null;
         base.DisConnect();
     }
 }
Пример #3
0
        /// <summary>
        /// 注意这个回调不在主线�?
        /// </summary>
        /// <param name="ar"></param>
        private void ConnectCallback(IAsyncResult ar)
        {
            lock (ar)
            {
                TcpClient c = (TcpClient)ar.AsyncState;

                if (c != null && c == client)
                {
                    try
                    {
                        c.EndConnect(ar);
                        if (c == client)
                        {
                            lock (lockobj)
                            {
                                if (c == client)
                                {
                                    if (c.Connected)
                                    {
                                        _receiver = new SocketReceiver(this, client);
                                        _sender   = new SocketSender(this, client);
                                        Status    = status.Connected;
                                    }
                                    else
                                    {
                                        Disconnect();
                                    }
                                }
                                else
                                {
                                    c.Close();
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.LogError(e.ToString());
                        Disconnect();
                    }
                }
            }
        }
Пример #4
0
 private void OnConnectedSucc()
 {
     //初始化发送接收
     m_cSender   = new SocketSender(m_cSocket);
     m_cReceiver = new SocketReceiver(m_cSocket);
 }