diff --git a/Server/ChannelSession.cs b/Server/ChannelSession.cs index 41e6ee8..ae11461 100644 --- a/Server/ChannelSession.cs +++ b/Server/ChannelSession.cs @@ -45,7 +45,11 @@ public class ChannelSession : TcpSession using (var ms = new MemoryStream(Decrypt(buffer))) using (var cs = new CryptoStream(ms, this.decryptor, CryptoStreamMode.Read)) { - cs.Read(buffer); + var amountOfReadBytes = cs.Read(buffer); + if (amountOfReadBytes != buffer.Length) + { + this.logger.LogError("Amount of read bytes is not equal to buffer length."); + } } base.OnReceived(buffer, offset, size); diff --git a/Server/PacketHandlers/LoginHandler.cs b/Server/PacketHandlers/LoginHandler.cs index 2d435e4..dbd1a65 100644 --- a/Server/PacketHandlers/LoginHandler.cs +++ b/Server/PacketHandlers/LoginHandler.cs @@ -78,7 +78,14 @@ public class LoginHandler : IPacketHandler IsGameMaster = true }; var sess = session as AuthSession; - sess.AccountId = account.Id; - sess.Send(loginResponsePacket); + if (account != null) + { + if (sess != null) + { + sess.AccountId = account.Id; + } + } + + sess?.Send(loginResponsePacket); } } diff --git a/Server/PacketLoggerMessages.cs b/Server/PacketLoggerMessages.cs new file mode 100644 index 0000000..25257b4 --- /dev/null +++ b/Server/PacketLoggerMessages.cs @@ -0,0 +1,50 @@ +namespace Server; + +using JetBrains.Annotations; +using Microsoft.Extensions.Logging; +using Packets; + +public static partial class PacketLoggerMessages +{ + [LoggerMessage(EventId = 0, Level = LogLevel.Information, + Message = "Packet creation function created for {PacketID}")] + public static partial void PacketCreationFunctionCreated(this ILogger logger, OperationCode packetId); + + [LoggerMessage(EventId = 1, Level = LogLevel.Trace, + Message = "Packet with ID: {PacketID} has been added as {PacketName}")] + public static partial void PacketWithIdAdded(this ILogger logger, OperationCode packetId, string packetName); + + [LoggerMessage(EventId = 2, Level = LogLevel.Trace, + Message = "PacketHandler with ID: {PacketID} has been added as {PacketName}")] + public static partial void PacketHandlerWithIdAdded(this ILogger logger, OperationCode packetId, + [CanBeNull] string packetName); + + [LoggerMessage(EventId = 3, Level = LogLevel.Information, + Message = "Packet with ID: {PacketID} has been received")] + public static partial void PacketReceived(this ILogger logger, OperationCode packetId); + + [LoggerMessage(EventId = 4, Level = LogLevel.Trace, + Message = "[{SessionID}] Packet with ID: {PacketID} is being dequeued")] + public static partial void PacketDequeued(this ILogger logger, Guid sessionId, OperationCode packetId); + + [LoggerMessage(EventId = 5, Level = LogLevel.Information, + Message = "Couldn't find Packet type for Id: {PacketID}")] + public static partial void PacketTypeNotFound(this ILogger logger, OperationCode packetId); + + [LoggerMessage(EventId = 6, Level = LogLevel.Trace, + Message = "[{SessionID}] Packet with ID: {PacketID} has finished")] + public static partial void PacketFinished(this ILogger logger, Guid sessionId, OperationCode packetId); + + [LoggerMessage(EventId = 7, Level = LogLevel.Critical, + Message = "No PacketHandlers have been found")] + public static partial void NoPacketHandlersFound(this ILogger logger); + + [LoggerMessage(EventId = 8, Level = LogLevel.Information, + Message = "Packet data {PacketData}")] + public static partial void PacketData(this ILogger logger, string packetData); + + + [LoggerMessage(EventId = 9, Level = LogLevel.Critical, + Message = "No Packets have been found")] + public static partial void NoPacketsFound(this ILogger logger); +} diff --git a/Server/Packets/Outgoing/LoginResponseReason.cs b/Server/Packets/Outgoing/LoginResponseReason.cs index f9dcf51..b4e8bff 100644 --- a/Server/Packets/Outgoing/LoginResponseReason.cs +++ b/Server/Packets/Outgoing/LoginResponseReason.cs @@ -16,8 +16,8 @@ public enum LoginResponseReason : byte LockedDuePayment, UserDeletedBigpoint, NoMoreTries, - XMLError12, - XMLRPCError, + XmlError12, + XmlRpcError, FileLoadFail, // arbitrary file load error (maybe we're able to load custom files) ThisIsNotAServiceArea, None diff --git a/Server/Services/PacketDistributorService.cs b/Server/Services/PacketDistributorService.cs index 4000f40..ab5699d 100644 --- a/Server/Services/PacketDistributorService.cs +++ b/Server/Services/PacketDistributorService.cs @@ -60,7 +60,7 @@ public class PacketDistributorService : IHostedService Return(packetVariable); }).Compile(); - logger.LogInformation("Packet creation function created for {Opcode}", packetsType.Key); + logger.PacketCreationFunctionCreated(packetsType.Key); tempDeserializationMap.Add(packetsType.Key, lambda); } @@ -73,18 +73,20 @@ public class PacketDistributorService : IHostedService private Dictionary GetPacketsWithId(Assembly executingAssembly) { + // ! : We are filtering if types that don't have an instance of the required Attribute var packetsWithId = executingAssembly.GetTypes().AsParallel() - .Where(type => type.HasInterface(typeof(IPacket)) && !type.IsInterface && !type.IsAbstract) - .Where(type => type.GetCustomAttribute() != null) - .ToDictionary(type => type.GetCustomAttribute().Code); + .Where(type => type.HasInterface(typeof(IPacket)) && type is { IsInterface: false, IsAbstract: false }) + .Select(type => new { Type = type, Attribute = type.GetCustomAttribute() }) + .Where(item => item.Attribute is not null) + .ToDictionary(item => item.Attribute!.Code, item => item.Type); if (packetsWithId is not { Count: 0 }) { - packetsWithId.AsParallel().ForAll(packet => this.logger.LogTrace("Packet with ID: {PacketID} has been added as {PacketName}", packet.Key, - packet.Value.FullName)); + packetsWithId.AsParallel() + .ForAll(packet => this.logger.PacketWithIdAdded(packet.Key, packet.Value.FullName)); return packetsWithId; } - this.logger.LogCritical("No Packets have been found"); + this.logger.NoPacketsFound(); throw new IncompleteInitialization(); } @@ -100,13 +102,12 @@ public class PacketDistributorService : IHostedService if (packetHandlersWithId is not { Count: 0 }) { - packetHandlersWithId.AsParallel().ForAll(packetHandler => this.logger.LogTrace("PacketHandler with ID: {PacketID} has been added as {PacketName}", - packetHandler.Key, - packetHandler.Value.FullName)); + packetHandlersWithId.AsParallel().ForAll(packetHandler => + this.logger.PacketHandlerWithIdAdded(packetHandler.Key, packetHandler.Value.FullName)); return packetHandlersWithId; } - this.logger.LogCritical("No PacketHandlers have been found"); + this.logger.NoPacketHandlersFound(); throw new IncompleteInitialization(); } @@ -114,8 +115,7 @@ public class PacketDistributorService : IHostedService { this.concurrentQueue.Enqueue(rawPacket); this.DequeueRawPacket(); - this.logger.LogInformation("Packet with ID: {MessageOperationCode} has been received", - rawPacket.OperationCode); + this.logger.PacketReceived(rawPacket.OperationCode); } private void DequeueRawPacket() @@ -128,21 +128,18 @@ public class PacketDistributorService : IHostedService private void InvokePacketHandler(RawPacket item) { - this.logger.LogTrace("[{TempId}] Packet with ID: {MessageOperationCode} is being dequeued", - item.Session.Id, item.OperationCode); + this.logger.PacketDequeued(item.Session.Id, item.OperationCode); if (!this.deserializationMap.ContainsKey(item.OperationCode)) { - this.logger.LogInformation("Couldn't find Packet type for Id: {Opcode}", item.OperationCode); + this.logger.PacketTypeNotFound(item.OperationCode); return; } var packet = this.deserializationMap[item.OperationCode](item.MessageBody); - this.logger.LogInformation("Packet data {PacketData}", JsonConvert.SerializeObject(packet)); + this.logger.PacketData(JsonConvert.SerializeObject(packet)); this.packetHandlersInstantiation[item.OperationCode].GetType().GetMethod("HandleAsync") ?.Invoke(this.packetHandlersInstantiation[item.OperationCode], new object[] { packet, item.Session }); - this.logger.LogTrace("[{TempId}] Packet with ID: {MessageOperationCode} has finished", - item.Session.Id, - item.OperationCode); + this.logger.PacketFinished(item.Session.Id, item.OperationCode); } } diff --git a/build.ps1 b/build.ps1 index c0c0e61..2719418 100644 --- a/build.ps1 +++ b/build.ps1 @@ -13,7 +13,7 @@ $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent # CONFIGURATION ########################################################################### -$BuildProjectFile = "$PSScriptRoot\build\_build.csproj" +$BuildProjectFile = "$PSScriptRoot\build\build.csproj" $TempDirectory = "$PSScriptRoot\\.nuke\temp" $DotNetGlobalFile = "$PSScriptRoot\\global.json" diff --git a/build.sh b/build.sh index 2f10dcb..25e0d46 100644 --- a/build.sh +++ b/build.sh @@ -8,8 +8,7 @@ SCRIPT_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd) ########################################################################### # CONFIGURATION ########################################################################### - -BUILD_PROJECT_FILE="$SCRIPT_DIR/build/_build.csproj" +BUILD_PROJECT_FILE="$SCRIPT_DIR/build/build.csproj" TEMP_DIRECTORY="$SCRIPT_DIR//.nuke/temp" DOTNET_GLOBAL_FILE="$SCRIPT_DIR//global.json"