aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskari Timperi <oskari.timperi@iki.fi>2018-01-16 17:52:25 +0200
committerOskari Timperi <oskari.timperi@iki.fi>2018-01-16 17:52:49 +0200
commitfe74d0707215320a23ed10f989e87437c4f7ea4d (patch)
tree564321548896a6440054bad60d7461d7bd7e5850
parent296da7a73501fb7ce8c0f4c0e18060ebf7eada97 (diff)
downloadmqtt-fe74d0707215320a23ed10f989e87437c4f7ea4d.tar.gz
mqtt-fe74d0707215320a23ed10f989e87437c4f7ea4d.zip
Fix keepalive handling
Previously the code would send pings only when SocketSelect() would timeout. This was not working when a client was subscribed to a topic and would receive periodic messages but wouldn't itself send anything. This meant that SocketSelect() wouldn't timeout at any point and we wouldn't execute the check for sending a PINGREQ. The fix is to do the check for last sent packet before calling SocketSelect() so that if we haven't been sending anything for a while, we really send the PINGREQ. Fixes #10
-rw-r--r--src/client.c22
1 files changed, 13 insertions, 9 deletions
diff --git a/src/client.c b/src/client.c
index b95c8d5..51e86d9 100644
--- a/src/client.c
+++ b/src/client.c
@@ -374,7 +374,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
{
@@ -462,15 +475,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)