mirror of https://github.com/arendst/Tasmota.git
Update PubSubClient.cpp
This commit is contained in:
parent
c60c0bf964
commit
bb567b523c
|
@ -127,7 +127,7 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
|
|||
if (_client->connected()) {
|
||||
result = 1;
|
||||
} else {
|
||||
if (domain != NULL) {
|
||||
if (domain.length() != 0) {
|
||||
result = _client->connect(this->domain.c_str(), this->port);
|
||||
} else {
|
||||
result = _client->connect(this->ip, this->port);
|
||||
|
@ -321,9 +321,10 @@ boolean PubSubClient::loop() {
|
|||
} else {
|
||||
buffer[0] = MQTTPINGREQ;
|
||||
buffer[1] = 0;
|
||||
_client->write(buffer,2);
|
||||
lastOutActivity = t;
|
||||
lastInActivity = t;
|
||||
if (_client->write(buffer,2) != 0) {
|
||||
lastOutActivity = t;
|
||||
lastInActivity = t;
|
||||
}
|
||||
pingOutstanding = true;
|
||||
}
|
||||
}
|
||||
|
@ -351,9 +352,9 @@ boolean PubSubClient::loop() {
|
|||
buffer[1] = 2;
|
||||
buffer[2] = (msgId >> 8);
|
||||
buffer[3] = (msgId & 0xFF);
|
||||
_client->write(buffer,4);
|
||||
lastOutActivity = t;
|
||||
|
||||
if (_client->write(buffer,4) != 0) {
|
||||
lastOutActivity = t;
|
||||
}
|
||||
} else {
|
||||
payload = buffer+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++) {
|
||||
rc += _client->write((char)pgm_read_byte_near(payload + i));
|
||||
}
|
||||
|
||||
lastOutActivity = millis();
|
||||
if (rc > 0) {
|
||||
lastOutActivity = millis();
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
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 false;
|
||||
|
@ -486,19 +490,27 @@ int PubSubClient::endPublish() {
|
|||
}
|
||||
|
||||
size_t PubSubClient::write(uint8_t data) {
|
||||
lastOutActivity = millis();
|
||||
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) {
|
||||
lastOutActivity = millis();
|
||||
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) {
|
||||
|
@ -544,7 +556,9 @@ boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
|
|||
return result;
|
||||
#else
|
||||
rc = _client->write(buf+(MQTT_MAX_HEADER_SIZE-hlen),length+hlen);
|
||||
lastOutActivity = millis();
|
||||
if (rc != 0) {
|
||||
lastOutActivity = millis();
|
||||
}
|
||||
return (rc == hlen+length);
|
||||
#endif
|
||||
}
|
||||
|
@ -623,22 +637,19 @@ uint16_t PubSubClient::writeString(const char* string, uint8_t* buf, uint16_t po
|
|||
|
||||
|
||||
boolean PubSubClient::connected() {
|
||||
boolean rc;
|
||||
if (_client == NULL ) {
|
||||
rc = false;
|
||||
} else {
|
||||
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;
|
||||
}
|
||||
this->_state = MQTT_DISCONNECTED;
|
||||
return false;
|
||||
}
|
||||
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) {
|
||||
|
|
Loading…
Reference in New Issue