Commit e1043748d6caf872b71f3cdc06273a2ac38c2b58
1 parent
15eb0798
Avoid copying subtopics in publish
Showing
3 changed files
with
32 additions
and
7 deletions
publishcopyfactory.cpp
| ... | ... | @@ -21,6 +21,7 @@ MqttPacket *PublishCopyFactory::getOptimumPacket(const char max_qos, const Proto |
| 21 | 21 | { |
| 22 | 22 | if (packet) |
| 23 | 23 | { |
| 24 | + // TODO: you can't do this when there are client specific mqtt5 properties | |
| 24 | 25 | if (packet->getProtocolVersion() == protocolVersion && orgQos == max_qos) |
| 25 | 26 | { |
| 26 | 27 | assert(orgQos == packet->getQos()); | ... | ... |
types.cpp
| ... | ... | @@ -100,7 +100,7 @@ size_t SubAck::getLengthWithoutFixedHeader() const |
| 100 | 100 | return result; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | -Publish::Publish(const std::string &topic, const std::string &payload, char qos) : | |
| 103 | +PublishBase::PublishBase(const std::string &topic, const std::string &payload, char qos) : | |
| 104 | 104 | topic(topic), |
| 105 | 105 | payload(payload), |
| 106 | 106 | qos(qos) |
| ... | ... | @@ -108,7 +108,7 @@ Publish::Publish(const std::string &topic, const std::string &payload, char qos) |
| 108 | 108 | |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | -size_t Publish::getLengthWithoutFixedHeader() const | |
| 111 | +size_t PublishBase::getLengthWithoutFixedHeader() const | |
| 112 | 112 | { |
| 113 | 113 | int result = topic.length() + payload.length() + 2; |
| 114 | 114 | |
| ... | ... | @@ -122,13 +122,25 @@ size_t Publish::getLengthWithoutFixedHeader() const |
| 122 | 122 | * @brief Publish::setClientSpecificProperties generates the properties byte array for one client. You're supposed to call it before any publish. |
| 123 | 123 | * |
| 124 | 124 | */ |
| 125 | -void Publish::setClientSpecificProperties() | |
| 125 | +void PublishBase::setClientSpecificProperties() | |
| 126 | 126 | { |
| 127 | 127 | if (propertyBuilder) |
| 128 | 128 | propertyBuilder->clearClientSpecificBytes(); |
| 129 | 129 | // TODO. Expires at? |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | +Publish::Publish(const Publish &other) : | |
| 133 | + PublishBase(other) | |
| 134 | +{ | |
| 135 | + | |
| 136 | +} | |
| 137 | + | |
| 138 | +Publish::Publish(const std::string &topic, const std::string &payload, char qos) : | |
| 139 | + PublishBase(topic, payload, qos) | |
| 140 | +{ | |
| 141 | + | |
| 142 | +} | |
| 143 | + | |
| 132 | 144 | PubAck::PubAck(uint16_t packet_id) : |
| 133 | 145 | packet_id(packet_id) |
| 134 | 146 | { | ... | ... |
types.h
| ... | ... | @@ -189,11 +189,13 @@ public: |
| 189 | 189 | size_t getLengthWithoutFixedHeader() const; |
| 190 | 190 | }; |
| 191 | 191 | |
| 192 | -class Publish | |
| 192 | +/** | |
| 193 | + * @brief The PublishBase class was incepted to have an easy default copy constuctor that doesn't copy all fields. | |
| 194 | + */ | |
| 195 | +class PublishBase | |
| 193 | 196 | { |
| 194 | 197 | public: |
| 195 | 198 | std::string topic; |
| 196 | - std::vector<std::string> subtopics; | |
| 197 | 199 | std::string payload; |
| 198 | 200 | char qos = 0; |
| 199 | 201 | bool retain = false; // Note: existing subscribers don't get publishes of retained messages with retain=1. [MQTT-3.3.1-9] |
| ... | ... | @@ -203,12 +205,22 @@ public: |
| 203 | 205 | std::chrono::seconds expiresAfter; |
| 204 | 206 | std::shared_ptr<Mqtt5PropertyBuilder> propertyBuilder; |
| 205 | 207 | |
| 206 | - Publish() = default; | |
| 207 | - Publish(const std::string &topic, const std::string &payload, char qos); | |
| 208 | + PublishBase() = default; | |
| 209 | + PublishBase(const std::string &topic, const std::string &payload, char qos); | |
| 208 | 210 | size_t getLengthWithoutFixedHeader() const; |
| 209 | 211 | void setClientSpecificProperties(); |
| 210 | 212 | }; |
| 211 | 213 | |
| 214 | +class Publish : public PublishBase | |
| 215 | +{ | |
| 216 | +public: | |
| 217 | + std::vector<std::string> subtopics; | |
| 218 | + | |
| 219 | + Publish() = default; | |
| 220 | + Publish(const Publish &other); | |
| 221 | + Publish(const std::string &topic, const std::string &payload, char qos); | |
| 222 | +}; | |
| 223 | + | |
| 212 | 224 | class PubAck |
| 213 | 225 | { |
| 214 | 226 | public: | ... | ... |