chore: minor improvements

This commit is contained in:
Timothy Schenk 2024-08-07 19:05:41 +02:00
parent 2e9539c3c0
commit b5ac05a853
Signed by: rainote
SSH key fingerprint: SHA256:pnkNSDwpAnaip00xaZlVFHKKsS7T8UtOomMzvs0yITE

View file

@ -13,13 +13,15 @@ public class PacketDistributorService<TPacketIdEnum, TSession> : IHostedService
public PacketDistributorService(IServiceProvider serviceProvider, public PacketDistributorService(IServiceProvider serviceProvider,
IEnumerable<Assembly> sourcesContainingPackets, IEnumerable<Assembly> sourcesContainingPacketHandlers) IEnumerable<Assembly> sourcesContainingPackets, IEnumerable<Assembly> sourcesContainingPacketHandlers)
{ {
_packetDistributor = new PacketDistributor<TPacketIdEnum, TSession>(serviceProvider, sourcesContainingPackets, _packetDistributor = new PacketDistributor<TPacketIdEnum, TSession>(serviceProvider,
sourcesContainingPacketHandlers); sourcesContainingPackets,
sourcesContainingPacketHandlers
);
} }
public Task StartAsync(CancellationToken cancellationToken) public async Task StartAsync(CancellationToken cancellationToken)
{ {
return _packetDistributor.DequeuePacketAsync(cancellationToken); await _packetDistributor.DequeuePacketAsync(cancellationToken);
} }
public Task StopAsync(CancellationToken cancellationToken) public Task StopAsync(CancellationToken cancellationToken)
@ -32,15 +34,10 @@ public class PacketDistributorService<TPacketIdEnum, TSession> : IHostedService
return _packetDistributor.AddPacketAsync(packetData, operationCode, session); return _packetDistributor.AddPacketAsync(packetData, operationCode, session);
} }
public TPacketIdEnum GetOperationCodeByPacketType(IPacket packet) public DotNext.Optional<TPacketIdEnum> GetOperationCodeByPacketType(IPacket packet)
{ {
var type = packet.GetType(); var type = packet.GetType();
_packetDistributor.PacketIdMap.TryGetValue(type, out var value); _packetDistributor.PacketIdMap.TryGetValue(type, out var value);
if (value is null) return value ?? DotNext.Optional<TPacketIdEnum>.None;
{
throw new ArgumentOutOfRangeException(type.Name);
}
return value;
} }
} }