| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using MQTTnet.Client.Receiving;
- using MQTTnet.Diagnostics;
- using System;
- using System.Threading.Tasks;
- namespace MQTTnet.Server
- {
- public class MqttServerEventDispatcher
- {
- readonly IMqttNetLogger _logger;
- public MqttServerEventDispatcher(IMqttNetLogger logger)
- {
- _logger = logger ?? throw new ArgumentNullException(nameof(logger));
- }
- public IMqttServerClientConnectedHandler ClientConnectedHandler { get; set; }
- public IMqttServerClientDisconnectedHandler ClientDisconnectedHandler { get; set; }
- public IMqttServerClientSubscribedTopicHandler ClientSubscribedTopicHandler { get; set; }
- public IMqttServerClientUnsubscribedTopicHandler ClientUnsubscribedTopicHandler { get; set; }
- public IMqttApplicationMessageReceivedHandler ApplicationMessageReceivedHandler { get; set; }
- public async Task SafeNotifyClientConnectedAsync(string clientId)
- {
- try
- {
- var handler = ClientConnectedHandler;
- if (handler == null)
- {
- return;
- }
- await handler.HandleClientConnectedAsync(new MqttServerClientConnectedEventArgs(clientId)).ConfigureAwait(false);
- }
- catch (Exception exception)
- {
- _logger.Error(exception, "Error while handling custom 'ClientConnected' event.");
- }
- }
- public async Task SafeNotifyClientDisconnectedAsync(string clientId, MqttClientDisconnectType disconnectType)
- {
- try
- {
- var handler = ClientDisconnectedHandler;
- if (handler == null)
- {
- return;
- }
- await handler.HandleClientDisconnectedAsync(new MqttServerClientDisconnectedEventArgs(clientId, disconnectType)).ConfigureAwait(false);
- }
- catch (Exception exception)
- {
- _logger.Error(exception, "Error while handling custom 'ClientDisconnected' event.");
- }
- }
- public async Task SafeNotifyClientSubscribedTopicAsync(string clientId, MqttTopicFilter topicFilter)
- {
- try
- {
- var handler = ClientSubscribedTopicHandler;
- if (handler == null)
- {
- return;
- }
- await handler.HandleClientSubscribedTopicAsync(new MqttServerClientSubscribedTopicEventArgs(clientId, topicFilter)).ConfigureAwait(false);
- }
- catch (Exception exception)
- {
- _logger.Error(exception, "Error while handling custom 'ClientSubscribedTopic' event.");
- }
- }
- public async Task SafeNotifyClientUnsubscribedTopicAsync(string clientId, string topicFilter)
- {
- try
- {
- var handler = ClientUnsubscribedTopicHandler;
- if (handler == null)
- {
- return;
- }
- await handler.HandleClientUnsubscribedTopicAsync(new MqttServerClientUnsubscribedTopicEventArgs(clientId, topicFilter)).ConfigureAwait(false);
- }
- catch (Exception exception)
- {
- _logger.Error(exception, "Error while handling custom 'ClientUnsubscribedTopic' event.");
- }
- }
- public async Task SafeNotifyApplicationMessageReceivedAsync(string senderClientId, MqttApplicationMessage applicationMessage)
- {
- try
- {
- var handler = ApplicationMessageReceivedHandler;
- if (handler == null)
- {
- return;
- }
- await handler.HandleApplicationMessageReceivedAsync(new MqttApplicationMessageReceivedEventArgs(senderClientId, applicationMessage)).ConfigureAwait(false); ;
- }
- catch (Exception exception)
- {
- _logger.Error(exception, "Error while handling custom 'ApplicationMessageReceived' event.");
- }
- }
- }
- }
|