From 1a8bcae47cd2aa05a703bc89c9eaf331d4d2c0b8 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Thu, 9 May 2019 22:08:56 +0200 Subject: [PATCH] Add authentication result to disconnected handler. --- Build/MQTTnet.nuspec | 1 + .../MqttClientDisconnectedEventArgs.cs | 6 +++++- Source/MQTTnet/Client/MqttClient.cs | 20 ++++++++++--------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Build/MQTTnet.nuspec b/Build/MQTTnet.nuspec index a7902e0..d3bd632 100644 --- a/Build/MQTTnet.nuspec +++ b/Build/MQTTnet.nuspec @@ -13,6 +13,7 @@ * [Core] Fixed missing properties from PUBLISH packet in _MqttApplicationMessage_ (thanks to @pcbing). * [Core] Fixed wrong encoding of PUBREL and PUBCOMP packets for MQTTv5 (thanks to @perphilipp). +* [Client] Added the authentication result to the disconnected handler (only set when connecting failed). * [Server] Fixed a bug which returns wrong flag for existing session in CONNACK packet (thanks to @avengerstark). * [nuget] .NET Framework builds are now using 4.5.2 or 4.6.1 builds instead of netstandard 2.0. diff --git a/Source/MQTTnet/Client/Disconnecting/MqttClientDisconnectedEventArgs.cs b/Source/MQTTnet/Client/Disconnecting/MqttClientDisconnectedEventArgs.cs index 7506042..7658e15 100644 --- a/Source/MQTTnet/Client/Disconnecting/MqttClientDisconnectedEventArgs.cs +++ b/Source/MQTTnet/Client/Disconnecting/MqttClientDisconnectedEventArgs.cs @@ -1,17 +1,21 @@ using System; +using MQTTnet.Client.Connecting; namespace MQTTnet.Client.Disconnecting { public class MqttClientDisconnectedEventArgs : EventArgs { - public MqttClientDisconnectedEventArgs(bool clientWasConnected, Exception exception) + public MqttClientDisconnectedEventArgs(bool clientWasConnected, Exception exception, MqttClientAuthenticateResult authenticateResult) { ClientWasConnected = clientWasConnected; Exception = exception; + AuthenticateResult = authenticateResult; } public bool ClientWasConnected { get; } public Exception Exception { get; } + + public MqttClientAuthenticateResult AuthenticateResult { get; } } } diff --git a/Source/MQTTnet/Client/MqttClient.cs b/Source/MQTTnet/Client/MqttClient.cs index 10387bc..8db5513 100644 --- a/Source/MQTTnet/Client/MqttClient.cs +++ b/Source/MQTTnet/Client/MqttClient.cs @@ -61,6 +61,8 @@ namespace MQTTnet.Client ThrowIfConnected("It is not allowed to connect with a server after the connection is established."); + MqttClientAuthenticateResult authenticateResult = null; + try { Options = options; @@ -81,7 +83,7 @@ namespace MQTTnet.Client _packetReceiverTask = Task.Run(() => TryReceivePacketsAsync(backgroundCancellationToken), backgroundCancellationToken); - var authenticateResult = await AuthenticateAsync(adapter, options.WillMessage, cancellationToken).ConfigureAwait(false); + authenticateResult = await AuthenticateAsync(adapter, options.WillMessage, cancellationToken).ConfigureAwait(false); _sendTracker.Restart(); @@ -108,7 +110,7 @@ namespace MQTTnet.Client if (!DisconnectIsPending()) { - await DisconnectInternalAsync(null, exception).ConfigureAwait(false); + await DisconnectInternalAsync(null, exception, authenticateResult).ConfigureAwait(false); } throw; @@ -131,7 +133,7 @@ namespace MQTTnet.Client { if (!DisconnectIsPending()) { - await DisconnectInternalAsync(null, null).ConfigureAwait(false); + await DisconnectInternalAsync(null, null, null).ConfigureAwait(false); } } } @@ -232,7 +234,7 @@ namespace MQTTnet.Client if (IsConnected) throw new MqttProtocolViolationException(message); } - private async Task DisconnectInternalAsync(Task sender, Exception exception) + private async Task DisconnectInternalAsync(Task sender, Exception exception, MqttClientAuthenticateResult authenticateResult) { var clientWasConnected = IsConnected; @@ -267,7 +269,7 @@ namespace MQTTnet.Client var disconnectedHandler = DisconnectedHandler; if (disconnectedHandler != null) { - await disconnectedHandler.HandleDisconnectedAsync(new MqttClientDisconnectedEventArgs(clientWasConnected, exception)).ConfigureAwait(false); + await disconnectedHandler.HandleDisconnectedAsync(new MqttClientDisconnectedEventArgs(clientWasConnected, exception, authenticateResult)).ConfigureAwait(false); } } } @@ -376,7 +378,7 @@ namespace MQTTnet.Client if (!DisconnectIsPending()) { - await DisconnectInternalAsync(_keepAlivePacketsSenderTask, exception).ConfigureAwait(false); + await DisconnectInternalAsync(_keepAlivePacketsSenderTask, exception, null).ConfigureAwait(false); } } finally @@ -404,7 +406,7 @@ namespace MQTTnet.Client { if (!DisconnectIsPending()) { - await DisconnectInternalAsync(_packetReceiverTask, null).ConfigureAwait(false); + await DisconnectInternalAsync(_packetReceiverTask, null, null).ConfigureAwait(false); } return; @@ -436,7 +438,7 @@ namespace MQTTnet.Client if (!DisconnectIsPending()) { - await DisconnectInternalAsync(_packetReceiverTask, exception).ConfigureAwait(false); + await DisconnectInternalAsync(_packetReceiverTask, exception, null).ConfigureAwait(false); } } finally @@ -497,7 +499,7 @@ namespace MQTTnet.Client if (!DisconnectIsPending()) { - await DisconnectInternalAsync(_packetReceiverTask, exception).ConfigureAwait(false); + await DisconnectInternalAsync(_packetReceiverTask, exception, null).ConfigureAwait(false); } } }