| | | 1 | | using System.Runtime.Serialization; |
| | | 2 | | using MessagePack; |
| | | 3 | | using MessagePack.Formatters; |
| | | 4 | | |
| | | 5 | | namespace NLightning.Transport.Ipc.MessagePack.Formatters; |
| | | 6 | | |
| | | 7 | | using Domain.Node; |
| | | 8 | | using Domain.Utils; |
| | | 9 | | |
| | | 10 | | public class FeatureSetFormatter : IMessagePackFormatter<FeatureSet?> |
| | | 11 | | { |
| | | 12 | | public void Serialize(ref MessagePackWriter writer, FeatureSet? value, MessagePackSerializerOptions options) |
| | 0 | 13 | | { |
| | 0 | 14 | | if (value is null) |
| | 0 | 15 | | { |
| | 0 | 16 | | writer.WriteNil(); |
| | 0 | 17 | | return; |
| | | 18 | | } |
| | | 19 | | |
| | 0 | 20 | | using var bitWriter = new BitWriter(value.SizeInBits); |
| | 0 | 21 | | value.WriteToBitWriter(bitWriter, value.SizeInBits, false); |
| | 0 | 22 | | writer.Write(value.SizeInBits); |
| | 0 | 23 | | writer.Write(bitWriter.ToArray()); |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | public FeatureSet? Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) |
| | 0 | 27 | | { |
| | 0 | 28 | | if (reader.TryReadNil()) |
| | 0 | 29 | | return null; |
| | | 30 | | |
| | 0 | 31 | | var sizeInBits = reader.ReadInt32(); |
| | 0 | 32 | | var bytes = reader.ReadBytes() ?? |
| | 0 | 33 | | throw new SerializationException($"Error deserializing {nameof(FeatureSet)})"); |
| | 0 | 34 | | var bitReader = new BitReader(bytes.FirstSpan.ToArray()); |
| | 0 | 35 | | return FeatureSet.DeserializeFromBitReader(bitReader, sizeInBits, false); |
| | 0 | 36 | | } |
| | | 37 | | } |