From 21a9974e5d9df9847885dae018292b6522fa43a6 Mon Sep 17 00:00:00 2001 From: SeppPenner Date: Mon, 21 Sep 2020 18:23:11 +0200 Subject: [PATCH 1/2] Tried to add a MqttServerOptions fluent method for the client disconnected handler. --- Source/MQTTnet/Server/IMqttServerOptions.cs | 2 ++ ...qttServerClientDisconnectedHandlerDelegate.cs | 16 ++++++++-------- Source/MQTTnet/Server/MqttServerOptions.cs | 2 ++ .../MQTTnet/Server/MqttServerOptionsBuilder.cs | 12 ++++++++++++ 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/Source/MQTTnet/Server/IMqttServerOptions.cs b/Source/MQTTnet/Server/IMqttServerOptions.cs index 61900ed..05dab5e 100644 --- a/Source/MQTTnet/Server/IMqttServerOptions.cs +++ b/Source/MQTTnet/Server/IMqttServerOptions.cs @@ -27,5 +27,7 @@ namespace MQTTnet.Server IMqttRetainedMessagesManager RetainedMessagesManager { get; } IMqttServerApplicationMessageInterceptor UndeliveredMessageInterceptor { get; set; } + + IMqttServerClientDisconnectedHandler ClientDisconnectedInterceptor { get; set; } } } \ No newline at end of file diff --git a/Source/MQTTnet/Server/MqttServerClientDisconnectedHandlerDelegate.cs b/Source/MQTTnet/Server/MqttServerClientDisconnectedHandlerDelegate.cs index b1d495a..1ca9464 100644 --- a/Source/MQTTnet/Server/MqttServerClientDisconnectedHandlerDelegate.cs +++ b/Source/MQTTnet/Server/MqttServerClientDisconnectedHandlerDelegate.cs @@ -5,27 +5,27 @@ namespace MQTTnet.Server { public class MqttServerClientDisconnectedHandlerDelegate : IMqttServerClientDisconnectedHandler { - private readonly Func _handler; + private readonly Func _callback; - public MqttServerClientDisconnectedHandlerDelegate(Action handler) + public MqttServerClientDisconnectedHandlerDelegate(Action callback) { - if (handler == null) throw new ArgumentNullException(nameof(handler)); + if (callback == null) throw new ArgumentNullException(nameof(callback)); - _handler = eventArgs => + _callback = eventArgs => { - handler(eventArgs); + callback(eventArgs); return Task.FromResult(0); }; } - public MqttServerClientDisconnectedHandlerDelegate(Func handler) + public MqttServerClientDisconnectedHandlerDelegate(Func callback) { - _handler = handler ?? throw new ArgumentNullException(nameof(handler)); + _callback = callback ?? throw new ArgumentNullException(nameof(callback)); } public Task HandleClientDisconnectedAsync(MqttServerClientDisconnectedEventArgs eventArgs) { - return _handler(eventArgs); + return _callback(eventArgs); } } } diff --git a/Source/MQTTnet/Server/MqttServerOptions.cs b/Source/MQTTnet/Server/MqttServerOptions.cs index b1c595e..45de09c 100644 --- a/Source/MQTTnet/Server/MqttServerOptions.cs +++ b/Source/MQTTnet/Server/MqttServerOptions.cs @@ -33,5 +33,7 @@ namespace MQTTnet.Server public IMqttRetainedMessagesManager RetainedMessagesManager { get; set; } = new MqttRetainedMessagesManager(); public IMqttServerApplicationMessageInterceptor UndeliveredMessageInterceptor { get; set; } + + public IMqttServerClientDisconnectedHandler ClientDisconnectedInterceptor { get; set; } } } diff --git a/Source/MQTTnet/Server/MqttServerOptionsBuilder.cs b/Source/MQTTnet/Server/MqttServerOptionsBuilder.cs index 38c9183..66779c6 100644 --- a/Source/MQTTnet/Server/MqttServerOptionsBuilder.cs +++ b/Source/MQTTnet/Server/MqttServerOptionsBuilder.cs @@ -163,6 +163,18 @@ namespace MQTTnet.Server return this; } + public MqttServerOptionsBuilder WithDisconnectedInterceptor(IMqttServerClientDisconnectedHandler value) + { + _options.ClientDisconnectedInterceptor = value; + return this; + } + + public MqttServerOptionsBuilder WithDisconnectedInterceptor(Action value) + { + _options.ClientDisconnectedInterceptor = new MqttServerClientDisconnectedHandlerDelegate(value); + return this; + } + public MqttServerOptionsBuilder WithApplicationMessageInterceptor(IMqttServerApplicationMessageInterceptor value) { _options.ApplicationMessageInterceptor = value; From 252772007b6d9ec2580817653f0d6a9b10ac8fb9 Mon Sep 17 00:00:00 2001 From: SeppPenner Date: Sun, 27 Sep 2020 17:43:06 +0200 Subject: [PATCH 2/2] Changed MqttServerClientDisconnectedHandlerDelegate back again. --- ...qttServerClientDisconnectedHandlerDelegate.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Source/MQTTnet/Server/MqttServerClientDisconnectedHandlerDelegate.cs b/Source/MQTTnet/Server/MqttServerClientDisconnectedHandlerDelegate.cs index 1ca9464..b1d495a 100644 --- a/Source/MQTTnet/Server/MqttServerClientDisconnectedHandlerDelegate.cs +++ b/Source/MQTTnet/Server/MqttServerClientDisconnectedHandlerDelegate.cs @@ -5,27 +5,27 @@ namespace MQTTnet.Server { public class MqttServerClientDisconnectedHandlerDelegate : IMqttServerClientDisconnectedHandler { - private readonly Func _callback; + private readonly Func _handler; - public MqttServerClientDisconnectedHandlerDelegate(Action callback) + public MqttServerClientDisconnectedHandlerDelegate(Action handler) { - if (callback == null) throw new ArgumentNullException(nameof(callback)); + if (handler == null) throw new ArgumentNullException(nameof(handler)); - _callback = eventArgs => + _handler = eventArgs => { - callback(eventArgs); + handler(eventArgs); return Task.FromResult(0); }; } - public MqttServerClientDisconnectedHandlerDelegate(Func callback) + public MqttServerClientDisconnectedHandlerDelegate(Func handler) { - _callback = callback ?? throw new ArgumentNullException(nameof(callback)); + _handler = handler ?? throw new ArgumentNullException(nameof(handler)); } public Task HandleClientDisconnectedAsync(MqttServerClientDisconnectedEventArgs eventArgs) { - return _callback(eventArgs); + return _handler(eventArgs); } } }