diff --git a/mqttpacket.cpp b/mqttpacket.cpp index 52a8481..f935412 100644 --- a/mqttpacket.cpp +++ b/mqttpacket.cpp @@ -44,6 +44,7 @@ MqttPacket::MqttPacket(CirBuf &buf, size_t packet_len, size_t fixed_header_lengt unsigned char _packetType = (first_byte & 0xF0) >> 4; packetType = (PacketType)_packetType; pos += fixed_header_length; + externallyReceived = true; } MqttPacket::MqttPacket(const ConnAck &connAck) : @@ -123,7 +124,7 @@ size_t MqttPacket::getRequiredSizeForPublish(const ProtocolVersion protocolVersi MqttPacket::MqttPacket(const ProtocolVersion protocolVersion, const Publish &_publish) : bites(getRequiredSizeForPublish(protocolVersion, _publish)) { - if (publishData.topic.length() > 0xFFFF) + if (_publish.topic.length() > 0xFFFF) { throw ProtocolError("Topic path too long."); } @@ -1301,6 +1302,7 @@ const Publish &MqttPacket::getPublishData() bool MqttPacket::containsClientSpecificProperties() const { assert(packetType == PacketType::PUBLISH); + assert(this->externallyReceived); if (protocolVersion <= ProtocolVersion::Mqtt311 || !publishData.propertyBuilder) return false; diff --git a/mqttpacket.h b/mqttpacket.h index 856a718..1b2795d 100644 --- a/mqttpacket.h +++ b/mqttpacket.h @@ -36,6 +36,13 @@ License along with FlashMQ. If not, see . #include "variablebyteint.h" #include "mqtt5properties.h" +/** + * @brief The MqttPacket class represents incoming and outgonig packets. + * + * Be sure to understand the 'externallyReceived' member. See in-code documentation. + * + * TODO: I could perhaps make a subclass for the externally received one. + */ class MqttPacket { #ifdef TESTING @@ -55,6 +62,12 @@ class MqttPacket size_t payloadStart = 0; size_t payloadLen = 0; bool hasTopicAlias = false; + + // It's important to understand that this class is used for incoming packets as well as new outgoing packets. When we create + // new outgoing packets, we generally know exactly who it's for and the information is only stored in this->bites. So, the + // publishData and fields like hasTopicAlias are invalid in those cases. + bool externallyReceived = false; + Logger *logger = Logger::getInstance(); char *readBytes(size_t length);