feat: add couchdb & refactor namespace
This commit is contained in:
parent
8fbca94140
commit
12aeff8ad6
17 changed files with 121 additions and 57 deletions
|
@ -2,19 +2,20 @@
|
||||||
using MassTransit.Mediator;
|
using MassTransit.Mediator;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using NetCoreServer;
|
using NetCoreServer;
|
||||||
|
using Server.Packets;
|
||||||
|
|
||||||
namespace Server;
|
namespace Server;
|
||||||
|
|
||||||
public abstract class AuthSession : TcpSession
|
public class AuthSession : TcpSession
|
||||||
{
|
{
|
||||||
private readonly ILogger<AuthSession> _logger;
|
|
||||||
private readonly IMediator _mediator;
|
private readonly IMediator _mediator;
|
||||||
|
private readonly ILogger<AuthSession> _logger;
|
||||||
|
|
||||||
protected AuthSession(TcpServer
|
public AuthSession(TcpServer
|
||||||
server, ILogger<AuthSession> logger, IMediator mediator) : base(server)
|
server, IMediator mediator, ILogger<AuthSession> logger) : base(server)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
|
||||||
_mediator = mediator;
|
_mediator = mediator;
|
||||||
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnReceived(byte[] buffer, long offset, long size)
|
protected override void OnReceived(byte[] buffer, long offset, long size)
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
using MassTransit;
|
using MassTransit;
|
||||||
|
using Server.Packets;
|
||||||
|
using Server.Services;
|
||||||
|
|
||||||
namespace Server;
|
namespace Server.Consumers;
|
||||||
|
|
||||||
public class PacketConsumer : IConsumer<RawPacket>
|
public class PacketConsumer : IConsumer<RawPacket>
|
||||||
{
|
{
|
||||||
|
|
19
Server/DB/Documents/Account.cs
Normal file
19
Server/DB/Documents/Account.cs
Normal file
|
@ -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; }
|
||||||
|
}
|
14
Server/DB/WonderkingContext.cs
Normal file
14
Server/DB/WonderkingContext.cs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
using CouchDB.Driver;
|
||||||
|
using CouchDB.Driver.Options;
|
||||||
|
using Server.DB.Documents;
|
||||||
|
|
||||||
|
namespace Server.DB;
|
||||||
|
|
||||||
|
public class WonderkingContext : CouchContext
|
||||||
|
{
|
||||||
|
public CouchDatabase<Account> Accounts { get; set; }
|
||||||
|
|
||||||
|
public WonderkingContext(CouchOptions<WonderkingContext> options) : base(options)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,6 @@
|
||||||
namespace Server;
|
using Server.Packets;
|
||||||
|
|
||||||
|
namespace Server.PacketHandlers;
|
||||||
|
|
||||||
public interface IPacketHandler<in T> where T: IPacket
|
public interface IPacketHandler<in T> where T: IPacket
|
||||||
{
|
{
|
||||||
|
|
|
@ -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<LoginInfoPacket>
|
public class LoginHandler : IPacketHandler<LoginInfoPacket>
|
||||||
{
|
{
|
||||||
private readonly ILogger<LoginHandler> _logger;
|
private readonly ILogger<LoginHandler> _logger;
|
||||||
|
private readonly WonderkingContext _wonderkingContext;
|
||||||
|
|
||||||
public LoginHandler(ILogger<LoginHandler> logger)
|
public LoginHandler(ILogger<LoginHandler> logger, WonderkingContext wonderkingContext)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
_wonderkingContext = wonderkingContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(LoginInfoPacket packet)
|
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)); });
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
namespace Server;
|
namespace Server.Packets;
|
||||||
|
|
||||||
public interface IPacket
|
public interface IPacket
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
using System.Runtime.InteropServices;
|
using Server.Packets.SerializationUtilities;
|
||||||
|
|
||||||
namespace Server;
|
namespace Server.Packets;
|
||||||
|
|
||||||
[PacketId(OperationCode.LoginInfo)]
|
[PacketId(OperationCode.LoginInfo)]
|
||||||
public class LoginInfoPacket : IPacket
|
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;
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
namespace Server;
|
namespace Server.Packets;
|
||||||
|
|
||||||
public enum OperationCode : ushort
|
public enum OperationCode : ushort
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
using MassTransit;
|
using MassTransit;
|
||||||
|
|
||||||
namespace Server;
|
namespace Server.Packets;
|
||||||
|
|
||||||
[MessageUrn("packets")]
|
[MessageUrn("packets")]
|
||||||
public class RawPacket
|
public class RawPacket
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
namespace Server;
|
namespace Server.Packets.SerializationUtilities;
|
||||||
|
|
||||||
public class FieldOffsetAttribute : Attribute
|
public class FieldOffsetAttribute : Attribute
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
namespace Server;
|
namespace Server.Packets.SerializationUtilities;
|
||||||
|
|
||||||
|
|
||||||
public class PacketId : Attribute
|
public class PacketId : Attribute
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace Server;
|
namespace Server.Packets.SerializationUtilities;
|
||||||
|
|
||||||
public class TextEncodingAttribute : Attribute
|
public class TextEncodingAttribute : Attribute
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,19 +1,28 @@
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using CouchDB.Driver.DependencyInjection;
|
||||||
using MassTransit;
|
using MassTransit;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Server;
|
using Server;
|
||||||
|
using Server.DB;
|
||||||
Console.WriteLine(BitConverter.IsLittleEndian);
|
using Server.Services;
|
||||||
|
|
||||||
var builder = Host.CreateApplicationBuilder();
|
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}.log", LogLevel.Trace);
|
||||||
builder.Logging.AddFile("Logs/Server-{Date}.json.log", isJson: true, minimumLevel: LogLevel.Trace);
|
builder.Logging.AddFile("Logs/Server-{Date}.json.log", isJson: true, minimumLevel: LogLevel.Trace);
|
||||||
builder.Services.AddLogging();
|
builder.Services.AddCouchContext<WonderkingContext>(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<PacketDistributorService>();
|
builder.Services.AddSingleton<PacketDistributorService>();
|
||||||
builder.Services.AddHostedService<PacketDistributorService>(provider =>
|
builder.Services.AddHostedService<PacketDistributorService>(provider =>
|
||||||
provider.GetService<PacketDistributorService>() ?? throw new InvalidOperationException());
|
provider.GetService<PacketDistributorService>() ?? throw new InvalidOperationException());
|
||||||
|
@ -24,8 +33,7 @@ builder.Services.AddMassTransit(x =>
|
||||||
});
|
});
|
||||||
builder.Services.AddHostedService<WonderkingAuthServer>(provider => new WonderkingAuthServer(IPAddress.Any, 10001,
|
builder.Services.AddHostedService<WonderkingAuthServer>(provider => new WonderkingAuthServer(IPAddress.Any, 10001,
|
||||||
provider.GetService<ILogger<WonderkingAuthServer>>() ?? throw new InvalidOperationException(),
|
provider.GetService<ILogger<WonderkingAuthServer>>() ?? throw new InvalidOperationException(),
|
||||||
provider.GetService<IServiceProvider>() ?? throw new InvalidOperationException(),
|
provider.GetService<IServiceProvider>() ?? throw new InvalidOperationException()));
|
||||||
provider.GetService<ILoggerFactory>() ?? throw new InvalidOperationException()));
|
|
||||||
|
|
||||||
using IHost host = builder.Build();
|
using IHost host = builder.Build();
|
||||||
await host.RunAsync();
|
await host.RunAsync();
|
|
@ -17,6 +17,8 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="CouchDB.NET" Version="3.4.0"/>
|
||||||
|
<PackageReference Include="CouchDB.NET.DependencyInjection" Version="3.4.0"/>
|
||||||
<PackageReference Include="JetBrains.Annotations" Version="2023.2.0"/>
|
<PackageReference Include="JetBrains.Annotations" Version="2023.2.0"/>
|
||||||
<PackageReference Include="MassTransit" Version="8.0.16"/>
|
<PackageReference Include="MassTransit" Version="8.0.16"/>
|
||||||
<PackageReference Include="MassTransit.Analyzers" Version="8.0.16">
|
<PackageReference Include="MassTransit.Analyzers" Version="8.0.16">
|
||||||
|
@ -35,8 +37,7 @@
|
||||||
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0"/>
|
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Remove="settings.json" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -7,9 +7,12 @@ using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.VisualBasic.CompilerServices;
|
using Microsoft.VisualBasic.CompilerServices;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using Server.PacketHandlers;
|
||||||
|
using Server.Packets;
|
||||||
|
using Server.Packets.SerializationUtilities;
|
||||||
using FieldAccessException = System.FieldAccessException;
|
using FieldAccessException = System.FieldAccessException;
|
||||||
|
|
||||||
namespace Server;
|
namespace Server.Services;
|
||||||
|
|
||||||
public class PacketDistributorService : IHostedService
|
public class PacketDistributorService : IHostedService
|
||||||
{
|
{
|
||||||
|
@ -104,7 +107,7 @@ public class PacketDistributorService : IHostedService
|
||||||
value = typeOfField.Name switch
|
value = typeOfField.Name switch
|
||||||
{
|
{
|
||||||
nameof(String) => Encoding.ASCII.GetString(item.MessageBody, fieldOffsetAttribute.Offset,
|
nameof(String) => Encoding.ASCII.GetString(item.MessageBody, fieldOffsetAttribute.Offset,
|
||||||
fieldOffsetAttribute.Size),
|
fieldOffsetAttribute.Size).TrimEnd('\0'),
|
||||||
"Byte[]" => new ArraySegment<byte>(item.MessageBody, fieldOffsetAttribute.Offset,
|
"Byte[]" => new ArraySegment<byte>(item.MessageBody, fieldOffsetAttribute.Offset,
|
||||||
fieldOffsetAttribute.Size).ToArray(),
|
fieldOffsetAttribute.Size).ToArray(),
|
||||||
nameof(Boolean) => BitConverter.ToBoolean(item.MessageBody, fieldOffsetAttribute.Offset),
|
nameof(Boolean) => BitConverter.ToBoolean(item.MessageBody, fieldOffsetAttribute.Offset),
|
||||||
|
|
|
@ -5,20 +5,18 @@ using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using NetCoreServer;
|
using NetCoreServer;
|
||||||
|
|
||||||
namespace Server;
|
namespace Server.Services;
|
||||||
|
|
||||||
public class WonderkingAuthServer : TcpServer, IHostedService
|
public class WonderkingAuthServer : TcpServer, IHostedService
|
||||||
{
|
{
|
||||||
private readonly ILogger<WonderkingAuthServer> _logger;
|
private readonly ILogger<WonderkingAuthServer> _logger;
|
||||||
private readonly IServiceProvider _serviceProvider;
|
private readonly IServiceProvider _serviceProvider;
|
||||||
private readonly ILoggerFactory _loggerFactory;
|
|
||||||
|
|
||||||
public WonderkingAuthServer(IPAddress address, int port, ILogger<WonderkingAuthServer> logger,
|
public WonderkingAuthServer(IPAddress address, int port, ILogger<WonderkingAuthServer> logger,
|
||||||
IServiceProvider serviceProvider, ILoggerFactory loggerFactory) : base(address, port)
|
IServiceProvider serviceProvider) : base(address, port)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_serviceProvider = serviceProvider;
|
_serviceProvider = serviceProvider;
|
||||||
_loggerFactory = loggerFactory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override TcpSession CreateSession() =>
|
protected override TcpSession CreateSession() =>
|
||||||
|
|
Loading…
Reference in a new issue