diff --git a/Source/MQTTnet.Server/Internal/MqttClientSessionsManager.cs b/Source/MQTTnet.Server/Internal/MqttClientSessionsManager.cs index 70ff7b22a..b99fa8e03 100644 --- a/Source/MQTTnet.Server/Internal/MqttClientSessionsManager.cs +++ b/Source/MQTTnet.Server/Internal/MqttClientSessionsManager.cs @@ -409,7 +409,12 @@ public async Task HandleClientConnectionAsync(IMqttChannelAdapter channelAdapter if (connectedClient.Id != null && !connectedClient.IsTakenOver && _eventContainer.ClientDisconnectedEvent.HasHandlers) { var disconnectType = connectedClient.DisconnectPacket != null ? MqttClientDisconnectType.Clean : MqttClientDisconnectType.NotClean; - var eventArgs = new ClientDisconnectedEventArgs(connectedClient.ConnectPacket, connectedClient.DisconnectPacket, disconnectType, endpoint, connectedClient.Session.Items); + var eventArgs = new ClientDisconnectedEventArgs( + connectedClient.ConnectPacket, + connectedClient.DisconnectPacket, + disconnectType, + endpoint, + connectedClient.Session.Items); await _eventContainer.ClientDisconnectedEvent.InvokeAsync(eventArgs).ConfigureAwait(false); } @@ -677,39 +682,39 @@ static bool ShouldPersistSession(MqttConnectedClient connectedClient) switch (connectedClient.ChannelAdapter.PacketFormatterAdapter.ProtocolVersion) { case MqttProtocolVersion.V500: + { + // MQTT 5.0 section 3.1.2.11.2 + // The Client and Server MUST store the Session State after the Network Connection is closed if the Session Expiry Interval is greater than 0 [MQTT-3.1.2-23]. + // + // A Client that only wants to process messages while connected will set the Clean Start to 1 and set the Session Expiry Interval to 0. + // It will not receive Application Messages published before it connected and has to subscribe afresh to any topics that it is interested + // in each time it connects. + + var effectiveSessionExpiryInterval = connectedClient.DisconnectPacket?.SessionExpiryInterval ?? 0U; + if (effectiveSessionExpiryInterval == 0U) { - // MQTT 5.0 section 3.1.2.11.2 - // The Client and Server MUST store the Session State after the Network Connection is closed if the Session Expiry Interval is greater than 0 [MQTT-3.1.2-23]. - // - // A Client that only wants to process messages while connected will set the Clean Start to 1 and set the Session Expiry Interval to 0. - // It will not receive Application Messages published before it connected and has to subscribe afresh to any topics that it is interested - // in each time it connects. - - var effectiveSessionExpiryInterval = connectedClient.DisconnectPacket?.SessionExpiryInterval ?? 0U; - if (effectiveSessionExpiryInterval == 0U) - { - // From RFC: If the Session Expiry Interval is absent, the Session Expiry Interval in the CONNECT packet is used. - effectiveSessionExpiryInterval = connectedClient.ConnectPacket.SessionExpiryInterval; - } - - return effectiveSessionExpiryInterval != 0U; + // From RFC: If the Session Expiry Interval is absent, the Session Expiry Interval in the CONNECT packet is used. + effectiveSessionExpiryInterval = connectedClient.ConnectPacket.SessionExpiryInterval; } + return effectiveSessionExpiryInterval != 0U; + } + case MqttProtocolVersion.V311: - { - // MQTT 3.1.1 section 3.1.2.4: persist only if 'not CleanSession' - // - // If CleanSession is set to 1, the Client and Server MUST discard any previous Session and start a new one. - // This Session lasts as long as the Network Connection. State data associated with this Session MUST NOT be - // reused in any subsequent Session [MQTT-3.1.2-6]. + { + // MQTT 3.1.1 section 3.1.2.4: persist only if 'not CleanSession' + // + // If CleanSession is set to 1, the Client and Server MUST discard any previous Session and start a new one. + // This Session lasts as long as the Network Connection. State data associated with this Session MUST NOT be + // reused in any subsequent Session [MQTT-3.1.2-6]. - return !connectedClient.ConnectPacket.CleanSession; - } + return !connectedClient.ConnectPacket.CleanSession; + } case MqttProtocolVersion.V310: - { - return true; - } + { + return true; + } default: throw new NotSupportedException();