| | 1 | | using System.Runtime.Serialization; |
| | 2 | | using NLightning.Domain.Channels.ValueObjects; |
| | 3 | | using NLightning.Domain.Protocol.Interfaces; |
| | 4 | | using NLightning.Domain.Serialization.Interfaces; |
| | 5 | |
|
| | 6 | | namespace NLightning.Infrastructure.Serialization.Payloads; |
| | 7 | |
|
| | 8 | | using Converters; |
| | 9 | | using Domain.Protocol.Payloads; |
| | 10 | | using Exceptions; |
| | 11 | |
|
| | 12 | | public class ErrorPayloadSerializer : IPayloadSerializer<ErrorPayload> |
| | 13 | | { |
| | 14 | | private readonly IValueObjectSerializerFactory _valueObjectSerializerFactory; |
| | 15 | |
|
| 8 | 16 | | public ErrorPayloadSerializer(IValueObjectSerializerFactory valueObjectSerializerFactory) |
| | 17 | | { |
| 8 | 18 | | _valueObjectSerializerFactory = valueObjectSerializerFactory; |
| 8 | 19 | | } |
| | 20 | |
|
| | 21 | | public async Task SerializeAsync(IMessagePayload payload, Stream stream) |
| | 22 | | { |
| 12 | 23 | | if (payload is not ErrorPayload errorPayload) |
| 0 | 24 | | throw new SerializationException($"Payload is not of type {nameof(ErrorPayload)}"); |
| | 25 | |
|
| | 26 | | // Get the value object serializer |
| 12 | 27 | | var channelIdSerializer = |
| 12 | 28 | | _valueObjectSerializerFactory.GetSerializer<ChannelId>() |
| 12 | 29 | | ?? throw new SerializationException($"No serializer found for value object type {nameof(ChannelId)}"); |
| 12 | 30 | | await channelIdSerializer.SerializeAsync(errorPayload.ChannelId, stream); |
| | 31 | |
|
| | 32 | | // Serialize other types |
| 12 | 33 | | if (errorPayload.Data is null) |
| | 34 | | { |
| 0 | 35 | | await stream.WriteAsync("\0\0"u8.ToArray()); |
| | 36 | | } |
| | 37 | | else |
| | 38 | | { |
| 12 | 39 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian((ushort)errorPayload.Data.Length)); |
| 12 | 40 | | await stream.WriteAsync(errorPayload.Data); |
| | 41 | | } |
| 12 | 42 | | } |
| | 43 | |
|
| | 44 | | public async Task<ErrorPayload?> DeserializeAsync(Stream stream) |
| | 45 | | { |
| | 46 | | try |
| | 47 | | { |
| | 48 | | // Get the value object serializer |
| 8 | 49 | | var channelIdSerializer = |
| 8 | 50 | | _valueObjectSerializerFactory.GetSerializer<ChannelId>() |
| 8 | 51 | | ?? throw new SerializationException($"No serializer found for value object type {nameof(ChannelId)}"); |
| 8 | 52 | | var channelId = await channelIdSerializer.DeserializeAsync(stream); |
| | 53 | |
|
| 8 | 54 | | var buffer = new byte[sizeof(ushort)]; |
| 8 | 55 | | await stream.ReadExactlyAsync(buffer); |
| 8 | 56 | | var dataLength = EndianBitConverter.ToUInt16BigEndian(buffer); |
| | 57 | |
|
| 8 | 58 | | buffer = new byte[dataLength]; |
| 8 | 59 | | await stream.ReadExactlyAsync(buffer); |
| | 60 | |
|
| 8 | 61 | | return new ErrorPayload(channelId, buffer); |
| | 62 | | } |
| 0 | 63 | | catch (Exception e) |
| | 64 | | { |
| 0 | 65 | | throw new PayloadSerializationException("Error deserializing ErrorPayload", e); |
| | 66 | | } |
| 8 | 67 | | } |
| | 68 | |
|
| | 69 | | async Task<IMessagePayload?> IPayloadSerializer.DeserializeAsync(Stream stream) |
| | 70 | | { |
| 0 | 71 | | return await DeserializeAsync(stream); |
| 0 | 72 | | } |
| | 73 | | } |