|
|
@@ -54,12 +54,12 @@ namespace MQTTnet.Client |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
_cancellationTokenSource = new CancellationTokenSource(); |
|
|
|
_disconnectReason = new TaskCompletionSource<bool>(); |
|
|
|
_options = options; |
|
|
|
_packetIdentifierProvider.Reset(); |
|
|
|
_packetDispatcher.Reset(); |
|
|
|
|
|
|
|
_cancellationTokenSource = new CancellationTokenSource(); |
|
|
|
_disconnectReason = new TaskCompletionSource<bool>(); |
|
|
|
_adapter = _adapterFactory.CreateClientAdapter(options, _logger); |
|
|
|
|
|
|
|
_logger.Verbose($"Trying to connect with server ({_options.ChannelOptions})."); |
|
|
@@ -87,10 +87,12 @@ namespace MQTTnet.Client |
|
|
|
catch (Exception exception) |
|
|
|
{ |
|
|
|
_logger.Error(exception, "Error while connecting with server."); |
|
|
|
|
|
|
|
if (_disconnectReason.TrySetException(exception)) |
|
|
|
{ |
|
|
|
await DisconnectInternalAsync(null, exception).ConfigureAwait(false); |
|
|
|
} |
|
|
|
|
|
|
|
throw; |
|
|
|
} |
|
|
|
} |
|
|
@@ -183,7 +185,7 @@ namespace MQTTnet.Client |
|
|
|
|
|
|
|
public void Dispose() |
|
|
|
{ |
|
|
|
_cancellationTokenSource?.Cancel (false); |
|
|
|
_cancellationTokenSource?.Cancel(false); |
|
|
|
_cancellationTokenSource?.Dispose(); |
|
|
|
_cancellationTokenSource = null; |
|
|
|
|
|
|
@@ -224,9 +226,10 @@ namespace MQTTnet.Client |
|
|
|
|
|
|
|
private async Task DisconnectInternalAsync(Task sender, Exception exception) |
|
|
|
{ |
|
|
|
InitiateDisconnect(); |
|
|
|
|
|
|
|
var clientWasConnected = IsConnected; |
|
|
|
|
|
|
|
InitiateDisconnect(); |
|
|
|
|
|
|
|
IsConnected = false; |
|
|
|
|
|
|
|
try |
|
|
@@ -247,7 +250,7 @@ namespace MQTTnet.Client |
|
|
|
} |
|
|
|
finally |
|
|
|
{ |
|
|
|
Dispose (); |
|
|
|
Dispose(); |
|
|
|
_cleanDisconnectInitiated = false; |
|
|
|
|
|
|
|
_logger.Info("Disconnected."); |
|
|
@@ -261,16 +264,16 @@ namespace MQTTnet.Client |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
if (_cancellationTokenSource == null || _cancellationTokenSource.IsCancellationRequested) |
|
|
|
if (_cancellationTokenSource?.IsCancellationRequested == true) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
_cancellationTokenSource.Cancel(false); |
|
|
|
_cancellationTokenSource?.Cancel(false); |
|
|
|
} |
|
|
|
catch (Exception adapterException) |
|
|
|
catch (Exception exception) |
|
|
|
{ |
|
|
|
_logger.Warning(adapterException, "Error while initiating disconnect."); |
|
|
|
_logger.Warning(exception, "Error while initiating disconnect."); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@@ -377,8 +380,10 @@ namespace MQTTnet.Client |
|
|
|
{ |
|
|
|
while (!cancellationToken.IsCancellationRequested) |
|
|
|
{ |
|
|
|
var packet = await _adapter.ReceivePacketAsync(TimeSpan.Zero, cancellationToken).ConfigureAwait(false); |
|
|
|
if (packet != null) |
|
|
|
var packet = await _adapter.ReceivePacketAsync(TimeSpan.Zero, cancellationToken) |
|
|
|
.ConfigureAwait(false); |
|
|
|
|
|
|
|
if (packet != null && !cancellationToken.IsCancellationRequested) |
|
|
|
{ |
|
|
|
await ProcessReceivedPacketAsync(packet, cancellationToken).ConfigureAwait(false); |
|
|
|
} |
|
|
@@ -393,7 +398,6 @@ namespace MQTTnet.Client |
|
|
|
|
|
|
|
if (exception is OperationCanceledException) |
|
|
|
{ |
|
|
|
_logger.Verbose ("MQTT OperationCanceled exception while receiving packets."); |
|
|
|
} |
|
|
|
else if (exception is MqttCommunicationException) |
|
|
|
{ |
|
|
@@ -492,16 +496,20 @@ namespace MQTTnet.Client |
|
|
|
|
|
|
|
private void StartReceivingPackets(CancellationToken cancellationToken) |
|
|
|
{ |
|
|
|
_packetReceiverTask = Task.Run( |
|
|
|
_packetReceiverTask = Task.Factory.StartNew( |
|
|
|
() => ReceivePacketsAsync(cancellationToken), |
|
|
|
cancellationToken); |
|
|
|
cancellationToken, |
|
|
|
TaskCreationOptions.LongRunning, |
|
|
|
TaskScheduler.Default); |
|
|
|
} |
|
|
|
|
|
|
|
private void StartSendingKeepAliveMessages(CancellationToken cancellationToken) |
|
|
|
{ |
|
|
|
_keepAliveMessageSenderTask = Task.Run( |
|
|
|
_keepAliveMessageSenderTask = Task.Factory.StartNew( |
|
|
|
() => SendKeepAliveMessagesAsync(cancellationToken), |
|
|
|
cancellationToken); |
|
|
|
cancellationToken, |
|
|
|
TaskCreationOptions.LongRunning, |
|
|
|
TaskScheduler.Default); |
|
|
|
} |
|
|
|
|
|
|
|
private void FireApplicationMessageReceivedEvent(MqttPublishPacket publishPacket) |
|
|
|