| | 1 | | using Microsoft.Extensions.Options; |
| | 2 | |
|
| | 3 | | namespace NLightning.Infrastructure.Protocol.Services; |
| | 4 | |
|
| | 5 | | using Domain.Exceptions; |
| | 6 | | using Domain.Factories; |
| | 7 | | using Domain.Node.Options; |
| | 8 | | using Domain.Protocol.Messages; |
| | 9 | | using Domain.Protocol.Messages.Interfaces; |
| | 10 | | using Domain.Protocol.Services; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Service for managing the ping pong protocol. |
| | 14 | | /// </summary> |
| | 15 | | /// <remarks> |
| | 16 | | /// This class is used to manage the ping pong protocol. |
| | 17 | | /// </remarks> |
| | 18 | | internal class PingPongService : IPingPongService |
| | 19 | | { |
| | 20 | | private readonly IMessageFactory _messageFactory; |
| | 21 | | private readonly NodeOptions _nodeOptions; |
| 0 | 22 | | private readonly Random _random = new(); |
| | 23 | |
|
| 0 | 24 | | private TaskCompletionSource<bool> _pongReceivedTaskSource = new(); |
| | 25 | | private PingMessage _pingMessage; |
| | 26 | |
|
| | 27 | | /// <inheritdoc /> |
| | 28 | | public event EventHandler<IMessage>? PingMessageReadyEvent; |
| | 29 | |
|
| | 30 | | /// <inheritdoc /> |
| | 31 | | public event EventHandler<Exception>? DisconnectEvent; |
| | 32 | |
|
| 0 | 33 | | public PingPongService(IMessageFactory messageFactory, IOptions<NodeOptions> nodeOptions) |
| | 34 | | { |
| 0 | 35 | | _messageFactory = messageFactory; |
| 0 | 36 | | _nodeOptions = nodeOptions.Value; |
| 0 | 37 | | _pingMessage = (PingMessage)messageFactory.CreatePingMessage(); |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <inheritdoc /> |
| | 41 | | /// <remarks> |
| | 42 | | /// Ping messages are sent to the peer at random intervals ranging from 30 seconds to 5 minutes. |
| | 43 | | /// If a pong message is not received within the network timeout, DisconnectEvent is raised. |
| | 44 | | /// </remarks> |
| | 45 | | public async Task StartPingAsync(CancellationToken cancellationToken) |
| | 46 | | { |
| | 47 | | // Send the first ping message |
| 0 | 48 | | while (!cancellationToken.IsCancellationRequested) |
| | 49 | | { |
| 0 | 50 | | PingMessageReadyEvent?.Invoke(this, _pingMessage); |
| | 51 | |
|
| 0 | 52 | | using var pongTimeoutTokenSource = CancellationTokenSource |
| 0 | 53 | | .CreateLinkedTokenSource(cancellationToken, |
| 0 | 54 | | new CancellationTokenSource(_nodeOptions.NetworkTimeout).Token); |
| | 55 | |
|
| 0 | 56 | | var task = await Task.WhenAny(_pongReceivedTaskSource.Task, Task.Delay(-1, pongTimeoutTokenSource.Token)); |
| 0 | 57 | | if (task.IsFaulted) |
| | 58 | | { |
| 0 | 59 | | DisconnectEvent? |
| 0 | 60 | | .Invoke(this, new ConnectionException("Pong message not received within network timeout.")); |
| 0 | 61 | | return; |
| | 62 | | } |
| | 63 | |
|
| 0 | 64 | | if (task.IsCanceled) |
| | 65 | | { |
| 0 | 66 | | continue; |
| | 67 | | } |
| | 68 | |
|
| 0 | 69 | | await Task.Delay(_random.Next(30_000, 300_000), cancellationToken); |
| | 70 | |
|
| 0 | 71 | | _pongReceivedTaskSource = new TaskCompletionSource<bool>(); |
| 0 | 72 | | _pingMessage = (PingMessage)_messageFactory.CreatePingMessage(); |
| 0 | 73 | | } |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | /// <inheritdoc /> |
| | 77 | | /// <remarks> |
| | 78 | | /// Handles a pong message. |
| | 79 | | /// If the pong message has a different length than the ping message, DisconnectEvent is raised. |
| | 80 | | /// </remarks> |
| | 81 | | public void HandlePong(IMessage message) |
| | 82 | | { |
| | 83 | | // if the pong message has a different length than the ping message, disconnect |
| 0 | 84 | | if (message is not PongMessage pongMessage || |
| 0 | 85 | | pongMessage.Payload.BytesLength != _pingMessage.Payload.NumPongBytes) |
| | 86 | | { |
| 0 | 87 | | DisconnectEvent?.Invoke(this, new Exception("Pong message has different length than ping message.")); |
| 0 | 88 | | return; |
| | 89 | | } |
| | 90 | |
|
| 0 | 91 | | _pongReceivedTaskSource.TrySetResult(true); |
| 0 | 92 | | } |
| | 93 | | } |