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