Пример #1
0
        /// <summary>
        /// 关闭客户端连接
        /// </summary>
        /// <param name="isSilently">如果为true,则不会触发任何事件</param>
        public void Close(bool isSilently = false)
        {
            if (null != _socket)
            {
                try
                {
                    _socket.Shutdown(SocketShutdown.Send);
                }
                catch
                {
                }

                _tsa.Clear();
                _socket.Close();
                _socket = null;
                _buffer = null;
                _receiveEA.Dispose();
                _receiveEA = null;
                _sendEA.Dispose();
                _sendEA = null;

                if (false == isSilently)
                {
                    onShutdown?.Invoke(this);
                }
            }
        }
Пример #2
0
 public void Dispose()
 {
     _tsa.Clear();
     _listener.Dispose();
     _sendChannel.Dispose();
     _listener     = null;
     _sendChannel  = null;
     onReceiveData = null;
 }
Пример #3
0
 public void Dispose()
 {
     _tsa.Clear();
     if (_listener != null)
     {
         _listener.Dispose();
         _listener = null;
     }
     onReceiveData = null;
 }
Пример #4
0
        public void Reconnect()
        {
            Close(true);

            _tsa.Clear();
            IPEndPoint ipe    = new IPEndPoint(IPAddress.Parse(Host), Port);
            Socket     socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            _connectEA = new SocketAsyncEventArgs();
            _connectEA.RemoteEndPoint = ipe;
            _connectEA.Completed     += OnAsyncEventCompleted;
            if (!socket.ConnectAsync(_connectEA))
            {
                OnAsyncEventCompleted(null, _connectEA);
            }
        }