From 5e5cae6a2cca26c839e4bdb9128ea3967e27fb66 Mon Sep 17 00:00:00 2001 From: Ian Craggs Date: Tue, 1 Aug 2023 12:08:51 +0100 Subject: [PATCH] Always try sendPacket at least once, as in the C++ client #223 --- MQTTClient-C/src/MQTTClient.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/MQTTClient-C/src/MQTTClient.c b/MQTTClient-C/src/MQTTClient.c index 558b6391..903f3c14 100755 --- a/MQTTClient-C/src/MQTTClient.c +++ b/MQTTClient-C/src/MQTTClient.c @@ -36,16 +36,18 @@ static int sendPacket(MQTTClient* c, int32_t length, Timer* timer) int rc = MQTTCLIENT_FAILURE, sent = 0; - while (sent < length && !TimerIsExpired(timer)) + while (sent < length) { rc = c->ipstack->mqttwrite(c->ipstack, &c->buf[sent], length - sent, TimerLeftMS(timer)); - if (rc < 0) // there was an error writing the data + if (rc < 0) /* there was an error writing the data */ break; sent += rc; + if (TimerIsExpired(timer)) /* only check expiry after at least one attempt to write */ + break; } if (sent == length) { - TimerCountdown(&c->last_sent, c->keepAliveInterval); // record the fact that we have successfully sent the packet + TimerCountdown(&c->last_sent, c->keepAliveInterval); /* record the fact that we have successfully sent the packet */ rc = MQTTCLIENT_SUCCESS; } else