Commit 6158d91b6f3b2c90960f9eaeec4a956049188ba2

Authored by Wiebe Cazemier
1 parent 75657bf5

Allow wills to be retained

publishcopyfactory.cpp
@@ -52,7 +52,7 @@ MqttPacket *PublishCopyFactory::getOptimumPacket(const char max_qos, const Proto @@ -52,7 +52,7 @@ MqttPacket *PublishCopyFactory::getOptimumPacket(const char max_qos, const Proto
52 return cachedPack.get(); 52 return cachedPack.get();
53 } 53 }
54 54
55 - // Getting a packet of a Publish object happens on will messages and SYS topics and maybe some others. It's low traffic, anyway. 55 + // Getting an instance of a Publish object happens at least on retained messages, will messages and SYS topics. It's low traffic, anyway.
56 assert(publish); 56 assert(publish);
57 57
58 this->oneShotPacket = std::make_unique<MqttPacket>(protocolVersion, *publish); 58 this->oneShotPacket = std::make_unique<MqttPacket>(protocolVersion, *publish);
subscriptionstore.cpp
@@ -312,6 +312,9 @@ void SubscriptionStore::sendQueuedWillMessages() @@ -312,6 +312,9 @@ void SubscriptionStore::sendQueuedWillMessages()
312 PublishCopyFactory factory(p.get()); 312 PublishCopyFactory factory(p.get());
313 queuePacketAtSubscribers(factory); 313 queuePacketAtSubscribers(factory);
314 314
  315 + if (p->retain)
  316 + setRetainedMessage(*p.get(), (*p.get()).subtopics);
  317 +
315 s->clearWill(); 318 s->clearWill();
316 } 319 }
317 it = pendingWillMessages.erase(it); 320 it = pendingWillMessages.erase(it);
@@ -336,6 +339,9 @@ void SubscriptionStore::queueWillMessage(const std::shared_ptr&lt;Publish&gt; &amp;willMes @@ -336,6 +339,9 @@ void SubscriptionStore::queueWillMessage(const std::shared_ptr&lt;Publish&gt; &amp;willMes
336 PublishCopyFactory factory(willMessage.get()); 339 PublishCopyFactory factory(willMessage.get());
337 queuePacketAtSubscribers(factory); 340 queuePacketAtSubscribers(factory);
338 341
  342 + if (willMessage->retain)
  343 + setRetainedMessage(*willMessage.get(), (*willMessage.get()).subtopics);
  344 +
339 // Avoid sending two immediate wills when a session is destroyed with the client disconnect. 345 // Avoid sending two immediate wills when a session is destroyed with the client disconnect.
340 if (session) // session is null when you're destroying a client before a session is assigned. 346 if (session) // session is null when you're destroying a client before a session is assigned.
341 session->clearWill(); 347 session->clearWill();
@@ -518,6 +524,8 @@ uint64_t SubscriptionStore::giveClientRetainedMessages(const std::shared_ptr&lt;Cli @@ -518,6 +524,8 @@ uint64_t SubscriptionStore::giveClientRetainedMessages(const std::shared_ptr&lt;Cli
518 524
519 void SubscriptionStore::setRetainedMessage(const Publish &publish, const std::vector<std::string> &subtopics) 525 void SubscriptionStore::setRetainedMessage(const Publish &publish, const std::vector<std::string> &subtopics)
520 { 526 {
  527 + assert(!subtopics.empty());
  528 +
521 RetainedMessageNode *deepestNode = &retainedMessagesRoot; 529 RetainedMessageNode *deepestNode = &retainedMessagesRoot;
522 if (!subtopics.empty() && !subtopics[0].empty() > 0 && subtopics[0][0] == '$') 530 if (!subtopics.empty() && !subtopics[0].empty() > 0 && subtopics[0][0] == '$')
523 deepestNode = &retainedMessagesRootDollar; 531 deepestNode = &retainedMessagesRootDollar;