Ejemplo n.º 1
0
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            var readEventArg = new SocketAsyncEventArgs();

            readEventArg.Completed += ReadComplete;
            _bufferControl.Set(readEventArg);

            if (e.AcceptSocket?.RemoteEndPoint == null)
            {
                if (IsStarted)
                {
                    StartAccept(e);
                }
                return;
            }

            var session = new Session(this, readEventArg, e.AcceptSocket);

            readEventArg.UserToken = session;

            _sessions.TryAdd(session.Id, session);

            _protocol.OnConnect(session);

            var willRaiseEvent = e.AcceptSocket.ReceiveAsync(readEventArg);

            if (!willRaiseEvent)
            {
                ProcessReceive(readEventArg);
            }

            StartAccept(e);
        }
Ejemplo n.º 2
0
        private void ProcessConnect(SocketAsyncEventArgs e)
        {
            var errorCode = e.SocketError;

            switch (errorCode)
            {
            case SocketError.TimedOut:
            case SocketError.ConnectionRefused:
                Log.Info("Connection to ({0}) failed, trying new attempt...", _remoteEndPoint);
                Thread.Sleep(5000);
                StartConnect();
                return;

            case SocketError.Interrupted:
            case SocketError.OperationAborted:
                return;
            }

            if (errorCode != SocketError.Success)
            {
                throw new SocketException((int)errorCode);
            }

            var readEventArg = new SocketAsyncEventArgs();

            readEventArg.Completed += ReadComplete;
            _bufferControl.Set(readEventArg);

            if (_socket?.RemoteEndPoint == null)
            {
                Log.Info("Connection to ({0}) failed, trying new attempt...", _remoteEndPoint);
                Thread.Sleep(10000);
                StartConnect();
                return;
            }

            var session = new Session(this, readEventArg, _socket);

            readEventArg.UserToken = session;

            _protocol.OnConnect(session);

            var willRaiseEvent = _socket.ReceiveAsync(readEventArg);

            if (!willRaiseEvent)
            {
                ProcessReceive(readEventArg);
            }
        }