| | | 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 MaxLength = 1000; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// The number of bytes to send in the pong message. |
| | | 23 | | /// </summary> |
| | 44 | 24 | | public ushort NumPongBytes { get; internal init; } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// The number of bytes to ignore. |
| | | 28 | | /// </summary> |
| | 64 | 29 | | public ushort BytesLength { get; internal init; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// The ignored bytes. |
| | | 33 | | /// </summary> |
| | 52 | 34 | | public byte[] Ignored { get; internal init; } |
| | | 35 | | |
| | 20 | 36 | | public PingPayload() |
| | | 37 | | { |
| | 20 | 38 | | var randomGenerator = new Random(); |
| | | 39 | | // Get number of bytes at random between HashConstants.SHA256_HASH_LEN and ushort.MaxValue |
| | 20 | 40 | | NumPongBytes = (ushort)randomGenerator.Next(byte.MaxValue, MaxLength); |
| | 20 | 41 | | BytesLength = (ushort)randomGenerator.Next(CryptoConstants.Sha256HashLen, 4 * CryptoConstants.Sha256HashLen); |
| | | 42 | | |
| | 20 | 43 | | Ignored = new byte[BytesLength]; |
| | 20 | 44 | | randomGenerator.NextBytes(Ignored); |
| | 20 | 45 | | } |
| | | 46 | | } |