chore: make everything proper async

This commit is contained in:
Timothy Schenk 2024-02-05 09:02:27 +01:00
parent c378ddba07
commit e2eb461fb3
Signed by: rainote
SSH key fingerprint: SHA256:pnkNSDwpAnaip00xaZlVFHKKsS7T8UtOomMzvs0yITE
4 changed files with 23 additions and 9 deletions

View file

@ -19,8 +19,7 @@ public class PacketConsumer : IConsumer<RawPacket>
public Task Consume(ConsumeContext<RawPacket> context)
{
_distributorService.AddPacket(context.Message.MessageBody, context.Message.OperationCode,
return _distributorService.AddPacketAsync(context.Message.MessageBody, context.Message.OperationCode,
context.Message.Session);
return Task.CompletedTask;
}
}

View file

@ -8,7 +8,7 @@ namespace Rai.PacketMediator;
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
public interface IPacketHandler<in T, in S> : IPacketHandler<S> where T : IIncomingPacket
{
[UsedImplicitly]
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
public Task HandleAsync(T packet, S session);
async Task<bool> IPacketHandler<S>.TryHandleAsync(IIncomingPacket packet, S session)

View file

@ -20,6 +20,7 @@ public class PacketDistributorService<T, S> : Microsoft.Extensions.Hosting.IHost
private readonly IServiceProvider _serviceProvider;
private readonly Assembly[] _sourcesContainingPackets;
private readonly TaskFactory _taskFactory;
private ImmutableDictionary<T,
Func<byte[], IIncomingPacket>> _deserializationMap;
@ -34,6 +35,7 @@ public class PacketDistributorService<T, S> : Microsoft.Extensions.Hosting.IHost
_serviceProvider = serviceProvider;
_sourcesContainingPackets = sourcesContainingPackets;
_activitySource = new ActivitySource(nameof(PacketMediator));
_taskFactory = new TaskFactory();
}
private Dictionary<T, Type> RetrievePacketsDictionary()
@ -127,21 +129,21 @@ public class PacketDistributorService<T, S> : 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<T, S> : 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)!;
}
}

View file

@ -12,8 +12,20 @@
<PackageReference Include="DotNext.Threading" Version="5.0.1" />
<PackageReference Include="DotNext.Unsafe" Version="5.0.1" />
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.139">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.8.14">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Nullable.Extended.Analyzer" Version="1.15.6169">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>