refactor: move classes to corresponding namespaces

This commit is contained in:
Timothy Schenk 2023-08-09 22:04:10 +02:00
parent 2efbbe792c
commit 57fe6f9d8d
16 changed files with 43 additions and 42 deletions

View file

@ -1,19 +1,12 @@
using System.Buffers.Binary; using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Text.Json.Serialization;
using MassTransit.Mediator; using MassTransit.Mediator;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NetCoreServer; using NetCoreServer;
using Buffer = NetCoreServer.Buffer;
namespace Server; namespace Server;
public abstract class AesSession : TcpSession public abstract class AuthSession : TcpSession
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IMediator _mediator; private readonly IMediator _mediator;
@ -27,7 +20,7 @@ public abstract class AesSession : TcpSession
private readonly ICryptoTransform _encryptor; private readonly ICryptoTransform _encryptor;
private readonly ICryptoTransform _decryptor; private readonly ICryptoTransform _decryptor;
protected AesSession(TcpServer protected AuthSession(TcpServer
server, ILogger logger, IMediator mediator) : base(server) server, ILogger logger, IMediator mediator) : base(server)
{ {
_logger = logger; _logger = logger;

View file

@ -3,16 +3,16 @@ WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src WORKDIR /src
COPY ["Serer/Serer.csproj", "Serer/"] COPY ["Server/Server.csproj", "Server/"]
RUN dotnet restore "Serer/Serer.csproj" RUN dotnet restore "Server/Server.csproj"
COPY . . COPY . .
WORKDIR "/src/Serer" WORKDIR "/src/Serer"
RUN dotnet build "Serer.csproj" -c Release -o /app/build RUN dotnet build "Server.csproj" -c Release -o /app/build
FROM build AS publish FROM build AS publish
RUN dotnet publish "Serer.csproj" -c Release -o /app/publish /p:UseAppHost=false RUN dotnet publish "Server.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final FROM base AS final
WORKDIR /app WORKDIR /app
COPY --from=publish /app/publish . COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Serer.dll"] ENTRYPOINT ["dotnet", "Server.dll"]

View file

@ -1,6 +1,6 @@
namespace Server; namespace Server;
public interface IPacketHandler<T> where T: IPacket public interface IPacketHandler<in T> where T: IPacket
{ {
public void Handle(T packet); public void Handle(T packet);
} }

View file

@ -6,16 +6,16 @@ namespace Server;
public class RawPacket public class RawPacket
{ {
public readonly OperationCode OperationCode; public readonly OperationCode OperationCode;
public readonly byte[] MessageBody; public byte[] MessageBody;
public readonly TimeSpan ClientAliveTime; public readonly TimeSpan ClientAliveTime;
public readonly byte UnknownValue; public readonly byte UnknownValue;
public readonly byte UnknownValue2; public readonly byte UnknownValue2;
public readonly Guid SessionId; public readonly Guid SessionId;
public readonly AesSession Session; public readonly AuthSession Session;
public RawPacket(OperationCode operationCode, byte[] messageBody, uint aliveTime, byte unknownValue2, public RawPacket(OperationCode operationCode, byte[] messageBody, uint aliveTime, byte unknownValue2,
byte unknownValue, Guid sessionId, AesSession session) byte unknownValue, Guid sessionId, AuthSession session)
{ {
MessageBody = messageBody; MessageBody = messageBody;
UnknownValue2 = unknownValue2; UnknownValue2 = unknownValue2;

View file

@ -7,6 +7,7 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<RootNamespace>Server</RootNamespace> <RootNamespace>Server</RootNamespace>
<LangVersion>default</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@ -29,4 +30,8 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -1,14 +1,13 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using MassTransit.Internals; using MassTransit.Internals;
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 Microsoft.VisualBasic.CompilerServices; using Microsoft.VisualBasic.CompilerServices;
using Newtonsoft.Json; using Newtonsoft.Json;
using FieldAccessException = System.FieldAccessException;
namespace Server; namespace Server;
@ -42,13 +41,13 @@ public class PacketDistributorService : IHostedService
packetsWithId.AsParallel().ForAll(packet => packetsWithId.AsParallel().ForAll(packet =>
{ {
_logger.LogInformation("Packet with ID: {PacketID} has been added as {PacketName}", packet.Key, _logger.LogTrace("Packet with ID: {PacketID} has been added as {PacketName}", packet.Key,
packet.Value.FullName); packet.Value.FullName);
}); });
_packetsTypes = packetsWithId; _packetsTypes = packetsWithId;
var packetHandlersWithId = Assembly.GetEntryAssembly()?.GetTypes().Where(t => var packetHandlersWithId = Assembly.GetEntryAssembly()?.GetTypes().AsParallel().Where(t =>
t is { IsClass: true, IsAbstract: false } && t t is { IsClass: true, IsAbstract: false } && t
.GetInterfaces().Any(i => .GetInterfaces().Any(i =>
i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler<>))).ToDictionary(type => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler<>))).ToDictionary(type =>
@ -64,7 +63,7 @@ public class PacketDistributorService : IHostedService
packetHandlersWithId.AsParallel().ForAll(packetHandler => packetHandlersWithId.AsParallel().ForAll(packetHandler =>
{ {
_logger.LogInformation("Packet with ID: {PacketID} has been added as {PacketName}", packetHandler.Key, _logger.LogTrace("Packet with ID: {PacketID} has been added as {PacketName}", packetHandler.Key,
packetHandler.Value.FullName); packetHandler.Value.FullName);
}); });
@ -90,7 +89,7 @@ public class PacketDistributorService : IHostedService
{ {
Parallel.Invoke(() => Parallel.Invoke(() =>
{ {
_logger.LogInformation("[{TempId}] Packet with ID: {MessageOperationCode} is being dequeued", _logger.LogTrace("[{TempId}] Packet with ID: {MessageOperationCode} is being dequeued",
item.Session.Id, item.OperationCode); item.Session.Id, item.OperationCode);
var packetType = _packetsTypes[item.OperationCode]; var packetType = _packetsTypes[item.OperationCode];
var packet = Activator.CreateInstance(packetType); var packet = Activator.CreateInstance(packetType);
@ -98,22 +97,26 @@ public class PacketDistributorService : IHostedService
{ {
var typeOfField = field.FieldType; var typeOfField = field.FieldType;
var fieldOffsetAttribute = field.GetCustomAttribute<FieldOffsetAttribute>(); var fieldOffsetAttribute = field.GetCustomAttribute<FieldOffsetAttribute>();
if (fieldOffsetAttribute == null)
throw new FieldAccessException();
object value; object value;
_logger.LogInformation("Type of field of {fieldName}: {Name}", field.Name, typeOfField.Name); _logger.LogDebug("Type of field of {fieldName}: {Name}", field.Name, typeOfField.Name);
switch (typeOfField.Name) value = typeOfField.Name switch
{ {
case nameof(String): nameof(String) => Encoding.ASCII.GetString(item.MessageBody, fieldOffsetAttribute.Offset,
{ fieldOffsetAttribute.Size),
value = Encoding.ASCII.GetString(item.MessageBody, fieldOffsetAttribute.Offset, "Byte[]" => new ArraySegment<byte>(item.MessageBody, fieldOffsetAttribute.Offset,
fieldOffsetAttribute.Size); fieldOffsetAttribute.Size).ToArray(),
break; nameof(Boolean) => BitConverter.ToBoolean(item.MessageBody, fieldOffsetAttribute.Offset),
} nameof(Byte) => item.MessageBody[fieldOffsetAttribute.Offset],
default: nameof(Int16) => BitConverter.ToInt16(item.MessageBody, fieldOffsetAttribute.Offset),
{ nameof(UInt16) => BitConverter.ToUInt16(item.MessageBody, fieldOffsetAttribute.Offset),
value = 0; nameof(Int32) => BitConverter.ToInt32(item.MessageBody, fieldOffsetAttribute.Offset),
break; nameof(UInt32) => BitConverter.ToUInt32(item.MessageBody, fieldOffsetAttribute.Offset),
} nameof(Double) => BitConverter.ToDouble(item.MessageBody, fieldOffsetAttribute.Offset),
} nameof(Single) => BitConverter.ToSingle(item.MessageBody, fieldOffsetAttribute.Offset),
_ => throw new InvalidOperationException()
};
field.SetValue(packet, value); field.SetValue(packet, value);
}); });
@ -121,8 +124,8 @@ public class PacketDistributorService : IHostedService
ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider, ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider,
_packetHandlers[item.OperationCode]); _packetHandlers[item.OperationCode]);
packetHandler.GetType().GetMethod("Handle")?.Invoke(packetHandler, new[] { packet }); packetHandler.GetType().GetMethod("Handle")?.Invoke(packetHandler, new[] { packet });
_logger.LogInformation("Packet data {PacketData}", JsonConvert.SerializeObject(packet)); _logger.LogDebug("Packet data {PacketData}", JsonConvert.SerializeObject(packet));
_logger.LogInformation("[{TempId}] Packet with ID: {MessageOperationCode} has finished", _logger.LogTrace("[{TempId}] Packet with ID: {MessageOperationCode} has finished",
item.Session.Id, item.Session.Id,
item.OperationCode); item.OperationCode);
}); });

View file

@ -5,7 +5,7 @@ using NetCoreServer;
namespace Server; namespace Server;
public class WonderkingSession : AesSession public class WonderkingSession : AuthSession
{ {
private readonly IMediator _mediator; private readonly IMediator _mediator;