From d5ff025cbbbf03c94eeee6ac221a691beae50c48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Mon, 20 Dec 2021 20:51:07 +0100 Subject: [PATCH] feat: call packet responders in local client --- .../NostaleLocalClient.cs | 53 +++++++++++++++++-- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/Local/NosSmooth.LocalClient/NostaleLocalClient.cs b/Local/NosSmooth.LocalClient/NostaleLocalClient.cs index 3af319b..e202c14 100644 --- a/Local/NosSmooth.LocalClient/NostaleLocalClient.cs +++ b/Local/NosSmooth.LocalClient/NostaleLocalClient.cs @@ -25,6 +25,7 @@ namespace NosSmooth.LocalClient; public class NostaleLocalClient : BaseNostaleClient { private readonly IPacketSerializer _packetSerializer; + private readonly PacketSerializerProvider _packetSerializerProvider; private readonly IPacketHandler _packetHandler; private readonly ILogger _logger; private readonly NosClient _client; @@ -36,6 +37,7 @@ public class NostaleLocalClient : BaseNostaleClient /// /// The command processor. /// The packet serializer. + /// The packet serializer provider. /// The packet handler. /// The logger. /// The options for the client. @@ -45,6 +47,7 @@ public class NostaleLocalClient : BaseNostaleClient ( CommandProcessor commandProcessor, IPacketSerializer packetSerializer, + PacketSerializerProvider packetSerializerProvider, IPacketHandler packetHandler, ILogger logger, IOptions options, @@ -55,6 +58,7 @@ public class NostaleLocalClient : BaseNostaleClient { _options = options.Value; _packetSerializer = packetSerializer; + _packetSerializerProvider = packetSerializerProvider; _packetHandler = packetHandler; _logger = logger; _client = client; @@ -115,9 +119,8 @@ public class NostaleLocalClient : BaseNostaleClient return _interceptor.InterceptReceive(ref packet); } - // TODO: handlers + Task.Run(async () => await ProcessPacketAsync(PacketType.Received, packet)); - _logger.LogInformation($"Received packet {packet}"); return true; } @@ -133,19 +136,61 @@ public class NostaleLocalClient : BaseNostaleClient return _interceptor.InterceptSend(ref packet); } - // TODO: handlers + Task.Run(async () => await ProcessPacketAsync(PacketType.Sent, packet)); - _logger.LogInformation($"Sent packet {packet}"); return true; } private void SendPacket(string packetString) { _client.GetNetwork().SendPacket(packetString); + _logger.LogDebug($"Sending client packet {packetString}"); } private void ReceivePacket(string packetString) { _client.GetNetwork().ReceivePacket(packetString); + _logger.LogDebug($"Receiving client packet {packetString}"); + } + + private async Task ProcessPacketAsync(PacketType type, string packetString) + { + IPacketSerializer serializer; + if (type == PacketType.Received) + { + serializer = _packetSerializerProvider.ServerSerializer; + } + else + { + serializer = _packetSerializerProvider.ClientSerializer; + } + + var packet = serializer.Deserialize(packetString); + if (!packet.IsSuccess) + { + _logger.LogWarning($"Could not parse {packetString}. Reason: {packet.Error.Message}"); + return; + } + + Result result; + if (type == PacketType.Received) + { + result = await _packetHandler.HandleReceivedPacketAsync(packet.Entity); + } + else + { + result = await _packetHandler.HandleSentPacketAsync(packet.Entity); + } + + if (!result.IsSuccess) + { + _logger.LogWarning($"There was an error whilst handling packet {packetString}. Error: {result.Error.Message}"); + } + } + + private enum PacketType + { + Sent, + Received, } } \ No newline at end of file -- 2.48.1