| | 1 | | namespace NLightning.Domain.Protocol.Tlv; |
| | 2 | |
|
| | 3 | | using ValueObjects; |
| | 4 | |
|
| | 5 | | public class BaseTlv |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// The type of the TLV |
| | 9 | | /// </summary> |
| 728 | 10 | | public BigSize Type { get; } |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// The length of the value |
| | 14 | | /// </summary> |
| 1108 | 15 | | public BigSize Length { get; protected init; } |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// The value |
| | 19 | | /// </summary> |
| 1312 | 20 | | public byte[] Value { get; internal set; } |
| | 21 | |
|
| 448 | 22 | | public BaseTlv(BigSize type, BigSize length, byte[] value) |
| | 23 | | { |
| 448 | 24 | | Type = type; |
| 448 | 25 | | Length = length; |
| 448 | 26 | | Value = value; |
| 448 | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Create a new TLV |
| | 31 | | /// </summary> |
| | 32 | | /// <param name="type">The type of the TLV</param> |
| | 33 | | /// <param name="value">The value of the TLV</param> |
| | 34 | | /// <remarks> |
| | 35 | | /// The length of the value is automatically calculated |
| | 36 | | /// </remarks> |
| 372 | 37 | | public BaseTlv(BigSize type, byte[] value) : this(type, value.Length, value) |
| 372 | 38 | | { } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Create a new TLV |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="type">The type of the TLV</param> |
| | 44 | | /// <remarks> |
| | 45 | | /// Used internally to create a TLV with a length of 0 and an empty value. |
| | 46 | | /// </remarks> |
| 292 | 47 | | internal BaseTlv(BigSize type) : this(type, []) |
| 292 | 48 | | { } |
| | 49 | |
|
| | 50 | | /// <inheritdoc /> |
| | 51 | | public override int GetHashCode() |
| | 52 | | { |
| | 53 | | // ReSharper disable once NonReadonlyMemberInGetHashCode |
| 0 | 54 | | return HashCode.Combine(Type, Length, Value.GetHashCode()); |
| | 55 | | } |
| | 56 | |
|
| | 57 | | /// <inheritdoc /> |
| | 58 | | public override bool Equals(object? obj) |
| | 59 | | { |
| 100 | 60 | | return obj is BaseTlv tlv && Equals(tlv); |
| | 61 | | } |
| | 62 | |
|
| | 63 | | private bool Equals(BaseTlv other) |
| | 64 | | { |
| 100 | 65 | | return Type.Equals(other.Type) && Length.Equals(other.Length) && Value.SequenceEqual(other.Value); |
| | 66 | | } |
| | 67 | |
|
| | 68 | | public static bool operator ==(BaseTlv left, BaseTlv right) |
| | 69 | | { |
| 0 | 70 | | return left.Equals(right); |
| | 71 | | } |
| | 72 | |
|
| | 73 | | public static bool operator !=(BaseTlv left, BaseTlv right) |
| | 74 | | { |
| 0 | 75 | | return !(left == right); |
| | 76 | | } |
| | 77 | | } |