Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add poll() return status #115

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/MqttClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -441,8 +446,7 @@ void MqttClient::poll()
if (_rxLengthMultiplier > (128 * 128 * 128L)) {
// malformed
stop();

return;
return 0;
}

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

if (malformedResponse) {
stop();
return;
return 0;
}

if (_rxType == MQTT_PUBLISH) {
Expand Down Expand Up @@ -538,12 +542,12 @@ void MqttClient::poll()
if (_rxMessageQoS) {
if (_rxLength < (_rxMessageTopicLength + 2)) {
stop();
return;
return 0;
}
} else {
if (_rxLength < _rxMessageTopicLength) {
stop();
return;
return 0;
}
}

Expand Down Expand Up @@ -645,6 +649,8 @@ void MqttClient::poll()
stop();
}
}

return _connected ? 1 : 0;
}

int MqttClient::connect(IPAddress ip, uint16_t port)
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/MqttClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down