From 6158d91b6f3b2c90960f9eaeec4a956049188ba2 Mon Sep 17 00:00:00 2001 From: Wiebe Cazemier Date: Thu, 14 Apr 2022 21:11:26 +0200 Subject: [PATCH] Allow wills to be retained --- publishcopyfactory.cpp | 2 +- subscriptionstore.cpp | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/publishcopyfactory.cpp b/publishcopyfactory.cpp index df9c00f..d75044f 100644 --- a/publishcopyfactory.cpp +++ b/publishcopyfactory.cpp @@ -52,7 +52,7 @@ MqttPacket *PublishCopyFactory::getOptimumPacket(const char max_qos, const Proto return cachedPack.get(); } - // Getting a packet of a Publish object happens on will messages and SYS topics and maybe some others. It's low traffic, anyway. + // Getting an instance of a Publish object happens at least on retained messages, will messages and SYS topics. It's low traffic, anyway. assert(publish); this->oneShotPacket = std::make_unique(protocolVersion, *publish); diff --git a/subscriptionstore.cpp b/subscriptionstore.cpp index 90d6466..9e99af9 100644 --- a/subscriptionstore.cpp +++ b/subscriptionstore.cpp @@ -312,6 +312,9 @@ void SubscriptionStore::sendQueuedWillMessages() PublishCopyFactory factory(p.get()); queuePacketAtSubscribers(factory); + if (p->retain) + setRetainedMessage(*p.get(), (*p.get()).subtopics); + s->clearWill(); } it = pendingWillMessages.erase(it); @@ -336,6 +339,9 @@ void SubscriptionStore::queueWillMessage(const std::shared_ptr &willMes PublishCopyFactory factory(willMessage.get()); queuePacketAtSubscribers(factory); + if (willMessage->retain) + setRetainedMessage(*willMessage.get(), (*willMessage.get()).subtopics); + // Avoid sending two immediate wills when a session is destroyed with the client disconnect. if (session) // session is null when you're destroying a client before a session is assigned. session->clearWill(); @@ -518,6 +524,8 @@ uint64_t SubscriptionStore::giveClientRetainedMessages(const std::shared_ptr &subtopics) { + assert(!subtopics.empty()); + RetainedMessageNode *deepestNode = &retainedMessagesRoot; if (!subtopics.empty() && !subtopics[0].empty() > 0 && subtopics[0][0] == '$') deepestNode = &retainedMessagesRootDollar; -- libgit2 0.21.4