Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

145 linhas
5.8 KiB

  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. if (tcs.Task.IsCompleted || tcs.Task.IsCanceled)
  105. {
  106. return Task.FromResult(0);
  107. }
  108. tcs.TrySetResult(eventArgs.ApplicationMessage.Payload);
  109. return Task.FromResult(0);
  110. }
  111. public void Dispose()
  112. {
  113. _mqttClient.ApplicationMessageReceivedHandler = _applicationMessageReceivedHandler.OriginalHandler;
  114. foreach (var tcs in _waitingCalls)
  115. {
  116. tcs.Value.TrySetCanceled();
  117. }
  118. _waitingCalls.Clear();
  119. }
  120. }
  121. }