| | | 1 | | using System.Collections; |
| | | 2 | | using System.Runtime.Serialization; |
| | | 3 | | |
| | | 4 | | namespace NLightning.Infrastructure.Serialization.Node; |
| | | 5 | | |
| | | 6 | | using Converters; |
| | | 7 | | using Domain.Node; |
| | | 8 | | using Interfaces; |
| | | 9 | | |
| | | 10 | | public class FeatureSetSerializer : IFeatureSetSerializer |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Serializes the features to a binary writer. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="featureSet">The features to serialize.</param> |
| | | 16 | | /// <param name="stream">The stream to write to.</param> |
| | | 17 | | /// <param name="asGlobal">If the features should be serialized as a global feature set.</param> |
| | | 18 | | /// <param name="includeLength">If the length of the byte array should be included.</param> |
| | | 19 | | /// <remarks> |
| | | 20 | | /// If the features are serialized as a global feature set, only the first 13 bits are serialized. |
| | | 21 | | /// </remarks> |
| | | 22 | | /// <remarks> |
| | | 23 | | /// If the length of the byte array is included, the first 2 bytes are written as the length of the byte array. |
| | | 24 | | /// </remarks> |
| | | 25 | | public async Task SerializeAsync(FeatureSet featureSet, Stream stream, bool asGlobal = false, |
| | | 26 | | bool includeLength = true) |
| | | 27 | | { |
| | | 28 | | // Convert BitArray to a byte array |
| | 120 | 29 | | var bytes = featureSet.GetBytes(asGlobal) ?? throw new SerializationException("Feature set is empty"); |
| | | 30 | | |
| | | 31 | | // Set bytes as big endian |
| | 120 | 32 | | if (BitConverter.IsLittleEndian) |
| | 120 | 33 | | Array.Reverse(bytes); |
| | | 34 | | |
| | | 35 | | // Trim leading zero bytes |
| | 120 | 36 | | var leadingZeroBytes = 0; |
| | 356 | 37 | | foreach (var t in bytes) |
| | | 38 | | { |
| | 116 | 39 | | if (t == 0) |
| | | 40 | | { |
| | 0 | 41 | | leadingZeroBytes++; |
| | | 42 | | } |
| | | 43 | | else |
| | | 44 | | { |
| | | 45 | | break; |
| | | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | 120 | 49 | | var trimmedBytes = bytes[leadingZeroBytes..]; |
| | | 50 | | |
| | | 51 | | // Write the length of the byte array or 1 if all bytes are zero |
| | 120 | 52 | | if (includeLength) |
| | 92 | 53 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian((ushort)trimmedBytes.Length)); |
| | | 54 | | |
| | | 55 | | // Otherwise, return the array starting from the first non-zero byte |
| | 120 | 56 | | await stream.WriteAsync(trimmedBytes); |
| | 120 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Deserializes the features from a binary reader. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="stream">The stream to read from.</param> |
| | | 63 | | /// <param name="includeLength">If the length of the byte array is included.</param> |
| | | 64 | | /// <remarks> |
| | | 65 | | /// If the length of the byte array is included, the first 2 bytes are read as the length of the byte array. |
| | | 66 | | /// </remarks> |
| | | 67 | | /// <returns>The deserialized features.</returns> |
| | | 68 | | /// <exception cref="SerializationException">Error deserializing Features</exception> |
| | | 69 | | public async Task<FeatureSet> DeserializeAsync(Stream stream, bool includeLength = true) |
| | | 70 | | { |
| | | 71 | | try |
| | | 72 | | { |
| | 88 | 73 | | var length = 8; |
| | | 74 | | |
| | 88 | 75 | | var bytes = new byte[2]; |
| | 88 | 76 | | if (includeLength) |
| | | 77 | | { |
| | | 78 | | // Read the length of the byte array |
| | 84 | 79 | | await stream.ReadExactlyAsync(bytes); |
| | 84 | 80 | | length = EndianBitConverter.ToUInt16BigEndian(bytes); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | // Read the byte array |
| | 88 | 84 | | bytes = new byte[length]; |
| | 88 | 85 | | await stream.ReadExactlyAsync(bytes); |
| | | 86 | | |
| | 88 | 87 | | if (BitConverter.IsLittleEndian) |
| | 88 | 88 | | Array.Reverse(bytes); |
| | | 89 | | |
| | | 90 | | // Convert the byte array to BitArray |
| | 88 | 91 | | return new FeatureSet { FeatureFlags = new BitArray(bytes) }; |
| | | 92 | | } |
| | 0 | 93 | | catch (Exception e) |
| | | 94 | | { |
| | 0 | 95 | | throw new SerializationException("Error deserializing Features", e); |
| | | 96 | | } |
| | 88 | 97 | | } |
| | | 98 | | } |