diff --git a/mqttpacket.cpp b/mqttpacket.cpp index 6248322..5ffa6cd 100644 --- a/mqttpacket.cpp +++ b/mqttpacket.cpp @@ -1074,14 +1074,28 @@ std::string MqttPacket::getPayloadCopy() const return payload; } + + +uint8_t MqttPacket::getFixedHeaderLength() const +{ + size_t result = this->fixed_header_length; + + if (result == 0) + { + result++; // first byte it always there. + result += remainingLength.getLen(); + } + + return result; +} + size_t MqttPacket::getSizeIncludingNonPresentHeader() const { size_t total = bites.size(); if (fixed_header_length == 0) { - total++; - total += remainingLength.getLen(); + total += getFixedHeaderLength(); } return total; diff --git a/mqttpacket.h b/mqttpacket.h index 1116fd3..5b6cb15 100644 --- a/mqttpacket.h +++ b/mqttpacket.h @@ -116,6 +116,7 @@ public: void handlePubRel(); void handlePubComp(); + uint8_t getFixedHeaderLength() const; size_t getSizeIncludingNonPresentHeader() const; const std::vector &getBites() const { return bites; } char getQos() const { return publishData.qos; } diff --git a/sessionsandsubscriptionsdb.cpp b/sessionsandsubscriptionsdb.cpp index fa497f3..8a0e0ef 100644 --- a/sessionsandsubscriptionsdb.cpp +++ b/sessionsandsubscriptionsdb.cpp @@ -111,6 +111,7 @@ SessionsAndSubscriptionsResult SessionsAndSubscriptionsDB::readDataV2() const uint32_t nrOfQueuedQoSPackets = readUint32(eofFound); for (uint32_t i = 0; i < nrOfQueuedQoSPackets; i++) { + const uint16_t fixed_header_length = readUint16(eofFound); const uint16_t id = readUint16(eofFound); const uint32_t packlen = readUint32(eofFound); @@ -121,11 +122,13 @@ SessionsAndSubscriptionsResult SessionsAndSubscriptionsDB::readDataV2() readCheck(cirbuf.headPtr(), 1, packlen, f); cirbuf.advanceHead(packlen); - MqttPacket pack(cirbuf, packlen, 2, dummyClient); // TODO: store the 2 in the file + MqttPacket pack(cirbuf, packlen, fixed_header_length, dummyClient); pack.parsePublishData(); Publish pub(pack.getPublishData()); + // TODO: update the pub.createdAt + logger->logf(LOG_DEBUG, "Loaded QoS %d message for topic '%s'.", pub.qos, pub.topic.c_str()); ses->qosPacketQueue.queuePublish(std::move(pub), id); } @@ -247,7 +250,7 @@ void SessionsAndSubscriptionsDB::saveData(const std::vectorlogf(LOG_DEBUG, "Saving QoS %d message for topic '%s'.", pub.qos, pub.topic.c_str()); - pub.clearClientSpecificProperties(); + pub.clearClientSpecificProperties(); // TODO: unnecessary? Unwanted even? I need to store the expiration interval. And how to load it? MqttPacket pack(ProtocolVersion::Mqtt5, pub); pack.setPacketId(p.getPacketId()); @@ -256,6 +259,9 @@ void SessionsAndSubscriptionsDB::saveData(const std::vector