Skip to content

Commit

Permalink
Log changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nckex committed Jul 17, 2023
1 parent 8866f9f commit 35bff92
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 85 deletions.
2 changes: 1 addition & 1 deletion NetX/NetX.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PackageProjectUrl>https://github.com/project-nevareth/NetX</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<Version>2.2.1</Version>
<Version>2.2.2</Version>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
Expand Down
5 changes: 4 additions & 1 deletion NetX/NetXClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions NetX/NetXServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
{
Expand Down
76 changes: 4 additions & 72 deletions samples/ServerClientSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
12 changes: 3 additions & 9 deletions samples/ServerClientSample/SampleServerProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 35bff92

Please sign in to comment.