From 11c5d9f159e5de5d2b70b363b405d7598632fa73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Tue, 21 Dec 2021 21:59:06 +0100 Subject: [PATCH] feat: pass event args to packet responders instead of plain packets --- Core/NosSmooth.Core/Packets/IPacketHandler.cs | 6 ++-- .../Packets/IPacketResponder.cs | 4 +-- Core/NosSmooth.Core/Packets/PacketHandler.cs | 36 ++++++++++++++----- .../NostaleLocalClient.cs | 14 +++----- Samples/SimpleChat/SayResponder.cs | 6 ++-- 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/Core/NosSmooth.Core/Packets/IPacketHandler.cs b/Core/NosSmooth.Core/Packets/IPacketHandler.cs index 041333b..ffef41b 100644 --- a/Core/NosSmooth.Core/Packets/IPacketHandler.cs +++ b/Core/NosSmooth.Core/Packets/IPacketHandler.cs @@ -20,15 +20,17 @@ public interface IPacketHandler /// Calls a responder for the given 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(IPacket packet, CancellationToken ct = default); + public Task HandleReceivedPacketAsync(IPacket packet, string packetString, CancellationToken ct = default); /// /// Calls a responder for the given 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 HandleSentPacketAsync(IPacket packet, CancellationToken ct = default); + public Task HandleSentPacketAsync(IPacket packet, string packetString, CancellationToken ct = default); } \ No newline at end of file diff --git a/Core/NosSmooth.Core/Packets/IPacketResponder.cs b/Core/NosSmooth.Core/Packets/IPacketResponder.cs index 1fab810..efda0f7 100644 --- a/Core/NosSmooth.Core/Packets/IPacketResponder.cs +++ b/Core/NosSmooth.Core/Packets/IPacketResponder.cs @@ -32,7 +32,7 @@ public interface IPacketResponder : IPacketResponder /// The packet to respond to. /// The cancellation token for cancelling the operation. /// A result that may or may not have succeeded. - public Task Respond(TPacket packet, CancellationToken ct = default); + public Task Respond(PacketEventArgs packet, CancellationToken ct = default); } /// @@ -47,6 +47,6 @@ public interface IEveryPacketResponder : IPacketResponder /// The cancellation token for cancelling the operation. /// The type of the packet. /// A result that may or may not have succeeded. - public Task Respond(TPacket packet, CancellationToken ct = default) + public Task Respond(PacketEventArgs packet, CancellationToken ct = default) where TPacket : IPacket; } \ No newline at end of file diff --git a/Core/NosSmooth.Core/Packets/PacketHandler.cs b/Core/NosSmooth.Core/Packets/PacketHandler.cs index 22ab8ca..3a06978 100644 --- a/Core/NosSmooth.Core/Packets/PacketHandler.cs +++ b/Core/NosSmooth.Core/Packets/PacketHandler.cs @@ -31,12 +31,20 @@ public class PacketHandler : IPacketHandler } /// - public Task HandleReceivedPacketAsync(IPacket packet, CancellationToken ct) => HandlePacketAsync(packet, ct); + public Task HandleReceivedPacketAsync(IPacket packet, string packetString, CancellationToken ct) + => HandlePacketAsync(PacketType.Received, packet, packetString, ct); /// - public Task HandleSentPacketAsync(IPacket packet, CancellationToken ct) => HandlePacketAsync(packet, ct); + public Task HandleSentPacketAsync(IPacket packet, string packetString, CancellationToken ct) + => HandlePacketAsync(PacketType.Sent, packet, packetString, ct); - private Task HandlePacketAsync(IPacket packet, CancellationToken ct = default) + private Task HandlePacketAsync + ( + PacketType packetType, + IPacket packet, + string packetString, + CancellationToken ct = default + ) { var processMethod = GetType().GetMethod ( @@ -50,18 +58,30 @@ public class PacketHandler : IPacketHandler } var boundProcessMethod = processMethod.MakeGenericMethod(packet.GetType()); - return (Task)boundProcessMethod.Invoke(this, new object[] { packet, ct })!; + return (Task)boundProcessMethod.Invoke(this, new object[] + { + packetType, + packet, + packetString, + ct + })!; } - private async Task DispatchResponder(TPacket packet, CancellationToken ct) + private async Task DispatchResponder( + PacketType packetType, + TPacket packet, + string packetString, + CancellationToken ct + ) where TPacket : class, IPacket { using var scope = _provider.CreateScope(); var packetResponders = scope.ServiceProvider.GetServices>(); var genericPacketResponders = scope.ServiceProvider.GetServices(); - var tasks = packetResponders.Select(responder => responder.Respond(packet, ct)).ToList(); - tasks.AddRange(genericPacketResponders.Select(responder => responder.Respond(packet, ct))); + var packetEventArgs = new PacketEventArgs(packetType, packet, packetString); + var tasks = packetResponders.Select(responder => responder.Respond(packetEventArgs, ct)).ToList(); + tasks.AddRange(genericPacketResponders.Select(responder => responder.Respond(packetEventArgs, ct))); var results = await Task.WhenAll(tasks); @@ -78,7 +98,7 @@ public class PacketHandler : IPacketHandler { 0 => Result.FromSuccess(), 1 => errors[0], - _ => new AggregateError(errors.Cast().ToList()) + _ => new AggregateError(errors.Cast().ToArray()) }; } } \ No newline at end of file diff --git a/Local/NosSmooth.LocalClient/NostaleLocalClient.cs b/Local/NosSmooth.LocalClient/NostaleLocalClient.cs index 0670732..369c412 100644 --- a/Local/NosSmooth.LocalClient/NostaleLocalClient.cs +++ b/Local/NosSmooth.LocalClient/NostaleLocalClient.cs @@ -9,6 +9,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using NosSmooth.Core.Client; using NosSmooth.Core.Commands; +using NosSmooth.Core.Extensions; using NosSmooth.Core.Packets; using NosSmoothCore; using Remora.Results; @@ -170,22 +171,17 @@ public class NostaleLocalClient : BaseNostaleClient Result result; if (type == PacketType.Received) { - result = await _packetHandler.HandleReceivedPacketAsync(packet.Entity); + result = await _packetHandler.HandleReceivedPacketAsync(packet.Entity, packetString); } else { - result = await _packetHandler.HandleSentPacketAsync(packet.Entity); + result = await _packetHandler.HandleSentPacketAsync(packet.Entity, packetString); } if (!result.IsSuccess) { - _logger.LogWarning($"There was an error whilst handling packet {packetString}. Error: {result.Error.Message}"); + _logger.LogError("There was an error whilst handling packet"); + _logger.LogResultError(result); } } - - private enum PacketType - { - Sent, - Received, - } } \ No newline at end of file diff --git a/Samples/SimpleChat/SayResponder.cs b/Samples/SimpleChat/SayResponder.cs index b2e801f..478a254 100644 --- a/Samples/SimpleChat/SayResponder.cs +++ b/Samples/SimpleChat/SayResponder.cs @@ -31,7 +31,7 @@ public class SayResponder : IPacketResponder, IPacketResponder - public Task Respond(SayPacket packet, CancellationToken ct = default) + public Task Respond(PacketEventArgs packet, CancellationToken ct = default) { return _client.ReceivePacketAsync( new SayPacket() @@ -41,8 +41,8 @@ public class SayResponder : IPacketResponder, IPacketResponder - public Task Respond(MsgPacket packet, CancellationToken ct = default) + /// + public Task Respond(PacketEventArgs packet, CancellationToken ct = default) { return _client.ReceivePacketAsync( new SayPacket() -- 2.48.1