public async Task StopAsync(MqttDisconnectReasonCode reason) { Status = MqttClientConnectionStatus.Finalizing; _disconnectReason = reason; if (reason == MqttDisconnectReasonCode.SessionTakenOver || reason == MqttDisconnectReasonCode.KeepAliveTimeout) { // Is is very important to send the DISCONNECT packet here BEFORE cancelling the // token because the entire connection is closed (disposed) as soon as the cancellation // token is cancelled. To there is no chance that the DISCONNECT packet will ever arrive // at the client! try { await _channelAdapter.SendPacketAsync(new MqttDisconnectPacket { ReasonCode = reason }, _serverOptions.DefaultCommunicationTimeout, CancellationToken.None).ConfigureAwait(false); } catch (Exception exception) { _logger.Warning(exception, "Client '{0}': Error while sending DISCONNECT packet after takeover.", ClientId); } } StopInternal(); await(_packageReceiverTask ?? PlatformAbstractionLayer.CompletedTask); }
public MqttDisconnectPacket Create(MqttDisconnectReasonCode reasonCode) { if (reasonCode == MqttDisconnectReasonCode.NormalDisconnection) { return(_normalDisconnection); } return(new MqttDisconnectPacket { ReasonCode = reasonCode }); }
public async Task StopAsync(MqttDisconnectReasonCode reason) { IsRunning = false; if (reason == MqttDisconnectReasonCode.SessionTakenOver || reason == MqttDisconnectReasonCode.KeepAliveTimeout) { // Is is very important to send the DISCONNECT packet here BEFORE cancelling the // token because the entire connection is closed (disposed) as soon as the cancellation // token is cancelled. To there is no chance that the DISCONNECT packet will ever arrive // at the client! await TrySendDisconnectPacket(reason).ConfigureAwait(false); } StopInternal(); }
async Task TrySendDisconnectPacket(MqttDisconnectReasonCode reasonCode) { try { var disconnectPacket = _packetFactories.Disconnect.Create(reasonCode); using (var timeout = new CancellationTokenSource(_serverOptions.DefaultCommunicationTimeout)) { await SendPacketAsync(disconnectPacket, timeout.Token).ConfigureAwait(false); } } catch (Exception exception) { _logger.Warning(exception, "Client '{0}': Error while sending DISCONNECT packet (ReasonCode = {1}).", Id, reasonCode); } }