Exemplo n.º 1
0
        private async void ProcessClientAsync(TcpClient tcpClient, int clientIndex, CancellationToken cancellationToken)
        {
            var clientContext = new TcpClientContext(clientIndex, tcpClient);

            try
            {
                using (tcpClient)
                    using (var networkStream = tcpClient.GetStream())
                        using (var packetStream = _packetStreamFactory.New())
                        {
                            if (ClientConnectedEvent != null)
                            {
                                ClientConnectedEvent(clientContext, EventArgs.Empty);
                            }

                            Trace.TraceInformation("[Server] New client ({0}) connected", clientIndex);

                            var buffer = new byte[4096];

                            while (!cancellationToken.IsCancellationRequested)
                            {
                                var timeoutTask = Task.Delay(TimeSpan.FromMilliseconds(_connectionTimeoutPeriod),
                                                             cancellationToken);
                                var bytesTask     = networkStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);
                                var completedTask = await Task.WhenAny(timeoutTask, bytesTask).ConfigureAwait(false);

                                if (completedTask == timeoutTask)
                                {
                                    var message = Encoding.UTF8.GetBytes("Client timed out");
                                    await networkStream.WriteAsync(message, 0, message.Length, cancellationToken);
                                }

                                var bytes = bytesTask.Result;
                                if (bytes == 0)
                                {
                                    break;
                                }

                                if (MessageReceivedEvent == null)
                                {
                                    continue;
                                }

                                var results = packetStream.ParseBytes(buffer.Take(bytes).ToArray());

                                if (results == null)
                                {
                                    continue;
                                }

                                foreach (var result in results)
                                {
                                    MessageReceivedEvent(clientContext, result);
                                }
                            }
                        }
            }
            catch (Exception exception)
            {
                Trace.TraceWarning("[Server] Client ({0}) threw exception: {1}", clientIndex, exception.Message);
            }

            if (ClientDisconnectedEvent != null)
            {
                ClientDisconnectedEvent(clientContext, EventArgs.Empty);
            }

            Trace.TraceInformation("[Server] Client ({0}) disconnected", clientIndex);
        }
Exemplo n.º 2
0
        private async void ProcessServerAsync(TcpClient tcpClient, CancellationToken token)
        {
            using (tcpClient)
                using (_networkStream = tcpClient.GetStream())
                    using (var packetStream = _packetStreamFactory.New())
                    {
                        if (ClientConnectedEvent != null)
                        {
                            ClientConnectedEvent(this, null);
                        }

                        Trace.TraceInformation("[Client] Connected to server");

                        var buffer = new byte[4096];

                        while (!token.IsCancellationRequested)
                        {
                            var timeoutTask   = Task.Delay(TimeSpan.FromMilliseconds(_connectionTimeoutPeriod), token);
                            var bytesTask     = _networkStream.ReadAsync(buffer, 0, buffer.Length, token);
                            var completedTask = await Task.WhenAny(timeoutTask, bytesTask).ConfigureAwait(false);

                            if (completedTask == timeoutTask)
                            {
                                if (!token.IsCancellationRequested)
                                {
                                    Trace.TraceInformation("[Client] Server timed out");
                                }
                            }

                            var bytes = bytesTask.Result;
                            if (bytes == 0)
                            {
                                break;
                            }

                            if (MessageReceivedEvent == null)
                            {
                                continue;
                            }

                            var results = packetStream.ParseBytes(buffer.Take(bytes).ToArray());

                            if (results == null)
                            {
                                continue;
                            }

                            foreach (var result in results)
                            {
                                MessageReceivedEvent(this, result);
                            }
                        }
                    }

            if (ClientDisconnectedEvent != null)
            {
                ClientDisconnectedEvent(this, null);
            }

            Trace.TraceInformation("[Client] Diconnected from server");
            throw new NotImplementedException();
        }