Skip to content

Commit

Permalink
Include QoS in getPublishLayoutCompareKey
Browse files Browse the repository at this point in the history
Doesn't change anything, just makes more sense.
  • Loading branch information
halfgaar committed Nov 22, 2024
1 parent 8116852 commit 353630a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
3 changes: 2 additions & 1 deletion client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ PacketDropReason Client::writeMqttPacketAndBlameThisClient(
MqttPacket *p = copyFactory.getOptimumPacket(max_qos, this->protocolVersion, topic_alias, skip_topic, subscriptionIdentifier);

assert(static_cast<bool>(p->getQos()) == static_cast<bool>(max_qos));
assert(PublishCopyFactory::getPublishLayoutCompareKey(protocolVersion) == PublishCopyFactory::getPublishLayoutCompareKey(p->getProtocolVersion()));
assert(PublishCopyFactory::getPublishLayoutCompareKey(this->protocolVersion, p->getQos()) ==
PublishCopyFactory::getPublishLayoutCompareKey(p->getProtocolVersion(), p->getQos()));

if (p->getQos() > 0)
{
Expand Down
27 changes: 17 additions & 10 deletions publishcopyfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,16 @@ MqttPacket *PublishCopyFactory::getOptimumPacket(
return &*this->oneShotPacket;
}

if (!packet->biteArrayCannotBeReused() &&
getPublishLayoutCompareKey(packet->getProtocolVersion()) == getPublishLayoutCompareKey(protocolVersion) &&
static_cast<bool>(orgQos) == static_cast<bool>(actualQos))
const int layout_key_target = getPublishLayoutCompareKey(protocolVersion, actualQos);

if (!packet->biteArrayCannotBeReused() && getPublishLayoutCompareKey(packet->getProtocolVersion(), orgQos) == layout_key_target)
{
return packet;
}

// Note that this cache also possibly contains the expiration interval, but because we're only hitting this block for on-line
// publishers, the interval has not decreased and is fine.
const int cache_key = (getPublishLayoutCompareKey(protocolVersion) * 10) + static_cast<bool>(actualQos);
std::optional<MqttPacket> &cachedPack = constructedPacketCache[cache_key];
std::optional<MqttPacket> &cachedPack = constructedPacketCache[layout_key_target];

if (!cachedPack)
{
Expand Down Expand Up @@ -206,20 +205,28 @@ void PublishCopyFactory::setSharedSubscriptionHashKey(size_t hash)
this->sharedSubscriptionHashKey = hash;
}

int PublishCopyFactory::getPublishLayoutCompareKey(ProtocolVersion pv)
int PublishCopyFactory::getPublishLayoutCompareKey(ProtocolVersion pv, uint8_t qos)
{
int key = 0;

switch (pv)
{
case ProtocolVersion::None:
return 0;
key = 0;
break;
case ProtocolVersion::Mqtt31:
case ProtocolVersion::Mqtt311:
return 1;
key = 1;
break;
case ProtocolVersion::Mqtt5:
return 2;
key = 2;
break;
default:
return 3;
key = 3;
}

key = (key * 10) + static_cast<bool>(qos);
return key;
}


Expand Down
3 changes: 1 addition & 2 deletions publishcopyfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ class PublishCopyFactory
const std::optional<std::string> &getResponseTopic() const;
void setSharedSubscriptionHashKey(size_t hash);
size_t getSharedSubscriptionHashKey() const { return sharedSubscriptionHashKey; }
static int getPublishLayoutCompareKey(ProtocolVersion pv);

static int getPublishLayoutCompareKey(ProtocolVersion pv, uint8_t qos);
};

#endif // PUBLISHCOPYFACTORY_H

0 comments on commit 353630a

Please sign in to comment.