public Task DisconnectAsync() { _session.Stop(MqttClientDisconnectType.NotClean); return(Task.FromResult(0)); }
private async Task TrySendNextQueuedPacketAsync(IMqttChannelAdapter adapter, CancellationToken cancellationToken) { MqttBasePacket packet = null; try { lock (_queue) { if (_queue.Count > 0) { packet = _queue.Dequeue(); } } if (packet == null) { await _queueAutoResetEvent.WaitOneAsync(cancellationToken).ConfigureAwait(false); return; } if (cancellationToken.IsCancellationRequested) { return; } adapter.SendPacketAsync(packet, cancellationToken).GetAwaiter().GetResult(); _logger.Verbose("Enqueued packet sent (ClientId: {0}).", _clientSession.ClientId); } catch (Exception exception) { if (exception is MqttCommunicationTimedOutException) { _logger.Warning(exception, "Sending publish packet failed: Timeout (ClientId: {0}).", _clientSession.ClientId); } else if (exception is MqttCommunicationException) { _logger.Warning(exception, "Sending publish packet failed: Communication exception (ClientId: {0}).", _clientSession.ClientId); } else if (exception is OperationCanceledException) { } else { _logger.Error(exception, "Sending publish packet failed (ClientId: {0}).", _clientSession.ClientId); } if (packet is MqttPublishPacket publishPacket) { if (publishPacket.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce) { publishPacket.Dup = true; Enqueue(publishPacket); } } if (!cancellationToken.IsCancellationRequested) { _clientSession.Stop(MqttClientDisconnectType.NotClean); } } }