~ruther/NosSmooth

ref: 58d8743477820408507f19d1c1ce75b73e23615f NosSmooth/Packets/NosSmooth.PacketSerializer/PacketSerializer.cs -rw-r--r-- 3.0 KiB
58d87434 — Rutherther chore: bump versions 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
//  PacketSerializer.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using NosSmooth.Packets;
using NosSmooth.PacketSerializer.Abstractions;
using NosSmooth.PacketSerializer.Abstractions.Attributes;
using NosSmooth.PacketSerializer.Abstractions.Errors;
using NosSmooth.PacketSerializer.Errors;
using NosSmooth.PacketSerializer.Packets;
using Remora.Results;

namespace NosSmooth.PacketSerializer;

/// <summary>
/// Serializer of packets.
/// </summary>
public class PacketSerializer : IPacketSerializer
{
    private readonly IPacketTypesRepository _packetTypesRepository;

    /// <summary>
    /// Initializes a new instance of the <see cref="PacketSerializer"/> class.
    /// </summary>
    /// <param name="packetTypesRepository">The repository of packet types.</param>
    public PacketSerializer(IPacketTypesRepository packetTypesRepository)
    {
        _packetTypesRepository = packetTypesRepository;
    }

    /// <inheritdoc/>
    public Result<string> Serialize(IPacket obj)
    {
        var stringBuilder = new PacketStringBuilder();
        var infoResult = _packetTypesRepository.FindPacketInfo(obj.GetType());
        if (!infoResult.IsSuccess)
        {
            return Result<string>.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<string>.FromError(serializeResult);
        }

        return stringBuilder.ToString();
    }

    /// <inheritdoc/>
    public Result<IPacket> Deserialize(ReadOnlySpan<char> packetString, PacketSource preferredSource)
    {
        var packetStringEnumerator = new PacketStringEnumerator(packetString);
        var headerTokenResult = packetStringEnumerator.GetNextToken(out var packetToken);
        if (!headerTokenResult.IsSuccess)
        {
            return Result<IPacket>.FromError(headerTokenResult);
        }

        var packetInfoResult = _packetTypesRepository.FindPacketInfo(packetToken.Token.ToString(), preferredSource);
        if (!packetInfoResult.IsSuccess)
        {
            return Result<IPacket>.FromError(packetInfoResult);
        }

        var packetInfo = packetInfoResult.Entity;
        var deserializedResult = packetInfo.PacketConverter.Deserialize(ref packetStringEnumerator, default);
        if (!deserializedResult.IsSuccess)
        {
            return Result<IPacket>.FromError(deserializedResult);
        }

        var packet = deserializedResult.Entity as IPacket;

        if (packet is null)
        {
            return new DeserializedValueNullError(packetInfo.PacketType);
        }

        return Result<IPacket>.FromSuccess(packet);
    }
}
Do not follow this link