QOS2 messages not being sent in order when sending queued messages after automatic re-connection [message #1820800] |
Wed, 29 January 2020 10:15 |
Kiran G Messages: 1 Registered: January 2020 |
Junior Member |
|
|
I am implementing persistence module using Paho c++ which deletes a record from a database only when the corresponding data has been successfully published to the cloud or queued internally by paho. Relevant incomplete code snippet below:
```
void mqtt_module::start() {
connect();
while (!kill_flag) {
if(client->is_connected()) {
records = read_persistance_database();
if (records.size() > 0) {
for (std::vector<record>::iterator it = records.begin(); it != records.end(); ++it) {
try {
int* rowid;
rowid = new int;
*rowid = it->rowid;
auto pubmsg = mqtt::make_message(it->topic, it->row_data);
pubmsg->set_qos(it->qos);
logger->debug("[{}] : [MQTT] publish to topic {} message {}", LOG_TAG,it->topic,it->row_data);
client->publish(pubmsg);
pubtok->set_user_context(static_cast<void*>(rowid));
delete_record(*rowid); //If publish exception doesnt happen, delete record
} catch (const mqtt::exception& exc) {
break; //If publish fails, break and read the DB again. this way messages are not sent out of sequence
}
}
records.clear();
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
}
}
void mqtt_module::on_success(const mqtt::token& tok) {
int* rowid;
rowid = static_cast<int*>(tok.get_user_context());
logger->info("Publush success for row {} ",*rowid);
}
bool mqtt_module::connect() {
//callback cb;
client->set_callback(*this);
mqtt::connect_options connopts(config->mqtt_username, config->mqtt_password);
connopts.set_keep_alive_interval(3);
connopts.set_connect_timeout(1);
connopts.set_automatic_reconnect(true);
connopts.set_clean_session(false);
mqtt::ssl_options sslopts;
sslopts.set_trust_store(config->mqtt_ca_path);
mqtt::message willmsg(LWT_TOPIC, LWT_PAYLOAD, QOS, true);
mqtt::will_options will(willmsg);
connopts.set_will(will);
connopts.set_ssl(sslopts);
try {
mqtt::token_ptr conntok = client->connect(connopts);
conntok->wait();
} catch (const mqtt::exception& exc) {
logger->debug(exc.what());
return false;
}
return true;
}
void mqtt_module::connection_lost(const string& cause) {
}
void mqtt_module::connected(const std::string& cause) {
}
void mqtt_module::delivery_complete(mqtt::delivery_token_ptr tok) {
}
```
When I test this by disconnecting internet, there is a short duration from the moment at which I disconnect and the moment when paho decides that its disconnected, when publish does not raise disconnected exception. My understanding is that during this time paho will internally que these messages. When the internet is re-connected it will first send the messages in this queue. My issue is that, when paho sends the messages which were queued during this period , I am seeing out of order messages in the broker, when the QOS is 2. QOS 1 is fine. I am using paho(v1.3.1) and pahocpp(v1.1) . Kindly let me know how to debug/resolve this isse
|
|
|
Powered by
FUDForum. Page generated in 0.03194 seconds