@@ -236,6 +236,7 @@ class Client
236
236
237
237
private:
238
238
239
+ void closeSession ();
239
240
void cleanSession ();
240
241
int cycle (Timer& timer);
241
242
int waitfor (int packet_type, Timer& timer);
@@ -297,10 +298,8 @@ class Client
297
298
template <class Network , class Timer , int a, int MAX_MESSAGE_HANDLERS>
298
299
void MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::cleanSession()
299
300
{
300
- ping_outstanding = false ;
301
301
for (int i = 0 ; i < MAX_MESSAGE_HANDLERS; ++i)
302
302
messageHandlers[i].topicFilter = 0 ;
303
- isconnected = false ;
304
303
305
304
#if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2
306
305
inflightMsgid = 0 ;
@@ -315,11 +314,22 @@ void MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::cleanSession()
315
314
}
316
315
317
316
317
+ template <class Network , class Timer , int a, int MAX_MESSAGE_HANDLERS>
318
+ void MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::closeSession()
319
+ {
320
+ ping_outstanding = false ;
321
+ isconnected = false ;
322
+ if (cleansession)
323
+ cleanSession ();
324
+ }
325
+
326
+
318
327
template <class Network , class Timer , int a, int MAX_MESSAGE_HANDLERS>
319
328
MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::Client(Network& network, unsigned int command_timeout_ms) : ipstack(network), packetid()
320
329
{
321
330
this ->command_timeout_ms = command_timeout_ms;
322
- cleanSession ();
331
+ cleansession = true ;
332
+ closeSession ();
323
333
}
324
334
325
335
@@ -656,8 +666,8 @@ int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::cycle(Timer& timer)
656
666
exit :
657
667
if (rc == SUCCESS)
658
668
rc = packet_type;
659
- else
660
- isconnected = false ;
669
+ else if (isconnected)
670
+ closeSession () ;
661
671
return rc;
662
672
}
663
673
@@ -867,7 +877,7 @@ int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::su
867
877
868
878
exit :
869
879
if (rc == FAILURE)
870
- cleanSession ();
880
+ closeSession ();
871
881
return rc;
872
882
}
873
883
@@ -910,7 +920,7 @@ int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::un
910
920
911
921
exit :
912
922
if (rc != SUCCESS)
913
- cleanSession ();
923
+ closeSession ();
914
924
return rc;
915
925
}
916
926
@@ -958,7 +968,7 @@ int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(int len, Time
958
968
959
969
exit :
960
970
if (rc != SUCCESS)
961
- cleanSession ();
971
+ closeSession ();
962
972
return rc;
963
973
}
964
974
@@ -1029,11 +1039,7 @@ int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::disconnect()
1029
1039
int len = MQTTSerialize_disconnect (sendbuf, MAX_MQTT_PACKET_SIZE);
1030
1040
if (len > 0 )
1031
1041
rc = sendPacket (len, timer); // send the disconnect packet
1032
-
1033
- if (cleansession)
1034
- cleanSession ();
1035
- else
1036
- isconnected = false ;
1042
+ closeSession ();
1037
1043
return rc;
1038
1044
}
1039
1045
0 commit comments