From 35bff92c93693f52b009ca85500eeea73e2d932b Mon Sep 17 00:00:00 2001 From: Nicollas Date: Mon, 17 Jul 2023 14:47:59 -0300 Subject: [PATCH] Log changes --- NetX/NetX.csproj | 2 +- NetX/NetXClient.cs | 5 +- NetX/NetXServer.cs | 4 +- samples/ServerClientSample/Program.cs | 76 +------------------ .../SampleServerProcessor.cs | 12 +-- 5 files changed, 14 insertions(+), 85 deletions(-) diff --git a/NetX/NetX.csproj b/NetX/NetX.csproj index 3a04347..608b8be 100644 --- a/NetX/NetX.csproj +++ b/NetX/NetX.csproj @@ -7,7 +7,7 @@ https://github.com/project-nevareth/NetX git true - 2.2.1 + 2.2.2 true snupkg diff --git a/NetX/NetXClient.cs b/NetX/NetXClient.cs index 5e8beee..14997fb 100644 --- a/NetX/NetXClient.cs +++ b/NetX/NetXClient.cs @@ -23,7 +23,7 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default) _ = DispatchOnClientConnect(cancellationToken); - _logger?.LogInformation("{name}: Tcp client connected to {address}:{port}", _clientName, _options.EndPoint.Address, _options.EndPoint.Port); + _logger?.LogInformation("{name}: TCP Client connected to {address}:{port}", _clientName, _options.EndPoint.Address, _options.EndPoint.Port); _ = Task.Factory.StartNew(() => ProcessClientConnection(cancellationToken), cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default); } @@ -40,6 +40,9 @@ private async Task ProcessClientConnection(CancellationToken cancellationToken) } finally { + _logger?.LogInformation("{name}: TCP Client disconnected. Reason({reason})", + _clientName, DisconnectReason); + await ((NetXClientOptions)_options).Processor.OnDisconnectedAsync(DisconnectReason); } } diff --git a/NetX/NetXServer.cs b/NetX/NetXServer.cs index 0bcdacc..2c38ff4 100644 --- a/NetX/NetXServer.cs +++ b/NetX/NetXServer.cs @@ -58,7 +58,7 @@ public void Listen(CancellationToken cancellationToken = default) { _socket.Listen(_options.Backlog); - _logger?.LogInformation("{svrName}: Tcp server listening on {ip}:{port}", _serverName, _options.EndPoint.Address, _options.EndPoint.Port); + _logger?.LogInformation("{svrName}: TCP Server listening on {ip}:{port}", _serverName, _options.EndPoint.Address, _options.EndPoint.Port); _ = Task.Factory.StartNew( () => StartAcceptAsync(cancellationToken), @@ -91,7 +91,7 @@ private async Task StartAcceptAsync(CancellationToken listenCancellationToken) } } - _logger?.LogInformation("Shutdown {svrName} TCP server", _serverName); + _logger?.LogInformation("{svrName}: TCP server shutdown", _serverName); } catch (Exception ex) { diff --git a/samples/ServerClientSample/Program.cs b/samples/ServerClientSample/Program.cs index d5ca754..e805b36 100644 --- a/samples/ServerClientSample/Program.cs +++ b/samples/ServerClientSample/Program.cs @@ -55,79 +55,11 @@ public static async Task Main(string[] args) await _client.ConnectAsync(clientTokenSrc.Token); - var responses = new bool[byte.MaxValue]; - - for (byte i = 1; i < byte.MaxValue; i++) - { - byte value = i; - _ = Task.Run(async () => - { - try - { - var respon = await _client.RequestAsync(new byte[] { value }); - responses[value] = true; - await Task.Delay(100); - Log.Information("Received delayed response: {resp}", respon.Array); - } - catch (Exception ex) - { - Log.Error(ex, "Error"); - } - }); - } - - //_ = Task.Run(async () => - //{ - // while (!serverTokenSrc.IsCancellationRequested) - // { - // if (!responses.All(x => x)) - // { - // Log.Information("Waiting for responses"); - // await Task.Delay(1000); - // } - // else - // { - // Log.Information("All responses received"); - // break; - // } - // } - //}); - - try - { - await _client.RequestAsync(new byte[] { 0 }); - } - catch - { - responses[0] = true; - Log.Information("Success timeout error"); - } - - bool stopAll = false; - while (!stopAll) - { - var command = Console.ReadLine(); - - if (command == "stop_client") - { - Console.WriteLine("Stopping client"); - clientTokenSrc.Cancel(); - } - - if (command == "stop_server") - { - Console.WriteLine("Stopping server"); - serverTokenSrc.Cancel(); - } - - if (command == "stop") - { - stopAll = true; - } - - await Task.Yield(); - } + await _client.SendAsync(new byte[] { 0 }); + await Task.Delay(500); + _client.Disconnect(); + Console.ReadLine(); await Task.Delay(3000); } } diff --git a/samples/ServerClientSample/SampleServerProcessor.cs b/samples/ServerClientSample/SampleServerProcessor.cs index ecfec9b..10203c3 100644 --- a/samples/ServerClientSample/SampleServerProcessor.cs +++ b/samples/ServerClientSample/SampleServerProcessor.cs @@ -17,15 +17,9 @@ public ValueTask OnSessionConnectAsync(INetXSession session, CancellationToken c public async ValueTask OnReceivedMessageAsync(INetXSession session, NetXMessage message, CancellationToken cancellationToken) { - var messageId = message.Id; - var responseMess = message.Buffer.ToArray(); - var textMessage = Encoding.UTF8.GetString(message.Buffer.Span); - var token = cancellationToken; - - //if(responseMess[0] == 0) - // return; - - await session.ReplyAsync(messageId, responseMess, token); + Log.Information("Session received message"); + await Task.Delay(4000); + Log.Information("Session returned to function after delay"); } public ValueTask OnSessionDisconnectAsync(Guid sessionId, DisconnectReason reason)