From 12aeff8ad67653e202410f847f9d71fc31872808 Mon Sep 17 00:00:00 2001 From: Timothy Schenk Date: Thu, 10 Aug 2023 10:47:35 +0200 Subject: [PATCH] feat: add couchdb & refactor namespace --- Server/AuthSession.cs | 11 +++-- Server/Consumers/PacketConsumer.cs | 4 +- Server/DB/Documents/Account.cs | 19 ++++++++ Server/DB/WonderkingContext.cs | 14 ++++++ Server/PacketHandlers/IPacketHandler.cs | 4 +- Server/PacketHandlers/LoginInfoHandler.cs | 24 ++++++++-- Server/Packets/IPacket.cs | 2 +- Server/Packets/LoginInfoPacket.cs | 8 ++-- Server/Packets/OperationCode.cs | 2 +- Server/Packets/RawPacket.cs | 2 +- .../FieldOffsetAttribute.cs | 2 +- .../SerializationUtilities/PacketId.cs | 2 +- .../TextEncodingAttribute.cs | 2 +- Server/Program.cs | 20 +++++--- Server/Server.csproj | 47 ++++++++++--------- Server/Services/PacketDistributorService.cs | 7 ++- Server/Services/WonderkingAuthServer.cs | 8 ++-- 17 files changed, 121 insertions(+), 57 deletions(-) create mode 100644 Server/DB/Documents/Account.cs create mode 100644 Server/DB/WonderkingContext.cs diff --git a/Server/AuthSession.cs b/Server/AuthSession.cs index 0d732d7..d6c88e2 100644 --- a/Server/AuthSession.cs +++ b/Server/AuthSession.cs @@ -2,19 +2,20 @@ using MassTransit.Mediator; using Microsoft.Extensions.Logging; using NetCoreServer; +using Server.Packets; namespace Server; -public abstract class AuthSession : TcpSession +public class AuthSession : TcpSession { - private readonly ILogger _logger; private readonly IMediator _mediator; + private readonly ILogger _logger; - protected AuthSession(TcpServer - server, ILogger logger, IMediator mediator) : base(server) + public AuthSession(TcpServer + server, IMediator mediator, ILogger logger) : base(server) { - _logger = logger; _mediator = mediator; + _logger = logger; } protected override void OnReceived(byte[] buffer, long offset, long size) diff --git a/Server/Consumers/PacketConsumer.cs b/Server/Consumers/PacketConsumer.cs index c9282d6..0e67305 100644 --- a/Server/Consumers/PacketConsumer.cs +++ b/Server/Consumers/PacketConsumer.cs @@ -1,6 +1,8 @@ using MassTransit; +using Server.Packets; +using Server.Services; -namespace Server; +namespace Server.Consumers; public class PacketConsumer : IConsumer { diff --git a/Server/DB/Documents/Account.cs b/Server/DB/Documents/Account.cs new file mode 100644 index 0000000..ef4a464 --- /dev/null +++ b/Server/DB/Documents/Account.cs @@ -0,0 +1,19 @@ +using CouchDB.Driver.Types; + +namespace Server.DB.Documents; + +public class Account : CouchDocument +{ + public Account(string username, string password, string email, byte permissionLevel) + { + Username = username; + Password = password; + Email = email; + PermissionLevel = permissionLevel; + } + + public string Username { get; private set; } + public string Password { get; private set; } + public string Email { get; private set; } + public byte PermissionLevel { get; private set; } +} \ No newline at end of file diff --git a/Server/DB/WonderkingContext.cs b/Server/DB/WonderkingContext.cs new file mode 100644 index 0000000..7274cda --- /dev/null +++ b/Server/DB/WonderkingContext.cs @@ -0,0 +1,14 @@ +using CouchDB.Driver; +using CouchDB.Driver.Options; +using Server.DB.Documents; + +namespace Server.DB; + +public class WonderkingContext : CouchContext +{ + public CouchDatabase Accounts { get; set; } + + public WonderkingContext(CouchOptions options) : base(options) + { + } +} \ No newline at end of file diff --git a/Server/PacketHandlers/IPacketHandler.cs b/Server/PacketHandlers/IPacketHandler.cs index ffa44b2..2de9c93 100644 --- a/Server/PacketHandlers/IPacketHandler.cs +++ b/Server/PacketHandlers/IPacketHandler.cs @@ -1,4 +1,6 @@ -namespace Server; +using Server.Packets; + +namespace Server.PacketHandlers; public interface IPacketHandler where T: IPacket { diff --git a/Server/PacketHandlers/LoginInfoHandler.cs b/Server/PacketHandlers/LoginInfoHandler.cs index 014786f..4454fb3 100644 --- a/Server/PacketHandlers/LoginInfoHandler.cs +++ b/Server/PacketHandlers/LoginInfoHandler.cs @@ -1,18 +1,34 @@ -using Microsoft.Extensions.Logging; +using CouchDB.Driver.Query.Extensions; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Server.DB; +using Server.DB.Documents; +using Server.Packets; -namespace Server; +namespace Server.PacketHandlers; public class LoginHandler : IPacketHandler { private readonly ILogger _logger; + private readonly WonderkingContext _wonderkingContext; - public LoginHandler(ILogger logger) + public LoginHandler(ILogger logger, WonderkingContext wonderkingContext) { _logger = logger; + _wonderkingContext = wonderkingContext; } public void Handle(LoginInfoPacket packet) { - _logger.LogInformation("Login data: Username {Username} & Password {Password}", packet.Username, packet.Password); + _logger.LogInformation("Login data: Username {Username} & Password {Password}", packet.Username, + packet.Password); + var account = _wonderkingContext.Accounts.FirstOrDefault(a => a.Username == packet.Username); + if (account == null) + { + _wonderkingContext.Accounts.AddAsync(new Account(packet.Username, packet.Password, "", 0)); + } + var x = _wonderkingContext.Accounts.ToArray(); + Parallel.ForEach(x.AsParallel(), + (x, i) => { _logger.LogInformation("Account {Data}", JsonConvert.SerializeObject(x)); }); } } \ No newline at end of file diff --git a/Server/Packets/IPacket.cs b/Server/Packets/IPacket.cs index 8ac9909..3c9d2c0 100644 --- a/Server/Packets/IPacket.cs +++ b/Server/Packets/IPacket.cs @@ -1,4 +1,4 @@ -namespace Server; +namespace Server.Packets; public interface IPacket { diff --git a/Server/Packets/LoginInfoPacket.cs b/Server/Packets/LoginInfoPacket.cs index 3fb9c4e..73aceff 100644 --- a/Server/Packets/LoginInfoPacket.cs +++ b/Server/Packets/LoginInfoPacket.cs @@ -1,11 +1,11 @@ -using System.Runtime.InteropServices; +using Server.Packets.SerializationUtilities; -namespace Server; +namespace Server.Packets; [PacketId(OperationCode.LoginInfo)] public class LoginInfoPacket : IPacket { - [FieldOffset(0, 20)] public string Username; + [SerializationUtilities.FieldOffset(0, 20)] public string Username; - [FieldOffset(20, 31)] public string Password; + [SerializationUtilities.FieldOffset(20, 31)] public string Password; } \ No newline at end of file diff --git a/Server/Packets/OperationCode.cs b/Server/Packets/OperationCode.cs index e4c2ae4..a08669c 100644 --- a/Server/Packets/OperationCode.cs +++ b/Server/Packets/OperationCode.cs @@ -1,4 +1,4 @@ -namespace Server; +namespace Server.Packets; public enum OperationCode : ushort { diff --git a/Server/Packets/RawPacket.cs b/Server/Packets/RawPacket.cs index 7273dbd..e235615 100644 --- a/Server/Packets/RawPacket.cs +++ b/Server/Packets/RawPacket.cs @@ -1,6 +1,6 @@ using MassTransit; -namespace Server; +namespace Server.Packets; [MessageUrn("packets")] public class RawPacket diff --git a/Server/Packets/SerializationUtilities/FieldOffsetAttribute.cs b/Server/Packets/SerializationUtilities/FieldOffsetAttribute.cs index cfad9d7..eb39015 100644 --- a/Server/Packets/SerializationUtilities/FieldOffsetAttribute.cs +++ b/Server/Packets/SerializationUtilities/FieldOffsetAttribute.cs @@ -1,4 +1,4 @@ -namespace Server; +namespace Server.Packets.SerializationUtilities; public class FieldOffsetAttribute : Attribute { diff --git a/Server/Packets/SerializationUtilities/PacketId.cs b/Server/Packets/SerializationUtilities/PacketId.cs index 36f7538..0d266c3 100644 --- a/Server/Packets/SerializationUtilities/PacketId.cs +++ b/Server/Packets/SerializationUtilities/PacketId.cs @@ -1,4 +1,4 @@ -namespace Server; +namespace Server.Packets.SerializationUtilities; public class PacketId : Attribute diff --git a/Server/Packets/SerializationUtilities/TextEncodingAttribute.cs b/Server/Packets/SerializationUtilities/TextEncodingAttribute.cs index e606b20..b2953e5 100644 --- a/Server/Packets/SerializationUtilities/TextEncodingAttribute.cs +++ b/Server/Packets/SerializationUtilities/TextEncodingAttribute.cs @@ -1,6 +1,6 @@ using System.Text; -namespace Server; +namespace Server.Packets.SerializationUtilities; public class TextEncodingAttribute : Attribute { diff --git a/Server/Program.cs b/Server/Program.cs index c529e07..02a8d73 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -1,19 +1,28 @@ using System.Net; using System.Reflection; +using CouchDB.Driver.DependencyInjection; using MassTransit; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Server; - -Console.WriteLine(BitConverter.IsLittleEndian); +using Server.DB; +using Server.Services; var builder = Host.CreateApplicationBuilder(); -builder.Configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); +var configurationRoot = builder.Configuration.AddJsonFile("settings.json", optional: false, reloadOnChange: true) + .AddEnvironmentVariables().Build(); +builder.Services.AddLogging(); builder.Logging.AddFile("Logs/Server-{Date}.log", LogLevel.Trace); builder.Logging.AddFile("Logs/Server-{Date}.json.log", isJson: true, minimumLevel: LogLevel.Trace); -builder.Services.AddLogging(); +builder.Services.AddCouchContext(cfg => +{ + cfg.UseEndpoint(configurationRoot["DB:Endpoint"] ?? throw new InvalidOperationException()) + .UseBasicAuthentication(configurationRoot["DB:User"] ?? throw new InvalidOperationException(), + configurationRoot["DB:Password"] ?? throw new InvalidOperationException()) + .EnsureDatabaseExists(); +}); builder.Services.AddSingleton(); builder.Services.AddHostedService(provider => provider.GetService() ?? throw new InvalidOperationException()); @@ -24,8 +33,7 @@ builder.Services.AddMassTransit(x => }); builder.Services.AddHostedService(provider => new WonderkingAuthServer(IPAddress.Any, 10001, provider.GetService>() ?? throw new InvalidOperationException(), - provider.GetService() ?? throw new InvalidOperationException(), - provider.GetService() ?? throw new InvalidOperationException())); + provider.GetService() ?? throw new InvalidOperationException())); using IHost host = builder.Build(); await host.RunAsync(); \ No newline at end of file diff --git a/Server/Server.csproj b/Server/Server.csproj index b5f6fc1..331967b 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -11,32 +11,33 @@ - - .dockerignore - + + .dockerignore + - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - - - - - + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + - - - - + + + diff --git a/Server/Services/PacketDistributorService.cs b/Server/Services/PacketDistributorService.cs index 5b586d3..66e0264 100644 --- a/Server/Services/PacketDistributorService.cs +++ b/Server/Services/PacketDistributorService.cs @@ -7,9 +7,12 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.VisualBasic.CompilerServices; using Newtonsoft.Json; +using Server.PacketHandlers; +using Server.Packets; +using Server.Packets.SerializationUtilities; using FieldAccessException = System.FieldAccessException; -namespace Server; +namespace Server.Services; public class PacketDistributorService : IHostedService { @@ -104,7 +107,7 @@ public class PacketDistributorService : IHostedService value = typeOfField.Name switch { nameof(String) => Encoding.ASCII.GetString(item.MessageBody, fieldOffsetAttribute.Offset, - fieldOffsetAttribute.Size), + fieldOffsetAttribute.Size).TrimEnd('\0'), "Byte[]" => new ArraySegment(item.MessageBody, fieldOffsetAttribute.Offset, fieldOffsetAttribute.Size).ToArray(), nameof(Boolean) => BitConverter.ToBoolean(item.MessageBody, fieldOffsetAttribute.Offset), diff --git a/Server/Services/WonderkingAuthServer.cs b/Server/Services/WonderkingAuthServer.cs index 0aa46dd..882b14b 100644 --- a/Server/Services/WonderkingAuthServer.cs +++ b/Server/Services/WonderkingAuthServer.cs @@ -5,24 +5,22 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using NetCoreServer; -namespace Server; +namespace Server.Services; public class WonderkingAuthServer : TcpServer, IHostedService { private readonly ILogger _logger; private readonly IServiceProvider _serviceProvider; - private readonly ILoggerFactory _loggerFactory; public WonderkingAuthServer(IPAddress address, int port, ILogger logger, - IServiceProvider serviceProvider, ILoggerFactory loggerFactory) : base(address, port) + IServiceProvider serviceProvider) : base(address, port) { _logger = logger; _serviceProvider = serviceProvider; - _loggerFactory = loggerFactory; } protected override TcpSession CreateSession() => - ActivatorUtilities.CreateInstance(_serviceProvider, this); + ActivatorUtilities.CreateInstance(_serviceProvider,this); protected override void OnStarting() {