Commit 15eb0798c6ca85b3ba97a32468fd6e3fa711d971
1 parent
7b907c84
still need to avoid unneccessary copy of subtopics
Showing
4 changed files
with
12 additions
and
16 deletions
mqttpacket.cpp
| ... | ... | @@ -910,11 +910,6 @@ void MqttPacket::setDuplicate() |
| 910 | 910 | /** |
| 911 | 911 | * @brief MqttPacket::getPayloadCopy takes part of the vector of bytes and returns it as a string. |
| 912 | 912 | * @return |
| 913 | - * | |
| 914 | - * It's necessary sometimes, but it's against FlashMQ's concept of not parsing the payload. Normally, you can just write out | |
| 915 | - * the whole byte array of an original packet to subscribers. No need to copy and such. | |
| 916 | - * | |
| 917 | - * But, as stated, sometimes it's necessary. | |
| 918 | 913 | */ |
| 919 | 914 | std::string MqttPacket::getPayloadCopy() const |
| 920 | 915 | { |
| ... | ... | @@ -1128,12 +1123,12 @@ void MqttPacket::setRetain() |
| 1128 | 1123 | } |
| 1129 | 1124 | } |
| 1130 | 1125 | |
| 1131 | -Publish *MqttPacket::getPublishData() | |
| 1126 | +const Publish &MqttPacket::getPublishData() | |
| 1132 | 1127 | { |
| 1133 | 1128 | if (payloadLen > 0 && publishData.payload.empty()) |
| 1134 | 1129 | publishData.payload = getPayloadCopy(); |
| 1135 | 1130 | |
| 1136 | - return &publishData; | |
| 1131 | + return publishData; | |
| 1137 | 1132 | } |
| 1138 | 1133 | |
| 1139 | 1134 | void MqttPacket::readIntoBuf(CirBuf &buf) const | ... | ... |
mqttpacket.h
publishcopyfactory.cpp
| ... | ... | @@ -19,8 +19,6 @@ PublishCopyFactory::PublishCopyFactory(Publish *publish) : |
| 19 | 19 | |
| 20 | 20 | MqttPacket *PublishCopyFactory::getOptimumPacket(const char max_qos, const ProtocolVersion protocolVersion) |
| 21 | 21 | { |
| 22 | - // TODO: cache idea: a set of constructed packets per branch in this code, witn an int identifier. | |
| 23 | - | |
| 24 | 22 | if (packet) |
| 25 | 23 | { |
| 26 | 24 | if (packet->getProtocolVersion() == protocolVersion && orgQos == max_qos) |
| ... | ... | @@ -34,12 +32,12 @@ MqttPacket *PublishCopyFactory::getOptimumPacket(const char max_qos, const Proto |
| 34 | 32 | |
| 35 | 33 | if (!cachedPack) |
| 36 | 34 | { |
| 37 | - Publish *orgPublish = packet->getPublishData(); | |
| 38 | - orgPublish->splitTopic = false; | |
| 39 | - orgPublish->qos = max_qos; | |
| 35 | + Publish newPublish(packet->getPublishData()); | |
| 36 | + newPublish.splitTopic = false; | |
| 37 | + newPublish.qos = max_qos; | |
| 40 | 38 | if (protocolVersion >= ProtocolVersion::Mqtt5) |
| 41 | - orgPublish->setClientSpecificProperties(); | |
| 42 | - cachedPack = std::make_unique<MqttPacket>(protocolVersion, *orgPublish); | |
| 39 | + newPublish.setClientSpecificProperties(); | |
| 40 | + cachedPack = std::make_unique<MqttPacket>(protocolVersion, newPublish); | |
| 43 | 41 | } |
| 44 | 42 | |
| 45 | 43 | return cachedPack.get(); |
| ... | ... | @@ -100,11 +98,13 @@ Publish PublishCopyFactory::getNewPublish() const |
| 100 | 98 | |
| 101 | 99 | if (packet) |
| 102 | 100 | { |
| 103 | - Publish p(*packet->getPublishData()); | |
| 101 | + Publish p(packet->getPublishData()); | |
| 102 | + p.qos = orgQos; | |
| 104 | 103 | return p; |
| 105 | 104 | } |
| 106 | 105 | |
| 107 | 106 | Publish p(*publish); |
| 107 | + p.qos = orgQos; | |
| 108 | 108 | return p; |
| 109 | 109 | } |
| 110 | 110 | ... | ... |
types.cpp
| ... | ... | @@ -19,6 +19,7 @@ License along with FlashMQ. If not, see <https://www.gnu.org/licenses/>. |
| 19 | 19 | |
| 20 | 20 | #include "types.h" |
| 21 | 21 | #include "mqtt5properties.h" |
| 22 | +#include "mqttpacket.h" | |
| 22 | 23 | |
| 23 | 24 | ConnAck::ConnAck(const ProtocolVersion protVersion, ReasonCodes return_code, bool session_present) : |
| 24 | 25 | protocol_version(protVersion), | ... | ... |