~ruther/NosSmooth

ref: 8dcf58cbe2cee392418cb457c78eddff0bf612b5 NosSmooth/Core/NosSmooth.Core/Packets/Converters/ISpecificPacketSerializer.cs -rw-r--r-- 4.1 KiB
8dcf58cb — František Boháček chore: add DI dependency to Tests 3 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
//  ISpecificPacketSerializer.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.Reflection;
using NosCore.Packets.Attributes;
using NosCore.Packets.Interfaces;
using Remora.Results;

namespace NosSmooth.Core.Packets.Converters;

/// <summary>
/// Converts packets that cannot be handled easily by the default serializer.
/// </summary>
public interface ISpecificPacketSerializer
{
    /// <summary>
    /// Gets whether this is a serializer.
    /// </summary>
    public bool Serializer { get; }

    /// <summary>
    /// Gets whether this is a deserializer.
    /// </summary>
    public bool Deserializer { get; }

    /// <summary>
    /// Whether the current packet serializer should handle the packet.
    /// </summary>
    /// <param name="packetString">The string of the packet.</param>
    /// <returns>If this serializer should be used for handling.</returns>
    public bool ShouldHandle(string packetString);

    /// <summary>
    /// Whether the current packet serializer should handle the packet.
    /// </summary>
    /// <param name="packet">The packet object.</param>
    /// <returns>If this serializer should be used for handling.</returns>
    public bool ShouldHandle(IPacket packet);

    /// <summary>
    /// Serialize the given packet into string.
    /// </summary>
    /// <param name="packet">The string of the packet.</param>
    /// <returns>The serialized packet or an error.</returns>
    public Result<string> Serialize(IPacket packet);

    /// <summary>
    /// Deserialize the given packet to its type.
    /// </summary>
    /// <param name="packetString">The string of the packet.</param>
    /// <returns>The deserialized packet or an error.</returns>
    public Result<IPacket> Deserialize(string packetString);
}

/// <summary>
/// Converts packets that cannot be handled easily by the default serializer.
/// </summary>
/// <typeparam name="TPacket">The packet.</typeparam>
public abstract class SpecificPacketSerializer<TPacket> : ISpecificPacketSerializer
    where TPacket : IPacket
{
    private string? _packetHeader;

    /// <summary>
    /// Gets the packet header identifier.
    /// </summary>
    public string PacketHeader
    {
        get
        {
            if (_packetHeader is null)
            {
                _packetHeader = typeof(TPacket).GetCustomAttribute<PacketHeaderAttribute>()!.Identification + " ";
            }

            return _packetHeader;
        }
    }

    /// <inheritdoc />
    public abstract bool Serializer { get; }

    /// <inheritdoc />
    public abstract bool Deserializer { get; }

    /// <inheritdoc />
    public bool ShouldHandle(string packetString)
    {
        return packetString.StartsWith(PacketHeader);
    }

    /// <inheritdoc />
    public bool ShouldHandle(IPacket packet)
    {
        return typeof(TPacket) == packet.GetType();
    }

    /// <inheritdoc/>
    Result<string> ISpecificPacketSerializer.Serialize(IPacket packet)
    {
        return Serialize((TPacket)packet);
    }

    /// <inheritdoc/>
    Result<IPacket> ISpecificPacketSerializer.Deserialize(string packetString)
    {
        var result = Deserialize(packetString);
        if (!result.IsSuccess)
        {
            return Result<IPacket>.FromError(result);
        }

        return Result<IPacket>.FromSuccess(result.Entity);
    }

    /// <summary>
    /// Serialize the given packet into string.
    /// </summary>
    /// <param name="packet">The string of the packet.</param>
    /// <returns>The serialized packet or an error.</returns>
    public abstract Result<string> Serialize(TPacket packet);

    /// <summary>
    /// Deserialize the given packet to its type.
    /// </summary>
    /// <param name="packetString">The string of the packet.</param>
    /// <returns>The deserialized packet or an error.</returns>
    public abstract Result<TPacket> Deserialize(string packetString);
}
Do not follow this link