chore: removal of PacketMediator

This commit is contained in:
Timothy Schenk 2024-02-08 14:02:26 +01:00
parent 7ac4c8c0ef
commit cc1a0acc30
Signed by: rainote
SSH key fingerprint: SHA256:pnkNSDwpAnaip00xaZlVFHKKsS7T8UtOomMzvs0yITE
13 changed files with 3 additions and 309 deletions

View file

@ -98,6 +98,7 @@
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.7.0"/> <PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.7.0"/>
<PackageReference Include="OpenTelemetry.PersistentStorage.FileSystem" Version="1.0.0"/> <PackageReference Include="OpenTelemetry.PersistentStorage.FileSystem" Version="1.0.0"/>
<PackageReference Include="OpenTelemetry.ResourceDetectors.Container" Version="1.0.0-beta.6"/> <PackageReference Include="OpenTelemetry.ResourceDetectors.Container" Version="1.0.0-beta.6"/>
<PackageReference Include="Rai.PacketMediator" Version="0.0.5" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0"/> <PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0"/>
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0"/> <PackageReference Include="Serilog.Sinks.File" Version="5.0.0"/>
</ItemGroup> </ItemGroup>

View file

@ -11,8 +11,7 @@ RUN echo "Target: $TARGETARCH" && echo "Build: $BUILDPLATFORM"
WORKDIR /src WORKDIR /src
COPY ["Continuity.AuthServer/Continuity.AuthServer.csproj", "Continuity.AuthServer/"] COPY ["Continuity.AuthServer/Continuity.AuthServer.csproj", "Continuity.AuthServer/"]
COPY ["Wonderking/Wonderking.csproj", "Wonderking/"] COPY ["Wonderking/Wonderking.csproj", "Wonderking/"]
COPY ["Rai.PacketMediator/Rai.PacketMediator.csproj", "Rai.PacketMediator/"] RUN dotnet restore "Wonderking/Wonderking.csproj" -a $TARGETARCH && dotnet restore "Continuity.AuthServer/Continuity.AuthServer.csproj" -a $TARGETARCH
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
COPY . . COPY . .
FROM build AS publish FROM build AS publish

View file

@ -6,8 +6,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benchmarks", "Benchmarks\Be
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wonderking", "Wonderking\Wonderking.csproj", "{6B53A10B-C397-4347-BB00-A12272D0528E}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wonderking", "Wonderking\Wonderking.csproj", "{6B53A10B-C397-4347-BB00-A12272D0528E}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rai.PacketMediator", "Rai.PacketMediator\Rai.PacketMediator.csproj", "{D6FA787F-6B95-4679-BC6F-EED10B591E5C}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{6B53A10B-C397-4347-BB00-A12272D0528E}.Release|Any CPU.Build.0 = 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 EndGlobalSection
EndGlobal EndGlobal

View file

@ -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;

View file

@ -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);
}

View file

@ -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();
}

View file

@ -1,5 +0,0 @@
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
namespace Rai.PacketMediator;
public interface IPacket;

View file

@ -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<in TIncomingPacket, in TSession> : IPacketHandler<TSession>
where TIncomingPacket : IIncomingPacket
{
async Task<bool> IPacketHandler<TSession>.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<in TSession>
{
Task<bool> TryHandleAsync(IIncomingPacket packet, TSession session, CancellationToken cancellationToken);
}

View file

@ -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<TPacketIdEnum, TSession> where TPacketIdEnum : Enum
{
private readonly Channel<ValueTuple<byte[], TPacketIdEnum, TSession>> _channel;
private readonly ImmutableDictionary<TPacketIdEnum,
Func<byte[], IIncomingPacket>> _deserializationMap;
private readonly ConcurrentDictionary<TPacketIdEnum, IPacketHandler<TSession>?> _packetHandlersInstantiation;
public PacketDistributor(IServiceProvider serviceProvider,
IEnumerable<Assembly> sourcesContainingPackets, IEnumerable<Assembly> sourcesContainingPacketHandlers)
{
_channel = Channel.CreateUnbounded<ValueTuple<byte[], TPacketIdEnum, TSession>>(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<TPacketIdEnum, Func<byte[], IIncomingPacket>>();
_packetHandlersInstantiation = new ConcurrentDictionary<TPacketIdEnum, IPacketHandler<TSession>?>();
packetHandlers.ForEach(packetHandlerPair =>
{
var packetHandler =
ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider,
packetHandlerPair.Value);
_packetHandlersInstantiation.TryAdd(packetHandlerPair.Key, packetHandler as IPacketHandler<TSession>);
});
allIncomingPackets.ForEach(packetsType =>
{
var lambda = CodeGenerator.Lambda<Func<byte[], IIncomingPacket>>(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<Type, TPacketIdEnum> PacketIdMap { get; }
private static IEnumerable<KeyValuePair<TPacketIdEnum, Type>> GetAllPackets(
IEnumerable<Assembly> sourcesContainingPackets, Type packetType)
{
var packetsWithId = sourcesContainingPackets.SelectMany(a => a.GetTypes()
.Where(type => type is { IsInterface: false, IsAbstract: false } &&
type.GetInterfaces().Contains(packetType)
&& type.GetCustomAttributes<PacketIdAttribute<TPacketIdEnum>>().Any()
))
.Select(type =>
new { Type = type, Attribute = type.GetCustomAttribute<PacketIdAttribute<TPacketIdEnum>>() })
.Select(x => new KeyValuePair<TPacketIdEnum, Type>(x.Attribute!.Code, x.Type));
return packetsWithId;
}
private static IEnumerable<KeyValuePair<TPacketIdEnum, Type>> GetAllPacketHandlersWithId(
IEnumerable<Assembly> 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<PacketIdAttribute<TPacketIdEnum>>()
}))
.Where(x => x.PacketId != null)
.Select(x => new KeyValuePair<TPacketIdEnum, Type>(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)!;
}
}

View file

@ -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<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;
}
}

View file

@ -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<TPacketIdEnum> : Attribute where TPacketIdEnum : Enum
{
protected PacketIdAttribute(TPacketIdEnum code)
{
Code = code;
}
public TPacketIdEnum Code { get; }
}

View file

@ -1,25 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DotNext" Version="5.0.1"/>
<PackageReference Include="DotNext.Metaprogramming" Version="5.0.1"/>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0"/>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.139">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0"/>
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.8.14">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

View file

@ -31,6 +31,7 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Rai.PacketMediator" Version="0.0.5" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="..\LICENSE"> <None Include="..\LICENSE">
@ -41,7 +42,4 @@
<ItemGroup> <ItemGroup>
<Folder Include="Game\Writer\"/> <Folder Include="Game\Writer\"/>
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Rai.PacketMediator\Rai.PacketMediator.csproj"/>
</ItemGroup>
</Project> </Project>