PacketMediator/RaiNote.PacketMediator/PacketDistributorService.cs

44 lines
1.4 KiB
C#
Raw Permalink Normal View History

2024-02-26 12:18:04 +00:00
// Licensed to Timothy Schenk under the Apache 2.0 License.
2024-02-08 11:55:35 +00:00
using System.Reflection;
using Microsoft.Extensions.Hosting;
2024-04-04 14:32:04 +00:00
namespace RaiNote.PacketMediator;
2024-02-08 11:55:35 +00:00
public class PacketDistributorService<TPacketIdEnum, TSession> : IHostedService
where TPacketIdEnum : Enum
{
private readonly PacketDistributor<TPacketIdEnum, TSession> _packetDistributor;
public PacketDistributorService(IServiceProvider serviceProvider,
IEnumerable<Assembly> sourcesContainingPackets, IEnumerable<Assembly> sourcesContainingPacketHandlers)
{
2024-08-07 17:05:41 +00:00
_packetDistributor = new PacketDistributor<TPacketIdEnum, TSession>(serviceProvider,
sourcesContainingPackets,
sourcesContainingPacketHandlers
);
2024-02-08 11:55:35 +00:00
}
2024-08-07 17:05:41 +00:00
public async Task StartAsync(CancellationToken cancellationToken)
2024-02-08 11:55:35 +00:00
{
2024-08-07 17:05:41 +00:00
await _packetDistributor.DequeuePacketAsync(cancellationToken);
2024-02-08 11:55:35 +00:00
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public Task AddPacketAsync(byte[] packetData, TPacketIdEnum operationCode, TSession session)
{
return _packetDistributor.AddPacketAsync(packetData, operationCode, session);
}
2024-08-07 17:05:41 +00:00
public DotNext.Optional<TPacketIdEnum> GetOperationCodeByPacketType(IPacket packet)
2024-02-08 11:55:35 +00:00
{
var type = packet.GetType();
_packetDistributor.PacketIdMap.TryGetValue(type, out var value);
2024-08-07 17:05:41 +00:00
return value ?? DotNext.Optional<TPacketIdEnum>.None;
2024-02-08 11:55:35 +00:00
}
}