diff --git a/Source/MQTTnet.Extensions.ManagedClient/IManagedMqttClient.cs b/Source/MQTTnet.Extensions.ManagedClient/IManagedMqttClient.cs index 12a6560..644104f 100644 --- a/Source/MQTTnet.Extensions.ManagedClient/IManagedMqttClient.cs +++ b/Source/MQTTnet.Extensions.ManagedClient/IManagedMqttClient.cs @@ -13,7 +13,12 @@ namespace MQTTnet.Extensions.ManagedClient int PendingApplicationMessagesCount { get; } IManagedMqttClientOptions Options { get; } + IMqttClientConnectedHandler ConnectedHandler { get; set; } + [Obsolete("Use ConnectedHandler instead.")] event EventHandler Connected; + + IMqttClientDisconnectedHandler DisconnectedHandler { get; set; } + [Obsolete("Use DisconnectedHandler instead.")] event EventHandler Disconnected; event EventHandler ApplicationMessageProcessed; @@ -21,7 +26,7 @@ namespace MQTTnet.Extensions.ManagedClient event EventHandler ConnectingFailed; event EventHandler SynchronizingSubscriptionsFailed; - + Task StartAsync(IManagedMqttClientOptions options); Task StopAsync(); diff --git a/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs b/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs index b4eec1c..54750a1 100644 --- a/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs +++ b/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs @@ -4,6 +4,8 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using MQTTnet.Client; +using MQTTnet.Client.Connecting; +using MQTTnet.Client.Disconnecting; using MQTTnet.Client.Publishing; using MQTTnet.Client.Receiving; using MQTTnet.Diagnostics; @@ -38,9 +40,9 @@ namespace MQTTnet.Extensions.ManagedClient _mqttClient = mqttClient ?? throw new ArgumentNullException(nameof(mqttClient)); - _mqttClient.Connected += OnConnected; - _mqttClient.Disconnected += OnDisconnected; - _mqttClient.ApplicationMessageReceived += OnApplicationMessageReceived; + _mqttClient.UseConnectedHandler(OnConnected); + _mqttClient.UseDisconnectedHandler(OnDisconnected); + _mqttClient.UseReceivedApplicationMessageHandler(OnApplicationMessageReceived); _logger = logger.CreateChildLogger(nameof(ManagedMqttClient)); } @@ -50,9 +52,13 @@ namespace MQTTnet.Extensions.ManagedClient public int PendingApplicationMessagesCount => _messageQueue.Count; public IManagedMqttClientOptions Options { get; private set; } + public IMqttClientConnectedHandler ConnectedHandler { get; set; } public event EventHandler Connected; + + public IMqttClientDisconnectedHandler DisconnectedHandler { get; set; } public event EventHandler Disconnected; + public IMqttApplicationMessageHandler ReceivedApplicationMessageHandler { get => _mqttClient.ReceivedApplicationMessageHandler; @@ -415,19 +421,22 @@ namespace MQTTnet.Extensions.ManagedClient } } - private void OnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs eventArgs) + private Task OnApplicationMessageReceived(MqttApplicationMessageHandlerContext context) { - ApplicationMessageReceived?.Invoke(this, eventArgs); + ApplicationMessageReceived?.Invoke(this, new MqttApplicationMessageReceivedEventArgs(context.SenderClientId, context.ApplicationMessage)); + return Task.FromResult(0); } - private void OnDisconnected(object sender, MqttClientDisconnectedEventArgs eventArgs) + private Task OnDisconnected(MqttClientDisconnectedEventArgs eventArgs) { Disconnected?.Invoke(this, eventArgs); + return DisconnectedHandler?.HandleDisconnectedAsync(eventArgs); } - private void OnConnected(object sender, MqttClientConnectedEventArgs eventArgs) + private Task OnConnected(MqttClientConnectedEventArgs eventArgs) { Connected?.Invoke(this, eventArgs); + return ConnectedHandler?.HandleConnectedAsync(eventArgs); } private void StartPublishing() diff --git a/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClientExtensions.cs b/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClientExtensions.cs index 9a63565..50790d4 100644 --- a/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClientExtensions.cs +++ b/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClientExtensions.cs @@ -2,13 +2,66 @@ using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using MQTTnet.Client.Connecting; +using MQTTnet.Client.Disconnecting; using MQTTnet.Client.Publishing; +using MQTTnet.Client.Receiving; using MQTTnet.Protocol; namespace MQTTnet.Extensions.ManagedClient { public static class ManagedMqttClientExtensions { + public static IManagedMqttClient UseConnectedHandler(this IManagedMqttClient client, Func handler) + { + if (handler == null) + { + client.ConnectedHandler = null; + return client; + } + + client.ConnectedHandler = new MqttClientConnectedHandlerDelegate(handler); + return client; + } + + public static IManagedMqttClient UseDisconnectedHandler(this IManagedMqttClient client, Func handler) + { + if (handler == null) + { + client.DisconnectedHandler = null; + return client; + } + + client.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(handler); + return client; + } + + public static IManagedMqttClient UseReceivedApplicationMessageHandler(this IManagedMqttClient client, Func handler) + { + if (handler == null) + { + client.ReceivedApplicationMessageHandler = null; + return client; + } + + client.ReceivedApplicationMessageHandler = new MqttApplicationMessageHandlerDelegate(handler); + + return client; + } + + public static IManagedMqttClient UseReceivedApplicationMessageHandler(this IManagedMqttClient client, Action handler) + { + if (handler == null) + { + client.ReceivedApplicationMessageHandler = null; + return client; + } + + client.ReceivedApplicationMessageHandler = new MqttApplicationMessageHandlerDelegate(handler); + + return client; + } + public static Task SubscribeAsync(this IManagedMqttClient managedClient, params TopicFilter[] topicFilters) { if (managedClient == null) throw new ArgumentNullException(nameof(managedClient)); diff --git a/Source/MQTTnet/Client/Connecting/IMqttClientConnectedHandler.cs b/Source/MQTTnet/Client/Connecting/IMqttClientConnectedHandler.cs new file mode 100644 index 0000000..8b7be27 --- /dev/null +++ b/Source/MQTTnet/Client/Connecting/IMqttClientConnectedHandler.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace MQTTnet.Client.Connecting +{ + public interface IMqttClientConnectedHandler + { + Task HandleConnectedAsync(MqttClientConnectedEventArgs eventArgs); + } +} diff --git a/Source/MQTTnet/Client/Connecting/MqttClientConnectedHandlerDelegate.cs b/Source/MQTTnet/Client/Connecting/MqttClientConnectedHandlerDelegate.cs new file mode 100644 index 0000000..96812af --- /dev/null +++ b/Source/MQTTnet/Client/Connecting/MqttClientConnectedHandlerDelegate.cs @@ -0,0 +1,31 @@ +using System; +using System.Threading.Tasks; + +namespace MQTTnet.Client.Connecting +{ + public class MqttClientConnectedHandlerDelegate : IMqttClientConnectedHandler + { + private readonly Func _handler; + + public MqttClientConnectedHandlerDelegate(Action handler) + { + if (handler == null) throw new ArgumentNullException(nameof(handler)); + + _handler = context => + { + handler(context); + return Task.FromResult(0); + }; + } + + public MqttClientConnectedHandlerDelegate(Func handler) + { + _handler = handler ?? throw new ArgumentNullException(nameof(handler)); + } + + public Task HandleConnectedAsync(MqttClientConnectedEventArgs eventArgs) + { + return _handler(eventArgs); + } + } +} diff --git a/Source/MQTTnet/Client/Disconnecting/IMqttClientDisconnectedHandler.cs b/Source/MQTTnet/Client/Disconnecting/IMqttClientDisconnectedHandler.cs new file mode 100644 index 0000000..14347bf --- /dev/null +++ b/Source/MQTTnet/Client/Disconnecting/IMqttClientDisconnectedHandler.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace MQTTnet.Client.Disconnecting +{ + public interface IMqttClientDisconnectedHandler + { + Task HandleDisconnectedAsync(MqttClientDisconnectedEventArgs eventArgs); + } +} diff --git a/Source/MQTTnet/Client/Disconnecting/MqttClientDisconnectedHandlerDelegate.cs b/Source/MQTTnet/Client/Disconnecting/MqttClientDisconnectedHandlerDelegate.cs new file mode 100644 index 0000000..c02443e --- /dev/null +++ b/Source/MQTTnet/Client/Disconnecting/MqttClientDisconnectedHandlerDelegate.cs @@ -0,0 +1,31 @@ +using System; +using System.Threading.Tasks; + +namespace MQTTnet.Client.Disconnecting +{ + public class MqttClientDisconnectedHandlerDelegate : IMqttClientDisconnectedHandler + { + private readonly Func _handler; + + public MqttClientDisconnectedHandlerDelegate(Action handler) + { + if (handler == null) throw new ArgumentNullException(nameof(handler)); + + _handler = context => + { + handler(context); + return Task.FromResult(0); + }; + } + + public MqttClientDisconnectedHandlerDelegate(Func handler) + { + _handler = handler ?? throw new ArgumentNullException(nameof(handler)); + } + + public Task HandleDisconnectedAsync(MqttClientDisconnectedEventArgs eventArgs) + { + return _handler(eventArgs); + } + } +} diff --git a/Source/MQTTnet/Client/IMqttClient.cs b/Source/MQTTnet/Client/IMqttClient.cs index 0e00f00..4e25e9c 100644 --- a/Source/MQTTnet/Client/IMqttClient.cs +++ b/Source/MQTTnet/Client/IMqttClient.cs @@ -14,7 +14,12 @@ namespace MQTTnet.Client bool IsConnected { get; } IMqttClientOptions Options { get; } + IMqttClientConnectedHandler ConnectedHandler { get; set; } + [Obsolete("Use ConnectedHandler instead.")] event EventHandler Connected; + + IMqttClientDisconnectedHandler DisconnectedHandler { get; set; } + [Obsolete("Use DisconnectedHandler instead.")] event EventHandler Disconnected; Task ConnectAsync(IMqttClientOptions options, CancellationToken cancellationToken); diff --git a/Source/MQTTnet/Client/MqttClient.cs b/Source/MQTTnet/Client/MqttClient.cs index 97cd3f2..6cf248d 100644 --- a/Source/MQTTnet/Client/MqttClient.cs +++ b/Source/MQTTnet/Client/MqttClient.cs @@ -44,8 +44,12 @@ namespace MQTTnet.Client _logger = logger.CreateChildLogger(nameof(MqttClient)); } + public IMqttClientConnectedHandler ConnectedHandler { get; set; } + public event EventHandler Connected; + public IMqttClientDisconnectedHandler DisconnectedHandler { get; set; } + public event EventHandler Disconnected; public IMqttApplicationMessageHandler ReceivedApplicationMessageHandler { get; set; } @@ -93,10 +97,12 @@ namespace MQTTnet.Client } IsConnected = true; - Connected?.Invoke(this, new MqttClientConnectedEventArgs(authenticateResult)); _logger.Info("Connected."); + Connected?.Invoke(this, new MqttClientConnectedEventArgs(authenticateResult)); + await ConnectedHandler?.HandleConnectedAsync(new MqttClientConnectedEventArgs(authenticateResult)); + return authenticateResult; } catch (Exception exception) @@ -259,6 +265,7 @@ namespace MQTTnet.Client _logger.Info("Disconnected."); Disconnected?.Invoke(this, new MqttClientDisconnectedEventArgs(clientWasConnected, exception)); + await DisconnectedHandler?.HandleDisconnectedAsync(new MqttClientDisconnectedEventArgs(clientWasConnected, exception)); } } diff --git a/Source/MQTTnet/Client/MqttClientExtensions.cs b/Source/MQTTnet/Client/MqttClientExtensions.cs index 752763d..de1a77b 100644 --- a/Source/MQTTnet/Client/MqttClientExtensions.cs +++ b/Source/MQTTnet/Client/MqttClientExtensions.cs @@ -15,6 +15,30 @@ namespace MQTTnet.Client { public static class MqttClientExtensions { + public static IMqttClient UseConnectedHandler(this IMqttClient client, Func handler) + { + if (handler == null) + { + client.ConnectedHandler = null; + return client; + } + + client.ConnectedHandler = new MqttClientConnectedHandlerDelegate(handler); + return client; + } + + public static IMqttClient UseDisconnectedHandler(this IMqttClient client, Func handler) + { + if (handler == null) + { + client.DisconnectedHandler = null; + return client; + } + + client.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(handler); + return client; + } + public static IMqttClient UseReceivedApplicationMessageHandler(this IMqttClient client, Func handler) { if (handler == null) @@ -143,7 +167,7 @@ namespace MQTTnet.Client await client.PublishAsync(applicationMessage).ConfigureAwait(false); } } - + public static Task PublishAsync(this IMqttClient client, string topic) { if (client == null) throw new ArgumentNullException(nameof(client)); @@ -175,7 +199,7 @@ namespace MQTTnet.Client .WithPayload(payload) .Build()); } - + public static Task PublishAsync(this IMqttClient client, string topic, string payload, MqttQualityOfServiceLevel qualityOfServiceLevel) { if (client == null) throw new ArgumentNullException(nameof(client));