| | 1 | | using Microsoft.Extensions.Logging; |
| | 2 | | using NLightning.Domain.Protocol.Interfaces; |
| | 3 | | using NLightning.Domain.Serialization.Interfaces; |
| | 4 | |
|
| | 5 | | namespace NLightning.Infrastructure.Serialization.Messages; |
| | 6 | |
|
| | 7 | | using Converters; |
| | 8 | | using Domain.Protocol.Constants; |
| | 9 | | using Exceptions; |
| | 10 | |
|
| | 11 | | public class MessageSerializer : IMessageSerializer |
| | 12 | | { |
| | 13 | | private readonly ILogger<MessageSerializer> _logger; |
| | 14 | | private readonly IMessageTypeSerializerFactory _messageTypeSerializerFactory; |
| | 15 | |
|
| 0 | 16 | | public MessageSerializer(ILogger<MessageSerializer> logger, |
| 0 | 17 | | IMessageTypeSerializerFactory messageTypeSerializerFactory) |
| | 18 | | { |
| 0 | 19 | | _logger = logger; |
| 0 | 20 | | _messageTypeSerializerFactory = messageTypeSerializerFactory; |
| 0 | 21 | | } |
| | 22 | |
|
| | 23 | | public async Task SerializeAsync(IMessage message, Stream stream) |
| | 24 | | { |
| 0 | 25 | | var messageTypeSerializer = |
| 0 | 26 | | _messageTypeSerializerFactory.GetSerializer(message.Type) |
| 0 | 27 | | ?? throw new InvalidOperationException($"No serializer found for message type {message.Type}"); |
| | 28 | |
|
| | 29 | | // Write the message type to the stream |
| 0 | 30 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian((ushort)message.Type)); |
| | 31 | |
|
| | 32 | | // Serialize the message |
| 0 | 33 | | await messageTypeSerializer.SerializeAsync(message, stream); |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | public async Task<TMessage?> DeserializeMessageAsync<TMessage>(Stream stream) where TMessage : class, IMessage |
| | 37 | | { |
| | 38 | | // Get the type of the message |
| 0 | 39 | | var typeBytes = new byte[2]; |
| 0 | 40 | | await stream.ReadExactlyAsync(typeBytes); |
| 0 | 41 | | var type = EndianBitConverter.ToUInt16BigEndian(typeBytes); |
| | 42 | |
|
| | 43 | | // Try to get the serializer for the message type |
| 0 | 44 | | var messageTypeSerializer = _messageTypeSerializerFactory.GetSerializer<TMessage>(); |
| 0 | 45 | | if (messageTypeSerializer is not null) |
| 0 | 46 | | return await messageTypeSerializer.DeserializeAsync(stream); |
| | 47 | |
|
| | 48 | | // If the type is unknown and even, throw an exception |
| 0 | 49 | | if (type % 2 == 0) |
| | 50 | | { |
| 0 | 51 | | throw new InvalidMessageException($"Unknown message type {type}"); |
| | 52 | | } |
| | 53 | |
|
| 0 | 54 | | return null; |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | public async Task<IMessage?> DeserializeMessageAsync(Stream stream) |
| | 58 | | { |
| | 59 | | // Get the type of the message |
| 0 | 60 | | var typeBytes = new byte[2]; |
| 0 | 61 | | await stream.ReadExactlyAsync(typeBytes); |
| 0 | 62 | | var type = EndianBitConverter.ToUInt16BigEndian(typeBytes); |
| | 63 | |
|
| | 64 | | // Try to get the serializer for the message type |
| 0 | 65 | | var messageTypeSerializer = _messageTypeSerializerFactory.GetSerializer((MessageTypes)type); |
| 0 | 66 | | if (messageTypeSerializer is not null) |
| 0 | 67 | | return await messageTypeSerializer.DeserializeAsync(stream); |
| | 68 | |
|
| | 69 | | // If the type is unknown and even, throw an exception |
| 0 | 70 | | if (type % 2 != 0) |
| 0 | 71 | | return null; |
| | 72 | |
|
| 0 | 73 | | _logger.LogError("Unknown message type {type}", type); |
| 0 | 74 | | throw new InvalidMessageException($"Unknown message type {type}"); |
| 0 | 75 | | } |
| | 76 | | } |