From e736a777161b6c911dbc77f38b3d68abfe1c5122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sun, 25 Dec 2022 21:23:05 +0100 Subject: [PATCH] feat(packets): merge send and receive packet handling to one method --- Core/NosSmooth.Core/Packets/PacketHandler.cs | 43 +------------------- Samples/FileClient/Client.cs | 39 ++++++++++++------ 2 files changed, 28 insertions(+), 54 deletions(-) diff --git a/Core/NosSmooth.Core/Packets/PacketHandler.cs b/Core/NosSmooth.Core/Packets/PacketHandler.cs index a7a69b2..89a72d9 100644 --- a/Core/NosSmooth.Core/Packets/PacketHandler.cs +++ b/Core/NosSmooth.Core/Packets/PacketHandler.cs @@ -38,51 +38,12 @@ public class PacketHandler /// Calls a responder for the given packet. /// /// The current NosTale client. + /// The source of the packet. /// The packet. /// The string of the packet. /// The cancellation token for cancelling the operation. /// A result that may or may not have succeeded. - public Task HandleReceivedPacketAsync - ( - INostaleClient client, - IPacket packet, - string packetString, - CancellationToken ct = default - ) - => HandlePacketAsync - ( - client, - PacketSource.Server, - packet, - packetString, - ct - ); - - /// - /// Calls a responder for the given packet. - /// - /// The current NosTale client. - /// The packet. - /// The string of the packet. - /// The cancellation token for cancelling the operation. - /// A result that may or may not have succeeded. - public Task HandleSentPacketAsync - ( - INostaleClient client, - IPacket packet, - string packetString, - CancellationToken ct = default - ) - => HandlePacketAsync - ( - client, - PacketSource.Client, - packet, - packetString, - ct - ); - - private Task HandlePacketAsync + public Task HandlePacketAsync ( INostaleClient client, PacketSource packetType, diff --git a/Samples/FileClient/Client.cs b/Samples/FileClient/Client.cs index 63fd0aa..6e8879d 100644 --- a/Samples/FileClient/Client.cs +++ b/Samples/FileClient/Client.cs @@ -36,7 +36,8 @@ public class Client : BaseNostaleClient /// The command processor. /// The packet serializer. /// The logger. - public Client( + public Client + ( Stream stream, PacketHandler packetHandler, CommandProcessor commandProcessor, @@ -77,16 +78,14 @@ public class Client : BaseNostaleClient var source = type == "Recv" ? PacketSource.Server : PacketSource.Client; var packet = CreatePacket(packetStr, source); - Result result; - if (source == PacketSource.Client) - { - result = await _packetHandler.HandleSentPacketAsync(this, packet, packetStr, stopRequested); - } - else - { - result = await _packetHandler.HandleReceivedPacketAsync(this, packet, packetStr, stopRequested); - } - + Result result = await _packetHandler.HandlePacketAsync + ( + this, + source, + packet, + packetStr, + stopRequested + ); if (!result.IsSuccess) { _logger.LogResultError(result); @@ -99,13 +98,27 @@ public class Client : BaseNostaleClient /// public override async Task SendPacketAsync(string packetString, CancellationToken ct = default) { - return await _packetHandler.HandleReceivedPacketAsync(this, CreatePacket(packetString, PacketSource.Client), packetString, ct); + return await _packetHandler.HandlePacketAsync + ( + this, + PacketSource.Client, + CreatePacket(packetString, PacketSource.Client), + packetString, + ct + ); } /// public override async Task ReceivePacketAsync(string packetString, CancellationToken ct = default) { - return await _packetHandler.HandleReceivedPacketAsync(this, CreatePacket(packetString, PacketSource.Server), packetString, ct); + return await _packetHandler.HandlePacketAsync + ( + this, + PacketSource.Server, + CreatePacket(packetString, PacketSource.Server), + packetString, + ct + ); } private IPacket CreatePacket(string packetStr, PacketSource source) -- 2.49.0