From 46e862f35d852670da160bae0b2e683dd524ea23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Wed, 29 Dec 2021 15:15:07 +0100 Subject: [PATCH] feat: implement packet serializer --- Core/NosSmooth.Packets/PacketSerializer.cs | 72 ++++++++++++++++++---- 1 file changed, 61 insertions(+), 11 deletions(-) diff --git a/Core/NosSmooth.Packets/PacketSerializer.cs b/Core/NosSmooth.Packets/PacketSerializer.cs index 3c9dc93259b3bdb30e8d12a74dfcbecf3a687b5b..70b63f2b26d20cb09bd091ca78dc52c005cb7480 100644 --- a/Core/NosSmooth.Packets/PacketSerializer.cs +++ b/Core/NosSmooth.Packets/PacketSerializer.cs @@ -7,6 +7,7 @@ using System; using NosSmooth.Packets.Attributes; using NosSmooth.Packets.Converters; +using NosSmooth.Packets.Errors; using NosSmooth.Packets.Packets; using Remora.Results; @@ -17,24 +18,73 @@ namespace NosSmooth.Packets; /// public class PacketSerializer : IPacketSerializer { + private readonly PacketTypesRepository _packetTypesRepository; + /// - /// Serializes the given object to string by appending to the packet string builder. + /// Initializes a new instance of the class. /// - /// The packet to serialize. - /// A result that may or may not have succeeded. + /// The repository of packet types. + public PacketSerializer(PacketTypesRepository packetTypesRepository) + { + _packetTypesRepository = packetTypesRepository; + } + + /// public Result Serialize(IPacket obj) { - return Result.FromSuccess("as"); + var stringBuilder = new PacketStringBuilder(); + var infoResult = _packetTypesRepository.FindPacketInfo(obj.GetType()); + if (!infoResult.IsSuccess) + { + return Result.FromError(infoResult); + } + + var info = infoResult.Entity; + if (info.Header is null) + { + return new PacketMissingHeaderError(obj); + } + + stringBuilder.Append(info.Header); + var serializeResult = info.PacketConverter.Serialize(obj, stringBuilder); + if (!serializeResult.IsSuccess) + { + return Result.FromError(serializeResult); + } + + return stringBuilder.ToString(); } - /// - /// Convert the data from the enumerator to the given type. - /// - /// The packet string to deserialize. - /// The preferred source to check first. If packet with the given header is not found there, other sources will be checked as well. - /// The parsed object or an error. + /// public Result Deserialize(string packetString, PacketSource preferredSource) { - return Result.FromError(new ArgumentInvalidError("asdf", "asdf")); + var packetStringEnumerator = new PacketStringEnumerator(packetString); + var headerTokenResult = packetStringEnumerator.GetNextToken(); + if (!headerTokenResult.IsSuccess) + { + return Result.FromError(headerTokenResult); + } + + var packetInfoResult = _packetTypesRepository.FindPacketInfo(headerTokenResult.Entity.Token, preferredSource); + if (!packetInfoResult.IsSuccess) + { + return Result.FromError(packetInfoResult); + } + + var packetInfo = packetInfoResult.Entity; + var deserializedResult = packetInfo.PacketConverter.Deserialize(packetStringEnumerator); + if (!deserializedResult.IsSuccess) + { + return Result.FromError(deserializedResult); + } + + var packet = deserializedResult.Entity as IPacket; + + if (packet is null) + { + return new DeserializedValueNullError(packetInfo.PacketType); + } + + return Result.FromSuccess(packet); } } \ No newline at end of file