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.
 
 
 
 

33 lines
1.3 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using MQTTnet.Client.Receiving;
  4. namespace MQTTnet.Extensions.Rpc
  5. {
  6. public sealed class RpcAwareApplicationMessageReceivedHandler : IMqttApplicationMessageReceivedHandler
  7. {
  8. readonly Func<MqttApplicationMessageReceivedEventArgs, Task> _handleReceivedApplicationMessageAsync;
  9. public RpcAwareApplicationMessageReceivedHandler(
  10. IMqttApplicationMessageReceivedHandler originalHandler,
  11. Func<MqttApplicationMessageReceivedEventArgs, Task> handleReceivedApplicationMessageAsync)
  12. {
  13. OriginalHandler = originalHandler;
  14. _handleReceivedApplicationMessageAsync = handleReceivedApplicationMessageAsync ?? throw new ArgumentNullException(nameof(handleReceivedApplicationMessageAsync));
  15. }
  16. public IMqttApplicationMessageReceivedHandler OriginalHandler { get; }
  17. public async Task HandleApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs eventArgs)
  18. {
  19. // First try to check if there is a pending RPC call!
  20. await _handleReceivedApplicationMessageAsync(eventArgs).ConfigureAwait(false);
  21. if (OriginalHandler != null)
  22. {
  23. await OriginalHandler.HandleApplicationMessageReceivedAsync(eventArgs).ConfigureAwait(false);
  24. }
  25. }
  26. }
  27. }