continuity/Rai.PacketMediator/PacketDistributorService.cs

47 lines
1.5 KiB
C#
Raw Normal View History

2024-02-07 16:40:36 +01:00
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Reflection;
2024-02-07 16:40:36 +01:00
using Microsoft.Extensions.Hosting;
namespace Rai.PacketMediator;
2024-02-07 16:40:36 +01:00
public class PacketDistributorService<TPacketIdEnum, TSession> : IHostedService
2024-02-05 11:43:58 +01:00
where TPacketIdEnum : Enum
{
2024-02-05 12:08:00 +01:00
private readonly PacketDistributor<TPacketIdEnum, TSession> _packetDistributor;
public PacketDistributorService(IServiceProvider serviceProvider,
IEnumerable<Assembly> sourcesContainingPackets, IEnumerable<Assembly> sourcesContainingPacketHandlers)
{
_packetDistributor = new PacketDistributor<TPacketIdEnum, TSession>(serviceProvider, sourcesContainingPackets,
sourcesContainingPacketHandlers);
}
public Task StartAsync(CancellationToken cancellationToken)
{
return _packetDistributor.DequeuePacketAsync(cancellationToken);
}
2024-02-07 16:40:36 +01:00
public Task StopAsync(CancellationToken cancellationToken)
{
2024-02-07 16:40:36 +01:00
return Task.CompletedTask;
}
2024-02-07 16:40:36 +01:00
public Task AddPacketAsync(byte[] packetData, TPacketIdEnum operationCode, TSession session)
{
2024-02-07 16:40:36 +01:00
return _packetDistributor.AddPacketAsync(packetData, operationCode, session);
}
public TPacketIdEnum GetOperationCodeByPacketType(IPacket packet)
{
var type = packet.GetType();
2024-02-07 16:40:36 +01:00
_packetDistributor.PacketIdMap.TryGetValue(type, out var value);
if (value is null)
{
throw new ArgumentOutOfRangeException(type.Name);
}
return value;
}
}