aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--amalgamation/mqtt.c22
1 files changed, 13 insertions, 9 deletions
diff --git a/amalgamation/mqtt.c b/amalgamation/mqtt.c
index c0ff379..69d7acc 100644
--- a/amalgamation/mqtt.c
+++ b/amalgamation/mqtt.c
@@ -5835,7 +5835,20 @@ int MqttClientRunOnce(MqttClient *client, int timeout)
if (SIMPLEQ_EMPTY(&client->sendQueue))
{
+ int64_t elapsed;
+
LOG_DEBUG("nothing to write");
+
+ // If there's nothing to write at this point and we haven't sent
+ // any packets in keepalive seconds, we should send a ping.
+
+ elapsed = MqttGetCurrentTime() - client->lastPacketSentTime;
+ if (client->keepAlive > 0 && elapsed >= client->keepAlive*1000)
+ {
+ MqttClientQueueSimplePacket(client, MqttPacketTypePingReq);
+ client->pingSent = 1;
+ events |= EV_WRITE;
+ }
}
else
{
@@ -5923,15 +5936,6 @@ int MqttClientRunOnce(MqttClient *client, int timeout)
client->pingSent = 0;
client->stopped = 1;
}
- else if (SIMPLEQ_EMPTY(&client->sendQueue))
- {
- int64_t elapsed = MqttGetCurrentTime() - client->lastPacketSentTime;
- if (elapsed/1000 >= client->keepAlive && client->keepAlive > 0)
- {
- MqttClientQueueSimplePacket(client, MqttPacketTypePingReq);
- client->pingSent = 1;
- }
- }
}
if (client->stopped)