Exemplo n.º 1
0
        private void Connect(IPEndPoint endpoint)
        {
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 1);
            _recvBuffer             = new byte[BUFF_SIZE];
            _incompletePacketBuffer = new byte[BUFF_SIZE];
            _decompBuffer           = new byte[BUFF_SIZE];
            _sendQueue                = new SendQueue();
            _circularBuffer           = new CircularBuffer();
            _sendEventArgs            = new SocketAsyncEventArgs();
            _sendEventArgs.Completed += IO_Socket;
            _recvEventArgs            = new SocketAsyncEventArgs();
            _recvEventArgs.Completed += IO_Socket;
            _recvEventArgs.SetBuffer(_recvBuffer, 0, _recvBuffer.Length);
            _queue.Clear();
            _workingQueue.Clear();
            Statistics.Reset();
            SocketAsyncEventArgs connectEventArgs = new SocketAsyncEventArgs();

            connectEventArgs.Completed += (sender, e) =>
            {
                if (e.SocketError == SocketError.Success)
                {
                    Connected.Raise();
                    Statistics.ConnectedFrom = DateTime.Now;
                    StartRecv();
                }
                else
                {
                    Log.Message(LogTypes.Error, e.SocketError.ToString());
                    Disconnect();
                }
            };
            connectEventArgs.RemoteEndPoint = endpoint;
            _socket.ConnectAsync(connectEventArgs);
        }