| | 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 | | // If it's a global feature, cut out any bit greater than 13 |
| 192 | 29 | | if (asGlobal) |
| | 30 | | { |
| 12 | 31 | | featureSet.FeatureFlags.Length = 13; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | // Convert BitArray to byte array |
| 192 | 35 | | var bytes = new byte[(featureSet.FeatureFlags.Length + 7) / 8]; |
| 192 | 36 | | featureSet.FeatureFlags.CopyTo(bytes, 0); |
| | 37 | |
|
| | 38 | | // Set bytes as big endian |
| 192 | 39 | | if (BitConverter.IsLittleEndian) |
| | 40 | | { |
| 192 | 41 | | Array.Reverse(bytes); |
| | 42 | | } |
| | 43 | |
|
| | 44 | | // Trim leading zero bytes |
| 192 | 45 | | var leadingZeroBytes = 0; |
| 4740 | 46 | | foreach (var t in bytes) |
| | 47 | | { |
| 2272 | 48 | | if (t == 0) |
| | 49 | | { |
| 2084 | 50 | | leadingZeroBytes++; |
| | 51 | | } |
| | 52 | | else |
| | 53 | | { |
| | 54 | | break; |
| | 55 | | } |
| | 56 | | } |
| | 57 | |
|
| 192 | 58 | | var trimmedBytes = bytes[leadingZeroBytes..]; |
| | 59 | |
|
| | 60 | | // Write the length of the byte array or 1 if all bytes are zero |
| 192 | 61 | | if (includeLength) |
| | 62 | | { |
| 132 | 63 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian((ushort)trimmedBytes.Length)); |
| | 64 | | } |
| | 65 | |
|
| | 66 | | // Otherwise, return the array starting from the first non-zero byte |
| 192 | 67 | | await stream.WriteAsync(trimmedBytes); |
| 192 | 68 | | } |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Deserializes the features from a binary reader. |
| | 72 | | /// </summary> |
| | 73 | | /// <param name="stream">The stream to read from.</param> |
| | 74 | | /// <param name="includeLength">If the length of the byte array is included.</param> |
| | 75 | | /// <remarks> |
| | 76 | | /// If the length of the byte array is included, the first 2 bytes are read as the length of the byte array. |
| | 77 | | /// </remarks> |
| | 78 | | /// <returns>The deserialized features.</returns> |
| | 79 | | /// <exception cref="SerializationException">Error deserializing Features</exception> |
| | 80 | | public async Task<FeatureSet> DeserializeAsync(Stream stream, bool includeLength = true) |
| | 81 | | { |
| | 82 | | try |
| | 83 | | { |
| 88 | 84 | | var length = 8; |
| | 85 | |
|
| 88 | 86 | | var bytes = new byte[2]; |
| 88 | 87 | | if (includeLength) |
| | 88 | | { |
| | 89 | | // Read the length of the byte array |
| 84 | 90 | | await stream.ReadExactlyAsync(bytes); |
| 84 | 91 | | length = EndianBitConverter.ToUInt16BigEndian(bytes); |
| | 92 | | } |
| | 93 | |
|
| | 94 | | // Read the byte array |
| 88 | 95 | | bytes = new byte[length]; |
| 88 | 96 | | await stream.ReadExactlyAsync(bytes); |
| | 97 | |
|
| 88 | 98 | | if (BitConverter.IsLittleEndian) |
| | 99 | | { |
| 88 | 100 | | Array.Reverse(bytes); |
| | 101 | | } |
| | 102 | |
|
| | 103 | | // Convert the byte array to BitArray |
| 88 | 104 | | return new FeatureSet { FeatureFlags = new BitArray(bytes) }; |
| | 105 | | } |
| 0 | 106 | | catch (Exception e) |
| | 107 | | { |
| 0 | 108 | | throw new SerializationException("Error deserializing Features", e); |
| | 109 | | } |
| 88 | 110 | | } |
| | 111 | | } |