Skip to content

Commit d2b7e15

Browse files
author
Ian Craggs
committed
Add session state tests and correct behaviour for C++
1 parent bf6e041 commit d2b7e15

File tree

5 files changed

+235
-321
lines changed

5 files changed

+235
-321
lines changed

MQTTClient/src/MQTTClient.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ class Client
236236

237237
private:
238238

239+
void closeSession();
239240
void cleanSession();
240241
int cycle(Timer& timer);
241242
int waitfor(int packet_type, Timer& timer);
@@ -297,10 +298,8 @@ class Client
297298
template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS>
298299
void MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::cleanSession()
299300
{
300-
ping_outstanding = false;
301301
for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
302302
messageHandlers[i].topicFilter = 0;
303-
isconnected = false;
304303

305304
#if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2
306305
inflightMsgid = 0;
@@ -315,11 +314,22 @@ void MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::cleanSession()
315314
}
316315

317316

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+
318327
template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS>
319328
MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::Client(Network& network, unsigned int command_timeout_ms) : ipstack(network), packetid()
320329
{
321330
this->command_timeout_ms = command_timeout_ms;
322-
cleanSession();
331+
cleansession = true;
332+
closeSession();
323333
}
324334

325335

@@ -656,8 +666,8 @@ int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::cycle(Timer& timer)
656666
exit:
657667
if (rc == SUCCESS)
658668
rc = packet_type;
659-
else
660-
isconnected = false;
669+
else if (isconnected)
670+
closeSession();
661671
return rc;
662672
}
663673

@@ -867,7 +877,7 @@ int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::su
867877

868878
exit:
869879
if (rc == FAILURE)
870-
cleanSession();
880+
closeSession();
871881
return rc;
872882
}
873883

@@ -910,7 +920,7 @@ int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::un
910920

911921
exit:
912922
if (rc != SUCCESS)
913-
cleanSession();
923+
closeSession();
914924
return rc;
915925
}
916926

@@ -958,7 +968,7 @@ int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(int len, Time
958968

959969
exit:
960970
if (rc != SUCCESS)
961-
cleanSession();
971+
closeSession();
962972
return rc;
963973
}
964974

@@ -1029,11 +1039,7 @@ int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::disconnect()
10291039
int len = MQTTSerialize_disconnect(sendbuf, MAX_MQTT_PACKET_SIZE);
10301040
if (len > 0)
10311041
rc = sendPacket(len, timer); // send the disconnect packet
1032-
1033-
if (cleansession)
1034-
cleanSession();
1035-
else
1036-
isconnected = false;
1042+
closeSession();
10371043
return rc;
10381044
}
10391045

MQTTClient/src/linux/linux.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*
1313
* Contributors:
1414
* Ian Craggs - initial API and implementation and/or initial documentation
15+
* Ian Craggs - ensure read returns if no bytes read
1516
*******************************************************************************/
1617

1718
#include <sys/types.h>
@@ -108,6 +109,7 @@ class IPStack
108109
setsockopt(mysock, SOL_SOCKET, SO_RCVTIMEO, (char *)&interval, sizeof(struct timeval));
109110

110111
int bytes = 0;
112+
int i = 0; const int max_tries = 10;
111113
while (bytes < len)
112114
{
113115
int rc = ::recv(mysock, &buffer[bytes], (size_t)(len - bytes), 0);
@@ -119,6 +121,10 @@ class IPStack
119121
}
120122
else
121123
bytes += rc;
124+
if (++i >= max_tries)
125+
break;
126+
if (rc == 0)
127+
break;
122128
}
123129
return bytes;
124130
}

0 commit comments

Comments
 (0)