Skip to content

Added explicit settings to control segmented write length and delay #333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ void setOptions(int keepAlive, bool cleanSession, int timeout);
- The `cleanSession` option controls the session retention on the broker side (default: true).
- The `timeout` option controls the default timeout for all commands in milliseconds (default: 1000).

```c++
void setNetworkSegmentedWrite(size_t segmentLength, uint32_t writeDelayMs);
```

- The `segmentLength` option controls maximum segment write length for each successive segmented write into network buffer (default: 65535).
- The `writeDelayMs` option controls delay in ms between each successive segmented write into network buffer (default: 0).
- This function controls the flow of data into the network buffer, helping to prevent buffer overflows and network congestion, particularly when handling large payloads.

Set a custom clock source "custom millis" callback to enable deep sleep applications:

```c++
Expand Down
35 changes: 34 additions & 1 deletion src/MQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,29 @@ inline lwmqtt_err_t lwmqtt_arduino_network_write(void *ref, uint8_t *buffer, siz
auto n = (lwmqtt_arduino_network_t *)ref;

// write bytes
*sent = n->client->write(buffer, len);
size_t partial_written = 0;
size_t written = 0;

while (true) {
if (len - written > n->segmentLength) {
partial_written = n->client->write(buffer + written, n->segmentLength);
written += partial_written;
}
else {
partial_written = n->client->write(buffer + written, len - written);
written += partial_written;
break;
}

delay(n->writeDelayMs);

if (partial_written <= 0) {
return LWMQTT_NETWORK_FAILED_WRITE;
}
}

*sent = written;

if (*sent <= 0) {
return LWMQTT_NETWORK_FAILED_WRITE;
}
Expand Down Expand Up @@ -195,6 +217,9 @@ void MQTTClient::begin(Client &_client) {

// set callback
lwmqtt_set_callback(&this->client, (void *)&this->callback, MQTTClientHandler);

this->network.segmentLength = this->_segmentLength;
this->network.writeDelayMs = this->_writeDelayMs;
}

void MQTTClient::onMessage(MQTTClientCallbackSimple cb) {
Expand Down Expand Up @@ -314,6 +339,14 @@ void MQTTClient::setCleanSession(bool _cleanSession) { this->cleanSession = _cle

void MQTTClient::setTimeout(int _timeout) { this->timeout = _timeout; }

void MQTTClient::setNetworkSegmentedWrite(size_t segmentLength, uint32_t writeDelayMs) {
this->_segmentLength = segmentLength;
this->_writeDelayMs = writeDelayMs;

this->network.segmentLength = this->_segmentLength;
this->network.writeDelayMs = this->_writeDelayMs;
}

void MQTTClient::dropOverflow(bool enabled) {
// configure drop overflow
lwmqtt_drop_overflow(&this->client, enabled, &this->_droppedMessages);
Expand Down
7 changes: 7 additions & 0 deletions src/MQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ typedef struct {

typedef struct {
Client *client;
size_t segmentLength;
uint32_t writeDelayMs;
} lwmqtt_arduino_network_t;

class MQTTClient;
Expand Down Expand Up @@ -93,6 +95,9 @@ class MQTTClient {
lwmqtt_err_t _lastError = (lwmqtt_err_t)0;
uint32_t _droppedMessages = 0;

size_t _segmentLength = 65535;
uint32_t _writeDelayMs = 0;

public:
void *ref = nullptr;

Expand Down Expand Up @@ -141,6 +146,8 @@ class MQTTClient {
this->setTimeout(_timeout);
}

void setNetworkSegmentedWrite(size_t segmentLength, uint32_t writeDelayMs);

void dropOverflow(bool enabled);
uint32_t droppedMessages() { return this->_droppedMessages; }

Expand Down