private void ProcessConnect(SocketAsyncEventArgs e) { SocketError errorCode = e.SocketError; switch (errorCode) { case SocketError.TimedOut: case SocketError.ConnectionRefused: //_log.Info("Connection to {0} failed, retry...", _remoteEndPoint); Thread.Sleep(10000); StartConnect(); return; case SocketError.Interrupted: case SocketError.OperationAborted: return; default: break; } if (errorCode != SocketError.Success) { throw new SocketException((Int32)errorCode); } // Get the socket for the accepted client connection and put it into the // ReadEventArg object user token SocketAsyncEventArgs readEventArg = new SocketAsyncEventArgs(); readEventArg.Completed += ReadComplete; // assign a byte buffer from the buffer pool to the SocketAsyncEventArg object _bufferManager.SetBuffer(readEventArg); if (_connectSocket?.RemoteEndPoint is null) { //_log.Info("Connection to {0} failed, retry...", _remoteEndPoint); Thread.Sleep(10000); StartConnect(); return; } Session session = new Session(this, readEventArg, _connectSocket); readEventArg.UserToken = session; _protocolHandler.OnConnect(session); // As soon as the client is connected, post a receive to the connection Boolean willRaiseEvent = _connectSocket.ReceiveAsync(readEventArg); if (!willRaiseEvent) { ProcessReceive(readEventArg); } }
private void ProcessAccept(SocketAsyncEventArgs e) { // Get the socket for the accepted client connection and put it into the //ReadEventArg object user token SocketAsyncEventArgs readEventArg = new SocketAsyncEventArgs(); readEventArg.Completed += ReadComplete; // assign a byte buffer from the buffer pool to the SocketAsyncEventArg object _bufferManager.SetBuffer(readEventArg); if (e.AcceptSocket?.RemoteEndPoint is null) { if (IsStarted) { StartAccept(e); } return; } Session session = new Session(this, readEventArg, e.AcceptSocket); readEventArg.UserToken = session; _sessions.TryAdd(session.Id, session); _protocolHandler.OnConnect(session); // As soon as the client is connected, post a receive to the connection Boolean willRaiseEvent = e.AcceptSocket.ReceiveAsync(readEventArg); if (!willRaiseEvent) { ProcessReceive(readEventArg); } // Accept the next connection request StartAccept(e); }