diff --git a/Server/AesSession.cs b/Server/AuthSession.cs similarity index 94% rename from Server/AesSession.cs rename to Server/AuthSession.cs index 2aa37c0..c2727af 100644 --- a/Server/AesSession.cs +++ b/Server/AuthSession.cs @@ -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; diff --git a/Server/PacketConsumer.cs b/Server/Consumers/PacketConsumer.cs similarity index 100% rename from Server/PacketConsumer.cs rename to Server/Consumers/PacketConsumer.cs diff --git a/Server/Dockerfile b/Server/Dockerfile index 189051f..5d43d37 100644 --- a/Server/Dockerfile +++ b/Server/Dockerfile @@ -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"] diff --git a/Server/IPacketHandler.cs b/Server/PacketHandlers/IPacketHandler.cs similarity index 51% rename from Server/IPacketHandler.cs rename to Server/PacketHandlers/IPacketHandler.cs index f11243a..ffa44b2 100644 --- a/Server/IPacketHandler.cs +++ b/Server/PacketHandlers/IPacketHandler.cs @@ -1,6 +1,6 @@ namespace Server; -public interface IPacketHandler where T: IPacket +public interface IPacketHandler where T: IPacket { public void Handle(T packet); } \ No newline at end of file diff --git a/Server/LoginInfoHandler.cs b/Server/PacketHandlers/LoginInfoHandler.cs similarity index 100% rename from Server/LoginInfoHandler.cs rename to Server/PacketHandlers/LoginInfoHandler.cs diff --git a/Server/IPacket.cs b/Server/Packets/IPacket.cs similarity index 100% rename from Server/IPacket.cs rename to Server/Packets/IPacket.cs diff --git a/Server/LoginInfoPacket.cs b/Server/Packets/LoginInfoPacket.cs similarity index 100% rename from Server/LoginInfoPacket.cs rename to Server/Packets/LoginInfoPacket.cs diff --git a/Server/OperationCode.cs b/Server/Packets/OperationCode.cs similarity index 100% rename from Server/OperationCode.cs rename to Server/Packets/OperationCode.cs diff --git a/Server/RawPacket.cs b/Server/Packets/RawPacket.cs similarity index 82% rename from Server/RawPacket.cs rename to Server/Packets/RawPacket.cs index d98e783..fa84014 100644 --- a/Server/RawPacket.cs +++ b/Server/Packets/RawPacket.cs @@ -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; diff --git a/Server/FieldOffsetAttribute.cs b/Server/Packets/SerializationUtilities/FieldOffsetAttribute.cs similarity index 100% rename from Server/FieldOffsetAttribute.cs rename to Server/Packets/SerializationUtilities/FieldOffsetAttribute.cs diff --git a/Server/PacketHandler.cs b/Server/Packets/SerializationUtilities/PacketId.cs similarity index 100% rename from Server/PacketHandler.cs rename to Server/Packets/SerializationUtilities/PacketId.cs diff --git a/Server/TextEncodingAttribute.cs b/Server/Packets/SerializationUtilities/TextEncodingAttribute.cs similarity index 100% rename from Server/TextEncodingAttribute.cs rename to Server/Packets/SerializationUtilities/TextEncodingAttribute.cs diff --git a/Server/Server.csproj b/Server/Server.csproj index b566037..ab4a803 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -7,6 +7,7 @@ enable Linux Server + default @@ -29,4 +30,8 @@ + + + + diff --git a/Server/PacketDistributorService.cs b/Server/Services/PacketDistributorService.cs similarity index 67% rename from Server/PacketDistributorService.cs rename to Server/Services/PacketDistributorService.cs index 6b9fb48..5b586d3 100644 --- a/Server/PacketDistributorService.cs +++ b/Server/Services/PacketDistributorService.cs @@ -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(); + 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(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); }); diff --git a/Server/WonderkingAuthServer.cs b/Server/Services/WonderkingAuthServer.cs similarity index 100% rename from Server/WonderkingAuthServer.cs rename to Server/Services/WonderkingAuthServer.cs diff --git a/Server/WonderkingSession.cs b/Server/WonderkingSession.cs index f302e46..fef4a30 100644 --- a/Server/WonderkingSession.cs +++ b/Server/WonderkingSession.cs @@ -5,7 +5,7 @@ using NetCoreServer; namespace Server; -public class WonderkingSession : AesSession +public class WonderkingSession : AuthSession { private readonly IMediator _mediator;