MqttServerEventDispatcher.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using MQTTnet.Client.Receiving;
  2. using MQTTnet.Diagnostics;
  3. using System;
  4. using System.Threading.Tasks;
  5. namespace MQTTnet.Server
  6. {
  7. public class MqttServerEventDispatcher
  8. {
  9. readonly IMqttNetLogger _logger;
  10. public MqttServerEventDispatcher(IMqttNetLogger logger)
  11. {
  12. _logger = logger ?? throw new ArgumentNullException(nameof(logger));
  13. }
  14. public IMqttServerClientConnectedHandler ClientConnectedHandler { get; set; }
  15. public IMqttServerClientDisconnectedHandler ClientDisconnectedHandler { get; set; }
  16. public IMqttServerClientSubscribedTopicHandler ClientSubscribedTopicHandler { get; set; }
  17. public IMqttServerClientUnsubscribedTopicHandler ClientUnsubscribedTopicHandler { get; set; }
  18. public IMqttApplicationMessageReceivedHandler ApplicationMessageReceivedHandler { get; set; }
  19. public async Task SafeNotifyClientConnectedAsync(string clientId)
  20. {
  21. try
  22. {
  23. var handler = ClientConnectedHandler;
  24. if (handler == null)
  25. {
  26. return;
  27. }
  28. await handler.HandleClientConnectedAsync(new MqttServerClientConnectedEventArgs(clientId)).ConfigureAwait(false);
  29. }
  30. catch (Exception exception)
  31. {
  32. _logger.Error(exception, "Error while handling custom 'ClientConnected' event.");
  33. }
  34. }
  35. public async Task SafeNotifyClientDisconnectedAsync(string clientId, MqttClientDisconnectType disconnectType)
  36. {
  37. try
  38. {
  39. var handler = ClientDisconnectedHandler;
  40. if (handler == null)
  41. {
  42. return;
  43. }
  44. await handler.HandleClientDisconnectedAsync(new MqttServerClientDisconnectedEventArgs(clientId, disconnectType)).ConfigureAwait(false);
  45. }
  46. catch (Exception exception)
  47. {
  48. _logger.Error(exception, "Error while handling custom 'ClientDisconnected' event.");
  49. }
  50. }
  51. public async Task SafeNotifyClientSubscribedTopicAsync(string clientId, MqttTopicFilter topicFilter)
  52. {
  53. try
  54. {
  55. var handler = ClientSubscribedTopicHandler;
  56. if (handler == null)
  57. {
  58. return;
  59. }
  60. await handler.HandleClientSubscribedTopicAsync(new MqttServerClientSubscribedTopicEventArgs(clientId, topicFilter)).ConfigureAwait(false);
  61. }
  62. catch (Exception exception)
  63. {
  64. _logger.Error(exception, "Error while handling custom 'ClientSubscribedTopic' event.");
  65. }
  66. }
  67. public async Task SafeNotifyClientUnsubscribedTopicAsync(string clientId, string topicFilter)
  68. {
  69. try
  70. {
  71. var handler = ClientUnsubscribedTopicHandler;
  72. if (handler == null)
  73. {
  74. return;
  75. }
  76. await handler.HandleClientUnsubscribedTopicAsync(new MqttServerClientUnsubscribedTopicEventArgs(clientId, topicFilter)).ConfigureAwait(false);
  77. }
  78. catch (Exception exception)
  79. {
  80. _logger.Error(exception, "Error while handling custom 'ClientUnsubscribedTopic' event.");
  81. }
  82. }
  83. public async Task SafeNotifyApplicationMessageReceivedAsync(string senderClientId, MqttApplicationMessage applicationMessage)
  84. {
  85. try
  86. {
  87. var handler = ApplicationMessageReceivedHandler;
  88. if (handler == null)
  89. {
  90. return;
  91. }
  92. await handler.HandleApplicationMessageReceivedAsync(new MqttApplicationMessageReceivedEventArgs(senderClientId, applicationMessage)).ConfigureAwait(false); ;
  93. }
  94. catch (Exception exception)
  95. {
  96. _logger.Error(exception, "Error while handling custom 'ApplicationMessageReceived' event.");
  97. }
  98. }
  99. }
  100. }