From d3f2687e32719700b0ac9d4bc59c1d3632d02d90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Mu=CC=88ller?= Date: Tue, 12 Nov 2024 21:19:39 +0100 Subject: [PATCH 1/2] add return value to poll() --- src/MqttClient.cpp | 20 +++++++++++++++----- src/MqttClient.h | 2 +- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 221b230..75cddf4 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -411,7 +411,12 @@ int MqttClient::unsubscribe(const String& topic) return unsubscribe(topic.c_str()); } -void MqttClient::poll() +/** + * @brief Call poll() regularly to receive MQTT messages and send MQTT keep alives. + * @return 0 - client is no longer connected. + * 1 - client is still connected. + */ +int MqttClient::poll() { if (clientAvailable() == 0 && !clientConnected()) { _rxState = MQTT_CLIENT_RX_STATE_READ_TYPE; @@ -441,8 +446,7 @@ void MqttClient::poll() if (_rxLengthMultiplier > (128 * 128 * 128L)) { // malformed stop(); - - return; + return 0; } if ((b & 0x80) == 0) { // length done @@ -469,7 +473,7 @@ void MqttClient::poll() if (malformedResponse) { stop(); - return; + return 0; } if (_rxType == MQTT_PUBLISH) { @@ -543,7 +547,7 @@ void MqttClient::poll() } else { if (_rxLength < _rxMessageTopicLength) { stop(); - return; + return 0; } } @@ -645,6 +649,8 @@ void MqttClient::poll() stop(); } } + + return _connected ? 1 : 0; } int MqttClient::connect(IPAddress ip, uint16_t port) @@ -1150,6 +1156,10 @@ uint8_t MqttClient::clientConnected() return _client->connected(); } +/** + * @brief Return number of available received bytes. + * @return Number of available bytes received. + */ int MqttClient::clientAvailable() { return _client->available(); diff --git a/src/MqttClient.h b/src/MqttClient.h index 522f023..8e13335 100644 --- a/src/MqttClient.h +++ b/src/MqttClient.h @@ -76,7 +76,7 @@ class MqttClient : public Client { int unsubscribe(const char* topic); int unsubscribe(const String& topic); - void poll(); + int poll(); // from Client virtual int connect(IPAddress ip, uint16_t port = 1883); From 5eeec198713fbb8e6ac5f285de9e299dd3266a5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Mu=CC=88ller?= Date: Tue, 12 Nov 2024 21:36:04 +0100 Subject: [PATCH 2/2] add return value to poll() --- src/MqttClient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 75cddf4..e78a193 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -542,7 +542,7 @@ int MqttClient::poll() if (_rxMessageQoS) { if (_rxLength < (_rxMessageTopicLength + 2)) { stop(); - return; + return 0; } } else { if (_rxLength < _rxMessageTopicLength) {