diff --git a/subscriptionstore.cpp b/subscriptionstore.cpp index 7c5ca5b..d003ecf 100644 --- a/subscriptionstore.cpp +++ b/subscriptionstore.cpp @@ -359,8 +359,8 @@ void SubscriptionStore::queuePacketAtSubscribers(const std::vector } void SubscriptionStore::giveClientRetainedMessagesRecursively(std::vector::const_iterator cur_subtopic_it, std::vector::const_iterator end, - RetainedMessageNode *this_node, char max_qos, const std::shared_ptr &ses, - bool poundMode, uint64_t &count) const + RetainedMessageNode *this_node, + bool poundMode, std::forward_list &packetList) const { if (cur_subtopic_it == end) { @@ -368,15 +368,14 @@ void SubscriptionStore::giveClientRetainedMessagesRecursively(std::vectorwritePacket(packet, max_qos, true, count); + packetList.emplace_front(publish); } if (poundMode) { for (auto &pair : this_node->children) { std::unique_ptr &child = pair.second; - giveClientRetainedMessagesRecursively(cur_subtopic_it, end, child.get(), max_qos, ses, poundMode, count); + giveClientRetainedMessagesRecursively(cur_subtopic_it, end, child.get(), poundMode, packetList); } } @@ -393,7 +392,7 @@ void SubscriptionStore::giveClientRetainedMessagesRecursively(std::vector &child = pair.second; if (child) // I don't think it can ever be unset, but I'd rather avoid a crash. - giveClientRetainedMessagesRecursively(next_subtopic, end, child.get(), max_qos, ses, poundFound, count); + giveClientRetainedMessagesRecursively(next_subtopic, end, child.get(), poundFound, packetList); } } else @@ -402,7 +401,7 @@ void SubscriptionStore::giveClientRetainedMessagesRecursively(std::vector 0 && subscribeSubtopics[0][0] == '$') startNode = &retainedMessagesRootDollar; - RWLockGuard locker(&retainedMessagesRwlock); - locker.rdlock(); + std::forward_list packetList; + + { + RWLockGuard locker(&retainedMessagesRwlock); + locker.rdlock(); + giveClientRetainedMessagesRecursively(subscribeSubtopics.begin(), subscribeSubtopics.end(), startNode, false, packetList); + } - giveClientRetainedMessagesRecursively(subscribeSubtopics.begin(), subscribeSubtopics.end(), startNode, max_qos, ses, false, count); + for(const MqttPacket &packet : packetList) + { + ses->writePacket(packet, max_qos, true, count); + } return count; } diff --git a/subscriptionstore.h b/subscriptionstore.h index f1c7a0e..1a9041a 100644 --- a/subscriptionstore.h +++ b/subscriptionstore.h @@ -122,8 +122,7 @@ public: void queuePacketAtSubscribers(const std::vector &subtopics, const MqttPacket &packet, bool dollar = false); void giveClientRetainedMessagesRecursively(std::vector::const_iterator cur_subtopic_it, std::vector::const_iterator end, - RetainedMessageNode *this_node, char max_qos, const std::shared_ptr &ses, - bool poundMode, uint64_t &count) const; + RetainedMessageNode *this_node, bool poundMode, std::forward_list &packetList) const; uint64_t giveClientRetainedMessages(const std::shared_ptr &ses, const std::vector &subscribeSubtopics, char max_qos); void setRetainedMessage(const std::string &topic, const std::vector &subtopics, const std::string &payload, char qos);