Add authentication result to disconnected handler.

This commit is contained in:
Christian Kratky
2019-05-09 22:08:56 +02:00
parent 664549ab93
commit 1a8bcae47c
3 changed files with 17 additions and 10 deletions
+1
View File
@@ -13,6 +13,7 @@
<releaseNotes> <releaseNotes>
* [Core] Fixed missing properties from PUBLISH packet in _MqttApplicationMessage_ (thanks to @pcbing). * [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). * [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). * [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. * [nuget] .NET Framework builds are now using 4.5.2 or 4.6.1 builds instead of netstandard 2.0.
</releaseNotes> </releaseNotes>
@@ -1,17 +1,21 @@
using System; using System;
using MQTTnet.Client.Connecting;
namespace MQTTnet.Client.Disconnecting namespace MQTTnet.Client.Disconnecting
{ {
public class MqttClientDisconnectedEventArgs : EventArgs public class MqttClientDisconnectedEventArgs : EventArgs
{ {
public MqttClientDisconnectedEventArgs(bool clientWasConnected, Exception exception) public MqttClientDisconnectedEventArgs(bool clientWasConnected, Exception exception, MqttClientAuthenticateResult authenticateResult)
{ {
ClientWasConnected = clientWasConnected; ClientWasConnected = clientWasConnected;
Exception = exception; Exception = exception;
AuthenticateResult = authenticateResult;
} }
public bool ClientWasConnected { get; } public bool ClientWasConnected { get; }
public Exception Exception { get; } public Exception Exception { get; }
public MqttClientAuthenticateResult AuthenticateResult { get; }
} }
} }
+11 -9
View File
@@ -61,6 +61,8 @@ namespace MQTTnet.Client
ThrowIfConnected("It is not allowed to connect with a server after the connection is established."); ThrowIfConnected("It is not allowed to connect with a server after the connection is established.");
MqttClientAuthenticateResult authenticateResult = null;
try try
{ {
Options = options; Options = options;
@@ -81,7 +83,7 @@ namespace MQTTnet.Client
_packetReceiverTask = Task.Run(() => TryReceivePacketsAsync(backgroundCancellationToken), backgroundCancellationToken); _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(); _sendTracker.Restart();
@@ -108,7 +110,7 @@ namespace MQTTnet.Client
if (!DisconnectIsPending()) if (!DisconnectIsPending())
{ {
await DisconnectInternalAsync(null, exception).ConfigureAwait(false); await DisconnectInternalAsync(null, exception, authenticateResult).ConfigureAwait(false);
} }
throw; throw;
@@ -131,7 +133,7 @@ namespace MQTTnet.Client
{ {
if (!DisconnectIsPending()) 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); 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; var clientWasConnected = IsConnected;
@@ -267,7 +269,7 @@ namespace MQTTnet.Client
var disconnectedHandler = DisconnectedHandler; var disconnectedHandler = DisconnectedHandler;
if (disconnectedHandler != null) 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()) if (!DisconnectIsPending())
{ {
await DisconnectInternalAsync(_keepAlivePacketsSenderTask, exception).ConfigureAwait(false); await DisconnectInternalAsync(_keepAlivePacketsSenderTask, exception, null).ConfigureAwait(false);
} }
} }
finally finally
@@ -404,7 +406,7 @@ namespace MQTTnet.Client
{ {
if (!DisconnectIsPending()) if (!DisconnectIsPending())
{ {
await DisconnectInternalAsync(_packetReceiverTask, null).ConfigureAwait(false); await DisconnectInternalAsync(_packetReceiverTask, null, null).ConfigureAwait(false);
} }
return; return;
@@ -436,7 +438,7 @@ namespace MQTTnet.Client
if (!DisconnectIsPending()) if (!DisconnectIsPending())
{ {
await DisconnectInternalAsync(_packetReceiverTask, exception).ConfigureAwait(false); await DisconnectInternalAsync(_packetReceiverTask, exception, null).ConfigureAwait(false);
} }
} }
finally finally
@@ -497,7 +499,7 @@ namespace MQTTnet.Client
if (!DisconnectIsPending()) if (!DisconnectIsPending())
{ {
await DisconnectInternalAsync(_packetReceiverTask, exception).ConfigureAwait(false); await DisconnectInternalAsync(_packetReceiverTask, exception, null).ConfigureAwait(false);
} }
} }
} }