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 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 56 assert(publish);
57 57  
58 58 this->oneShotPacket = std::make_unique<MqttPacket>(protocolVersion, *publish);
... ...
subscriptionstore.cpp
... ... @@ -312,6 +312,9 @@ void SubscriptionStore::sendQueuedWillMessages()
312 312 PublishCopyFactory factory(p.get());
313 313 queuePacketAtSubscribers(factory);
314 314  
  315 + if (p->retain)
  316 + setRetainedMessage(*p.get(), (*p.get()).subtopics);
  317 +
315 318 s->clearWill();
316 319 }
317 320 it = pendingWillMessages.erase(it);
... ... @@ -336,6 +339,9 @@ void SubscriptionStore::queueWillMessage(const std::shared_ptr&lt;Publish&gt; &amp;willMes
336 339 PublishCopyFactory factory(willMessage.get());
337 340 queuePacketAtSubscribers(factory);
338 341  
  342 + if (willMessage->retain)
  343 + setRetainedMessage(*willMessage.get(), (*willMessage.get()).subtopics);
  344 +
339 345 // Avoid sending two immediate wills when a session is destroyed with the client disconnect.
340 346 if (session) // session is null when you're destroying a client before a session is assigned.
341 347 session->clearWill();
... ... @@ -518,6 +524,8 @@ uint64_t SubscriptionStore::giveClientRetainedMessages(const std::shared_ptr&lt;Cli
518 524  
519 525 void SubscriptionStore::setRetainedMessage(const Publish &publish, const std::vector<std::string> &subtopics)
520 526 {
  527 + assert(!subtopics.empty());
  528 +
521 529 RetainedMessageNode *deepestNode = &retainedMessagesRoot;
522 530 if (!subtopics.empty() && !subtopics[0].empty() > 0 && subtopics[0][0] == '$')
523 531 deepestNode = &retainedMessagesRootDollar;
... ...