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