Skip to content

Commit 7afcfb1

Browse files
committed
added methods to support QoS1/2 retry logic
1 parent deefe37 commit 7afcfb1

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,17 @@ bool publish(const char topic[], const char payload[], int length, bool retained
211211
- Beginning with version 2.6, payloads of arbitrary length may be published, see [Notes](#notes).
212212
- The functions return a boolean that indicates if the publishing has been successful (true).
213213
214+
Obtain the last used packet ID and prepare the publication of a duplicate message using the specified packet ID:
215+
216+
```c++
217+
uint16_t lastPacketID();
218+
void prepareDuplicate(uint16_t packetID);
219+
```
220+
221+
- These functions may be used to implement a retry logic for failed publications of QoS1 and QoS2 messages.
222+
- The `lastPacketID()` function can be used after calling `publish()` to obtain the used packet ID.
223+
- The `prepareDuplicate()` function may be called before `publish()` to temporarily change the next used packet ID and flag the message as a duplicate.
224+
214225
Subscribe to a topic:
215226

216227
```c++

src/MQTTClient.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,16 @@ bool MQTTClient::publish(const char topic[], const char payload[], int length, b
392392
message.retained = retained;
393393
message.qos = lwmqtt_qos_t(qos);
394394

395+
// prepare options
396+
lwmqtt_publish_options_t options = lwmqtt_default_publish_options;
397+
398+
// set duplicate packet id if available
399+
if (this->nextDupPacketID > 0) {
400+
options.dup_id = &this->nextDupPacketID;
401+
}
402+
395403
// publish message
396-
this->_lastError = lwmqtt_publish(&this->client, nullptr, lwmqtt_string(topic), message, this->timeout);
404+
this->_lastError = lwmqtt_publish(&this->client, &options, lwmqtt_string(topic), message, this->timeout);
397405
if (this->_lastError != LWMQTT_SUCCESS) {
398406
// close connection
399407
this->close();
@@ -404,6 +412,16 @@ bool MQTTClient::publish(const char topic[], const char payload[], int length, b
404412
return true;
405413
}
406414

415+
uint16_t MQTTClient::lastPacketID() {
416+
// get last packet id from client
417+
return this->client.last_packet_id;
418+
}
419+
420+
void MQTTClient::prepareDuplicate(uint16_t packetID) {
421+
// set next duplicate packet id
422+
this->nextDupPacketID = packetID;
423+
}
424+
407425
bool MQTTClient::subscribe(const char topic[], int qos) {
408426
// return immediately if not connected
409427
if (!this->connected()) {

src/MQTTClient.h

+4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class MQTTClient {
8888
lwmqtt_client_t client = lwmqtt_client_t();
8989

9090
bool _connected = false;
91+
uint16_t nextDupPacketID = 0;
9192
lwmqtt_return_code_t _returnCode = (lwmqtt_return_code_t)0;
9293
lwmqtt_err_t _lastError = (lwmqtt_err_t)0;
9394
uint32_t _droppedMessages = 0;
@@ -170,6 +171,9 @@ class MQTTClient {
170171
}
171172
bool publish(const char topic[], const char payload[], int length, bool retained, int qos);
172173

174+
uint16_t lastPacketID();
175+
void prepareDuplicate(uint16_t packetID);
176+
173177
bool subscribe(const String &topic) { return this->subscribe(topic.c_str()); }
174178
bool subscribe(const String &topic, int qos) { return this->subscribe(topic.c_str(), qos); }
175179
bool subscribe(const char topic[]) { return this->subscribe(topic, 0); }

0 commit comments

Comments
 (0)