Skip to content

Commit 4039196

Browse files
authored
Merge pull request #10 from MathewHDYT/master
Implement 2 buffers for receiving and sending
2 parents 97cc3a5 + ea26c76 commit 4039196

File tree

4 files changed

+86
-77
lines changed

4 files changed

+86
-77
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.4",
9+
"version": "2.10.0",
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.4
2+
version=2.10.0
33
author=ThingsBoard <[email protected]>
44
maintainer=ThingsBoard Team
55
sentence=A client library for MQTT messaging.

src/PubSubClient.cpp

Lines changed: 82 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ PubSubClient::PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGN
159159
}
160160

161161
PubSubClient::~PubSubClient() {
162-
free(this->buffer);
162+
free(this->receive_buffer);
163+
free(this->send_buffer);
163164
}
164165

165166
boolean PubSubClient::connect(const char *id) {
@@ -195,9 +196,9 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
195196

196197
if (result == 1) {
197198
nextMsgId = 1;
198-
// Leave room in the buffer for header and variable length field
199+
// Leave room in the receive_buffer for header and variable length field
199200
uint16_t length = MQTT_MAX_HEADER_SIZE;
200-
unsigned int j;
201+
size_t j;
201202

202203
#if MQTT_VERSION == MQTT_VERSION_3_1
203204
uint8_t d[9] = {0x00,0x06,'M','Q','I','s','d','p', MQTT_VERSION};
@@ -207,7 +208,7 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
207208
#define MQTT_HEADER_VERSION_LENGTH 7
208209
#endif
209210
for (j = 0;j<MQTT_HEADER_VERSION_LENGTH;j++) {
210-
this->buffer[length++] = d[j];
211+
this->receive_buffer[length++] = d[j];
211212
}
212213

213214
uint8_t v;
@@ -227,30 +228,30 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
227228
v = v|(0x80>>1);
228229
}
229230
}
230-
this->buffer[length++] = v;
231+
this->receive_buffer[length++] = v;
231232

232-
this->buffer[length++] = ((this->keepAlive) >> 8);
233-
this->buffer[length++] = ((this->keepAlive) & 0xFF);
233+
this->receive_buffer[length++] = ((this->keepAlive) >> 8);
234+
this->receive_buffer[length++] = ((this->keepAlive) & 0xFF);
234235

235236
CHECK_STRING_LENGTH(length,id)
236-
length = writeString(id,this->buffer,length);
237+
length = writeString(id,this->receive_buffer,length);
237238
if (willTopic) {
238239
CHECK_STRING_LENGTH(length,willTopic)
239-
length = writeString(willTopic,this->buffer,length);
240+
length = writeString(willTopic,this->receive_buffer,length);
240241
CHECK_STRING_LENGTH(length,willMessage)
241-
length = writeString(willMessage,this->buffer,length);
242+
length = writeString(willMessage,this->receive_buffer,length);
242243
}
243244

244245
if(user != NULL) {
245246
CHECK_STRING_LENGTH(length,user)
246-
length = writeString(user,this->buffer,length);
247+
length = writeString(user,this->receive_buffer,length);
247248
if(pass != NULL) {
248249
CHECK_STRING_LENGTH(length,pass)
249-
length = writeString(pass,this->buffer,length);
250+
length = writeString(pass,this->send_buffer,length);
250251
}
251252
}
252253

253-
write(MQTTCONNECT,this->buffer,length-MQTT_MAX_HEADER_SIZE);
254+
write(MQTTCONNECT,this->receive_buffer,length-MQTT_MAX_HEADER_SIZE);
254255

255256
lastInActivity = lastOutActivity = millis();
256257

@@ -266,13 +267,13 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
266267
uint32_t len = readPacket(&llen);
267268

268269
if (len == 4) {
269-
if (buffer[3] == 0) {
270+
if (receive_buffer[3] == 0) {
270271
lastInActivity = millis();
271272
pingOutstanding = false;
272273
_state = MQTT_CONNECTED;
273274
return true;
274275
} else {
275-
_state = buffer[3];
276+
_state = receive_buffer[3];
276277
}
277278
}
278279
_client->stop();
@@ -311,8 +312,8 @@ boolean PubSubClient::readByte(uint8_t * result, uint16_t * index){
311312

312313
uint32_t PubSubClient::readPacket(uint8_t* lengthLength) {
313314
uint16_t len = 0;
314-
if(!readByte(this->buffer, &len)) return 0;
315-
bool isPublish = (this->buffer[0]&0xF0) == MQTTPUBLISH;
315+
if(!readByte(this->receive_buffer, &len)) return 0;
316+
bool isPublish = (this->receive_buffer[0]&0xF0) == MQTTPUBLISH;
316317
uint32_t multiplier = 1;
317318
uint32_t length = 0;
318319
uint8_t digit = 0;
@@ -327,19 +328,19 @@ uint32_t PubSubClient::readPacket(uint8_t* lengthLength) {
327328
return 0;
328329
}
329330
if(!readByte(&digit)) return 0;
330-
this->buffer[len++] = digit;
331+
this->receive_buffer[len++] = digit;
331332
length += (digit & 127) * multiplier;
332333
multiplier <<=7; //multiplier *= 128
333334
} while ((digit & 128) != 0);
334335
*lengthLength = len-1;
335336

336337
if (isPublish) {
337338
// Read in topic length to calculate bytes to skip over for Stream writing
338-
if(!readByte(this->buffer, &len)) return 0;
339-
if(!readByte(this->buffer, &len)) return 0;
340-
skip = (this->buffer[*lengthLength+1]<<8)+this->buffer[*lengthLength+2];
339+
if(!readByte(this->receive_buffer, &len)) return 0;
340+
if(!readByte(this->receive_buffer, &len)) return 0;
341+
skip = (this->receive_buffer[*lengthLength+1]<<8)+this->receive_buffer[*lengthLength+2];
341342
start = 2;
342-
if (this->buffer[0]&MQTTQOS1) {
343+
if (this->receive_buffer[0]&MQTTQOS1) {
343344
// skip message id
344345
skip += 2;
345346
}
@@ -355,7 +356,7 @@ uint32_t PubSubClient::readPacket(uint8_t* lengthLength) {
355356
}
356357

357358
if (len < this->bufferSize) {
358-
this->buffer[len] = digit;
359+
this->receive_buffer[len] = digit;
359360
len++;
360361
}
361362
idx++;
@@ -381,41 +382,41 @@ boolean PubSubClient::loop_read() {
381382
}
382383
unsigned long t = millis();
383384
lastInActivity = t;
384-
uint8_t type = buffer[0]&0xF0;
385+
uint8_t type = receive_buffer[0]&0xF0;
385386

386387
switch(type) {
387388
case MQTTPUBLISH:
388389
{
389390
if (callback) {
390-
const boolean msgId_present = (buffer[0]&0x06) == MQTTQOS1;
391+
const boolean msgId_present = (receive_buffer[0]&0x06) == MQTTQOS1;
391392
const uint16_t tl_offset = llen+1;
392-
const uint16_t tl = (buffer[tl_offset]<<8)+buffer[tl_offset+1]; /* topic length in bytes */
393+
const uint16_t tl = (receive_buffer[tl_offset]<<8)+receive_buffer[tl_offset+1]; /* topic length in bytes */
393394
const uint16_t topic_offset = tl_offset+2;
394395
const uint16_t msgId_offset = topic_offset+tl;
395396
const uint16_t payload_offset = msgId_present ? msgId_offset+2 : msgId_offset;
396397
if ((payload_offset) >= this->bufferSize) {return false;}
397398
if (len < payload_offset) {return false;}
398-
memmove(buffer+topic_offset-1,buffer+topic_offset,tl); /* move topic inside buffer 1 byte to front */
399-
buffer[topic_offset-1+tl] = 0; /* end the topic as a 'C' string with \x00 */
400-
char *topic = (char*) buffer+topic_offset-1;
399+
memmove(receive_buffer+topic_offset-1,receive_buffer+topic_offset,tl); /* move topic inside receive_buffer 1 byte to front */
400+
receive_buffer[topic_offset-1+tl] = 0; /* end the topic as a 'C' string with \x00 */
401+
char *topic = (char*) receive_buffer+topic_offset-1;
401402
uint8_t *payload;
402403
// msgId only present for QOS>0
403404
if (msgId_present) {
404-
const uint16_t msgId = (buffer[msgId_offset]<<8)+buffer[msgId_offset+1];
405-
payload = buffer+payload_offset;
405+
const uint16_t msgId = (receive_buffer[msgId_offset]<<8)+receive_buffer[msgId_offset+1];
406+
payload = receive_buffer+payload_offset;
406407
callback(topic,payload,len-payload_offset);
407408
if (_client->connected()) {
408-
buffer[0] = MQTTPUBACK;
409-
buffer[1] = 2;
410-
buffer[2] = (msgId >> 8);
411-
buffer[3] = (msgId & 0xFF);
412-
if (_client->write(buffer,4) != 0) {
409+
receive_buffer[0] = MQTTPUBACK;
410+
receive_buffer[1] = 2;
411+
receive_buffer[2] = (msgId >> 8);
412+
receive_buffer[3] = (msgId & 0xFF);
413+
if (_client->write(receive_buffer,4) != 0) {
413414
lastOutActivity = t;
414415
}
415416
}
416417
} else {
417418
// No msgId
418-
payload = buffer+payload_offset;
419+
payload = receive_buffer+payload_offset;
419420
callback(topic,payload,len-payload_offset);
420421
}
421422
}
@@ -424,9 +425,9 @@ boolean PubSubClient::loop_read() {
424425
case MQTTPINGREQ:
425426
{
426427
if (_client->connected()) {
427-
buffer[0] = MQTTPINGRESP;
428-
buffer[1] = 0;
429-
_client->write(buffer,2);
428+
receive_buffer[0] = MQTTPINGRESP;
429+
receive_buffer[1] = 0;
430+
_client->write(receive_buffer,2);
430431
}
431432
break;
432433
}
@@ -451,9 +452,9 @@ boolean PubSubClient::loop() {
451452
_client->stop();
452453
return false;
453454
} else {
454-
buffer[0] = MQTTPINGREQ;
455-
buffer[1] = 0;
456-
if (_client->write(buffer,2) != 0) {
455+
receive_buffer[0] = MQTTPINGREQ;
456+
receive_buffer[1] = 0;
457+
if (_client->write(receive_buffer,2) != 0) {
457458
lastOutActivity = t;
458459
lastInActivity = t;
459460
}
@@ -483,22 +484,22 @@ boolean PubSubClient::publish(const char* topic, const uint8_t* payload, size_t
483484
// Too long
484485
return false;
485486
}
486-
// Leave room in the buffer for header and variable length field
487+
// Leave room in the send_buffer for header and variable length field
487488
uint16_t length = MQTT_MAX_HEADER_SIZE;
488-
length = writeString(topic,this->buffer,length);
489+
length = writeString(topic,this->send_buffer,length);
489490

490491
// Add payload
491492
uint16_t i;
492493
for (i=0;i<plength;i++) {
493-
this->buffer[length++] = payload[i];
494+
this->send_buffer[length++] = payload[i];
494495
}
495496

496497
// Write the header
497498
uint8_t header = MQTTPUBLISH;
498499
if (retained) {
499500
header |= 1;
500501
}
501-
return write(header,this->buffer,length-MQTT_MAX_HEADER_SIZE);
502+
return write(header,this->send_buffer,length-MQTT_MAX_HEADER_SIZE);
502503
}
503504
return false;
504505
}
@@ -510,10 +511,10 @@ boolean PubSubClient::publish_P(const char* topic, const char* payload, boolean
510511
boolean PubSubClient::publish_P(const char* topic, const uint8_t* payload, size_t plength, boolean retained) {
511512
uint8_t llen = 0;
512513
uint8_t digit;
513-
unsigned int rc = 0;
514+
size_t rc = 0;
514515
uint16_t tlen;
515-
unsigned int pos = 0;
516-
unsigned int i;
516+
size_t pos = 0;
517+
size_t i;
517518
uint8_t header;
518519
size_t len;
519520
size_t expectedLength;
@@ -528,21 +529,21 @@ boolean PubSubClient::publish_P(const char* topic, const uint8_t* payload, size_
528529
if (retained) {
529530
header |= 1;
530531
}
531-
this->buffer[pos++] = header;
532+
this->send_buffer[pos++] = header;
532533
len = plength + 2 + tlen;
533534
do {
534535
digit = len & 127; //digit = len %128
535536
len >>= 7; //len = len / 128
536537
if (len > 0) {
537538
digit |= 0x80;
538539
}
539-
this->buffer[pos++] = digit;
540+
this->send_buffer[pos++] = digit;
540541
llen++;
541542
} while(len>0);
542543

543-
pos = writeString(topic,this->buffer,pos);
544+
pos = writeString(topic,this->send_buffer,pos);
544545

545-
rc += _client->write(this->buffer,pos);
546+
rc += _client->write(this->send_buffer,pos);
546547

547548
for (i=0;i<plength;i++) {
548549
rc += _client->write((char)pgm_read_byte_near(payload + i));
@@ -559,13 +560,13 @@ boolean PubSubClient::beginPublish(const char* topic, size_t plength, boolean re
559560
if (connected()) {
560561
// Send the header and variable length field
561562
uint16_t length = MQTT_MAX_HEADER_SIZE;
562-
length = writeString(topic,this->buffer,length);
563+
length = writeString(topic,this->send_buffer,length);
563564
uint8_t header = MQTTPUBLISH;
564565
if (retained) {
565566
header |= 1;
566567
}
567-
size_t hlen = buildHeader(header, this->buffer, plength+length-MQTT_MAX_HEADER_SIZE);
568-
uint16_t rc = _client->write(this->buffer+(MQTT_MAX_HEADER_SIZE-hlen),length-(MQTT_MAX_HEADER_SIZE-hlen));
568+
size_t hlen = buildHeader(header, this->send_buffer, plength+length-MQTT_MAX_HEADER_SIZE);
569+
uint16_t rc = _client->write(this->send_buffer+(MQTT_MAX_HEADER_SIZE-hlen),length-(MQTT_MAX_HEADER_SIZE-hlen));
569570
lastOutActivity = millis();
570571
return (rc == (length-(MQTT_MAX_HEADER_SIZE-hlen)));
571572
}
@@ -651,17 +652,17 @@ boolean PubSubClient::subscribe(const char* topic, uint8_t qos) {
651652
return false;
652653
}
653654
if (connected()) {
654-
// Leave room in the buffer for header and variable length field
655+
// Leave room in the send_buffer for header and variable length field
655656
uint16_t length = MQTT_MAX_HEADER_SIZE;
656657
nextMsgId++;
657658
if (nextMsgId == 0) {
658659
nextMsgId = 1;
659660
}
660-
this->buffer[length++] = (nextMsgId >> 8);
661-
this->buffer[length++] = (nextMsgId & 0xFF);
662-
length = writeString((char*)topic, this->buffer,length);
663-
this->buffer[length++] = qos;
664-
return write(MQTTSUBSCRIBE|MQTTQOS1,this->buffer,length-MQTT_MAX_HEADER_SIZE);
661+
this->send_buffer[length++] = (nextMsgId >> 8);
662+
this->send_buffer[length++] = (nextMsgId & 0xFF);
663+
length = writeString((char*)topic, this->send_buffer,length);
664+
this->send_buffer[length++] = qos;
665+
return write(MQTTSUBSCRIBE|MQTTQOS1,this->send_buffer,length-MQTT_MAX_HEADER_SIZE);
665666
}
666667
return false;
667668
}
@@ -681,18 +682,18 @@ boolean PubSubClient::unsubscribe(const char* topic) {
681682
if (nextMsgId == 0) {
682683
nextMsgId = 1;
683684
}
684-
this->buffer[length++] = (nextMsgId >> 8);
685-
this->buffer[length++] = (nextMsgId & 0xFF);
686-
length = writeString(topic, this->buffer,length);
687-
return write(MQTTUNSUBSCRIBE|MQTTQOS1,this->buffer,length-MQTT_MAX_HEADER_SIZE);
685+
this->send_buffer[length++] = (nextMsgId >> 8);
686+
this->send_buffer[length++] = (nextMsgId & 0xFF);
687+
length = writeString(topic, this->send_buffer,length);
688+
return write(MQTTUNSUBSCRIBE|MQTTQOS1,this->send_buffer,length-MQTT_MAX_HEADER_SIZE);
688689
}
689690
return false;
690691
}
691692

692693
void PubSubClient::disconnect() {
693-
this->buffer[0] = MQTTDISCONNECT;
694-
this->buffer[1] = 0;
695-
_client->write(this->buffer,2);
694+
this->send_buffer[0] = MQTTDISCONNECT;
695+
this->send_buffer[1] = 0;
696+
_client->write(this->send_buffer,2);
696697
_state = MQTT_DISCONNECTED;
697698
_client->flush();
698699
_client->stop();
@@ -775,17 +776,24 @@ boolean PubSubClient::setBufferSize(uint16_t size) {
775776
return false;
776777
}
777778
if (this->bufferSize == 0) {
778-
this->buffer = (uint8_t*)malloc(size);
779+
this->receive_buffer = (uint8_t*)malloc(size);
780+
this->send_buffer = (uint8_t*)malloc(size);
779781
} else {
780-
uint8_t* newBuffer = (uint8_t*)realloc(this->buffer, size);
782+
uint8_t* newBuffer = (uint8_t*)realloc(this->receive_buffer, size);
781783
if (newBuffer != NULL) {
782-
this->buffer = newBuffer;
784+
this->receive_buffer = newBuffer;
785+
} else {
786+
return false;
787+
}
788+
newBuffer = (uint8_t*)realloc(this->send_buffer, size);
789+
if (newBuffer != NULL) {
790+
this->send_buffer = newBuffer;
783791
} else {
784792
return false;
785793
}
786794
}
787795
this->bufferSize = size;
788-
return (this->buffer != NULL);
796+
return (this->receive_buffer != NULL) && (this->send_buffer != NULL);
789797
}
790798

791799
uint16_t PubSubClient::getBufferSize() {

0 commit comments

Comments
 (0)