Skip to content

Commit 2a8dad1

Browse files
author
Ian Craggs
committed
Add offline buffering test, and reconnect function
1 parent 2eeb553 commit 2a8dad1

File tree

8 files changed

+1255
-133
lines changed

8 files changed

+1255
-133
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ SYNC_TESTS = ${addprefix ${blddir}/test/,${TEST_FILES_C}}
9696
TEST_FILES_CS = test3
9797
SYNC_SSL_TESTS = ${addprefix ${blddir}/test/,${TEST_FILES_CS}}
9898

99-
TEST_FILES_A = test4 test_mqtt4async
99+
TEST_FILES_A = test4 test9 test_mqtt4async
100100
ASYNC_TESTS = ${addprefix ${blddir}/test/,${TEST_FILES_A}}
101101

102102
TEST_FILES_AS = test5

build.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,18 @@
8282
</target>
8383

8484
<target name="test" >
85+
<exec executable="python3" dir="test" spawn="true">
86+
<arg value="mqttsas.py" />
87+
<arg value="${test.hostname}" />
88+
</exec>
8589
<if>
8690
<os family="windows"/>
8791
<then>
8892
<!-- TODO: build test2 for windows -->
8993
<foreach target="runAtest" param="aTest" list="test1,test4"/>
90-
</then>
94+
</then>
9195
<else>
92-
<foreach target="runAtest" param="aTest" list="test1,test2,test4"/>
96+
<foreach target="runAtest" param="aTest" list="test9,test1,test2,test4"/>
9397
</else>
9498
</if>
9599
<foreach target="runSSLtest" param="aTest" list="test3,test5"/>

src/MQTTAsync.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -858,15 +858,31 @@ int MQTTAsync_reconnect(MQTTAsync handle)
858858
FUNC_ENTRY;
859859
MQTTAsync_lock_mutex(mqttasync_mutex);
860860

861-
if (m->automaticReconnect && m->shouldBeConnected)
862-
{
863-
m->reconnectNow = 1;
864-
if (m->retrying == 0)
865-
{
866-
m->currentInterval = m->minRetryInterval;
867-
m->retrying = 1;
861+
if (m->automaticReconnect)
862+
{
863+
if (m->shouldBeConnected)
864+
{
865+
m->reconnectNow = 1;
866+
if (m->retrying == 0)
867+
{
868+
m->currentInterval = m->minRetryInterval;
869+
m->retrying = 1;
870+
}
871+
rc = MQTTASYNC_SUCCESS;
868872
}
869-
rc = MQTTASYNC_SUCCESS;
873+
}
874+
else
875+
{
876+
/* to reconnect, put the connect command to the head of the command queue */
877+
MQTTAsync_queuedCommand* conn = malloc(sizeof(MQTTAsync_queuedCommand));
878+
memset(conn, '\0', sizeof(MQTTAsync_queuedCommand));
879+
conn->client = m;
880+
conn->command = m->connect;
881+
/* make sure that the version attempts are restarted */
882+
if (m->c->MQTTVersion == MQTTVERSION_DEFAULT)
883+
conn->command.details.conn.MQTTVersion = 0;
884+
MQTTAsync_addCommand(conn, sizeof(m->connect));
885+
rc = MQTTASYNC_SUCCESS;
870886
}
871887

872888
MQTTAsync_unlock_mutex(mqttasync_mutex);
@@ -1367,11 +1383,14 @@ void MQTTAsync_checkTimeouts()
13671383
{
13681384
if (m->reconnectNow || MQTTAsync_elapsed(m->lastConnectionFailedTime) > (m->currentInterval * 1000))
13691385
{
1370-
/* put the connect command to the head of the command queue, using the next serverURI */
1386+
/* to reconnect put the connect command to the head of the command queue */
13711387
MQTTAsync_queuedCommand* conn = malloc(sizeof(MQTTAsync_queuedCommand));
13721388
memset(conn, '\0', sizeof(MQTTAsync_queuedCommand));
13731389
conn->client = m;
13741390
conn->command = m->connect;
1391+
/* make sure that the version attempts are restarted */
1392+
if (m->c->MQTTVersion == MQTTVERSION_DEFAULT)
1393+
conn->command.details.conn.MQTTVersion = 0;
13751394
Log(TRACE_MIN, -1, "Automatically attempting to reconnect");
13761395
MQTTAsync_addCommand(conn, sizeof(m->connect));
13771396
m->reconnectNow = 0;

src/MQTTAsync.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ DLLExport int MQTTAsync_setCallbacks(MQTTAsync handle, void* context, MQTTAsync_
465465
DLLExport int MQTTAsync_setConnected(MQTTAsync handle, void* context, MQTTAsync_connected* co);
466466

467467

468-
468+
DLLExport int MQTTAsync_reconnect(MQTTAsync handle);
469469

470470

471471
/**
@@ -755,8 +755,8 @@ typedef struct
755755
} MQTTAsync_connectOptions;
756756

757757

758-
#define MQTTAsync_connectOptions_initializer { {'M', 'Q', 'T', 'C'}, 4, 60, 1, 10, NULL, NULL, NULL, 30, 0, NULL, NULL, NULL, NULL, 0, NULL, 0, \
759-
0, 1, 60}
758+
#define MQTTAsync_connectOptions_initializer { {'M', 'Q', 'T', 'C'}, 4, 60, 1, 10, NULL, NULL, NULL, 30, 0,\
759+
NULL, NULL, NULL, NULL, 0, NULL, 0, 0, 1, 60}
760760

761761
/**
762762
* This function attempts to connect a previously-created client (see

0 commit comments

Comments
 (0)