Skip to content

Commit

Permalink
added methods to support QoS1/2 retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Feb 5, 2023
1 parent deefe37 commit 7afcfb1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ bool publish(const char topic[], const char payload[], int length, bool retained
- Beginning with version 2.6, payloads of arbitrary length may be published, see [Notes](#notes).
- The functions return a boolean that indicates if the publishing has been successful (true).
Obtain the last used packet ID and prepare the publication of a duplicate message using the specified packet ID:
```c++
uint16_t lastPacketID();
void prepareDuplicate(uint16_t packetID);
```

- These functions may be used to implement a retry logic for failed publications of QoS1 and QoS2 messages.
- The `lastPacketID()` function can be used after calling `publish()` to obtain the used packet ID.
- The `prepareDuplicate()` function may be called before `publish()` to temporarily change the next used packet ID and flag the message as a duplicate.

Subscribe to a topic:

```c++
Expand Down
20 changes: 19 additions & 1 deletion src/MQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,16 @@ bool MQTTClient::publish(const char topic[], const char payload[], int length, b
message.retained = retained;
message.qos = lwmqtt_qos_t(qos);

// prepare options
lwmqtt_publish_options_t options = lwmqtt_default_publish_options;

// set duplicate packet id if available
if (this->nextDupPacketID > 0) {
options.dup_id = &this->nextDupPacketID;
}

// publish message
this->_lastError = lwmqtt_publish(&this->client, nullptr, lwmqtt_string(topic), message, this->timeout);
this->_lastError = lwmqtt_publish(&this->client, &options, lwmqtt_string(topic), message, this->timeout);
if (this->_lastError != LWMQTT_SUCCESS) {
// close connection
this->close();
Expand All @@ -404,6 +412,16 @@ bool MQTTClient::publish(const char topic[], const char payload[], int length, b
return true;
}

uint16_t MQTTClient::lastPacketID() {
// get last packet id from client
return this->client.last_packet_id;
}

void MQTTClient::prepareDuplicate(uint16_t packetID) {
// set next duplicate packet id
this->nextDupPacketID = packetID;
}

bool MQTTClient::subscribe(const char topic[], int qos) {
// return immediately if not connected
if (!this->connected()) {
Expand Down
4 changes: 4 additions & 0 deletions src/MQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class MQTTClient {
lwmqtt_client_t client = lwmqtt_client_t();

bool _connected = false;
uint16_t nextDupPacketID = 0;
lwmqtt_return_code_t _returnCode = (lwmqtt_return_code_t)0;
lwmqtt_err_t _lastError = (lwmqtt_err_t)0;
uint32_t _droppedMessages = 0;
Expand Down Expand Up @@ -170,6 +171,9 @@ class MQTTClient {
}
bool publish(const char topic[], const char payload[], int length, bool retained, int qos);

uint16_t lastPacketID();
void prepareDuplicate(uint16_t packetID);

bool subscribe(const String &topic) { return this->subscribe(topic.c_str()); }
bool subscribe(const String &topic, int qos) { return this->subscribe(topic.c_str(), qos); }
bool subscribe(const char topic[]) { return this->subscribe(topic, 0); }
Expand Down

0 comments on commit 7afcfb1

Please sign in to comment.