Browse Source

Add thrown exception for disconnected event

release/3.x.x
Christian 6 years ago
parent
commit
c060c379d3
2 changed files with 17 additions and 13 deletions
  1. +13
    -12
      Frameworks/MQTTnet.NetStandard/Client/MqttClient.cs
  2. +4
    -1
      Frameworks/MQTTnet.NetStandard/Client/MqttClientDisconnectedEventArgs.cs

+ 13
- 12
Frameworks/MQTTnet.NetStandard/Client/MqttClient.cs View File

@@ -75,7 +75,8 @@ namespace MQTTnet.Client
catch (Exception exception) catch (Exception exception)
{ {
_logger.Error<MqttClient>(exception, "Error while connecting with server."); _logger.Error<MqttClient>(exception, "Error while connecting with server.");
await DisconnectInternalAsync().ConfigureAwait(false);
await DisconnectInternalAsync(exception).ConfigureAwait(false);

throw; throw;
} }
} }
@@ -93,7 +94,7 @@ namespace MQTTnet.Client
} }
finally finally
{ {
await DisconnectInternalAsync().ConfigureAwait(false);
await DisconnectInternalAsync(null).ConfigureAwait(false);
} }
} }


@@ -216,7 +217,7 @@ namespace MQTTnet.Client
if (IsConnected) throw new MqttProtocolViolationException(message); if (IsConnected) throw new MqttProtocolViolationException(message);
} }


private async Task DisconnectInternalAsync()
private async Task DisconnectInternalAsync(Exception exception)
{ {
var clientWasConnected = IsConnected; var clientWasConnected = IsConnected;
IsConnected = false; IsConnected = false;
@@ -236,14 +237,14 @@ namespace MQTTnet.Client
await _adapter.DisconnectAsync(_options.CommunicationTimeout).ConfigureAwait(false); await _adapter.DisconnectAsync(_options.CommunicationTimeout).ConfigureAwait(false);
_logger.Info<MqttClient>("Disconnected from adapter."); _logger.Info<MqttClient>("Disconnected from adapter.");
} }
catch (Exception exception)
catch (Exception adapterException)
{ {
_logger.Warning<MqttClient>(exception, "Error while disconnecting from adapter.");
_logger.Warning<MqttClient>(adapterException, "Error while disconnecting from adapter.");
} }
finally finally
{ {
_logger.Info<MqttClient>("Disconnected."); _logger.Info<MqttClient>("Disconnected.");
Disconnected?.Invoke(this, new MqttClientDisconnectedEventArgs(clientWasConnected));
Disconnected?.Invoke(this, new MqttClientDisconnectedEventArgs(clientWasConnected, exception));
} }
} }


@@ -363,7 +364,7 @@ namespace MQTTnet.Client
return; return;
} }


await DisconnectInternalAsync().ConfigureAwait(false);
await DisconnectInternalAsync(null).ConfigureAwait(false);
} }
catch (MqttCommunicationException exception) catch (MqttCommunicationException exception)
{ {
@@ -373,12 +374,12 @@ namespace MQTTnet.Client
} }


_logger.Warning<MqttClient>(exception, "MQTT communication exception while sending/receiving keep alive packets."); _logger.Warning<MqttClient>(exception, "MQTT communication exception while sending/receiving keep alive packets.");
await DisconnectInternalAsync().ConfigureAwait(false);
await DisconnectInternalAsync(exception).ConfigureAwait(false);
} }
catch (Exception exception) catch (Exception exception)
{ {
_logger.Warning<MqttClient>(exception, "Unhandled exception while sending/receiving keep alive packets."); _logger.Warning<MqttClient>(exception, "Unhandled exception while sending/receiving keep alive packets.");
await DisconnectInternalAsync().ConfigureAwait(false);
await DisconnectInternalAsync(exception).ConfigureAwait(false);
} }
finally finally
{ {
@@ -413,7 +414,7 @@ namespace MQTTnet.Client
return; return;
} }


await DisconnectInternalAsync().ConfigureAwait(false);
await DisconnectInternalAsync(null).ConfigureAwait(false);
} }
catch (MqttCommunicationException exception) catch (MqttCommunicationException exception)
{ {
@@ -423,12 +424,12 @@ namespace MQTTnet.Client
} }


_logger.Warning<MqttClient>(exception, "MQTT communication exception while receiving packets."); _logger.Warning<MqttClient>(exception, "MQTT communication exception while receiving packets.");
await DisconnectInternalAsync().ConfigureAwait(false);
await DisconnectInternalAsync(exception).ConfigureAwait(false);
} }
catch (Exception exception) catch (Exception exception)
{ {
_logger.Error<MqttClient>(exception, "Unhandled exception while receiving packets."); _logger.Error<MqttClient>(exception, "Unhandled exception while receiving packets.");
await DisconnectInternalAsync().ConfigureAwait(false);
await DisconnectInternalAsync(exception).ConfigureAwait(false);
} }
finally finally
{ {


+ 4
- 1
Frameworks/MQTTnet.NetStandard/Client/MqttClientDisconnectedEventArgs.cs View File

@@ -4,11 +4,14 @@ namespace MQTTnet.Client
{ {
public class MqttClientDisconnectedEventArgs : EventArgs public class MqttClientDisconnectedEventArgs : EventArgs
{ {
public MqttClientDisconnectedEventArgs(bool clientWasConnected)
public MqttClientDisconnectedEventArgs(bool clientWasConnected, Exception exception)
{ {
ClientWasConnected = clientWasConnected; ClientWasConnected = clientWasConnected;
Exception = exception;
} }


public bool ClientWasConnected { get; } public bool ClientWasConnected { get; }

public Exception Exception { get; }
} }
} }

Loading…
Cancel
Save