| | 1 | | namespace NLightning.Domain.Protocol.Payloads; |
| | 2 | |
|
| | 3 | | using Crypto.Constants; |
| | 4 | | using Interfaces; |
| | 5 | | using Messages; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// The ping payload. |
| | 9 | | /// </summary> |
| | 10 | | /// <remarks> |
| | 11 | | /// The ping payload is used to check if the other party is still alive. |
| | 12 | | /// </remarks> |
| | 13 | | /// <seealso cref="PingMessage"/> |
| | 14 | | public class PingPayload : IMessagePayload |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// The maximum length of the ignored bytes. |
| | 18 | | /// </summary> |
| | 19 | | private const ushort MAX_LENGTH = 65531; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// The number of bytes to send in the pong message. |
| | 23 | | /// </summary> |
| 72 | 24 | | public ushort NumPongBytes { get; internal init; } |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// The number of bytes to ignore. |
| | 28 | | /// </summary> |
| 96 | 29 | | public ushort BytesLength { get; internal init; } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// The ignored bytes. |
| | 33 | | /// </summary> |
| 84 | 34 | | public byte[] Ignored { get; internal init; } |
| | 35 | |
|
| 36 | 36 | | public PingPayload() |
| | 37 | | { |
| 36 | 38 | | var randomGenerator = new Random(); |
| | 39 | | // Get number of bytes at random between HashConstants.SHA256_HASH_LEN and ushort.MaxValue |
| 36 | 40 | | NumPongBytes = (ushort)randomGenerator.Next(byte.MaxValue, MAX_LENGTH); |
| 36 | 41 | | BytesLength = (ushort)randomGenerator.Next(HashConstants.SHA256_HASH_LEN, 4 * HashConstants.SHA256_HASH_LEN); |
| | 42 | |
|
| 36 | 43 | | Ignored = new byte[BytesLength]; |
| 36 | 44 | | randomGenerator.NextBytes(Ignored); |
| 36 | 45 | | } |
| | 46 | | } |