| | 1 | | using System.Runtime.Serialization; |
| | 2 | |
|
| | 3 | | namespace NLightning.Infrastructure.Serialization.Messages.Types; |
| | 4 | |
|
| | 5 | | using Domain.Protocol.Constants; |
| | 6 | | using Domain.Protocol.Factories; |
| | 7 | | using Domain.Protocol.Messages; |
| | 8 | | using Domain.Protocol.Messages.Interfaces; |
| | 9 | | using Domain.Protocol.Payloads; |
| | 10 | | using Domain.Protocol.Tlv; |
| | 11 | | using Domain.Serialization.Factories; |
| | 12 | | using Domain.Serialization.Messages.Types; |
| | 13 | | using Exceptions; |
| | 14 | | using Interfaces; |
| | 15 | |
|
| | 16 | | public class ChannelReadyMessageTypeSerializer : IMessageTypeSerializer<ChannelReadyMessage> |
| | 17 | | { |
| | 18 | | private readonly IPayloadSerializerFactory _payloadSerializerFactory; |
| | 19 | | private readonly ITlvConverterFactory _tlvConverterFactory; |
| | 20 | | private readonly ITlvStreamSerializer _tlvStreamSerializer; |
| | 21 | |
|
| 20 | 22 | | public ChannelReadyMessageTypeSerializer(IPayloadSerializerFactory payloadSerializerFactory, |
| 20 | 23 | | ITlvConverterFactory tlvConverterFactory, |
| 20 | 24 | | ITlvStreamSerializer tlvStreamSerializer) |
| | 25 | | { |
| 20 | 26 | | _payloadSerializerFactory = payloadSerializerFactory; |
| 20 | 27 | | _tlvConverterFactory = tlvConverterFactory; |
| 20 | 28 | | _tlvStreamSerializer = tlvStreamSerializer; |
| 20 | 29 | | } |
| | 30 | |
|
| | 31 | | public async Task SerializeAsync(IMessage message, Stream stream) |
| | 32 | | { |
| 8 | 33 | | if (message is not ChannelReadyMessage channelReadyMessage) |
| 0 | 34 | | throw new SerializationException($"Message is not of type {nameof(ChannelReadyMessage)}"); |
| | 35 | |
|
| | 36 | | // Get the payload serializer |
| 8 | 37 | | var payloadTypeSerializer = _payloadSerializerFactory.GetSerializer(message.Type) |
| 8 | 38 | | ?? throw new SerializationException("No serializer found for payload type"); |
| 8 | 39 | | await payloadTypeSerializer.SerializeAsync(message.Payload, stream); |
| | 40 | |
|
| | 41 | | // Serialize the TLV stream |
| 8 | 42 | | await _tlvStreamSerializer.SerializeAsync(channelReadyMessage.Extension, stream); |
| 8 | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Deserialize a ChannelReadyMessage from a stream. |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="stream">The stream to deserialize from.</param> |
| | 49 | | /// <returns>The deserialized ChannelReadyMessage.</returns> |
| | 50 | | /// <exception cref="MessageSerializationException">Error deserializing ChannelReadyMessage</exception> |
| | 51 | | public async Task<ChannelReadyMessage> DeserializeAsync(Stream stream) |
| | 52 | | { |
| | 53 | | try |
| | 54 | | { |
| | 55 | | // Deserialize payload |
| 12 | 56 | | var payloadSerializer = _payloadSerializerFactory.GetSerializer<ChannelReadyPayload>() |
| 12 | 57 | | ?? throw new SerializationException("No serializer found for payload type"); |
| 12 | 58 | | var payload = await payloadSerializer.DeserializeAsync(stream) |
| 12 | 59 | | ?? throw new SerializationException("Error serializing payload"); |
| | 60 | |
|
| | 61 | | // Deserialize extension |
| 12 | 62 | | if (stream.Position >= stream.Length) |
| 4 | 63 | | return new ChannelReadyMessage(payload); |
| | 64 | |
|
| 8 | 65 | | var extension = await _tlvStreamSerializer.DeserializeAsync(stream); |
| 4 | 66 | | if (extension is null) |
| 0 | 67 | | return new ChannelReadyMessage(payload); |
| | 68 | |
|
| 4 | 69 | | ShortChannelIdTlv? shortChannelIdTlv = null; |
| 4 | 70 | | if (extension.TryGetTlv(TlvConstants.SHORT_CHANNEL_ID, out var baseShortChannelId)) |
| | 71 | | { |
| 4 | 72 | | var tlvConverter = _tlvConverterFactory.GetConverter<ShortChannelIdTlv>() |
| 4 | 73 | | ?? throw new SerializationException( |
| 4 | 74 | | $"No serializer found for tlv type {nameof(ShortChannelIdTlv)}"); |
| 4 | 75 | | shortChannelIdTlv = tlvConverter.ConvertFromBase(baseShortChannelId!); |
| | 76 | | } |
| | 77 | |
|
| 4 | 78 | | return new ChannelReadyMessage(payload, shortChannelIdTlv); |
| | 79 | | } |
| 4 | 80 | | catch (SerializationException e) |
| | 81 | | { |
| 4 | 82 | | throw new MessageSerializationException("Error deserializing ChannelReadyMessage", e); |
| | 83 | | } |
| 8 | 84 | | } |
| | 85 | | async Task<IMessage> IMessageTypeSerializer.DeserializeAsync(Stream stream) |
| | 86 | | { |
| 0 | 87 | | return await DeserializeAsync(stream); |
| 0 | 88 | | } |
| | 89 | | } |