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