PacketMediator/PacketDistributorService.cs
Timothy Schenk 00db033f2e
Some checks failed
Release Rai.PacketMediator / preprocess (push) Successful in 2s
Release Rai.PacketMediator / build (push) Failing after 18s
Release Rai.PacketMediator / publish (push) Has been skipped
Release Rai.PacketMediator / generate-licences (push) Has been skipped
chore: Initial commit + v0.0.1
2024-02-08 13:01:12 +01:00

46 lines
1.5 KiB
C#

// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Reflection;
using Microsoft.Extensions.Hosting;
namespace Rai.PacketMediator;
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)
{
_packetDistributor = new PacketDistributor<TPacketIdEnum, TSession>(serviceProvider, sourcesContainingPackets,
sourcesContainingPacketHandlers);
}
public Task StartAsync(CancellationToken cancellationToken)
{
return _packetDistributor.DequeuePacketAsync(cancellationToken);
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public Task AddPacketAsync(byte[] packetData, TPacketIdEnum operationCode, TSession session)
{
return _packetDistributor.AddPacketAsync(packetData, operationCode, session);
}
public TPacketIdEnum GetOperationCodeByPacketType(IPacket packet)
{
var type = packet.GetType();
_packetDistributor.PacketIdMap.TryGetValue(type, out var value);
if (value is null)
{
throw new ArgumentOutOfRangeException(type.Name);
}
return value;
}
}