You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

98 rivejä
3.2 KiB

  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using MQTTnet.Diagnostics;
  6. using MQTTnet.Packets;
  7. namespace MQTTnet.Server
  8. {
  9. public sealed class MqttClientKeepAliveMonitor
  10. {
  11. private readonly Stopwatch _lastPacketReceivedTracker = new Stopwatch();
  12. private readonly Stopwatch _lastNonKeepAlivePacketReceivedTracker = new Stopwatch();
  13. private readonly string _clientId;
  14. private readonly Action _timeoutCallback;
  15. private readonly IMqttNetChildLogger _logger;
  16. private Task _workerTask;
  17. public MqttClientKeepAliveMonitor(string clientId, Action timeoutCallback, IMqttNetChildLogger logger)
  18. {
  19. if (logger == null) throw new ArgumentNullException(nameof(logger));
  20. _clientId = clientId;
  21. _timeoutCallback = timeoutCallback;
  22. _logger = logger.CreateChildLogger(nameof(MqttClientKeepAliveMonitor));
  23. }
  24. public TimeSpan LastPacketReceived => _lastPacketReceivedTracker.Elapsed;
  25. public TimeSpan LastNonKeepAlivePacketReceived => _lastNonKeepAlivePacketReceivedTracker.Elapsed;
  26. public void Start(int keepAlivePeriod, CancellationToken cancellationToken)
  27. {
  28. if (keepAlivePeriod == 0)
  29. {
  30. return;
  31. }
  32. _workerTask = Task.Run(() => RunAsync(keepAlivePeriod, cancellationToken).ConfigureAwait(false), cancellationToken);
  33. }
  34. public void WaitForCompletion()
  35. {
  36. if (_workerTask != null)
  37. {
  38. Task.WaitAll(_workerTask);
  39. }
  40. }
  41. private async Task RunAsync(int keepAlivePeriod, CancellationToken cancellationToken)
  42. {
  43. try
  44. {
  45. _lastPacketReceivedTracker.Restart();
  46. _lastNonKeepAlivePacketReceivedTracker.Restart();
  47. while (!cancellationToken.IsCancellationRequested)
  48. {
  49. // Values described here: [MQTT-3.1.2-24].
  50. if (_lastPacketReceivedTracker.Elapsed.TotalSeconds > keepAlivePeriod * 1.5D)
  51. {
  52. _logger.Warning(null, "Client '{0}': Did not receive any packet or keep alive signal.", _clientId);
  53. _timeoutCallback?.Invoke();
  54. return;
  55. }
  56. await Task.Delay(keepAlivePeriod, cancellationToken).ConfigureAwait(false);
  57. }
  58. }
  59. catch (OperationCanceledException)
  60. {
  61. }
  62. catch (Exception exception)
  63. {
  64. _logger.Error(exception, "Client '{0}': Unhandled exception while checking keep alive timeouts.", _clientId);
  65. }
  66. finally
  67. {
  68. _logger.Verbose("Client {0}: Stopped checking keep alive timeout.", _clientId);
  69. }
  70. }
  71. public void PacketReceived(MqttBasePacket packet)
  72. {
  73. _lastPacketReceivedTracker.Restart();
  74. if (!(packet is MqttPingReqPacket))
  75. {
  76. _lastNonKeepAlivePacketReceivedTracker.Restart();
  77. }
  78. }
  79. }
  80. }