Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

MqttRpcClient.cs 5.6 KiB

6 lat temu
6 lat temu
6 lat temu
6 lat temu
6 lat temu
6 lat temu
6 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MQTTnet.Client;
  7. using MQTTnet.Exceptions;
  8. using MQTTnet.Protocol;
  9. namespace MQTTnet.Extensions.Rpc
  10. {
  11. public class MqttRpcClient : IDisposable
  12. {
  13. private readonly ConcurrentDictionary<string, TaskCompletionSource<byte[]>> _waitingCalls = new ConcurrentDictionary<string, TaskCompletionSource<byte[]>>();
  14. private readonly IMqttClient _mqttClient;
  15. private readonly RpcAwareApplicationMessageReceivedHandler _applicationMessageReceivedHandler;
  16. public MqttRpcClient(IMqttClient mqttClient)
  17. {
  18. _mqttClient = mqttClient ?? throw new ArgumentNullException(nameof(mqttClient));
  19. _applicationMessageReceivedHandler = new RpcAwareApplicationMessageReceivedHandler(
  20. mqttClient.ApplicationMessageReceivedHandler,
  21. HandleApplicationMessageReceivedAsync);
  22. _mqttClient.ApplicationMessageReceivedHandler = _applicationMessageReceivedHandler;
  23. }
  24. public Task<byte[]> ExecuteAsync(TimeSpan timeout, string methodName, string payload, MqttQualityOfServiceLevel qualityOfServiceLevel)
  25. {
  26. return ExecuteAsync(timeout, methodName, Encoding.UTF8.GetBytes(payload), qualityOfServiceLevel, CancellationToken.None);
  27. }
  28. public Task<byte[]> ExecuteAsync(TimeSpan timeout, string methodName, string payload, MqttQualityOfServiceLevel qualityOfServiceLevel, CancellationToken cancellationToken)
  29. {
  30. return ExecuteAsync(timeout, methodName, Encoding.UTF8.GetBytes(payload), qualityOfServiceLevel, cancellationToken);
  31. }
  32. public Task<byte[]> ExecuteAsync(TimeSpan timeout, string methodName, byte[] payload, MqttQualityOfServiceLevel qualityOfServiceLevel)
  33. {
  34. return ExecuteAsync(timeout, methodName, payload, qualityOfServiceLevel, CancellationToken.None);
  35. }
  36. public async Task<byte[]> ExecuteAsync(TimeSpan timeout, string methodName, byte[] payload, MqttQualityOfServiceLevel qualityOfServiceLevel, CancellationToken cancellationToken)
  37. {
  38. if (methodName == null) throw new ArgumentNullException(nameof(methodName));
  39. if (methodName.Contains("/") || methodName.Contains("+") || methodName.Contains("#"))
  40. {
  41. throw new ArgumentException("The method name cannot contain /, + or #.");
  42. }
  43. if (!(_mqttClient.ApplicationMessageReceivedHandler is RpcAwareApplicationMessageReceivedHandler))
  44. {
  45. throw new InvalidOperationException("The application message received handler was modified.");
  46. }
  47. var requestTopic = $"MQTTnet.RPC/{Guid.NewGuid():N}/{methodName}";
  48. var responseTopic = requestTopic + "/response";
  49. var requestMessage = new MqttApplicationMessageBuilder()
  50. .WithTopic(requestTopic)
  51. .WithPayload(payload)
  52. .WithQualityOfServiceLevel(qualityOfServiceLevel)
  53. .Build();
  54. try
  55. {
  56. var tcs = new TaskCompletionSource<byte[]>();
  57. if (!_waitingCalls.TryAdd(responseTopic, tcs))
  58. {
  59. throw new InvalidOperationException();
  60. }
  61. await _mqttClient.SubscribeAsync(responseTopic, qualityOfServiceLevel).ConfigureAwait(false);
  62. await _mqttClient.PublishAsync(requestMessage).ConfigureAwait(false);
  63. using (var timeoutCts = new CancellationTokenSource(timeout))
  64. using (var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token))
  65. {
  66. linkedCts.Token.Register(() =>
  67. {
  68. if (!tcs.Task.IsCompleted && !tcs.Task.IsFaulted && !tcs.Task.IsCanceled)
  69. {
  70. tcs.TrySetCanceled();
  71. }
  72. });
  73. try
  74. {
  75. var result = await tcs.Task.ConfigureAwait(false);
  76. timeoutCts.Cancel(false);
  77. return result;
  78. }
  79. catch (OperationCanceledException exception)
  80. {
  81. if (timeoutCts.IsCancellationRequested && !cancellationToken.IsCancellationRequested)
  82. {
  83. throw new MqttCommunicationTimedOutException(exception);
  84. }
  85. else
  86. {
  87. throw;
  88. }
  89. }
  90. }
  91. }
  92. finally
  93. {
  94. _waitingCalls.TryRemove(responseTopic, out _);
  95. await _mqttClient.UnsubscribeAsync(responseTopic).ConfigureAwait(false);
  96. }
  97. }
  98. private Task HandleApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs eventArgs)
  99. {
  100. if (!_waitingCalls.TryRemove(eventArgs.ApplicationMessage.Topic, out var tcs))
  101. {
  102. return Task.FromResult(0);
  103. }
  104. tcs.TrySetResult(eventArgs.ApplicationMessage.Payload);
  105. return Task.FromResult(0);
  106. }
  107. public void Dispose()
  108. {
  109. _mqttClient.ApplicationMessageReceivedHandler = _applicationMessageReceivedHandler.OriginalHandler;
  110. foreach (var tcs in _waitingCalls)
  111. {
  112. tcs.Value.TrySetCanceled();
  113. }
  114. _waitingCalls.Clear();
  115. }
  116. }
  117. }