Add MAX_TRANSFER_SIZE def to chunk messages if needed

This commit is contained in:
Nick O'Leary 2015-09-07 18:06:17 +01:00
parent 545d0045f9
commit 2b582f6899
2 changed files with 23 additions and 3 deletions

View File

@ -421,10 +421,25 @@ boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
for (int i=0;i<llen;i++) { for (int i=0;i<llen;i++) {
buf[5-llen+i] = lenBuf[i]; buf[5-llen+i] = lenBuf[i];
} }
rc = _client->write(buf+(4-llen),length+1+llen);
#ifdef MQTT_MAX_TRANSFER_SIZE
uint8_t* writeBuf = buf+(4-llen);
uint8_t bytesRemaining = length+1+llen;
uint8_t bytesToWrite;
boolean result = true;
while((bytesRemaining > 0) && result) {
bytesToWrite = (bytesRemaining > MQTT_MAX_TRANSFER_SIZE)?MQTT_MAX_TRANSFER_SIZE:bytesRemaining;
rc = _client->write(writeBuf,bytesToWrite);
result = (rc == bytesToWrite);
bytesRemaining -= rc;
writeBuf += rc;
}
return result;
#else
rc = _client->write(buf+(4-llen),length+1+llen);
lastOutActivity = millis(); lastOutActivity = millis();
return (rc == 1+llen+length); return (rc == 1+llen+length);
#endif
} }
boolean PubSubClient::subscribe(const char* topic) { boolean PubSubClient::subscribe(const char* topic) {
@ -506,8 +521,9 @@ boolean PubSubClient::connected() {
if (!rc) { if (!rc) {
if (this->_state == MQTT_CONNECTED) { if (this->_state == MQTT_CONNECTED) {
this->_state = MQTT_CONNECTION_LOST; this->_state = MQTT_CONNECTION_LOST;
_client->flush();
_client->stop();
} }
_client->stop();
} }
} }
return rc; return rc;

View File

@ -19,13 +19,17 @@
//#define MQTT_VERSION MQTT_VERSION_3_1 //#define MQTT_VERSION MQTT_VERSION_3_1
#define MQTT_VERSION MQTT_VERSION_3_1_1 #define MQTT_VERSION MQTT_VERSION_3_1_1
// MQTT_MAX_PACKET_SIZE : Maximum packet size // MQTT_MAX_PACKET_SIZE : Maximum packet size
#define MQTT_MAX_PACKET_SIZE 128 #define MQTT_MAX_PACKET_SIZE 128
// MQTT_KEEPALIVE : keepAlive interval in Seconds // MQTT_KEEPALIVE : keepAlive interval in Seconds
#define MQTT_KEEPALIVE 15 #define MQTT_KEEPALIVE 15
// MQTT_MAX_TRANSFER_SIZE : limit how much data is passed to the network client
// in each write call. Needed for the Arduino Wifi Shield. Leave undefined to
// pass the entire MQTT packet in each write call.
//#define MQTT_MAX_TRANSFER_SIZE 80
// Possible values for client.state() // Possible values for client.state()
#define MQTT_CONNECTION_TIMEOUT -4 #define MQTT_CONNECTION_TIMEOUT -4
#define MQTT_CONNECTION_LOST -3 #define MQTT_CONNECTION_LOST -3