From efd261be77eb1253b9b9915f519a44c927f0f32c Mon Sep 17 00:00:00 2001 From: Timothy Schenk Date: Mon, 5 Feb 2024 18:12:12 +0100 Subject: [PATCH] chore: adjustments --- .../Consumers/PacketConsumer.cs | 5 ++-- Continuity.AuthServer/Dockerfile | 3 +- .../PacketHandlers/ChannelSelectionHandler.cs | 22 ++++++--------- .../CharacterCreationHandler.cs | 13 +++------ .../CharacterDeletionHandler.cs | 17 ++++------- .../CharacterNameCheckHandler.cs | 15 +++++----- .../PacketHandlers/LoginHandler.cs | 14 ++++------ Continuity.AuthServer/Program.cs | 28 +++++++++++++------ Rai.PacketMediator/PacketDistributor.cs | 24 ++++++++-------- docker-compose.yml | 3 +- 10 files changed, 69 insertions(+), 75 deletions(-) diff --git a/Continuity.AuthServer/Consumers/PacketConsumer.cs b/Continuity.AuthServer/Consumers/PacketConsumer.cs index a8288e2..a94bdd1 100644 --- a/Continuity.AuthServer/Consumers/PacketConsumer.cs +++ b/Continuity.AuthServer/Consumers/PacketConsumer.cs @@ -3,7 +3,6 @@ using Continuity.AuthServer.Packets; using JetBrains.Annotations; using MassTransit; -using NetCoreServer; using OpenTelemetry.Trace; using Rai.PacketMediator; using Wonderking.Packets; @@ -13,11 +12,11 @@ namespace Continuity.AuthServer.Consumers; [UsedImplicitly] public class PacketConsumer : IConsumer { - private readonly PacketDistributorService _distributorService; + private readonly PacketDistributorService _distributorService; private readonly TracerProvider _tracerProvider; - public PacketConsumer(PacketDistributorService distributorService, TracerProvider tracerProvider) + public PacketConsumer(PacketDistributorService distributorService, TracerProvider tracerProvider) { _distributorService = distributorService; _tracerProvider = tracerProvider; diff --git a/Continuity.AuthServer/Dockerfile b/Continuity.AuthServer/Dockerfile index 557f0df..dc6d200 100644 --- a/Continuity.AuthServer/Dockerfile +++ b/Continuity.AuthServer/Dockerfile @@ -11,7 +11,8 @@ RUN echo "Target: $TARGETARCH" && echo "Build: $BUILDPLATFORM" WORKDIR /src COPY ["Continuity.AuthServer/Continuity.AuthServer.csproj", "Continuity.AuthServer/"] COPY ["Wonderking/Wonderking.csproj", "Wonderking/"] -RUN dotnet restore "Wonderking/Wonderking.csproj" -a $TARGETARCH && dotnet restore "Continuity.AuthServer/Continuity.AuthServer.csproj" -a $TARGETARCH +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 COPY . . FROM build AS publish diff --git a/Continuity.AuthServer/PacketHandlers/ChannelSelectionHandler.cs b/Continuity.AuthServer/PacketHandlers/ChannelSelectionHandler.cs index ad13ac4..22a01b3 100644 --- a/Continuity.AuthServer/PacketHandlers/ChannelSelectionHandler.cs +++ b/Continuity.AuthServer/PacketHandlers/ChannelSelectionHandler.cs @@ -4,7 +4,6 @@ using Continuity.AuthServer.DB; using Continuity.AuthServer.DB.Documents; using DotNext.Collections.Generic; using Microsoft.EntityFrameworkCore; -using NetCoreServer; using Rai.PacketMediator; using Wonderking.Packets.Incoming; using Wonderking.Packets.Outgoing; @@ -12,7 +11,7 @@ using Wonderking.Packets.Outgoing.Data; namespace Continuity.AuthServer.PacketHandlers; -public partial class ChannelSelectionHandler : IPacketHandler +public partial class ChannelSelectionHandler : IPacketHandler { private readonly WonderkingContext _wonderkingContext; @@ -21,22 +20,17 @@ public partial class ChannelSelectionHandler : IPacketHandler() }; var accountExists = - await _wonderkingContext.Accounts.AsNoTracking().AnyAsync(a => a.Id == authSession.AccountId, cancellationToken: cancellationToken); + await _wonderkingContext.Accounts.AsNoTracking().AnyAsync(a => a.Id == session.AccountId, cancellationToken: cancellationToken); var amountOfCharacter = await _wonderkingContext.Characters.AsNoTracking().Include(c => c.Account) - .Where(c => c.Account.Id == authSession.AccountId).Take(3) + .Where(c => c.Account.Id == session.AccountId).Take(3) .CountAsync(cancellationToken: cancellationToken); if (!accountExists) @@ -51,13 +45,13 @@ public partial class ChannelSelectionHandler : IPacketHandler c.Account).Include(c => c.GuildMember) .ThenInclude(gm => gm.Guild) - .Where(c => c.Account.Id == authSession.AccountId && c.GuildMember.Guild != null) + .Where(c => c.Account.Id == session.AccountId && c.GuildMember.Guild != null) .Select(c => c.GuildMember.Guild.Name).Take(3).ToArrayAsync(cancellationToken: cancellationToken); } else @@ -71,11 +65,11 @@ public partial class ChannelSelectionHandler : IPacketHandler 0 && guildNameResponsePacket.GuildNames.Select(n => n != string.Empty).Any()) { - await authSession.SendAsync(guildNameResponsePacket); + await session.SendAsync(guildNameResponsePacket); } } diff --git a/Continuity.AuthServer/PacketHandlers/CharacterCreationHandler.cs b/Continuity.AuthServer/PacketHandlers/CharacterCreationHandler.cs index 8c821e4..082943c 100644 --- a/Continuity.AuthServer/PacketHandlers/CharacterCreationHandler.cs +++ b/Continuity.AuthServer/PacketHandlers/CharacterCreationHandler.cs @@ -4,7 +4,6 @@ using Continuity.AuthServer.DB; using Continuity.AuthServer.DB.Documents; using Continuity.AuthServer.Services; using Microsoft.EntityFrameworkCore; -using NetCoreServer; using Rai.PacketMediator; using Wonderking.Game.Data.Character; using Wonderking.Game.Mapping; @@ -14,7 +13,7 @@ using Wonderking.Packets.Outgoing.Data; namespace Continuity.AuthServer.PacketHandlers; -public class CharacterCreationHandler : IPacketHandler +public class CharacterCreationHandler : IPacketHandler { private readonly CharacterStatsMappingConfiguration _characterStatsMapping; private readonly ItemObjectPoolService _itemObjectPoolService; @@ -28,15 +27,11 @@ public class CharacterCreationHandler : IPacketHandler a.Id == authSession.AccountId, cancellationToken: cancellationToken); + var account = await _wonderkingContext.Accounts.FirstOrDefaultAsync(a => a.Id == session.AccountId, cancellationToken: cancellationToken); if (account is null) { @@ -70,7 +65,7 @@ public class CharacterCreationHandler : IPacketHandler new Tuple(item.ItemId, item.Slot)).AsEnumerable()) }; - await authSession.SendAsync(new CharacterCreationResponsePacket + await session.SendAsync(new CharacterCreationResponsePacket { Character = character, Slot = packet.Slot, diff --git a/Continuity.AuthServer/PacketHandlers/CharacterDeletionHandler.cs b/Continuity.AuthServer/PacketHandlers/CharacterDeletionHandler.cs index 04cb560..13bd4eb 100644 --- a/Continuity.AuthServer/PacketHandlers/CharacterDeletionHandler.cs +++ b/Continuity.AuthServer/PacketHandlers/CharacterDeletionHandler.cs @@ -2,14 +2,13 @@ using Continuity.AuthServer.DB; using Microsoft.EntityFrameworkCore; -using NetCoreServer; using Rai.PacketMediator; using Wonderking.Packets.Incoming; using Wonderking.Packets.Outgoing; namespace Continuity.AuthServer.PacketHandlers; -public class CharacterDeletionHandler : IPacketHandler +public class CharacterDeletionHandler : IPacketHandler { private readonly WonderkingContext _wonderkingContext; @@ -18,27 +17,21 @@ public class CharacterDeletionHandler : IPacketHandler x.Name == packet.Name && - x.Account.Id == authSession.AccountId, cancellationToken: cancellationToken); + x.Account.Id == session.AccountId, cancellationToken: cancellationToken); var response = new CharacterDeleteResponsePacket { HasToBeZero = 0 }; if (character == null) { - await authSession.SendAsync(response); + await session.SendAsync(response); return; } _wonderkingContext.Characters.Remove(character); await _wonderkingContext.SaveChangesAsync(cancellationToken); - await authSession.SendAsync(response); + await session.SendAsync(response); } } diff --git a/Continuity.AuthServer/PacketHandlers/CharacterNameCheckHandler.cs b/Continuity.AuthServer/PacketHandlers/CharacterNameCheckHandler.cs index 6897d4f..31f0607 100644 --- a/Continuity.AuthServer/PacketHandlers/CharacterNameCheckHandler.cs +++ b/Continuity.AuthServer/PacketHandlers/CharacterNameCheckHandler.cs @@ -2,14 +2,13 @@ using Continuity.AuthServer.DB; using Microsoft.EntityFrameworkCore; -using NetCoreServer; using Rai.PacketMediator; using Wonderking.Packets.Incoming; using Wonderking.Packets.Outgoing; namespace Continuity.AuthServer.PacketHandlers; -public class CharacterNameCheckHandler : IPacketHandler +public class CharacterNameCheckHandler : IPacketHandler { private readonly WonderkingContext _wonderkingContext; @@ -18,14 +17,14 @@ public class CharacterNameCheckHandler : IPacketHandler c.Name == packet.Name, cancellationToken: cancellationToken); + var isTaken = + await _wonderkingContext.Characters.AnyAsync(c => c.Name == packet.Name, + cancellationToken: cancellationToken); var responsePacket = new CharacterNameCheckPacketResponse { IsTaken = isTaken }; - if (session is AuthSession authSession) - { - await authSession.SendAsync(responsePacket); - } + + await session.SendAsync(responsePacket); } } diff --git a/Continuity.AuthServer/PacketHandlers/LoginHandler.cs b/Continuity.AuthServer/PacketHandlers/LoginHandler.cs index 6185441..a8da16d 100644 --- a/Continuity.AuthServer/PacketHandlers/LoginHandler.cs +++ b/Continuity.AuthServer/PacketHandlers/LoginHandler.cs @@ -10,7 +10,6 @@ using Konscious.Security.Cryptography; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; -using NetCoreServer; using Rai.PacketMediator; using Wonderking.Packets.Incoming; using Wonderking.Packets.Outgoing; @@ -18,12 +17,12 @@ using Wonderking.Packets.Outgoing.Data; namespace Continuity.AuthServer.PacketHandlers; -public class LoginHandler : IPacketHandler +public class LoginHandler : IPacketHandler { private readonly IConfiguration _configuration; private readonly ILogger _logger; private readonly WonderkingContext _wonderkingContext; - private static readonly ActivitySource _activitySource = new ActivitySource(nameof(Server)); + private static readonly ActivitySource _activitySource = new(nameof(Server)); public LoginHandler(ILogger logger, WonderkingContext wonderkingContext, IConfiguration configuration) { @@ -32,7 +31,7 @@ public class LoginHandler : IPacketHandler _configuration = configuration; } - public async Task HandleAsync(LoginInfoPacket packet, TcpSession session, CancellationToken cancellationToken) + public async Task HandleAsync(LoginInfoPacket packet, AuthSession session, CancellationToken cancellationToken) { LoginResponseReason loginResponseReason; _logger.LoginData(packet.Username, packet.Password); @@ -70,14 +69,13 @@ public class LoginHandler : IPacketHandler IsGameMaster = true }; - var authSession = session as AuthSession; - if (account != null && authSession != null) + if (account != null) { - authSession.AccountId = account.Id; + session.AccountId = account.Id; } _logger.LogInformation("LoginResponsePacket: {@LoginResponsePacket}", loginResponsePacket); - _ = authSession?.SendAsync(loginResponsePacket); + _ = session?.SendAsync(loginResponsePacket); } private static async Task GetPasswordHashAsync(string password, byte[] salt, Guid userId) diff --git a/Continuity.AuthServer/Program.cs b/Continuity.AuthServer/Program.cs index 0ab1f80..7d8e734 100644 --- a/Continuity.AuthServer/Program.cs +++ b/Continuity.AuthServer/Program.cs @@ -3,7 +3,9 @@ using System.Net; using System.Reflection; using System.Text.Json; +using Continuity.AuthServer; using Continuity.AuthServer.DB; +using Continuity.AuthServer.PacketHandlers; using Continuity.AuthServer.Services; using MassTransit; using Microsoft.EntityFrameworkCore; @@ -12,7 +14,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -using NetCoreServer; using Npgsql; using OpenTelemetry.Logs; using OpenTelemetry.Metrics; @@ -106,24 +107,35 @@ builder.Services.AddSingleton( File.ReadAllText("config/character-stats.mapping.json")) ?? throw new InvalidOperationException()); builder.Services.AddSingleton(loggerFactory); -builder.Services.AddSingleton>(); +builder.Services.AddSingleton(provider => + new PacketDistributorService( + provider.GetRequiredService(), + [ + Assembly.GetAssembly(typeof(LoginHandler)), + Assembly.GetAssembly(typeof(OperationCode)), + ] + )); builder.Services.AddSingleton(); + +builder.Services.AddHostedService(provider => new WonderkingAuthServer(IPAddress.Any, 10001, + provider.GetService>() ?? throw new InvalidOperationException(), + provider.GetService() ?? throw new InvalidOperationException())); + builder.Services.AddHostedService(provider => provider.GetService() ?? throw new InvalidOperationException()); + builder.Services.AddHostedService(provider => - provider.GetService>() ?? - throw new InvalidOperationException()); + provider.GetService>() ?? throw new InvalidOperationException()); + builder.Services.AddMassTransit(x => { x.UsingInMemory((context, configurator) => configurator.ConfigureEndpoints(context)); x.AddMediator(cfg => cfg.AddConsumers(Assembly.GetExecutingAssembly())); }); -builder.Services.AddHostedService(provider => new WonderkingAuthServer(IPAddress.Any, 10001, - provider.GetService>() ?? throw new InvalidOperationException(), - provider.GetService() ?? throw new InvalidOperationException())); using var host = builder.Build(); -using (var scope = host.Services.CreateScope()) + +await using (var scope = host.Services.CreateAsyncScope()) { var db = scope.ServiceProvider.GetRequiredService(); await db.Database.MigrateAsync(); diff --git a/Rai.PacketMediator/PacketDistributor.cs b/Rai.PacketMediator/PacketDistributor.cs index 5ed9c63..3c86da6 100644 --- a/Rai.PacketMediator/PacketDistributor.cs +++ b/Rai.PacketMediator/PacketDistributor.cs @@ -4,6 +4,7 @@ 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; @@ -48,14 +49,14 @@ public class PacketDistributor where TPacketIdEnum : En var tempDeserializationMap = new ConcurrentDictionary>(); _packetHandlersInstantiation = new ConcurrentDictionary?>(); - Parallel.ForEach(packetHandlers, packetHandlerPair => + packetHandlers.ForEach(packetHandlerPair => { var packetHandler = ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider, packetHandlerPair.Value); _packetHandlersInstantiation.TryAdd(packetHandlerPair.Key, packetHandler as IPacketHandler); }); - Parallel.ForEach(packetDictionary, packetsType => + packetDictionary.ForEach(packetsType => { var lambda = CodeGenerator.Lambda>(fun => { @@ -94,19 +95,20 @@ public class PacketDistributor where TPacketIdEnum : En .Where(t => t is { IsClass: true, IsAbstract: false } && Array.Exists(t .GetInterfaces(), i => - i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler))) - .Select(type => new + i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler<,>))) + .Select(packetHandlerType => new { - Type = type, - PacketId = type + Type = packetHandlerType, + PacketId = packetHandlerType .GetInterfaces().First(t1 => t1 is { IsGenericType: true } && - t1.GetGenericTypeDefinition() == typeof(IPacketHandler)) - .GetGenericArguments().First(t => - t.GetCustomAttributes>().Any()) - .GetCustomAttributes>().First().Code + t1.GetGenericTypeDefinition() == typeof(IPacketHandler<,>)).GetGenericArguments() + .First(genericType => genericType.GetInterfaces().Any(packetType => + packetType == typeof(IPacket))) + .GetCustomAttribute>() })) - .ToDictionary(x => x.PacketId, x => x.Type); + .Where(x => x.PacketId != null) + .ToDictionary(x => x.PacketId!.Code, x => x.Type); return packetHandlersWithId; } diff --git a/docker-compose.yml b/docker-compose.yml index 7a5b546..0e3de18 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -31,7 +31,7 @@ services: source: config target: /app/config read_only: true - mem_limit: 100m + mem_limit: 1024m db: container_name: continuity-db @@ -52,6 +52,7 @@ services: interval: 10s timeout: 3s retries: 3 + mem_limit: 1024m jaeger: container_name: continuity-jaeger