Commit 546c8348aa071a46ee580f0dbfa1752fd4398623

Authored by Wiebe Cazemier
1 parent 4bf112b6

Add parseSubAckData()

Will be used for the test client I have in mind.
mqttpacket.cpp
... ... @@ -1275,6 +1275,52 @@ void MqttPacket::handlePubComp()
1275 1275 sender->getSession()->removeOutgoingQoS2MessageId(packet_id);
1276 1276 }
1277 1277  
  1278 +SubAckData MqttPacket::parseSubAckData()
  1279 +{
  1280 + if (this->packetType != PacketType::SUBACK)
  1281 + throw std::runtime_error("Packet must be suback packet.");
  1282 +
  1283 + setPosToDataStart();
  1284 +
  1285 + SubAckData result;
  1286 +
  1287 + result.packet_id = readTwoBytesToUInt16();
  1288 + this->packet_id = result.packet_id;
  1289 +
  1290 + if (this->protocolVersion >= ProtocolVersion::Mqtt5 )
  1291 + {
  1292 + const size_t proplen = decodeVariableByteIntAtPos();
  1293 + const size_t prop_end_at = pos + proplen;
  1294 +
  1295 + while (pos < prop_end_at)
  1296 + {
  1297 + const Mqtt5Properties prop = static_cast<Mqtt5Properties>(readByte());
  1298 +
  1299 + switch (prop)
  1300 + {
  1301 + case Mqtt5Properties::ReasonString:
  1302 + result.reasonString = readBytesToString();
  1303 + break;
  1304 + case Mqtt5Properties::UserProperty:
  1305 + readUserProperty();
  1306 + break;
  1307 + default:
  1308 + throw ProtocolError("Invalid property in suback.", ReasonCodes::ProtocolError);
  1309 + }
  1310 + }
  1311 + }
  1312 +
  1313 + // payload starts here
  1314 +
  1315 + while (!atEnd())
  1316 + {
  1317 + uint8_t code = readByte();
  1318 + result.subAckCodes.push_back(code);
  1319 + }
  1320 +
  1321 + return result;
  1322 +}
  1323 +
1278 1324 void MqttPacket::calculateRemainingLength()
1279 1325 {
1280 1326 assert(fixed_header_length == 0); // because you're not supposed to call this on packet that we already know the length of.
... ...
mqttpacket.h
... ... @@ -124,6 +124,7 @@ public:
124 124 void handlePubRec();
125 125 void handlePubRel();
126 126 void handlePubComp();
  127 + SubAckData parseSubAckData();
127 128  
128 129 uint8_t getFixedHeaderLength() const;
129 130 size_t getSizeIncludingNonPresentHeader() const;
... ...
packetdatatypes.h
... ... @@ -47,5 +47,12 @@ struct DisconnectData
47 47 uint32_t session_expiry_interval = 0;
48 48 };
49 49  
  50 +struct SubAckData
  51 +{
  52 + uint16_t packet_id;
  53 + std::string reasonString;
  54 + std::vector<uint8_t> subAckCodes;
  55 +};
  56 +
50 57  
51 58 #endif // PACKETDATATYPES_H
... ...