From c060c379d3623f27691dcfb722aea836358e5884 Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 22 Jan 2018 20:55:06 +0100 Subject: [PATCH] Add thrown exception for disconnected event --- .../MQTTnet.NetStandard/Client/MqttClient.cs | 25 ++++++++++--------- .../Client/MqttClientDisconnectedEventArgs.cs | 5 +++- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/Frameworks/MQTTnet.NetStandard/Client/MqttClient.cs b/Frameworks/MQTTnet.NetStandard/Client/MqttClient.cs index 7834816..a4ec82f 100644 --- a/Frameworks/MQTTnet.NetStandard/Client/MqttClient.cs +++ b/Frameworks/MQTTnet.NetStandard/Client/MqttClient.cs @@ -75,7 +75,8 @@ namespace MQTTnet.Client catch (Exception exception) { _logger.Error(exception, "Error while connecting with server."); - await DisconnectInternalAsync().ConfigureAwait(false); + await DisconnectInternalAsync(exception).ConfigureAwait(false); + throw; } } @@ -93,7 +94,7 @@ namespace MQTTnet.Client } finally { - await DisconnectInternalAsync().ConfigureAwait(false); + await DisconnectInternalAsync(null).ConfigureAwait(false); } } @@ -216,7 +217,7 @@ namespace MQTTnet.Client if (IsConnected) throw new MqttProtocolViolationException(message); } - private async Task DisconnectInternalAsync() + private async Task DisconnectInternalAsync(Exception exception) { var clientWasConnected = IsConnected; IsConnected = false; @@ -236,14 +237,14 @@ namespace MQTTnet.Client await _adapter.DisconnectAsync(_options.CommunicationTimeout).ConfigureAwait(false); _logger.Info("Disconnected from adapter."); } - catch (Exception exception) + catch (Exception adapterException) { - _logger.Warning(exception, "Error while disconnecting from adapter."); + _logger.Warning(adapterException, "Error while disconnecting from adapter."); } finally { _logger.Info("Disconnected."); - Disconnected?.Invoke(this, new MqttClientDisconnectedEventArgs(clientWasConnected)); + Disconnected?.Invoke(this, new MqttClientDisconnectedEventArgs(clientWasConnected, exception)); } } @@ -363,7 +364,7 @@ namespace MQTTnet.Client return; } - await DisconnectInternalAsync().ConfigureAwait(false); + await DisconnectInternalAsync(null).ConfigureAwait(false); } catch (MqttCommunicationException exception) { @@ -373,12 +374,12 @@ namespace MQTTnet.Client } _logger.Warning(exception, "MQTT communication exception while sending/receiving keep alive packets."); - await DisconnectInternalAsync().ConfigureAwait(false); + await DisconnectInternalAsync(exception).ConfigureAwait(false); } catch (Exception exception) { _logger.Warning(exception, "Unhandled exception while sending/receiving keep alive packets."); - await DisconnectInternalAsync().ConfigureAwait(false); + await DisconnectInternalAsync(exception).ConfigureAwait(false); } finally { @@ -413,7 +414,7 @@ namespace MQTTnet.Client return; } - await DisconnectInternalAsync().ConfigureAwait(false); + await DisconnectInternalAsync(null).ConfigureAwait(false); } catch (MqttCommunicationException exception) { @@ -423,12 +424,12 @@ namespace MQTTnet.Client } _logger.Warning(exception, "MQTT communication exception while receiving packets."); - await DisconnectInternalAsync().ConfigureAwait(false); + await DisconnectInternalAsync(exception).ConfigureAwait(false); } catch (Exception exception) { _logger.Error(exception, "Unhandled exception while receiving packets."); - await DisconnectInternalAsync().ConfigureAwait(false); + await DisconnectInternalAsync(exception).ConfigureAwait(false); } finally { diff --git a/Frameworks/MQTTnet.NetStandard/Client/MqttClientDisconnectedEventArgs.cs b/Frameworks/MQTTnet.NetStandard/Client/MqttClientDisconnectedEventArgs.cs index 26125b9..2161cc2 100644 --- a/Frameworks/MQTTnet.NetStandard/Client/MqttClientDisconnectedEventArgs.cs +++ b/Frameworks/MQTTnet.NetStandard/Client/MqttClientDisconnectedEventArgs.cs @@ -4,11 +4,14 @@ namespace MQTTnet.Client { public class MqttClientDisconnectedEventArgs : EventArgs { - public MqttClientDisconnectedEventArgs(bool clientWasConnected) + public MqttClientDisconnectedEventArgs(bool clientWasConnected, Exception exception) { ClientWasConnected = clientWasConnected; + Exception = exception; } public bool ClientWasConnected { get; } + + public Exception Exception { get; } } }