public ServerConnection([NotNull] Listener listener, [NotNull] IBufferManager bufferManager, [NotNull] ClientConnectionInfo connectionInfo, int id) { if (connectionInfo == null) { throw new ArgumentNullException("connectionInfo"); } if (listener == null) { throw new ArgumentNullException("listener"); } if (bufferManager == null) { throw new ArgumentNullException("bufferManager"); } this._connectionInfo = connectionInfo; this._clientSocket = connectionInfo.Socket; this._id = id; this._listener = listener; this._bufferManager = bufferManager; StartTime = DateTime.Now; ThreadId = Thread.CurrentThread.ManagedThreadId; ClientAddress = ((IPEndPoint)this._clientSocket.RemoteEndPoint).Address; }
private async Task ProcessConnection(ClientConnectionInfo connectionInfo) { int id = Interlocked.Increment(ref this._connectionCounter); ServerConnection connection = new ServerConnection(this, _compressionBufferManager, connectionInfo, id); await connection.Start(); }
internal void CloseClientSocket(ClientConnectionInfo connectionInfo) { // close the socket associated with the client try { if (!connectionInfo.IsInited) { this._clientConnectionInfoPool.Put(connectionInfo); return; } SocketAsyncEventArgs args = this._disconnectSocketArgsPool.Get(); args.UserToken = connectionInfo; connectionInfo.Socket.Shutdown(SocketShutdown.Send); bool willRaiseEvent = connectionInfo.Socket.DisconnectAsync(args); if (!willRaiseEvent) { DisconnectSocketCompletedHandler(this, args); } } // throws if client process has already closed catch (Exception) { } }
private async void ProcessAccept(SocketAsyncEventArgs socketAsyncEventArgs) { Interlocked.Increment(ref _accepts); // Get the socket for the accepted client connection and put it into the object user token ClientConnectionInfo connectionInfo = this._clientConnectionInfoPool.Get(); try { Socket acceptSocket = socketAsyncEventArgs.AcceptSocket; // Accept the next connection request AcceptAsync(socketAsyncEventArgs); connectionInfo.Init(acceptSocket, new NetworkStream(acceptSocket)); await ProcessConnection(connectionInfo); } catch (SocketException) { CloseClientSocket(connectionInfo); } catch (IOException) { CloseClientSocket(connectionInfo); } }
private void DisconnectSocketCompletedHandler(object sender, SocketAsyncEventArgs e) { Debug.Assert(e.SocketError == SocketError.Success); ClientConnectionInfo connectionInfo = (ClientConnectionInfo)e.UserToken; this._socketPool.Put(connectionInfo.Socket); connectionInfo.Stream.Dispose(); connectionInfo.Clean(); e.UserToken = null; this._disconnectSocketArgsPool.Put(e); this._clientConnectionInfoPool.Put(connectionInfo); }