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

View file

@ -3,16 +3,16 @@ WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["Serer/Serer.csproj", "Serer/"]
RUN dotnet restore "Serer/Serer.csproj"
COPY ["Server/Server.csproj", "Server/"]
RUN dotnet restore "Server/Server.csproj"
COPY . .
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
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
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Serer.dll"]
ENTRYPOINT ["dotnet", "Server.dll"]

View file

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

View file

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

View file

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

View file

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

View file

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