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 Microsoft.Extensions.Logging;
|
||||
using NetCoreServer;
|
||||
using Server.Packets;
|
||||
|
||||
namespace Server;
|
||||
|
||||
public abstract class AuthSession : TcpSession
|
||||
public class AuthSession : TcpSession
|
||||
{
|
||||
private readonly ILogger<AuthSession> _logger;
|
||||
private readonly IMediator _mediator;
|
||||
private readonly ILogger<AuthSession> _logger;
|
||||
|
||||
protected AuthSession(TcpServer
|
||||
server, ILogger<AuthSession> logger, IMediator mediator) : base(server)
|
||||
public AuthSession(TcpServer
|
||||
server, IMediator mediator, ILogger<AuthSession> logger) : base(server)
|
||||
{
|
||||
_logger = logger;
|
||||
_mediator = mediator;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
protected override void OnReceived(byte[] buffer, long offset, long size)
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
using MassTransit;
|
||||
using Server.Packets;
|
||||
using Server.Services;
|
||||
|
||||
namespace Server;
|
||||
namespace Server.Consumers;
|
||||
|
||||
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
|
||||
{
|
||||
|
|
|
@ -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>
|
||||
{
|
||||
private readonly ILogger<LoginHandler> _logger;
|
||||
private readonly WonderkingContext _wonderkingContext;
|
||||
|
||||
public LoginHandler(ILogger<LoginHandler> logger)
|
||||
public LoginHandler(ILogger<LoginHandler> 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)); });
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
namespace Server;
|
||||
namespace Server.Packets;
|
||||
|
||||
public interface IPacket
|
||||
{
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
namespace Server;
|
||||
namespace Server.Packets;
|
||||
|
||||
public enum OperationCode : ushort
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using MassTransit;
|
||||
|
||||
namespace Server;
|
||||
namespace Server.Packets;
|
||||
|
||||
[MessageUrn("packets")]
|
||||
public class RawPacket
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
namespace Server;
|
||||
namespace Server.Packets.SerializationUtilities;
|
||||
|
||||
public class FieldOffsetAttribute : Attribute
|
||||
{
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
namespace Server;
|
||||
namespace Server.Packets.SerializationUtilities;
|
||||
|
||||
|
||||
public class PacketId : Attribute
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using System.Text;
|
||||
|
||||
namespace Server;
|
||||
namespace Server.Packets.SerializationUtilities;
|
||||
|
||||
public class TextEncodingAttribute : Attribute
|
||||
{
|
||||
|
|
|
@ -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<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.AddHostedService<PacketDistributorService>(provider =>
|
||||
provider.GetService<PacketDistributorService>() ?? throw new InvalidOperationException());
|
||||
|
@ -24,8 +33,7 @@ builder.Services.AddMassTransit(x =>
|
|||
});
|
||||
builder.Services.AddHostedService<WonderkingAuthServer>(provider => new WonderkingAuthServer(IPAddress.Any, 10001,
|
||||
provider.GetService<ILogger<WonderkingAuthServer>>() ?? throw new InvalidOperationException(),
|
||||
provider.GetService<IServiceProvider>() ?? throw new InvalidOperationException(),
|
||||
provider.GetService<ILoggerFactory>() ?? throw new InvalidOperationException()));
|
||||
provider.GetService<IServiceProvider>() ?? throw new InvalidOperationException()));
|
||||
|
||||
using IHost host = builder.Build();
|
||||
await host.RunAsync();
|
|
@ -11,32 +11,33 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="..\.dockerignore">
|
||||
<Link>.dockerignore</Link>
|
||||
</Content>
|
||||
<Content Include="..\.dockerignore">
|
||||
<Link>.dockerignore</Link>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="JetBrains.Annotations" Version="2023.2.0" />
|
||||
<PackageReference Include="MassTransit" Version="8.0.16" />
|
||||
<PackageReference Include="MassTransit.Analyzers" Version="8.0.16">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
|
||||
<PackageReference Include="NetCoreServer" Version="7.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0" />
|
||||
<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="MassTransit" Version="8.0.16"/>
|
||||
<PackageReference Include="MassTransit.Analyzers" Version="8.0.16">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0"/>
|
||||
<PackageReference Include="NetCoreServer" Version="7.0.0"/>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
|
||||
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0"/>
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="settings.json" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
|
@ -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<byte>(item.MessageBody, fieldOffsetAttribute.Offset,
|
||||
fieldOffsetAttribute.Size).ToArray(),
|
||||
nameof(Boolean) => BitConverter.ToBoolean(item.MessageBody, fieldOffsetAttribute.Offset),
|
||||
|
|
|
@ -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<WonderkingAuthServer> _logger;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly ILoggerFactory _loggerFactory;
|
||||
|
||||
public WonderkingAuthServer(IPAddress address, int port, ILogger<WonderkingAuthServer> 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<AuthSession>(_serviceProvider, this);
|
||||
ActivatorUtilities.CreateInstance<AuthSession>(_serviceProvider,this);
|
||||
|
||||
protected override void OnStarting()
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue