Commit 15eb0798c6ca85b3ba97a32468fd6e3fa711d971

Authored by Wiebe Cazemier
1 parent 7b907c84

still need to avoid unneccessary copy of subtopics

mqttpacket.cpp
@@ -910,11 +910,6 @@ void MqttPacket::setDuplicate() @@ -910,11 +910,6 @@ void MqttPacket::setDuplicate()
910 /** 910 /**
911 * @brief MqttPacket::getPayloadCopy takes part of the vector of bytes and returns it as a string. 911 * @brief MqttPacket::getPayloadCopy takes part of the vector of bytes and returns it as a string.
912 * @return 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 std::string MqttPacket::getPayloadCopy() const 914 std::string MqttPacket::getPayloadCopy() const
920 { 915 {
@@ -1128,12 +1123,12 @@ void MqttPacket::setRetain() @@ -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 if (payloadLen > 0 && publishData.payload.empty()) 1128 if (payloadLen > 0 && publishData.payload.empty())
1134 publishData.payload = getPayloadCopy(); 1129 publishData.payload = getPayloadCopy();
1135 1130
1136 - return &publishData; 1131 + return publishData;
1137 } 1132 }
1138 1133
1139 void MqttPacket::readIntoBuf(CirBuf &buf) const 1134 void MqttPacket::readIntoBuf(CirBuf &buf) const
mqttpacket.h
@@ -121,7 +121,7 @@ public: @@ -121,7 +121,7 @@ public:
121 std::string getPayloadCopy() const; 121 std::string getPayloadCopy() const;
122 bool getRetain() const; 122 bool getRetain() const;
123 void setRetain(); 123 void setRetain();
124 - Publish *getPublishData(); 124 + const Publish &getPublishData();
125 }; 125 };
126 126
127 #endif // MQTTPACKET_H 127 #endif // MQTTPACKET_H
publishcopyfactory.cpp
@@ -19,8 +19,6 @@ PublishCopyFactory::PublishCopyFactory(Publish *publish) : @@ -19,8 +19,6 @@ PublishCopyFactory::PublishCopyFactory(Publish *publish) :
19 19
20 MqttPacket *PublishCopyFactory::getOptimumPacket(const char max_qos, const ProtocolVersion protocolVersion) 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 if (packet) 22 if (packet)
25 { 23 {
26 if (packet->getProtocolVersion() == protocolVersion && orgQos == max_qos) 24 if (packet->getProtocolVersion() == protocolVersion && orgQos == max_qos)
@@ -34,12 +32,12 @@ MqttPacket *PublishCopyFactory::getOptimumPacket(const char max_qos, const Proto @@ -34,12 +32,12 @@ MqttPacket *PublishCopyFactory::getOptimumPacket(const char max_qos, const Proto
34 32
35 if (!cachedPack) 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 if (protocolVersion >= ProtocolVersion::Mqtt5) 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 return cachedPack.get(); 43 return cachedPack.get();
@@ -100,11 +98,13 @@ Publish PublishCopyFactory::getNewPublish() const @@ -100,11 +98,13 @@ Publish PublishCopyFactory::getNewPublish() const
100 98
101 if (packet) 99 if (packet)
102 { 100 {
103 - Publish p(*packet->getPublishData()); 101 + Publish p(packet->getPublishData());
  102 + p.qos = orgQos;
104 return p; 103 return p;
105 } 104 }
106 105
107 Publish p(*publish); 106 Publish p(*publish);
  107 + p.qos = orgQos;
108 return p; 108 return p;
109 } 109 }
110 110
types.cpp
@@ -19,6 +19,7 @@ License along with FlashMQ. If not, see &lt;https://www.gnu.org/licenses/&gt;. @@ -19,6 +19,7 @@ License along with FlashMQ. If not, see &lt;https://www.gnu.org/licenses/&gt;.
19 19
20 #include "types.h" 20 #include "types.h"
21 #include "mqtt5properties.h" 21 #include "mqtt5properties.h"
  22 +#include "mqttpacket.h"
22 23
23 ConnAck::ConnAck(const ProtocolVersion protVersion, ReasonCodes return_code, bool session_present) : 24 ConnAck::ConnAck(const ProtocolVersion protVersion, ReasonCodes return_code, bool session_present) :
24 protocol_version(protVersion), 25 protocol_version(protVersion),