From cc1a0acc3088d473763e05363e477c57207ac5e1 Mon Sep 17 00:00:00 2001 From: Timothy Schenk Date: Thu, 8 Feb 2024 14:02:26 +0100 Subject: [PATCH] chore: removal of PacketMediator --- .../Continuity.AuthServer.csproj | 1 + Continuity.AuthServer/Dockerfile | 3 +- Continuity.sln | 6 - Rai.PacketMediator/IBidirectionalPacket.cs | 8 - Rai.PacketMediator/IIncomingPacket.cs | 12 -- Rai.PacketMediator/IOutgoingPacket.cs | 12 -- Rai.PacketMediator/IPacket.cs | 5 - Rai.PacketMediator/IPacketHandler.cs | 35 ----- Rai.PacketMediator/PacketDistributor.cs | 141 ------------------ .../PacketDistributorService.cs | 46 ------ Rai.PacketMediator/PacketIdAttribute.cs | 14 -- Rai.PacketMediator/Rai.PacketMediator.csproj | 25 ---- Wonderking/Wonderking.csproj | 4 +- 13 files changed, 3 insertions(+), 309 deletions(-) delete mode 100644 Rai.PacketMediator/IBidirectionalPacket.cs delete mode 100644 Rai.PacketMediator/IIncomingPacket.cs delete mode 100644 Rai.PacketMediator/IOutgoingPacket.cs delete mode 100644 Rai.PacketMediator/IPacket.cs delete mode 100644 Rai.PacketMediator/IPacketHandler.cs delete mode 100644 Rai.PacketMediator/PacketDistributor.cs delete mode 100644 Rai.PacketMediator/PacketDistributorService.cs delete mode 100644 Rai.PacketMediator/PacketIdAttribute.cs delete mode 100644 Rai.PacketMediator/Rai.PacketMediator.csproj diff --git a/Continuity.AuthServer/Continuity.AuthServer.csproj b/Continuity.AuthServer/Continuity.AuthServer.csproj index 08bd7a2..d7fa257 100644 --- a/Continuity.AuthServer/Continuity.AuthServer.csproj +++ b/Continuity.AuthServer/Continuity.AuthServer.csproj @@ -98,6 +98,7 @@ + diff --git a/Continuity.AuthServer/Dockerfile b/Continuity.AuthServer/Dockerfile index dc6d200..557f0df 100644 --- a/Continuity.AuthServer/Dockerfile +++ b/Continuity.AuthServer/Dockerfile @@ -11,8 +11,7 @@ RUN echo "Target: $TARGETARCH" && echo "Build: $BUILDPLATFORM" WORKDIR /src COPY ["Continuity.AuthServer/Continuity.AuthServer.csproj", "Continuity.AuthServer/"] COPY ["Wonderking/Wonderking.csproj", "Wonderking/"] -COPY ["Rai.PacketMediator/Rai.PacketMediator.csproj", "Rai.PacketMediator/"] -RUN dotnet restore "Wonderking/Wonderking.csproj" -a $TARGETARCH && dotnet restore "Rai.PacketMediator/Rai.PacketMediator.csproj" -a $TARGETARCH && dotnet restore "Continuity.AuthServer/Continuity.AuthServer.csproj" -a $TARGETARCH +RUN dotnet restore "Wonderking/Wonderking.csproj" -a $TARGETARCH && dotnet restore "Continuity.AuthServer/Continuity.AuthServer.csproj" -a $TARGETARCH COPY . . FROM build AS publish diff --git a/Continuity.sln b/Continuity.sln index 941ed9f..ebb8be6 100644 --- a/Continuity.sln +++ b/Continuity.sln @@ -6,8 +6,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benchmarks", "Benchmarks\Be EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wonderking", "Wonderking\Wonderking.csproj", "{6B53A10B-C397-4347-BB00-A12272D0528E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rai.PacketMediator", "Rai.PacketMediator\Rai.PacketMediator.csproj", "{D6FA787F-6B95-4679-BC6F-EED10B591E5C}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -26,9 +24,5 @@ Global {6B53A10B-C397-4347-BB00-A12272D0528E}.Debug|Any CPU.Build.0 = Debug|Any CPU {6B53A10B-C397-4347-BB00-A12272D0528E}.Release|Any CPU.ActiveCfg = Release|Any CPU {6B53A10B-C397-4347-BB00-A12272D0528E}.Release|Any CPU.Build.0 = Release|Any CPU - {D6FA787F-6B95-4679-BC6F-EED10B591E5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D6FA787F-6B95-4679-BC6F-EED10B591E5C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D6FA787F-6B95-4679-BC6F-EED10B591E5C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D6FA787F-6B95-4679-BC6F-EED10B591E5C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/Rai.PacketMediator/IBidirectionalPacket.cs b/Rai.PacketMediator/IBidirectionalPacket.cs deleted file mode 100644 index 88d8450..0000000 --- a/Rai.PacketMediator/IBidirectionalPacket.cs +++ /dev/null @@ -1,8 +0,0 @@ -// Licensed to Timothy Schenk under the GNU AGPL Version 3 License. - -using JetBrains.Annotations; - -namespace Rai.PacketMediator; - -[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)] -public interface IBidirectionalPacket : IOutgoingPacket, IIncomingPacket; diff --git a/Rai.PacketMediator/IIncomingPacket.cs b/Rai.PacketMediator/IIncomingPacket.cs deleted file mode 100644 index c0f5907..0000000 --- a/Rai.PacketMediator/IIncomingPacket.cs +++ /dev/null @@ -1,12 +0,0 @@ -// Licensed to Timothy Schenk under the GNU AGPL Version 3 License. - -using JetBrains.Annotations; - -namespace Rai.PacketMediator; - -[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)] -public interface IIncomingPacket : IPacket -{ - [UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)] - public void Deserialize(byte[] data); -} diff --git a/Rai.PacketMediator/IOutgoingPacket.cs b/Rai.PacketMediator/IOutgoingPacket.cs deleted file mode 100644 index ebf57c1..0000000 --- a/Rai.PacketMediator/IOutgoingPacket.cs +++ /dev/null @@ -1,12 +0,0 @@ -// Licensed to Timothy Schenk under the GNU AGPL Version 3 License. - -using JetBrains.Annotations; - -namespace Rai.PacketMediator; - -[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)] -public interface IOutgoingPacket : IPacket -{ - [UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)] - public byte[] Serialize(); -} diff --git a/Rai.PacketMediator/IPacket.cs b/Rai.PacketMediator/IPacket.cs deleted file mode 100644 index 1044db9..0000000 --- a/Rai.PacketMediator/IPacket.cs +++ /dev/null @@ -1,5 +0,0 @@ -// Licensed to Timothy Schenk under the GNU AGPL Version 3 License. - -namespace Rai.PacketMediator; - -public interface IPacket; diff --git a/Rai.PacketMediator/IPacketHandler.cs b/Rai.PacketMediator/IPacketHandler.cs deleted file mode 100644 index 72ef6bb..0000000 --- a/Rai.PacketMediator/IPacketHandler.cs +++ /dev/null @@ -1,35 +0,0 @@ -// Licensed to Timothy Schenk under the GNU AGPL Version 3 License. - -using System.Diagnostics; -using JetBrains.Annotations; - -namespace Rai.PacketMediator; - -[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)] -public interface IPacketHandler : IPacketHandler - where TIncomingPacket : IIncomingPacket -{ - async Task IPacketHandler.TryHandleAsync(IIncomingPacket packet, TSession session, - CancellationToken cancellationToken) - { - if (packet is not TIncomingPacket tPacket) - { - return false; - } - - using var activity = new ActivitySource(nameof(PacketMediator)).StartActivity(nameof(HandleAsync)); - activity?.AddTag("Handler", ToString()); - activity?.AddTag("Packet", packet.ToString()); - await HandleAsync(tPacket, session, cancellationToken); - - return true; - } - - [UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)] - public Task HandleAsync(TIncomingPacket packet, TSession session, CancellationToken cancellationToken); -} - -public interface IPacketHandler -{ - Task TryHandleAsync(IIncomingPacket packet, TSession session, CancellationToken cancellationToken); -} diff --git a/Rai.PacketMediator/PacketDistributor.cs b/Rai.PacketMediator/PacketDistributor.cs deleted file mode 100644 index f020340..0000000 --- a/Rai.PacketMediator/PacketDistributor.cs +++ /dev/null @@ -1,141 +0,0 @@ -// Licensed to Timothy Schenk under the GNU AGPL Version 3 License. - -using System.Collections.Concurrent; -using System.Collections.Immutable; -using System.Reflection; -using System.Threading.Channels; -using DotNext.Collections.Generic; -using DotNext.Linq.Expressions; -using DotNext.Metaprogramming; -using Microsoft.Extensions.DependencyInjection; - -namespace Rai.PacketMediator; - -public class PacketDistributor where TPacketIdEnum : Enum -{ - private readonly Channel> _channel; - - private readonly ImmutableDictionary> _deserializationMap; - - private readonly ConcurrentDictionary?> _packetHandlersInstantiation; - - public PacketDistributor(IServiceProvider serviceProvider, - IEnumerable sourcesContainingPackets, IEnumerable sourcesContainingPacketHandlers) - { - _channel = Channel.CreateUnbounded>(new UnboundedChannelOptions - { - AllowSynchronousContinuations = false, - SingleReader = false, - SingleWriter = false - }); - var containingPackets = sourcesContainingPackets as Assembly[] ?? sourcesContainingPackets.ToArray(); - var allIncomingPackets = GetAllPackets(containingPackets, typeof(IIncomingPacket)); - var allOutgoingPackets = GetAllPackets(containingPackets, typeof(IOutgoingPacket)); - - var packetHandlers = GetAllPacketHandlersWithId(sourcesContainingPacketHandlers); - - PacketIdMap = allOutgoingPackets.Select(x => new { PacketId = x.Key, Type = x.Value }) - .ToImmutableDictionary(x => x.Type, x => x.PacketId); - - var tempDeserializationMap = - new ConcurrentDictionary>(); - _packetHandlersInstantiation = new ConcurrentDictionary?>(); - packetHandlers.ForEach(packetHandlerPair => - { - var packetHandler = - ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider, - packetHandlerPair.Value); - _packetHandlersInstantiation.TryAdd(packetHandlerPair.Key, packetHandler as IPacketHandler); - }); - allIncomingPackets.ForEach(packetsType => - { - var lambda = CodeGenerator.Lambda>(fun => - { - var argPacketData = fun[0]; - var newPacket = packetsType.Value.New(); - - var packetVariable = CodeGenerator.DeclareVariable(packetsType.Value, "packet"); - CodeGenerator.Assign(packetVariable, newPacket); - CodeGenerator.Call(packetVariable, nameof(IIncomingPacket.Deserialize), argPacketData); - - CodeGenerator.Return(packetVariable); - }).Compile(); - tempDeserializationMap.TryAdd(packetsType.Key, lambda); - }); - - _deserializationMap = tempDeserializationMap.ToImmutableDictionary(); - } - - public ImmutableDictionary PacketIdMap { get; } - - private static IEnumerable> GetAllPackets( - IEnumerable sourcesContainingPackets, Type packetType) - { - var packetsWithId = sourcesContainingPackets.SelectMany(a => a.GetTypes() - .Where(type => type is { IsInterface: false, IsAbstract: false } && - type.GetInterfaces().Contains(packetType) - && type.GetCustomAttributes>().Any() - )) - .Select(type => - new { Type = type, Attribute = type.GetCustomAttribute>() }) - .Select(x => new KeyValuePair(x.Attribute!.Code, x.Type)); - - return packetsWithId; - } - - private static IEnumerable> GetAllPacketHandlersWithId( - IEnumerable sourcesContainingPacketHandlers) - { - var packetHandlersWithId = sourcesContainingPacketHandlers.SelectMany(assembly => assembly.GetTypes() - .Where(t => - t is { IsClass: true, IsAbstract: false } && Array.Exists(t - .GetInterfaces(), i => - i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler<,>))) - .Select(packetHandlerType => new - { - Type = packetHandlerType, - PacketId = packetHandlerType - .GetInterfaces().First(t1 => - t1 is { IsGenericType: true } && - t1.GetGenericTypeDefinition() == typeof(IPacketHandler<,>)).GetGenericArguments() - .First(genericType => genericType.GetInterfaces().Any(packetType => - packetType == typeof(IPacket))) - .GetCustomAttribute>() - })) - .Where(x => x.PacketId != null) - .Select(x => new KeyValuePair(x.PacketId!.Code, x.Type)); - - return packetHandlersWithId; - } - - public async Task AddPacketAsync(byte[] packetData, TPacketIdEnum operationCode, TSession session) - { - await _channel.Writer.WriteAsync((packetData, operationCode, session)); - } - - public async Task DequeuePacketAsync(CancellationToken cancellationToken) - { - while (await _channel.Reader.WaitToReadAsync(cancellationToken)) - { - while (_channel.Reader.TryRead(out var item)) - { - await InvokePacketHandlerAsync(item, cancellationToken); - } - } - } - - private async Task InvokePacketHandlerAsync((byte[], TPacketIdEnum, TSession) valueTuple, - CancellationToken cancellationToken) - { - var (packetData, operationCode, session) = valueTuple; - if (!_deserializationMap.TryGetValue(operationCode, out var func)) - { - return; - } - - var packet = func(packetData); - - await _packetHandlersInstantiation[operationCode]?.TryHandleAsync(packet, session, cancellationToken)!; - } -} diff --git a/Rai.PacketMediator/PacketDistributorService.cs b/Rai.PacketMediator/PacketDistributorService.cs deleted file mode 100644 index c1a94ee..0000000 --- a/Rai.PacketMediator/PacketDistributorService.cs +++ /dev/null @@ -1,46 +0,0 @@ -// Licensed to Timothy Schenk under the GNU AGPL Version 3 License. - -using System.Reflection; -using Microsoft.Extensions.Hosting; - -namespace Rai.PacketMediator; - -public class PacketDistributorService : IHostedService - where TPacketIdEnum : Enum -{ - private readonly PacketDistributor _packetDistributor; - - public PacketDistributorService(IServiceProvider serviceProvider, - IEnumerable sourcesContainingPackets, IEnumerable sourcesContainingPacketHandlers) - { - _packetDistributor = new PacketDistributor(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; - } -} diff --git a/Rai.PacketMediator/PacketIdAttribute.cs b/Rai.PacketMediator/PacketIdAttribute.cs deleted file mode 100644 index c4fdb64..0000000 --- a/Rai.PacketMediator/PacketIdAttribute.cs +++ /dev/null @@ -1,14 +0,0 @@ -// Licensed to Timothy Schenk under the GNU AGPL Version 3 License. - -namespace Rai.PacketMediator; - -[AttributeUsage(AttributeTargets.Class, Inherited = false)] -public abstract class PacketIdAttribute : Attribute where TPacketIdEnum : Enum -{ - protected PacketIdAttribute(TPacketIdEnum code) - { - Code = code; - } - - public TPacketIdEnum Code { get; } -} diff --git a/Rai.PacketMediator/Rai.PacketMediator.csproj b/Rai.PacketMediator/Rai.PacketMediator.csproj deleted file mode 100644 index 9b15be2..0000000 --- a/Rai.PacketMediator/Rai.PacketMediator.csproj +++ /dev/null @@ -1,25 +0,0 @@ - - - - net8.0 - enable - enable - - - - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - diff --git a/Wonderking/Wonderking.csproj b/Wonderking/Wonderking.csproj index 128c281..42cfba1 100644 --- a/Wonderking/Wonderking.csproj +++ b/Wonderking/Wonderking.csproj @@ -31,6 +31,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -41,7 +42,4 @@ - - -