diff --git a/Rai.PacketMediator/IPacketHandler.cs b/Rai.PacketMediator/IPacketHandler.cs index 6e24d7e..d2ad071 100644 --- a/Rai.PacketMediator/IPacketHandler.cs +++ b/Rai.PacketMediator/IPacketHandler.cs @@ -6,14 +6,14 @@ using JetBrains.Annotations; namespace Rai.PacketMediator; [UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)] -public interface IPacketHandler : IPacketHandler where T : IIncomingPacket +public interface IPacketHandler : IPacketHandler where TIncomingPacket : IIncomingPacket { [UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)] - public Task HandleAsync(T packet, S session); + public Task HandleAsync(TIncomingPacket packet, TSession session); - async Task IPacketHandler.TryHandleAsync(IIncomingPacket packet, S session) + async Task IPacketHandler.TryHandleAsync(IIncomingPacket packet, TSession session) { - if (packet is not T tPacket) + if (packet is not TIncomingPacket tPacket) { return false; } @@ -27,7 +27,7 @@ public interface IPacketHandler : IPacketHandler where T : IIncom } } -public interface IPacketHandler +public interface IPacketHandler { - Task TryHandleAsync(IIncomingPacket packet, S session); + Task TryHandleAsync(IIncomingPacket packet, TSession session); } diff --git a/Rai.PacketMediator/PacketDistributorService.cs b/Rai.PacketMediator/PacketDistributorService.cs index 89f2478..fb0500f 100644 --- a/Rai.PacketMediator/PacketDistributorService.cs +++ b/Rai.PacketMediator/PacketDistributorService.cs @@ -15,35 +15,35 @@ namespace Rai.PacketMediator; using static CodeGenerator; -public class PacketDistributorService : Microsoft.Extensions.Hosting.IHostedService, IDisposable where T : Enum +public class PacketDistributorService : Microsoft.Extensions.Hosting.IHostedService, IDisposable where TPacketIdEnum : Enum { - private readonly Channel> _channel; + private readonly Channel> _channel; private readonly IServiceProvider _serviceProvider; private readonly Assembly[] _sourcesContainingPackets; - private ImmutableDictionary> _deserializationMap; - private ConcurrentDictionary?> _packetHandlersInstantiation; + private ConcurrentDictionary?> _packetHandlersInstantiation; private readonly ActivitySource _activitySource; public PacketDistributorService(IServiceProvider serviceProvider, Assembly[] sourcesContainingPackets) { - _channel = Channel.CreateUnbounded>(); + _channel = Channel.CreateUnbounded>(); _serviceProvider = serviceProvider; _sourcesContainingPackets = sourcesContainingPackets; _activitySource = new ActivitySource(nameof(PacketMediator)); } - private Dictionary RetrievePacketsDictionary() + private Dictionary RetrievePacketsDictionary() { using var activity = this._activitySource.StartActivity(); var packetsWithId = this._sourcesContainingPackets.SelectMany(a => a.GetTypes() .Where(type => type is { IsInterface: false, IsAbstract: false } && type.GetInterfaces().Contains(typeof(IIncomingPacket))) - .Select(type => new { Type = type, Attribute = type.GetCustomAttribute>() }) + .Select(type => new { Type = type, Attribute = type.GetCustomAttribute>() }) .Where(item => item.Attribute is not null) .ToDictionary(item => item.Attribute!.Code, item => item.Type)).ToDictionary(); @@ -52,22 +52,22 @@ public class PacketDistributorService : Microsoft.Extensions.Hosting.IHost return packetsWithId; } - private Dictionary GetAllPacketHandlersWithId() + private Dictionary GetAllPacketHandlersWithId() { using var activity = this._activitySource.StartActivity(); var packetHandlersWithId = this._sourcesContainingPackets.SelectMany(assembly => assembly.GetTypes() .Where(t => t is { IsClass: true, IsAbstract: false } && Array.Exists(t .GetInterfaces(), i => - i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler))) + i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler))) .Select(type => new { Type = type, PacketId = type .GetInterfaces().First(t1 => - t1 is { IsGenericType: true } && t1.GetGenericTypeDefinition() == typeof(IPacketHandler)) - .GetGenericArguments().First(t => t.GetCustomAttributes>().Any()) - .GetCustomAttributes>().First().Code + t1 is { IsGenericType: true } && t1.GetGenericTypeDefinition() == typeof(IPacketHandler)) + .GetGenericArguments().First(t => t.GetCustomAttributes>().Any()) + .GetCustomAttributes>().First().Code }) .ToDictionary( x => x.PacketId, x => x.Type @@ -99,14 +99,14 @@ public class PacketDistributorService : Microsoft.Extensions.Hosting.IHost } var tempDeserializationMap = - new Dictionary>(); - _packetHandlersInstantiation = new ConcurrentDictionary?>(); + new Dictionary>(); + _packetHandlersInstantiation = new ConcurrentDictionary?>(); packetHandlers.ForEach(x => { var packetHandler = ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider, x.Value); - _packetHandlersInstantiation.TryAdd(x.Key, packetHandler as IPacketHandler); + _packetHandlersInstantiation.TryAdd(x.Key, packetHandler as IPacketHandler); }); foreach (var packetsType in packetDictionary) { @@ -130,7 +130,7 @@ public class PacketDistributorService : Microsoft.Extensions.Hosting.IHost return Task.CompletedTask; } - public async Task AddPacketAsync(byte[] packetData, T operationCode, S session) + public async Task AddPacketAsync(byte[] packetData, TPacketIdEnum operationCode, TSession session) { await _channel.Writer.WriteAsync((packetData, operationCode, session)); } @@ -146,7 +146,7 @@ public class PacketDistributorService : Microsoft.Extensions.Hosting.IHost } } - private async Task InvokePacketHandlerAsync((byte[], T, S) valueTuple) + private async Task InvokePacketHandlerAsync((byte[], TPacketIdEnum, TSession) valueTuple) { IIncomingPacket packet; var (packetData, operationCode, session) = valueTuple; diff --git a/Rai.PacketMediator/PacketIdAttribute.cs b/Rai.PacketMediator/PacketIdAttribute.cs index ba5029e..f9d32a5 100644 --- a/Rai.PacketMediator/PacketIdAttribute.cs +++ b/Rai.PacketMediator/PacketIdAttribute.cs @@ -3,12 +3,12 @@ namespace Rai.PacketMediator; [AttributeUsage(AttributeTargets.Class, Inherited = false)] -public abstract class PacketIdAttribute : Attribute where T : Enum +public abstract class PacketIdAttribute : Attribute where TPacketIdEnum : Enum { - protected PacketIdAttribute(T code) + protected PacketIdAttribute(TPacketIdEnum code) { Code = code; } - public T Code { get; } + public TPacketIdEnum Code { get; } }