From e2eb461fb34df8c96c798c9501adcc4ab09ff206 Mon Sep 17 00:00:00 2001 From: Timothy Schenk Date: Mon, 5 Feb 2024 09:02:27 +0100 Subject: [PATCH] chore: make everything proper async --- Continuity.AuthServer/Consumers/PacketConsumer.cs | 3 +-- Rai.PacketMediator/IPacketHandler.cs | 2 +- Rai.PacketMediator/PacketDistributorService.cs | 15 +++++++++------ Rai.PacketMediator/Rai.PacketMediator.csproj | 12 ++++++++++++ 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Continuity.AuthServer/Consumers/PacketConsumer.cs b/Continuity.AuthServer/Consumers/PacketConsumer.cs index 034cf57..5b9616d 100644 --- a/Continuity.AuthServer/Consumers/PacketConsumer.cs +++ b/Continuity.AuthServer/Consumers/PacketConsumer.cs @@ -19,8 +19,7 @@ public class PacketConsumer : IConsumer public Task Consume(ConsumeContext context) { - _distributorService.AddPacket(context.Message.MessageBody, context.Message.OperationCode, + return _distributorService.AddPacketAsync(context.Message.MessageBody, context.Message.OperationCode, context.Message.Session); - return Task.CompletedTask; } } diff --git a/Rai.PacketMediator/IPacketHandler.cs b/Rai.PacketMediator/IPacketHandler.cs index 12e8c5f..6e24d7e 100644 --- a/Rai.PacketMediator/IPacketHandler.cs +++ b/Rai.PacketMediator/IPacketHandler.cs @@ -8,7 +8,7 @@ namespace Rai.PacketMediator; [UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)] public interface IPacketHandler : IPacketHandler where T : IIncomingPacket { - [UsedImplicitly] + [UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)] public Task HandleAsync(T packet, S session); async Task IPacketHandler.TryHandleAsync(IIncomingPacket packet, S session) diff --git a/Rai.PacketMediator/PacketDistributorService.cs b/Rai.PacketMediator/PacketDistributorService.cs index e0cb920..dae4797 100644 --- a/Rai.PacketMediator/PacketDistributorService.cs +++ b/Rai.PacketMediator/PacketDistributorService.cs @@ -20,6 +20,7 @@ public class PacketDistributorService : Microsoft.Extensions.Hosting.IHost private readonly IServiceProvider _serviceProvider; private readonly Assembly[] _sourcesContainingPackets; + private readonly TaskFactory _taskFactory; private ImmutableDictionary> _deserializationMap; @@ -34,6 +35,7 @@ public class PacketDistributorService : Microsoft.Extensions.Hosting.IHost _serviceProvider = serviceProvider; _sourcesContainingPackets = sourcesContainingPackets; _activitySource = new ActivitySource(nameof(PacketMediator)); + _taskFactory = new TaskFactory(); } private Dictionary RetrievePacketsDictionary() @@ -127,21 +129,21 @@ public class PacketDistributorService : Microsoft.Extensions.Hosting.IHost return Task.CompletedTask; } - public void AddPacket(byte[] packetData, T operationCode, S session) + public async Task AddPacketAsync(byte[] packetData, T operationCode, S session) { _concurrentQueue.Enqueue((packetData, operationCode, session)); - DequeueRawPacket(); + await DequeueRawPacketAsync(); } - private void DequeueRawPacket() + private async Task DequeueRawPacketAsync() { if (_concurrentQueue.TryDequeue(out var item)) { - ThreadPool.QueueUserWorkItem(InvokePacketHandler, item, preferLocal: false); + await InvokePacketHandlerAsync(item); } } - private void InvokePacketHandler((byte[], T, S) valueTuple) + private async Task InvokePacketHandlerAsync((byte[], T, S) valueTuple) { IIncomingPacket packet; var (packetData, operationCode, session) = valueTuple; @@ -159,7 +161,8 @@ public class PacketDistributorService : Microsoft.Extensions.Hosting.IHost using (var packetHandlerActivity = _activitySource.StartActivity("PacketHandler")) { packetHandlerActivity?.SetTag("PacketId", operationCode); - _ = _packetHandlersInstantiation[operationCode]?.TryHandleAsync(packet, session); + // ! I don't see how it's possibly null here. + await _packetHandlersInstantiation[operationCode]?.TryHandleAsync(packet, session)!; } } diff --git a/Rai.PacketMediator/Rai.PacketMediator.csproj b/Rai.PacketMediator/Rai.PacketMediator.csproj index 94a160a..cada0eb 100644 --- a/Rai.PacketMediator/Rai.PacketMediator.csproj +++ b/Rai.PacketMediator/Rai.PacketMediator.csproj @@ -12,8 +12,20 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive +