diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2018-01-18 20:50:02 +0200 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2018-01-18 20:50:02 +0200 |
| commit | 84111b11a7dcbb7720de68c84048b9b9f0dadee9 (patch) | |
| tree | 3916162d4b0c9f622bcdd21837dde8f18cb40661 /amalgamation | |
| parent | f9a5e782ab8905d3a580456d174549f0223a6ba0 (diff) | |
| download | mqtt-84111b11a7dcbb7720de68c84048b9b9f0dadee9.tar.gz mqtt-84111b11a7dcbb7720de68c84048b9b9f0dadee9.zip | |
Try to fix keepalive handlingfix-keepalive
Diffstat (limited to 'amalgamation')
| -rw-r--r-- | amalgamation/mqtt.c | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/amalgamation/mqtt.c b/amalgamation/mqtt.c index 69d7acc..989e26a 100644 --- a/amalgamation/mqtt.c +++ b/amalgamation/mqtt.c @@ -5867,17 +5867,41 @@ int MqttClientRunOnce(MqttClient *client, int timeout) LOG_DEBUG("selecting"); - if (timeout < 0) + // If timeout is not valid, use keepalive + if (timeout <= 0) { timeout = client->keepAlive * 1000; - if (timeout == 0) + } + + // Timeout cannot be larger than timeout + if (timeout > client->keepAlive * 1000) + { + if (client->keepAlive > 0) { - timeout = 30 * 1000; + timeout = client->keepAlive * 1000; } } - else if (timeout > (client->keepAlive * 1000) && client->keepAlive > 0) + + // If timeout is zero at this point, use default timeout. + if (timeout == 0) { - timeout = client->keepAlive * 1000; + timeout = 30 * 1000; + } + + // If we are using keepalive with this connection, use a timeout that + // expires when the time from last sent packet equals keepalive. + if (client->keepAlive > 0) + { + int elapsed = MqttGetCurrentTime() - client->lastPacketSentTime; + int mintimeout = client->keepAlive * 1000 - elapsed; + timeout = (mintimeout < timeout) ? mintimeout : timeout; + // If timeout is negative, just use the keepalive as timeout. This + // situation should have also triggered the ping check above (in + // connected state). + if (timeout < 0) + { + timeout = client->keepAlive * 1000; + } } rv = SocketSelect(client->stream.sock, &events, timeout); |
