| | 1 | | using System.Buffers; |
| | 2 | |
|
| | 3 | | namespace NLightning.Infrastructure.Serialization.Tlv; |
| | 4 | |
|
| | 5 | | using Domain.Protocol.Tlv; |
| | 6 | | using Domain.Serialization.Factories; |
| | 7 | | using Domain.Serialization.Tlv; |
| | 8 | | using Domain.Serialization.ValueObjects; |
| | 9 | | using Domain.ValueObjects; |
| | 10 | |
|
| | 11 | | public class TlvSerializer : ITlvSerializer |
| | 12 | | { |
| | 13 | | private readonly IValueObjectTypeSerializer<BigSize> _bigSizeSerializer; |
| | 14 | |
|
| 16 | 15 | | public TlvSerializer(IValueObjectSerializerFactory valueObjectSerializerFactory) |
| | 16 | | { |
| 16 | 17 | | _bigSizeSerializer = valueObjectSerializerFactory.GetSerializer<BigSize>() |
| 16 | 18 | | ?? throw new ArgumentNullException(nameof(valueObjectSerializerFactory)); |
| 16 | 19 | | } |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Serializes a BaseTlv value into a stream. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="baseTlv">The BaseTlv value to serialize.</param> |
| | 25 | | /// <param name="stream">The stream where the serialized value will be written.</param> |
| | 26 | | /// <returns>A task that represents the asynchronous serialization operation.</returns> |
| | 27 | | /// <exception cref="ArgumentNullException">Thrown when the stream is null.</exception> |
| | 28 | | /// <exception cref="IOException">Thrown when an I/O error occurs during the write operation.</exception> |
| | 29 | | public async Task SerializeAsync(BaseTlv baseTlv, Stream stream) |
| | 30 | | { |
| 72 | 31 | | await _bigSizeSerializer.SerializeAsync(baseTlv.Type, stream); |
| 72 | 32 | | await _bigSizeSerializer.SerializeAsync(baseTlv.Length, stream); |
| | 33 | |
|
| 72 | 34 | | await stream.WriteAsync(baseTlv.Value); |
| 72 | 35 | | } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Deserializes a BaseTlv value from a stream. |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="stream">The stream from which the BaseTlv value will be deserialized.</param> |
| | 41 | | /// <returns>A task that represents the asynchronous deserialization operation, containing the deserialized BaseTlv |
| | 42 | | /// <exception cref="ArgumentException">Thrown when the stream is empty or contains insufficient data for deserializ |
| | 43 | | /// <exception cref="IOException">Thrown when an I/O error occurs during the read operation.</exception> |
| | 44 | | public async Task<BaseTlv?> DeserializeAsync(Stream stream) |
| | 45 | | { |
| 112 | 46 | | if (stream.Position == stream.Length) |
| 0 | 47 | | return null; |
| | 48 | |
|
| 112 | 49 | | byte[]? value = null; |
| | 50 | |
|
| | 51 | | try |
| | 52 | | { |
| 112 | 53 | | var type = await _bigSizeSerializer.DeserializeAsync(stream); |
| 112 | 54 | | var length = await _bigSizeSerializer.DeserializeAsync(stream); |
| | 55 | |
|
| 108 | 56 | | value = ArrayPool<byte>.Shared.Rent(length); |
| 108 | 57 | | await stream.ReadExactlyAsync(value.AsMemory()[..(int)length]); |
| | 58 | |
|
| 72 | 59 | | return new BaseTlv(type, length, value[..(int)length]); |
| | 60 | | } |
| | 61 | | finally |
| | 62 | | { |
| 112 | 63 | | if (value is not null) |
| 108 | 64 | | ArrayPool<byte>.Shared.Return(value); |
| | 65 | | } |
| 72 | 66 | | } |
| | 67 | | } |