Tighten up the session state description in the section on Clean Session.
3.1.2.4 Clean Session
Position: bit 1 of the Connect Flags byte.
This bit specifies the handling of the Session state.
The Client and Server can store Session state to enable reliable messaging to continue across a sequence of Network Connections. This bit is used to control the lifetime of the Session state.
If set to 0, the Server resumes communications with the Client based on state from the current Session (as identified by the Client identifier). If there is no Session associated with the Client identifier the Server creates a new Session. The Client and Server MUST store the Session after the Client and Server are disconnected. After disconnection, the Server MUST store further QoS 1 and QoS 2 messages that match any subscriptions that the client had at the time of disconnection as part of the Session state. It MAY also store QoS 0 messages that meet the same criteria.
If 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.
The Session state in the Client consists of:
• QoS 1 and QoS 2 messages which have been sent to the Server, but have not been completely acknowledged.
• QoS 2 messages which have been received from the Server, but have not been completely acknowledged.
The Session state in the Server consists of:
• The existence of a Session, even if the rest of the Session state is empty.
• The Client's subscriptions.
• QoS 1 and QoS 2 messages which have been sent to the Client, but have not been completely acknowledged.
• QoS 1 and QoS 2 messages pending transmission to the Client.
• QoS 2 messages which have been received from the Client, but have not been completely acknowledged.
• Optionally, QoS 0 messages pending transmission to the Client.
Retained publications do not form part of the Session state in the Server. They MUST NOT be deleted when the Session ends.
CONNACK Control Packet change
Add a flag to enable the server to tell the client whether it has stored session state.
3.2.2 Variable header
The variable header format is shown in the table below.
Byte 1 is the "Connect Acknowledge Flags". Bits 7-1 are reserved and must be set to 0.
Bit 0 is the Session Present Flag.
3.2.2.1 Session Present
Position: bit 0 of the Connect Acknowledge Flags.
If the Server accepts a connection with CleanSession set to 1, the Server MUST set Session Present to 0 in the CONNACK packet in addition to setting a zero return code in the CONNACK packet.
If the Server accepts a connection with CleanSession set to 0, the value set in Session Present depends on whether the Server already has stored Session state for the supplied client ID. If the Server has stored Session state, it MUST set Session
Present to 1 in the CONNACK packet. If the Server does not have stored Session state, it MUST set Session Present to 0 in the CONNACK packet. This is in addition to setting a zero return code in the CONNACK packet.
The Session Present flag enables a Client to establish whether the Client and Server have a consistent view about whether there is already stored Session state.
Once the initial setup of a Session is complete, a Client with stored Session state will expect the Server to maintain its stored Session state. In the event that the value of Session Present received by the Client from the Server is not as expected, the Client can choose whether to proceed with the Session or to disconnect. The Client can discard the Session state on both Client and Server by disconnecting, connecting with Clean Session set to 1 and then disconnecting again.
If a server sends a CONNACK packet containing a non-zero return code it MUST set Session Present to 0.