Commit 6fed4d58b7d71da026140b2e99b171b31e9a58f2
1 parent
03fd22bd
Half idea of using the circular buffer to store packets
I'm not sure how I'm continueing. I need a safe point.
Showing
3 changed files
with
35 additions
and
24 deletions
mqttpacket.cpp
| ... | ... | @@ -776,7 +776,7 @@ void MqttPacket::handleUnsubscribe() |
| 776 | 776 | sender->writeMqttPacket(response); |
| 777 | 777 | } |
| 778 | 778 | |
| 779 | -void MqttPacket::handlePublish() | |
| 779 | +void MqttPacket::handlePublish(const bool stopAfterParsing) | |
| 780 | 780 | { |
| 781 | 781 | const uint16_t variable_header_length = readTwoBytesToUInt16(); |
| 782 | 782 | |
| ... | ... | @@ -900,11 +900,15 @@ void MqttPacket::handlePublish() |
| 900 | 900 | logger->logf(LOG_DEBUG, "Publish received, topic '%s'. QoS=%d. Retain=%d, dup=%d", publishData.topic.c_str(), qos, retain, dup); |
| 901 | 901 | #endif |
| 902 | 902 | |
| 903 | - sender->getThreadData()->incrementReceivedMessageCount(); | |
| 904 | - | |
| 905 | 903 | payloadLen = remainingAfterPos(); |
| 906 | 904 | payloadStart = pos; |
| 907 | 905 | |
| 906 | + sender->getThreadData()->incrementReceivedMessageCount(); | |
| 907 | + | |
| 908 | + // TODO: or maybe create a function parsePublishData(). | |
| 909 | + if (stopAfterParsing) | |
| 910 | + return; | |
| 911 | + | |
| 908 | 912 | Authentication &authentication = *ThreadGlobals::getAuth(); |
| 909 | 913 | |
| 910 | 914 | // Working with a local copy because the subscribing action will modify this->packet_id. See the PublishCopyFactory. | ... | ... |
mqttpacket.h
| ... | ... | @@ -96,7 +96,7 @@ public: |
| 96 | 96 | void handleSubscribe(); |
| 97 | 97 | void handleUnsubscribe(); |
| 98 | 98 | void handlePing(); |
| 99 | - void handlePublish(); | |
| 99 | + void handlePublish(const bool stopAfterParsing = false); | |
| 100 | 100 | void handlePubAck(); |
| 101 | 101 | void handlePubRec(); |
| 102 | 102 | void handlePubRel(); | ... | ... |
sessionsandsubscriptionsdb.cpp
| ... | ... | @@ -82,6 +82,12 @@ SessionsAndSubscriptionsResult SessionsAndSubscriptionsDB::readDataV2() |
| 82 | 82 | continue; |
| 83 | 83 | |
| 84 | 84 | std::vector<char> reserved(RESERVED_SPACE_SESSIONS_DB_V2); |
| 85 | + CirBuf cirbuf(1024); | |
| 86 | + | |
| 87 | + // TODO: all that settings and thread data needs to be removed from Client. | |
| 88 | + std::shared_ptr<ThreadData> dummyThreadData; | |
| 89 | + std::shared_ptr<Settings> dummySettings(new Settings()); // TODO: this is wrong: these are not from config file | |
| 90 | + std::shared_ptr<Client> dummyClient(new Client(0, dummyThreadData, nullptr, false, nullptr, dummySettings, false)); | |
| 85 | 91 | |
| 86 | 92 | for (uint32_t i = 0; i < nrOfSessions; i++) |
| 87 | 93 | { |
| ... | ... | @@ -106,22 +112,20 @@ SessionsAndSubscriptionsResult SessionsAndSubscriptionsDB::readDataV2() |
| 106 | 112 | for (uint32_t i = 0; i < nrOfQueuedQoSPackets; i++) |
| 107 | 113 | { |
| 108 | 114 | const uint16_t id = readUint16(eofFound); |
| 109 | - const uint32_t topicSize = readUint32(eofFound); | |
| 110 | - const uint32_t payloadSize = readUint32(eofFound); | |
| 115 | + const uint32_t packlen = readUint32(eofFound); | |
| 111 | 116 | |
| 112 | 117 | assert(id > 0); |
| 113 | 118 | |
| 114 | - readCheck(buf.data(), 1, 1, f); | |
| 115 | - const unsigned char qos = buf[0]; | |
| 119 | + cirbuf.reset(); | |
| 120 | + cirbuf.ensureFreeSpace(packlen + 32); | |
| 116 | 121 | |
| 117 | - readCheck(buf.data(), 1, topicSize, f); | |
| 118 | - const std::string topic(buf.data(), topicSize); | |
| 122 | + readCheck(cirbuf.headPtr(), 1, packlen, f); | |
| 123 | + cirbuf.advanceHead(packlen); | |
| 124 | + MqttPacket pack(cirbuf, packlen, 2, dummyClient); // TODO: store the 2 in the file | |
| 119 | 125 | |
| 120 | - makeSureBufSize(payloadSize); | |
| 121 | - readCheck(buf.data(), 1, payloadSize, f); | |
| 122 | - const std::string payload(buf.data(), payloadSize); | |
| 126 | + pack.handlePublish(true); | |
| 127 | + Publish pub(pack.getPublishData()); | |
| 123 | 128 | |
| 124 | - Publish pub(topic, payload, qos); | |
| 125 | 129 | logger->logf(LOG_DEBUG, "Loaded QoS %d message for topic '%s'.", pub.qos, pub.topic.c_str()); |
| 126 | 130 | ses->qosPacketQueue.queuePublish(std::move(pub), id); |
| 127 | 131 | } |
| ... | ... | @@ -211,6 +215,8 @@ void SessionsAndSubscriptionsDB::saveData(const std::vector<std::unique_ptr<Sess |
| 211 | 215 | |
| 212 | 216 | writeUint32(sessions.size()); |
| 213 | 217 | |
| 218 | + CirBuf cirbuf(1024); | |
| 219 | + | |
| 214 | 220 | for (const std::unique_ptr<Session> &ses : sessions) |
| 215 | 221 | { |
| 216 | 222 | logger->logf(LOG_DEBUG, "Saving session '%s'.", ses->getClientId().c_str()); |
| ... | ... | @@ -231,22 +237,23 @@ void SessionsAndSubscriptionsDB::saveData(const std::vector<std::unique_ptr<Sess |
| 231 | 237 | |
| 232 | 238 | for (const QueuedPublish &p: ses->qosPacketQueue) |
| 233 | 239 | { |
| 240 | + qosPacketsCounted++; | |
| 241 | + | |
| 234 | 242 | const Publish &pub = p.getPublish(); |
| 243 | + assert(!pub.splitTopic); | |
| 244 | + assert(pub.topicAlias == 0); | |
| 235 | 245 | |
| 236 | 246 | logger->logf(LOG_DEBUG, "Saving QoS %d message for topic '%s'.", pub.qos, pub.topic.c_str()); |
| 237 | 247 | |
| 238 | - qosPacketsCounted++; | |
| 248 | + const MqttPacket pack(ProtocolVersion::Mqtt5, pub); | |
| 249 | + const uint32_t packSize = pack.getSizeIncludingNonPresentHeader(); | |
| 250 | + cirbuf.reset(); | |
| 251 | + cirbuf.ensureFreeSpace(packSize + 32); | |
| 252 | + pack.readIntoBuf(cirbuf); | |
| 239 | 253 | |
| 240 | 254 | writeUint16(p.getPacketId()); |
| 241 | - | |
| 242 | - writeUint32(pub.topic.length()); | |
| 243 | - writeUint32(pub.payload.size()); | |
| 244 | - | |
| 245 | - const char qos = pub.qos; | |
| 246 | - writeCheck(&qos, 1, 1, f); | |
| 247 | - | |
| 248 | - writeCheck(pub.topic.c_str(), 1, pub.topic.length(), f); | |
| 249 | - writeCheck(pub.payload.c_str(), 1, pub.payload.length(), f); | |
| 255 | + writeUint32(packSize); | |
| 256 | + writeCheck(cirbuf.tailPtr(), 1, cirbuf.usedBytes(), f); | |
| 250 | 257 | } |
| 251 | 258 | |
| 252 | 259 | assert(qosPacketsExpected == qosPacketsCounted); | ... | ... |