Update PubSubClient.cpp

This commit is contained in:
Theo Arends 2019-11-17 17:42:57 +01:00
parent c60c0bf964
commit bb567b523c
1 changed files with 43 additions and 32 deletions

View File

@ -127,7 +127,7 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
if (_client->connected()) { if (_client->connected()) {
result = 1; result = 1;
} else { } else {
if (domain != NULL) { if (domain.length() != 0) {
result = _client->connect(this->domain.c_str(), this->port); result = _client->connect(this->domain.c_str(), this->port);
} else { } else {
result = _client->connect(this->ip, this->port); result = _client->connect(this->ip, this->port);
@ -321,9 +321,10 @@ boolean PubSubClient::loop() {
} else { } else {
buffer[0] = MQTTPINGREQ; buffer[0] = MQTTPINGREQ;
buffer[1] = 0; buffer[1] = 0;
_client->write(buffer,2); if (_client->write(buffer,2) != 0) {
lastOutActivity = t; lastOutActivity = t;
lastInActivity = t; lastInActivity = t;
}
pingOutstanding = true; pingOutstanding = true;
} }
} }
@ -351,9 +352,9 @@ boolean PubSubClient::loop() {
buffer[1] = 2; buffer[1] = 2;
buffer[2] = (msgId >> 8); buffer[2] = (msgId >> 8);
buffer[3] = (msgId & 0xFF); buffer[3] = (msgId & 0xFF);
_client->write(buffer,4); if (_client->write(buffer,4) != 0) {
lastOutActivity = t; lastOutActivity = t;
}
} else { } else {
payload = buffer+llen+3+tl; payload = buffer+llen+3+tl;
callback(topic,payload,len-llen-3-tl); callback(topic,payload,len-llen-3-tl);
@ -456,11 +457,12 @@ boolean PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsig
for (i=0;i<plength;i++) { for (i=0;i<plength;i++) {
rc += _client->write((char)pgm_read_byte_near(payload + i)); rc += _client->write((char)pgm_read_byte_near(payload + i));
} }
if (rc > 0) {
lastOutActivity = millis(); lastOutActivity = millis();
}
// Header (1 byte) + llen + identifier (2 bytes) + topic len + payload len // Header (1 byte) + llen + identifier (2 bytes) + topic len + payload len
const int expectedLength = 1 + llen + 2 + tlen + plength; const unsigned int expectedLength = 1 + llen + 2 + tlen + plength;
return (rc == expectedLength); return (rc == expectedLength);
} }
@ -475,7 +477,9 @@ boolean PubSubClient::beginPublish(const char* topic, unsigned int plength, bool
} }
size_t hlen = buildHeader(header, buffer, plength+length-MQTT_MAX_HEADER_SIZE); size_t hlen = buildHeader(header, buffer, plength+length-MQTT_MAX_HEADER_SIZE);
uint16_t rc = _client->write(buffer+(MQTT_MAX_HEADER_SIZE-hlen),length-(MQTT_MAX_HEADER_SIZE-hlen)); uint16_t rc = _client->write(buffer+(MQTT_MAX_HEADER_SIZE-hlen),length-(MQTT_MAX_HEADER_SIZE-hlen));
lastOutActivity = millis(); if (rc > 0) {
lastOutActivity = millis();
}
return (rc == (length-(MQTT_MAX_HEADER_SIZE-hlen))); return (rc == (length-(MQTT_MAX_HEADER_SIZE-hlen)));
} }
return false; return false;
@ -486,19 +490,27 @@ int PubSubClient::endPublish() {
} }
size_t PubSubClient::write(uint8_t data) { size_t PubSubClient::write(uint8_t data) {
lastOutActivity = millis();
if (_client == nullptr) { if (_client == nullptr) {
return 0; lastOutActivity = millis();
return 0;
} }
return _client->write(data); size_t rc = _client->write(data);
if (rc != 0) {
lastOutActivity = millis();
}
return rc;
} }
size_t PubSubClient::write(const uint8_t *buffer, size_t size) { size_t PubSubClient::write(const uint8_t *buffer, size_t size) {
lastOutActivity = millis();
if (_client == nullptr) { if (_client == nullptr) {
return 0; lastOutActivity = millis();
return 0;
} }
return _client->write(buffer,size); size_t rc = _client->write(buffer,size);
if (rc != 0) {
lastOutActivity = millis();
}
return rc;
} }
size_t PubSubClient::buildHeader(uint8_t header, uint8_t* buf, uint16_t length) { size_t PubSubClient::buildHeader(uint8_t header, uint8_t* buf, uint16_t length) {
@ -544,7 +556,9 @@ boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
return result; return result;
#else #else
rc = _client->write(buf+(MQTT_MAX_HEADER_SIZE-hlen),length+hlen); rc = _client->write(buf+(MQTT_MAX_HEADER_SIZE-hlen),length+hlen);
lastOutActivity = millis(); if (rc != 0) {
lastOutActivity = millis();
}
return (rc == hlen+length); return (rc == hlen+length);
#endif #endif
} }
@ -623,22 +637,19 @@ uint16_t PubSubClient::writeString(const char* string, uint8_t* buf, uint16_t po
boolean PubSubClient::connected() { boolean PubSubClient::connected() {
boolean rc;
if (_client == NULL ) { if (_client == NULL ) {
rc = false; this->_state = MQTT_DISCONNECTED;
} else { return false;
rc = (int)_client->connected();
if (!rc) {
if (this->_state == MQTT_CONNECTED) {
this->_state = MQTT_CONNECTION_LOST;
_client->flush();
_client->stop();
}
} else {
return this->_state == MQTT_CONNECTED;
}
} }
return rc; if (_client->connected() == 0) {
bool lastStateConnected = this->_state == MQTT_CONNECTED;
this->disconnect();
if (lastStateConnected) {
this->_state = MQTT_CONNECTION_LOST;
}
return false;
}
return this->_state == MQTT_CONNECTED;
} }
PubSubClient& PubSubClient::setServer(uint8_t * ip, uint16_t port) { PubSubClient& PubSubClient::setServer(uint8_t * ip, uint16_t port) {