Skip to content

Commit 0bcddc9

Browse files
authored
Merge pull request #2 from MathewHDYT/master
Increase internal buffer size
2 parents a4d4245 + f8dfd97 commit 0bcddc9

File tree

4 files changed

+33
-34
lines changed

4 files changed

+33
-34
lines changed

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "git",
77
"url": "https://github.com/thingsboard/pubsubclient.git"
88
},
9-
"version": "2.9.1",
9+
"version": "2.9.2",
1010
"exclude": "tests",
1111
"examples": "examples/*/*.ino",
1212
"frameworks": "arduino",

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=TBPubSubClient
2-
version=2.9.1
2+
version=2.9.2
33
author=Nick O'Leary <[email protected]>
44
maintainer=ThingsBoard Team
55
sentence=A client library for MQTT messaging.

src/TBPubSubClient.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
196196
if (result == 1) {
197197
nextMsgId = 1;
198198
// Leave room in the buffer for header and variable length field
199-
uint16_t length = MQTT_MAX_HEADER_SIZE;
199+
size_t length = MQTT_MAX_HEADER_SIZE;
200200
unsigned int j;
201201

202202
#if MQTT_VERSION == MQTT_VERSION_3_1
@@ -299,8 +299,8 @@ boolean PubSubClient::readByte(uint8_t * result) {
299299
}
300300

301301
// reads a byte into result[*index] and increments index
302-
boolean PubSubClient::readByte(uint8_t * result, uint16_t * index){
303-
uint16_t current_index = *index;
302+
boolean PubSubClient::readByte(uint8_t * result, size_t * index){
303+
size_t current_index = *index;
304304
uint8_t * write_address = &(result[current_index]);
305305
if(readByte(write_address)){
306306
*index = current_index + 1;
@@ -310,7 +310,7 @@ boolean PubSubClient::readByte(uint8_t * result, uint16_t * index){
310310
}
311311

312312
uint32_t PubSubClient::readPacket(uint8_t* lengthLength) {
313-
uint16_t len = 0;
313+
size_t len = 0;
314314
if(!readByte(this->buffer, &len)) return 0;
315315
bool isPublish = (this->buffer[0]&0xF0) == MQTTPUBLISH;
316316
uint32_t multiplier = 1;
@@ -344,9 +344,9 @@ uint32_t PubSubClient::readPacket(uint8_t* lengthLength) {
344344
skip += 2;
345345
}
346346
}
347-
uint32_t idx = len;
347+
size_t idx = len;
348348

349-
for (uint32_t i = start;i<length;i++) {
349+
for (size_t i = start;i<length;i++) {
350350
if(!readByte(&digit)) return 0;
351351
if (this->stream) {
352352
if (isPublish && idx-*lengthLength-2>skip) {
@@ -386,7 +386,7 @@ boolean PubSubClient::loop() {
386386
}
387387
if (_client->available()) {
388388
uint8_t llen;
389-
uint16_t len = readPacket(&llen);
389+
size_t len = readPacket(&llen);
390390
uint16_t msgId = 0;
391391
uint8_t *payload;
392392
if (len > 0) {
@@ -452,11 +452,11 @@ boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigne
452452
return false;
453453
}
454454
// Leave room in the buffer for header and variable length field
455-
uint16_t length = MQTT_MAX_HEADER_SIZE;
455+
size_t length = MQTT_MAX_HEADER_SIZE;
456456
length = writeString(topic,this->buffer,length);
457457

458458
// Add payload
459-
uint16_t i;
459+
size_t i;
460460
for (i=0;i<plength;i++) {
461461
this->buffer[length++] = payload[i];
462462
}
@@ -479,7 +479,7 @@ boolean PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsig
479479
uint8_t llen = 0;
480480
uint8_t digit;
481481
unsigned int rc = 0;
482-
uint16_t tlen;
482+
size_t tlen;
483483
unsigned int pos = 0;
484484
unsigned int i;
485485
uint8_t header;
@@ -526,14 +526,14 @@ boolean PubSubClient::publish_P(const char* topic, const uint8_t* payload, unsig
526526
boolean PubSubClient::beginPublish(const char* topic, unsigned int plength, boolean retained) {
527527
if (connected()) {
528528
// Send the header and variable length field
529-
uint16_t length = MQTT_MAX_HEADER_SIZE;
529+
size_t length = MQTT_MAX_HEADER_SIZE;
530530
length = writeString(topic,this->buffer,length);
531531
uint8_t header = MQTTPUBLISH;
532532
if (retained) {
533533
header |= 1;
534534
}
535535
size_t hlen = buildHeader(header, this->buffer, plength+length-MQTT_MAX_HEADER_SIZE);
536-
uint16_t rc = _client->write(this->buffer+(MQTT_MAX_HEADER_SIZE-hlen),length-(MQTT_MAX_HEADER_SIZE-hlen));
536+
size_t rc = _client->write(this->buffer+(MQTT_MAX_HEADER_SIZE-hlen),length-(MQTT_MAX_HEADER_SIZE-hlen));
537537
lastOutActivity = millis();
538538
return (rc == (length-(MQTT_MAX_HEADER_SIZE-hlen)));
539539
}
@@ -554,12 +554,12 @@ size_t PubSubClient::write(const uint8_t *buffer, size_t size) {
554554
return _client->write(buffer,size);
555555
}
556556

557-
size_t PubSubClient::buildHeader(uint8_t header, uint8_t* buf, uint16_t length) {
557+
size_t PubSubClient::buildHeader(uint8_t header, uint8_t* buf, size_t length) {
558558
uint8_t lenBuf[4];
559559
uint8_t llen = 0;
560560
uint8_t digit;
561561
uint8_t pos = 0;
562-
uint16_t len = length;
562+
size_t len = length;
563563
do {
564564

565565
digit = len & 127; //digit = len %128
@@ -578,13 +578,13 @@ size_t PubSubClient::buildHeader(uint8_t header, uint8_t* buf, uint16_t length)
578578
return llen+1; // Full header size is variable length bit plus the 1-byte fixed header
579579
}
580580

581-
boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
582-
uint16_t rc;
583-
uint8_t hlen = buildHeader(header, buf, length);
581+
boolean PubSubClient::write(uint8_t header, uint8_t* buf, size_t length) {
582+
size_t rc;
583+
size_t hlen = buildHeader(header, buf, length);
584584

585585
#ifdef MQTT_MAX_TRANSFER_SIZE
586586
uint8_t* writeBuf = buf+(MQTT_MAX_HEADER_SIZE-hlen);
587-
uint16_t bytesRemaining = length+hlen; //Match the length type
587+
size_t bytesRemaining = length+hlen; //Match the length type
588588
uint8_t bytesToWrite;
589589
boolean result = true;
590590
while((bytesRemaining > 0) && result) {
@@ -620,7 +620,7 @@ boolean PubSubClient::subscribe(const char* topic, uint8_t qos) {
620620
}
621621
if (connected()) {
622622
// Leave room in the buffer for header and variable length field
623-
uint16_t length = MQTT_MAX_HEADER_SIZE;
623+
size_t length = MQTT_MAX_HEADER_SIZE;
624624
nextMsgId++;
625625
if (nextMsgId == 0) {
626626
nextMsgId = 1;
@@ -644,7 +644,7 @@ boolean PubSubClient::unsubscribe(const char* topic) {
644644
return false;
645645
}
646646
if (connected()) {
647-
uint16_t length = MQTT_MAX_HEADER_SIZE;
647+
size_t length = MQTT_MAX_HEADER_SIZE;
648648
nextMsgId++;
649649
if (nextMsgId == 0) {
650650
nextMsgId = 1;
@@ -667,9 +667,9 @@ void PubSubClient::disconnect() {
667667
lastInActivity = lastOutActivity = millis();
668668
}
669669

670-
uint16_t PubSubClient::writeString(const char* string, uint8_t* buf, uint16_t pos) {
670+
size_t PubSubClient::writeString(const char* string, uint8_t* buf, size_t pos) {
671671
const char* idp = string;
672-
uint16_t i = 0;
672+
size_t i = 0;
673673
pos += 2;
674674
while (*idp) {
675675
buf[pos++] = *idp++;
@@ -737,7 +737,7 @@ int PubSubClient::state() {
737737
return this->_state;
738738
}
739739

740-
boolean PubSubClient::setBufferSize(uint16_t size) {
740+
boolean PubSubClient::setBufferSize(size_t size) {
741741
if (size == 0) {
742742
// Cannot set it back to 0
743743
return false;
@@ -756,7 +756,7 @@ boolean PubSubClient::setBufferSize(uint16_t size) {
756756
return (this->buffer != NULL);
757757
}
758758

759-
uint16_t PubSubClient::getBufferSize() {
759+
size_t PubSubClient::getBufferSize() {
760760
return this->bufferSize;
761761
}
762762
PubSubClient& PubSubClient::setKeepAlive(uint16_t keepAlive) {

src/TBPubSubClient.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class PubSubClient : public Print {
9393
private:
9494
Client* _client;
9595
uint8_t* buffer;
96-
uint16_t bufferSize;
96+
size_t bufferSize;
9797
uint16_t keepAlive;
9898
uint16_t socketTimeout;
9999
uint16_t nextMsgId;
@@ -103,14 +103,14 @@ class PubSubClient : public Print {
103103
MQTT_CALLBACK_SIGNATURE;
104104
uint32_t readPacket(uint8_t*);
105105
boolean readByte(uint8_t * result);
106-
boolean readByte(uint8_t * result, uint16_t * index);
107-
boolean write(uint8_t header, uint8_t* buf, uint16_t length);
108-
uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos);
106+
boolean readByte(uint8_t * result, size_t * index);
107+
boolean write(uint8_t header, uint8_t* buf, size_t length);
108+
size_t writeString(const char* string, uint8_t* buf, size_t pos);
109109
// Build up the header ready to send
110110
// Returns the size of the header
111111
// Note: the header is built at the end of the first MQTT_MAX_HEADER_SIZE bytes, so will start
112112
// (MQTT_MAX_HEADER_SIZE - <returned size>) bytes into the buffer
113-
size_t buildHeader(uint8_t header, uint8_t* buf, uint16_t length);
113+
size_t buildHeader(uint8_t header, uint8_t* buf, size_t length);
114114
IPAddress ip;
115115
const char* domain;
116116
uint16_t port;
@@ -143,8 +143,8 @@ class PubSubClient : public Print {
143143
PubSubClient& setKeepAlive(uint16_t keepAlive);
144144
PubSubClient& setSocketTimeout(uint16_t timeout);
145145

146-
boolean setBufferSize(uint16_t size);
147-
uint16_t getBufferSize();
146+
boolean setBufferSize(size_t size);
147+
size_t getBufferSize();
148148

149149
boolean connect(const char* id);
150150
boolean connect(const char* id, const char* user, const char* pass);
@@ -184,5 +184,4 @@ class PubSubClient : public Print {
184184

185185
};
186186

187-
188187
#endif

0 commit comments

Comments
 (0)