Commit 483db298a840ce6c2061d5d94a3d2f3f20eae2dd
1 parent
5fb96542
Separate QoS 2 parsing and handling
Showing
3 changed files
with
42 additions
and
17 deletions
mqttpacket.cpp
| @@ -1223,21 +1223,29 @@ void MqttPacket::handlePubAck() | @@ -1223,21 +1223,29 @@ void MqttPacket::handlePubAck() | ||
| 1223 | sender->getSession()->clearQosMessage(packet_id, true); | 1223 | sender->getSession()->clearQosMessage(packet_id, true); |
| 1224 | } | 1224 | } |
| 1225 | 1225 | ||
| 1226 | -/** | ||
| 1227 | - * @brief MqttPacket::handlePubRec handles QoS 2 'publish received' packets. The publisher receives these. | ||
| 1228 | - */ | ||
| 1229 | -void MqttPacket::handlePubRec() | 1226 | +PubRecData MqttPacket::parsePubRecData() |
| 1230 | { | 1227 | { |
| 1231 | - const uint16_t packet_id = readTwoBytesToUInt16(); | ||
| 1232 | - | ||
| 1233 | - ReasonCodes reasonCode = ReasonCodes::Success; // Default when not specified, or MQTT3 | 1228 | + setPosToDataStart(); |
| 1229 | + this->publishData.qos = 2; | ||
| 1230 | + this->packet_id = readTwoBytesToUInt16(); | ||
| 1231 | + PubRecData result; | ||
| 1234 | 1232 | ||
| 1235 | if (!atEnd()) | 1233 | if (!atEnd()) |
| 1236 | { | 1234 | { |
| 1237 | - reasonCode = static_cast<ReasonCodes>(readByte()); | 1235 | + result.reasonCode = static_cast<ReasonCodes>(readByte()); |
| 1238 | } | 1236 | } |
| 1239 | 1237 | ||
| 1240 | - const bool publishTerminatesHere = reasonCode >= ReasonCodes::UnspecifiedError; | 1238 | + return result; |
| 1239 | +} | ||
| 1240 | + | ||
| 1241 | +/** | ||
| 1242 | + * @brief MqttPacket::handlePubRec handles QoS 2 'publish received' packets. The publisher receives these. | ||
| 1243 | + */ | ||
| 1244 | +void MqttPacket::handlePubRec() | ||
| 1245 | +{ | ||
| 1246 | + PubRecData data = parsePubRecData(); | ||
| 1247 | + | ||
| 1248 | + const bool publishTerminatesHere = data.reasonCode >= ReasonCodes::UnspecifiedError; | ||
| 1241 | const bool foundAndRemoved = sender->getSession()->clearQosMessage(packet_id, publishTerminatesHere); | 1249 | const bool foundAndRemoved = sender->getSession()->clearQosMessage(packet_id, publishTerminatesHere); |
| 1242 | 1250 | ||
| 1243 | // "If it has sent a PUBREC with a Reason Code of 0x80 or greater, the receiver MUST treat any subsequent PUBLISH packet | 1251 | // "If it has sent a PUBREC with a Reason Code of 0x80 or greater, the receiver MUST treat any subsequent PUBLISH packet |
| @@ -1254,16 +1262,24 @@ void MqttPacket::handlePubRec() | @@ -1254,16 +1262,24 @@ void MqttPacket::handlePubRec() | ||
| 1254 | } | 1262 | } |
| 1255 | } | 1263 | } |
| 1256 | 1264 | ||
| 1265 | +void MqttPacket::parsePubRelData() | ||
| 1266 | +{ | ||
| 1267 | + // MQTT-3.6.1-1, but why do we care, and only care for certain control packets? | ||
| 1268 | + if (first_byte & 0b1101) | ||
| 1269 | + throw ProtocolError("PUBREL first byte LSB must be 0010.", ReasonCodes::MalformedPacket); | ||
| 1270 | + | ||
| 1271 | + setPosToDataStart(); | ||
| 1272 | + this->publishData.qos = 2; | ||
| 1273 | + this->packet_id = readTwoBytesToUInt16(); | ||
| 1274 | +} | ||
| 1275 | + | ||
| 1257 | /** | 1276 | /** |
| 1258 | * @brief MqttPacket::handlePubRel handles QoS 2 'publish release'. The publisher sends these. | 1277 | * @brief MqttPacket::handlePubRel handles QoS 2 'publish release'. The publisher sends these. |
| 1259 | */ | 1278 | */ |
| 1260 | void MqttPacket::handlePubRel() | 1279 | void MqttPacket::handlePubRel() |
| 1261 | { | 1280 | { |
| 1262 | - // MQTT-3.6.1-1, but why do we care, and only care for certain control packets? | ||
| 1263 | - if (first_byte & 0b1101) | ||
| 1264 | - throw ProtocolError("PUBREL first byte LSB must be 0010.", ReasonCodes::MalformedPacket); | 1281 | + parsePubRelData(); |
| 1265 | 1282 | ||
| 1266 | - const uint16_t packet_id = readTwoBytesToUInt16(); | ||
| 1267 | const bool foundAndRemoved = sender->getSession()->removeIncomingQoS2MessageId(packet_id); | 1283 | const bool foundAndRemoved = sender->getSession()->removeIncomingQoS2MessageId(packet_id); |
| 1268 | const ReasonCodes reason = foundAndRemoved ? ReasonCodes::Success : ReasonCodes::PacketIdentifierNotFound; | 1284 | const ReasonCodes reason = foundAndRemoved ? ReasonCodes::Success : ReasonCodes::PacketIdentifierNotFound; |
| 1269 | 1285 | ||
| @@ -1272,12 +1288,19 @@ void MqttPacket::handlePubRel() | @@ -1272,12 +1288,19 @@ void MqttPacket::handlePubRel() | ||
| 1272 | sender->writeMqttPacket(response); | 1288 | sender->writeMqttPacket(response); |
| 1273 | } | 1289 | } |
| 1274 | 1290 | ||
| 1291 | +void MqttPacket::parsePubComp() | ||
| 1292 | +{ | ||
| 1293 | + setPosToDataStart(); | ||
| 1294 | + this->publishData.qos = 2; | ||
| 1295 | + this->packet_id = readTwoBytesToUInt16(); | ||
| 1296 | +} | ||
| 1297 | + | ||
| 1275 | /** | 1298 | /** |
| 1276 | * @brief MqttPacket::handlePubComp handles QoS 2 'publish complete'. The publisher receives these. | 1299 | * @brief MqttPacket::handlePubComp handles QoS 2 'publish complete'. The publisher receives these. |
| 1277 | */ | 1300 | */ |
| 1278 | void MqttPacket::handlePubComp() | 1301 | void MqttPacket::handlePubComp() |
| 1279 | { | 1302 | { |
| 1280 | - const uint16_t packet_id = readTwoBytesToUInt16(); | 1303 | + parsePubComp(); |
| 1281 | sender->getSession()->removeOutgoingQoS2MessageId(packet_id); | 1304 | sender->getSession()->removeOutgoingQoS2MessageId(packet_id); |
| 1282 | } | 1305 | } |
| 1283 | 1306 |
mqttpacket.h
| @@ -122,8 +122,11 @@ public: | @@ -122,8 +122,11 @@ public: | ||
| 122 | void handlePublish(); | 122 | void handlePublish(); |
| 123 | void parsePubAckData(); | 123 | void parsePubAckData(); |
| 124 | void handlePubAck(); | 124 | void handlePubAck(); |
| 125 | + PubRecData parsePubRecData(); | ||
| 125 | void handlePubRec(); | 126 | void handlePubRec(); |
| 127 | + void parsePubRelData(); | ||
| 126 | void handlePubRel(); | 128 | void handlePubRel(); |
| 129 | + void parsePubComp(); | ||
| 127 | void handlePubComp(); | 130 | void handlePubComp(); |
| 128 | SubAckData parseSubAckData(); | 131 | SubAckData parseSubAckData(); |
| 129 | 132 |
packetdatatypes.h
| @@ -54,10 +54,9 @@ struct SubAckData | @@ -54,10 +54,9 @@ struct SubAckData | ||
| 54 | std::vector<uint8_t> subAckCodes; | 54 | std::vector<uint8_t> subAckCodes; |
| 55 | }; | 55 | }; |
| 56 | 56 | ||
| 57 | -struct PubAckData | 57 | +struct PubRecData |
| 58 | { | 58 | { |
| 59 | - uint16_t packet_id; | 59 | + ReasonCodes reasonCode = ReasonCodes::Success; // Default when not specified, or MQTT3; |
| 60 | }; | 60 | }; |
| 61 | 61 | ||
| 62 | - | ||
| 63 | #endif // PACKETDATATYPES_H | 62 | #endif // PACKETDATATYPES_H |