continuity/Continuity.AuthServer/Services/PacketDistributorService.cs

188 lines
7 KiB
C#
Raw Normal View History

2023-11-21 20:37:50 +00:00
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
2023-11-20 18:58:30 +00:00
using System.Collections.Concurrent;
using System.Collections.Immutable;
using System.Diagnostics;
2023-08-09 14:23:41 +00:00
using System.Reflection;
using Continuity.AuthServer.LoggerMessages;
using Continuity.AuthServer.PacketHandlers;
using Continuity.AuthServer.Packets;
using DotNext.Collections.Generic;
2023-11-19 16:07:28 +00:00
using DotNext.Linq.Expressions;
using DotNext.Metaprogramming;
2023-08-09 18:14:14 +00:00
using MassTransit.Internals;
using Microsoft.Extensions.DependencyInjection;
2023-08-09 14:23:41 +00:00
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
2023-08-09 18:14:14 +00:00
using Microsoft.VisualBasic.CompilerServices;
2023-08-14 20:30:35 +00:00
using Newtonsoft.Json;
2023-11-19 16:07:28 +00:00
using Wonderking.Packets;
namespace Continuity.AuthServer.Services;
2023-11-19 16:07:28 +00:00
using static CodeGenerator;
using static ExpressionBuilder;
2023-08-09 14:23:41 +00:00
public class PacketDistributorService : IHostedService, IDisposable
2023-08-09 14:23:41 +00:00
{
chore: .net8 support and extended support for pipeline jobs fix: incorrect command fix: incorrect command fix: incorrect command fix: export tools path chore: test dir fix: test build script fix: missing semicolon fix: missing semicolon fix: missing buildscript fix: missing buildscript chore: update build to .net7 ci: set root dir ci: set root dir revert: .net 8 support for benchmarks ci: disable docker for ci ci: disable docker for ci ci: disable docker for ci ci: trigger ci: i stg ci: let's hope its the correct one ci: i stg ci: further sonarscanner setup ci: add tools ci: rearrange ci: verbose output ci: hardcoded project key ci: test env ci: test more env ci: env test again ci: test env ci: shit ci: final setup ci: final setup ci: final setup ci: adjust buildscript ci: nuke ci: install java ci: install java refactor: loggermessages to own namespace chore: switch to dotnet foundation editorconfig preset ci: switch to basic gitea ci ci: steps ci: add missing runs-on ci: remove unnecessary actions ci: test attempt? ci: add missing name for step ci: fix missing project name reference ci: lets try again ci: again ci: again.. ci: idk at this point ci: append path prematurely ci: add path to bash cli: I really don't know ci: again.... ci: idk ci: again.... ci: another one ci: fix incorrect path add-path ci: add dependency track support ci: fix upload ci: forgot to adjust data for action ci: incorrect path? ci: add version tag ci: idk ci: fix incorrect path for bom ci: fix version tag ci: disable github license resolution for now ci: does this work ci: another one bites the dust chore: .net 8 and extended pipeline support ci: again chore: dockerignore added chore(deps): update dependency benchmarkdotnet to v0.13.10 Signed-off-by: noreply@rainote.dev ci: dependency track can run on any branch ci: first attempt docker image ci: again ci: some fixes ci: idk ci: does this help ci: idk ci: another one ci: forgot ci: downgrade qemu setup ci: downgrade.. ci: v1 includes docker ci: rearrange ci: idk what to do with this ci: let's try cat ci: alt ci: depressing ci: yikes ci: ah come on ci: let's try new version again ci: again ci: another one ci: another one ci: confusion ci: try single ci: aaaa ci: one more time ci: again main (#69) Reviewed-on: https://forge.rainote.dev/wonderking/continuity/pulls/69 Co-authored-by: Timothy Schenk <admin@rainote.dev> Co-committed-by: Timothy Schenk <admin@rainote.dev> chore: ci jobs expanded & .net 8 support ci: branch name sanitization for docker ci: another attempt ci: forgot actor chore: remove nuke remnants chore(deps): update dependency dotnet-sdk to v7.0.403 Signed-off-by: noreply@rainote.dev refactor: shared data into library refactor: rewrite for packethandler/id map chore: logging chore: Sonar Warnings chore: upgrade to .net8 image chore: fetch all required information for characters chore: upgrade to .net 8 ci: install .net 7.0 alongside 8.0 for sbom fix: incorrect job ci: let's try arm64 again ci: adjust project for .net 8 support chore: adjustments to composefile chore: analyzer setup chore: analyzer and project settings updated chore: ci jobs expanded & .net 8 support
2023-10-27 17:47:17 +00:00
private readonly ConcurrentQueue<RawPacket> _concurrentQueue;
2023-11-19 16:07:28 +00:00
private readonly ILogger<PacketDistributorService> _logger;
private readonly IServiceProvider _serviceProvider;
private ImmutableDictionary<OperationCode,
Func<byte[], IPacket>> _deserializationMap;
2023-08-14 19:30:32 +00:00
private ConcurrentDictionary<OperationCode, IPacketHandler> _packetHandlersInstantiation;
private readonly ActivitySource _activitySource;
2023-08-09 14:23:41 +00:00
2023-08-09 18:14:14 +00:00
public PacketDistributorService(ILogger<PacketDistributorService> logger, IServiceProvider serviceProvider)
2023-08-09 14:23:41 +00:00
{
2023-11-19 16:07:28 +00:00
_concurrentQueue = new ConcurrentQueue<RawPacket>();
_logger = logger;
_serviceProvider = serviceProvider;
_activitySource = new ActivitySource(nameof(Server));
}
public Task StartAsync(CancellationToken cancellationToken)
{
var tempDeserializationMap =
new Dictionary<OperationCode, Func<byte[], IPacket>>();
2023-08-09 18:14:14 +00:00
2023-11-08 18:48:58 +00:00
var wonderkingAssembly = Assembly.GetAssembly(typeof(IPacket));
2023-11-19 16:07:28 +00:00
var packetsTypes = GetPacketsWithId(wonderkingAssembly);
var packetHandlers = GetAllPacketHandlersWithId(Assembly.GetExecutingAssembly());
_packetHandlersInstantiation = new ConcurrentDictionary<OperationCode, IPacketHandler>();
packetHandlers.ForEach(x =>
{
var packetHandler =
ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider,
x.Value);
_packetHandlersInstantiation.TryAdd(x.Key, packetHandler as IPacketHandler);
});
foreach (var packetsType in packetsTypes)
{
var lambda = Lambda<Func<byte[], IPacket>>(fun =>
{
var argPacketData = fun[0];
var newPacket = packetsType.Value.New();
var packetVariable = DeclareVariable(packetsType.Value, "packet");
Assign(packetVariable, newPacket);
Call(packetVariable, nameof(IPacket.Deserialize), argPacketData);
Return(packetVariable);
}).Compile();
_logger.PacketCreationFunctionCreated(packetsType.Key);
tempDeserializationMap.Add(packetsType.Key, lambda);
}
2023-11-19 16:07:28 +00:00
_deserializationMap = tempDeserializationMap.ToImmutableDictionary();
return Task.CompletedTask;
}
2023-11-19 16:07:28 +00:00
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
private Dictionary<OperationCode, Type> GetPacketsWithId(Assembly executingAssembly)
{
2023-10-27 15:40:58 +00:00
// ! : We are filtering if types that don't have an instance of the required Attribute
var packetsWithId = executingAssembly.GetTypes().AsParallel()
2023-10-27 15:40:58 +00:00
.Where(type => type.HasInterface(typeof(IPacket)) && type is { IsInterface: false, IsAbstract: false })
2023-11-13 18:45:52 +00:00
.Where(type => type.Namespace?.Contains("Incoming") ?? false)
2023-10-27 15:40:58 +00:00
.Select(type => new { Type = type, Attribute = type.GetCustomAttribute<PacketIdAttribute>() })
.Where(item => item.Attribute is not null)
.ToDictionary(item => item.Attribute!.Code, item => item.Type);
if (packetsWithId is not { Count: 0 })
2023-08-09 14:23:41 +00:00
{
2023-10-27 15:40:58 +00:00
packetsWithId.AsParallel()
2023-11-19 16:07:28 +00:00
.ForAll(packet => _logger.PacketWithIdAdded(packet.Key, packet.Value.FullName));
return packetsWithId;
2023-08-09 14:23:41 +00:00
}
2023-08-09 18:14:14 +00:00
2023-11-19 16:07:28 +00:00
_logger.NoPacketsFound();
throw new IncompleteInitialization();
}
2023-08-09 18:14:14 +00:00
private Dictionary<OperationCode, Type> GetAllPacketHandlersWithId(Assembly assembly)
{
chore: .net8 support and extended support for pipeline jobs fix: incorrect command fix: incorrect command fix: incorrect command fix: export tools path chore: test dir fix: test build script fix: missing semicolon fix: missing semicolon fix: missing buildscript fix: missing buildscript chore: update build to .net7 ci: set root dir ci: set root dir revert: .net 8 support for benchmarks ci: disable docker for ci ci: disable docker for ci ci: disable docker for ci ci: trigger ci: i stg ci: let's hope its the correct one ci: i stg ci: further sonarscanner setup ci: add tools ci: rearrange ci: verbose output ci: hardcoded project key ci: test env ci: test more env ci: env test again ci: test env ci: shit ci: final setup ci: final setup ci: final setup ci: adjust buildscript ci: nuke ci: install java ci: install java refactor: loggermessages to own namespace chore: switch to dotnet foundation editorconfig preset ci: switch to basic gitea ci ci: steps ci: add missing runs-on ci: remove unnecessary actions ci: test attempt? ci: add missing name for step ci: fix missing project name reference ci: lets try again ci: again ci: again.. ci: idk at this point ci: append path prematurely ci: add path to bash cli: I really don't know ci: again.... ci: idk ci: again.... ci: another one ci: fix incorrect path add-path ci: add dependency track support ci: fix upload ci: forgot to adjust data for action ci: incorrect path? ci: add version tag ci: idk ci: fix incorrect path for bom ci: fix version tag ci: disable github license resolution for now ci: does this work ci: another one bites the dust chore: .net 8 and extended pipeline support ci: again chore: dockerignore added chore(deps): update dependency benchmarkdotnet to v0.13.10 Signed-off-by: noreply@rainote.dev ci: dependency track can run on any branch ci: first attempt docker image ci: again ci: some fixes ci: idk ci: does this help ci: idk ci: another one ci: forgot ci: downgrade qemu setup ci: downgrade.. ci: v1 includes docker ci: rearrange ci: idk what to do with this ci: let's try cat ci: alt ci: depressing ci: yikes ci: ah come on ci: let's try new version again ci: again ci: another one ci: another one ci: confusion ci: try single ci: aaaa ci: one more time ci: again main (#69) Reviewed-on: https://forge.rainote.dev/wonderking/continuity/pulls/69 Co-authored-by: Timothy Schenk <admin@rainote.dev> Co-committed-by: Timothy Schenk <admin@rainote.dev> chore: ci jobs expanded & .net 8 support ci: branch name sanitization for docker ci: another attempt ci: forgot actor chore: remove nuke remnants chore(deps): update dependency dotnet-sdk to v7.0.403 Signed-off-by: noreply@rainote.dev refactor: shared data into library refactor: rewrite for packethandler/id map chore: logging chore: Sonar Warnings chore: upgrade to .net8 image chore: fetch all required information for characters chore: upgrade to .net 8 ci: install .net 7.0 alongside 8.0 for sbom fix: incorrect job ci: let's try arm64 again ci: adjust project for .net 8 support chore: adjustments to composefile chore: analyzer setup chore: analyzer and project settings updated chore: ci jobs expanded & .net 8 support
2023-10-27 17:47:17 +00:00
// ! : We are filtering if types that don't have an instance of the required Attribute
var packetHandlersWithId = assembly.GetTypes().AsParallel()
.Where(t =>
t is { IsClass: true, IsAbstract: false } && Array.Exists(t
.GetInterfaces(), i =>
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()[0].GetCustomAttribute<PacketIdAttribute>()?.Code
})
.Where(x => x.PacketId is not null)
.ToDictionary(
x => x.PacketId!.Value, x => x.Type
);
2023-08-09 18:14:14 +00:00
if (packetHandlersWithId is not { Count: 0 })
2023-08-09 18:14:14 +00:00
{
2023-10-27 15:40:58 +00:00
packetHandlersWithId.AsParallel().ForAll(packetHandler =>
2023-11-19 16:07:28 +00:00
_logger.PacketHandlerWithIdAdded(packetHandler.Key, packetHandler.Value.FullName));
return packetHandlersWithId;
2023-08-09 18:14:14 +00:00
}
2023-11-19 16:07:28 +00:00
_logger.NoPacketHandlersFound();
throw new IncompleteInitialization();
2023-08-09 14:23:41 +00:00
}
2023-08-09 18:14:14 +00:00
public void AddPacket(RawPacket rawPacket)
2023-08-09 14:23:41 +00:00
{
2023-11-19 16:07:28 +00:00
_concurrentQueue.Enqueue(rawPacket);
DequeueRawPacket();
_logger.PacketReceived(rawPacket.OperationCode);
2023-08-09 14:23:41 +00:00
}
private void DequeueRawPacket()
2023-08-09 14:23:41 +00:00
{
2023-11-19 16:07:28 +00:00
if (_concurrentQueue.TryDequeue(out var item))
2023-08-09 14:23:41 +00:00
{
2023-11-25 13:37:54 +00:00
ThreadPool.QueueUserWorkItem(InvokePacketHandler, item, preferLocal: false);
2023-08-09 18:14:14 +00:00
}
2023-08-09 14:23:41 +00:00
}
2023-08-13 16:29:39 +00:00
private void InvokePacketHandler(RawPacket item)
{
IPacket packet;
2023-11-19 16:07:28 +00:00
_logger.PacketDequeued(item.Session.Id, item.OperationCode);
if (!_deserializationMap.TryGetValue(item.OperationCode, out var value))
2023-08-13 16:29:39 +00:00
{
2023-11-19 16:07:28 +00:00
_logger.PacketTypeNotFound(item.OperationCode);
2023-08-13 16:29:39 +00:00
return;
}
using (var packetParsingActivity = _activitySource.StartActivity("PacketParsing"))
{
packetParsingActivity?.SetTag("PacketId", item.OperationCode);
packet = value(item.MessageBody);
}
using (var packetHandlerActivity = _activitySource.StartActivity("PacketHandler"))
{
packetHandlerActivity?.SetTag("PacketId", item.OperationCode);
_ = _packetHandlersInstantiation[item.OperationCode].TryHandleAsync(packet, item.Session);
}
_logger.PacketData(JsonConvert.SerializeObject(packet));
2023-11-19 16:07:28 +00:00
_logger.PacketFinished(item.Session.Id, item.OperationCode);
}
public void Dispose()
{
GC.SuppressFinalize(this);
_activitySource.Dispose();
}
2023-08-11 09:31:30 +00:00
}