From 9b6cf5a5fe36f7caa1e329d99b92c17bcabf6e8f Mon Sep 17 00:00:00 2001
From: Tomoaki Ichige <ichigetomoaki1207@gmail.com>
Date: Sun, 1 Sep 2024 02:27:09 +0900
Subject: [PATCH] fix: always perform keep-alive check regardless of data being
 null or not

---
 .../src/commonMain/kotlin/MQTTClient.kt       | 48 +++++++++----------
 1 file changed, 24 insertions(+), 24 deletions(-)

diff --git a/kmqtt-client/src/commonMain/kotlin/MQTTClient.kt b/kmqtt-client/src/commonMain/kotlin/MQTTClient.kt
index 40cdeeb..218fd46 100644
--- a/kmqtt-client/src/commonMain/kotlin/MQTTClient.kt
+++ b/kmqtt-client/src/commonMain/kotlin/MQTTClient.kt
@@ -369,34 +369,34 @@ public class MQTTClient(
                 onDisconnected(null)
                 throw e
             }
-        } else {
-            // If connack not received in a reasonable amount of time, then disconnect
-            val currentTime = currentTimeMillis()
-            val lastActive = lastActiveTimestamp.value
-            val isConnackReceived = connackReceived.value
+        }
+
+        // If connack not received in a reasonable amount of time, then disconnect
+        val currentTime = currentTimeMillis()
+        val lastActive = lastActiveTimestamp.value
+        val isConnackReceived = connackReceived.value
 
-            if (!isConnackReceived && currentTime > lastActive + (connackTimeout * 1000)) {
+        if (!isConnackReceived && currentTime > lastActive + (connackTimeout * 1000)) {
+            close()
+            lastException = Exception("CONNACK not received in 30 seconds")
+            throw lastException!!
+        }
+
+        val actualKeepAlive = keepAlive.value
+        if (actualKeepAlive != 0 && isConnackReceived) {
+            if (currentTime > lastActive + (actualKeepAlive * 1000)) {
+                // Timeout
                 close()
-                lastException = Exception("CONNACK not received in 30 seconds")
+                lastException = MQTTException(ReasonCode.KEEP_ALIVE_TIMEOUT)
                 throw lastException!!
-            }
-
-            val actualKeepAlive = keepAlive.value
-            if (actualKeepAlive != 0 && isConnackReceived) {
-                if (currentTime > lastActive + (actualKeepAlive * 1000)) {
-                    // Timeout
-                    close()
-                    lastException = MQTTException(ReasonCode.KEEP_ALIVE_TIMEOUT)
-                    throw lastException!!
-                } else if (currentTime > lastActive + (actualKeepAlive * 1000 * 0.9)) {
-                    val pingreq = if (mqttVersion == MQTTVersion.MQTT3_1_1) {
-                        MQTT4Pingreq()
-                    } else {
-                        MQTT5Pingreq()
-                    }
-                    send(pingreq.toByteArray())
-                    // TODO if not receiving pingresp after a reasonable amount of time, close connection
+            } else if (currentTime > lastActive + (actualKeepAlive * 1000 * 0.9)) {
+                val pingreq = if (mqttVersion == MQTTVersion.MQTT3_1_1) {
+                    MQTT4Pingreq()
+                } else {
+                    MQTT5Pingreq()
                 }
+                send(pingreq.toByteArray())
+                // TODO if not receiving pingresp after a reasonable amount of time, close connection
             }
         }
     }
-- 
GitLab