aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--amalgamation/mqtt.c34
-rw-r--r--src/client.c34
2 files changed, 58 insertions, 10 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);
diff --git a/src/client.c b/src/client.c
index 51e86d9..5814c66 100644
--- a/src/client.c
+++ b/src/client.c
@@ -406,17 +406,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);