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.
 
 
 
 

146 lines
5.4 KiB

  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using MQTTnet.Client;
  5. using MQTTnet.Exceptions;
  6. using MQTTnet.Protocol;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using MQTTnet.Implementations;
  12. namespace MQTTnet.Extensions.Rpc
  13. {
  14. public sealed class MqttRpcClient : IDisposable
  15. {
  16. readonly ConcurrentDictionary<string, TaskCompletionSource<byte[]>> _waitingCalls = new ConcurrentDictionary<string, TaskCompletionSource<byte[]>>();
  17. readonly MqttClient _mqttClient;
  18. readonly MqttRpcClientOptions _options;
  19. public MqttRpcClient(MqttClient mqttClient, MqttRpcClientOptions options)
  20. {
  21. _mqttClient = mqttClient ?? throw new ArgumentNullException(nameof(mqttClient));
  22. _options = options ?? throw new ArgumentNullException(nameof(options));
  23. _mqttClient.ApplicationMessageReceivedAsync += HandleApplicationMessageReceivedAsync;
  24. }
  25. public async Task<byte[]> ExecuteAsync(TimeSpan timeout, string methodName, byte[] payload, MqttQualityOfServiceLevel qualityOfServiceLevel)
  26. {
  27. using (var timeoutToken = new CancellationTokenSource(timeout))
  28. {
  29. try
  30. {
  31. return await ExecuteAsync(methodName, payload, qualityOfServiceLevel, timeoutToken.Token).ConfigureAwait(false);
  32. }
  33. catch (OperationCanceledException exception)
  34. {
  35. if (timeoutToken.IsCancellationRequested)
  36. {
  37. throw new MqttCommunicationTimedOutException(exception);
  38. }
  39. throw;
  40. }
  41. }
  42. }
  43. public async Task<byte[]> ExecuteAsync(string methodName, byte[] payload, MqttQualityOfServiceLevel qualityOfServiceLevel, CancellationToken cancellationToken)
  44. {
  45. if (methodName == null) throw new ArgumentNullException(nameof(methodName));
  46. var topicNames = _options.TopicGenerationStrategy.CreateRpcTopics(new TopicGenerationContext
  47. {
  48. MethodName = methodName,
  49. QualityOfServiceLevel = qualityOfServiceLevel,
  50. MqttClient = _mqttClient,
  51. Options = _options
  52. });
  53. var requestTopic = topicNames.RequestTopic;
  54. var responseTopic = topicNames.ResponseTopic;
  55. if (string.IsNullOrWhiteSpace(requestTopic))
  56. {
  57. throw new MqttProtocolViolationException("RPC request topic is empty.");
  58. }
  59. if (string.IsNullOrWhiteSpace(responseTopic))
  60. {
  61. throw new MqttProtocolViolationException("RPC response topic is empty.");
  62. }
  63. var requestMessage = new MqttApplicationMessageBuilder()
  64. .WithTopic(requestTopic)
  65. .WithPayload(payload)
  66. .WithQualityOfServiceLevel(qualityOfServiceLevel)
  67. .WithResponseTopic(responseTopic)
  68. .Build();
  69. try
  70. {
  71. #if NET452
  72. var awaitable = new TaskCompletionSource<byte[]>();
  73. #else
  74. var awaitable = new TaskCompletionSource<byte[]>(TaskCreationOptions.RunContinuationsAsynchronously);
  75. #endif
  76. if (!_waitingCalls.TryAdd(responseTopic, awaitable))
  77. {
  78. throw new InvalidOperationException();
  79. }
  80. var subscribeOptions = new MqttClientSubscribeOptionsBuilder()
  81. .WithTopicFilter(responseTopic, qualityOfServiceLevel)
  82. .Build();
  83. await _mqttClient.SubscribeAsync(subscribeOptions, cancellationToken).ConfigureAwait(false);
  84. await _mqttClient.PublishAsync(requestMessage, cancellationToken).ConfigureAwait(false);
  85. using (cancellationToken.Register(() => { awaitable.TrySetCanceled(); }))
  86. {
  87. return await awaitable.Task.ConfigureAwait(false);
  88. }
  89. }
  90. finally
  91. {
  92. _waitingCalls.TryRemove(responseTopic, out _);
  93. await _mqttClient.UnsubscribeAsync(responseTopic).ConfigureAwait(false);
  94. }
  95. }
  96. Task HandleApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs eventArgs)
  97. {
  98. if (!_waitingCalls.TryRemove(eventArgs.ApplicationMessage.Topic, out var awaitable))
  99. {
  100. return PlatformAbstractionLayer.CompletedTask;
  101. }
  102. #if NET452
  103. Task.Run(() => awaitable.TrySetResult(eventArgs.ApplicationMessage.Payload));
  104. #else
  105. awaitable.TrySetResult(eventArgs.ApplicationMessage.Payload);
  106. #endif
  107. // Set this message to handled to that other code can avoid execution etc.
  108. eventArgs.IsHandled = true;
  109. return PlatformAbstractionLayer.CompletedTask;
  110. }
  111. public void Dispose()
  112. {
  113. _mqttClient.ApplicationMessageReceivedAsync -= HandleApplicationMessageReceivedAsync;
  114. foreach (var tcs in _waitingCalls)
  115. {
  116. tcs.Value.TrySetCanceled();
  117. }
  118. _waitingCalls.Clear();
  119. }
  120. }
  121. }