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