Exemple #1
0
        // Handle new connection
        private async Task HandleConnectionAsync(TcpClient tcpClient)
        {
            await Task.Yield();
            // continue asynchronously on another threads

            var local = (IPEndPoint)tcpClient.Client.LocalEndPoint;
            var remote = (IPEndPoint)tcpClient.Client.RemoteEndPoint;

            OnClientConnected(new ClientConnectionInfo
            {
                LocalEndPoint = local,
                RemoteEndPoint = remote,
                Timestamp = DateTime.Now
            });

            using (var stream = tcpClient.GetStream())
            {
                try
                {
                    var context = new BinaryRequestContext(stream);

                    await _requestHandler.HandleRequest(context);

                }
                finally
                {
                    await stream.FlushAsync();
                    stream.Close();
                }
            }

            OnClientDisconnected(new ClientConnectionInfo
            {
                LocalEndPoint = local,
                RemoteEndPoint = remote,
                Timestamp = DateTime.Now
            });
        }