public ResponseStream(ConnectionInfo connectionInfo) { _connectionInfo = connectionInfo; _buf = connectionInfo.SendSocketAsyncEventArgs.Buffer; _maxLen = connectionInfo.ReceiveBufferSize - 2; // Space for CRLF after chunk StartOffset = connectionInfo.StartBufferOffset + connectionInfo.ReceiveBufferSize * 2; // Skip receive buffer and header buffer parts }
public void Start(IPEndPoint localEndPoint, Func<IDictionary<string, object>, Task> app) { _app = app; _listenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); _listenSocket.Bind(localEndPoint); _listenSocket.Listen(100); var reserveAtEnd = Status100Continue.Length; var constantsOffset = checked (_maxConnections*_receiveBufferSize*3); var buffer = new byte[checked (constantsOffset + reserveAtEnd)]; for (var i = 0; i < _maxConnections; i++) { var receiveEvent = new SocketAsyncEventArgs(); var sendEvent = new SocketAsyncEventArgs(); receiveEvent.Completed += IoCompleted; sendEvent.Completed += IoCompleted; receiveEvent.SetBuffer(buffer, 0, 0); sendEvent.SetBuffer(buffer, 0, 0); receiveEvent.DisconnectReuseSocket = true; sendEvent.DisconnectReuseSocket = true; var token = new ConnectionInfo(i * _receiveBufferSize * 3, _receiveBufferSize, constantsOffset, receiveEvent, sendEvent, _listenSocket, _app); receiveEvent.UserToken = token; sendEvent.UserToken = token; token.StartAccept(); } }