Ejemplo n.º 1
0
        internal async void ReceiveBytesFromTcpClientAsync(TcpClient tcpClient)
        {
            var clientInfo = new TcpClientInfo(tcpClient);

            clientInfoStorage.StoreInfo(clientInfo);

            Logger.LogInformation($"New connection accepted. Id: {clientInfo.ClientId}, " +
                                  $"Endpoint: {clientInfo.TcpClient.Client.RemoteEndPoint}");

            Logger.LogInformation($"Curent client count: {clientInfoStorage.Count()}");

            PacketProtocol pp = new PacketProtocol(500000)
            {
                MessageArrived = (message) =>
                {
                    clientInfo.ReceivedMessage.Enqueue(message);
                    OnMessageReceived?.Invoke(this, EventArgs.Empty);
                }
            };

            try
            {
                while (true)
                {
                    var bytes = await GetNextBytePortionAsync(clientInfo.Stream);

                    pp.ByteReceived(bytes);
                }
            }
            catch (ClientDisconnectedException)
            {
                Logger.LogDebug($"Client {clientInfo.ClientId} disconnected.");
            }
            catch (Exception ex)
            {
                Logger.LogError(ex.Message);
            }
            finally
            {
                clientInfoStorage.RemoveInfo(clientInfo);

                Logger.LogDebug($"Curent client count: {clientInfoStorage.Count()}");
            }
        }