Skip to content

Commit e7c5bb0

Browse files
committed
added two value constructor to specify buffer sizes separately
1 parent 8978728 commit e7c5bb0

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Other shields and boards should also work if they provide a [Client](https://www
2828

2929
## Notes
3030

31-
- The maximum size for packets being published and received is set by default to 128 bytes. To change the buffer sizes, you need to use `MQTTClient client(256)` instead of just `MQTTClient client` on the top of your sketch. The passed value denotes the read and write buffer size. **Beginning with version 2.6, the message payload is sent separately during publishing. Therefore, the write buffer is only needed to encode the packet header and topic, for which the default 128 bytes should be enough. However, the receiving of messages is still fully constrained by the read buffer.**
31+
- The maximum size for packets being published and received is set by default to 128 bytes. To change the buffer sizes, you need to use `MQTTClient client(256)` or `MQTTClient client(256, 512)` instead of just `MQTTClient client` at the top of your sketch. A single value denotes both the read and write buffer size, two values specify them separately. **Beginning with version 2.6, the message payload is sent directly during publishing. Therefore, the write buffer is only needed to encode the packet header and topic, for which the default 128 bytes should be enough. However, the receiving of messages is still fully constrained by the read buffer, which may be increased if necessary.**
3232

3333
- On the ESP8266 it has been reported that an additional `delay(10);` after `client.loop();` fixes many stability issues with WiFi connections.
3434

src/MQTTClient.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,12 @@ static void MQTTClientHandler(lwmqtt_client_t * /*client*/, void *ref, lwmqtt_st
158158
#endif
159159
}
160160

161-
MQTTClient::MQTTClient(int bufSize) {
161+
MQTTClient::MQTTClient(int readBufSize, int writeBufSize) {
162162
// allocate buffers
163-
this->bufSize = (size_t)bufSize;
164-
this->readBuf = (uint8_t *)malloc((size_t)bufSize + 1);
165-
this->writeBuf = (uint8_t *)malloc((size_t)bufSize);
163+
this->readBufSize = (size_t)readBufSize;
164+
this->writeBufSize = (size_t)writeBufSize;
165+
this->readBuf = (uint8_t *)malloc((size_t)readBufSize + 1);
166+
this->writeBuf = (uint8_t *)malloc((size_t)writeBufSize);
166167
}
167168

168169
MQTTClient::~MQTTClient() {
@@ -184,7 +185,7 @@ void MQTTClient::begin(Client &_client) {
184185
this->netClient = &_client;
185186

186187
// initialize client
187-
lwmqtt_init(&this->client, this->writeBuf, this->bufSize, this->readBuf, this->bufSize);
188+
lwmqtt_init(&this->client, this->writeBuf, this->writeBufSize, this->readBuf, this->readBufSize);
188189

189190
// set timers
190191
lwmqtt_set_timers(&this->client, &this->timer1, &this->timer2, lwmqtt_arduino_timer_set, lwmqtt_arduino_timer_get);

src/MQTTClient.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ typedef struct {
6565

6666
class MQTTClient {
6767
private:
68-
size_t bufSize = 0;
68+
size_t readBufSize = 0;
69+
size_t writeBufSize = 0;
6970
uint8_t *readBuf = nullptr;
7071
uint8_t *writeBuf = nullptr;
7172

@@ -94,7 +95,8 @@ class MQTTClient {
9495
public:
9596
void *ref = nullptr;
9697

97-
explicit MQTTClient(int bufSize = 128);
98+
explicit MQTTClient(int bufSize = 128) : MQTTClient(bufSize, bufSize) {}
99+
MQTTClient(int readSize, int writeBufSize);
98100

99101
~MQTTClient();
100102

0 commit comments

Comments
 (0)