Skip to content

Commit d3f2687

Browse files
committed
add return value to poll()
1 parent 0a07062 commit d3f2687

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

Diff for: src/MqttClient.cpp

+15-5
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,12 @@ int MqttClient::unsubscribe(const String& topic)
411411
return unsubscribe(topic.c_str());
412412
}
413413

414-
void MqttClient::poll()
414+
/**
415+
* @brief Call poll() regularly to receive MQTT messages and send MQTT keep alives.
416+
* @return 0 - client is no longer connected.
417+
* 1 - client is still connected.
418+
*/
419+
int MqttClient::poll()
415420
{
416421
if (clientAvailable() == 0 && !clientConnected()) {
417422
_rxState = MQTT_CLIENT_RX_STATE_READ_TYPE;
@@ -441,8 +446,7 @@ void MqttClient::poll()
441446
if (_rxLengthMultiplier > (128 * 128 * 128L)) {
442447
// malformed
443448
stop();
444-
445-
return;
449+
return 0;
446450
}
447451

448452
if ((b & 0x80) == 0) { // length done
@@ -469,7 +473,7 @@ void MqttClient::poll()
469473

470474
if (malformedResponse) {
471475
stop();
472-
return;
476+
return 0;
473477
}
474478

475479
if (_rxType == MQTT_PUBLISH) {
@@ -543,7 +547,7 @@ void MqttClient::poll()
543547
} else {
544548
if (_rxLength < _rxMessageTopicLength) {
545549
stop();
546-
return;
550+
return 0;
547551
}
548552
}
549553

@@ -645,6 +649,8 @@ void MqttClient::poll()
645649
stop();
646650
}
647651
}
652+
653+
return _connected ? 1 : 0;
648654
}
649655

650656
int MqttClient::connect(IPAddress ip, uint16_t port)
@@ -1150,6 +1156,10 @@ uint8_t MqttClient::clientConnected()
11501156
return _client->connected();
11511157
}
11521158

1159+
/**
1160+
* @brief Return number of available received bytes.
1161+
* @return Number of available bytes received.
1162+
*/
11531163
int MqttClient::clientAvailable()
11541164
{
11551165
return _client->available();

Diff for: src/MqttClient.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class MqttClient : public Client {
7676
int unsubscribe(const char* topic);
7777
int unsubscribe(const String& topic);
7878

79-
void poll();
79+
int poll();
8080

8181
// from Client
8282
virtual int connect(IPAddress ip, uint16_t port = 1883);

0 commit comments

Comments
 (0)