#include "qospacketqueue.h" #include "cassert" #include "mqttpacket.h" void QoSPacketQueue::erase(const uint16_t packet_id) { auto it = queue.begin(); auto end = queue.end(); while (it != end) { std::shared_ptr &p = *it; if (p->getPacketId() == packet_id) { size_t mem = p->getTotalMemoryFootprint(); qosQueueBytes -= mem; assert(qosQueueBytes >= 0); if (qosQueueBytes < 0) // Should not happen, but correcting a hypothetical bug is fine for this purpose. qosQueueBytes = 0; queue.erase(it); break; } it++; } } size_t QoSPacketQueue::size() const { return queue.size(); } size_t QoSPacketQueue::getByteSize() const { return qosQueueBytes; } /** * @brief QoSPacketQueue::queuePacket makes a copy of the packet because it has state for the receiver in question. * @param p * @param id * @return the packet copy. */ std::shared_ptr QoSPacketQueue::queuePacket(const MqttPacket &p, uint16_t id) { assert(p.getQos() > 0); std::shared_ptr copyPacket = p.getCopy(); copyPacket->setPacketId(id); queue.push_back(copyPacket); qosQueueBytes += copyPacket->getTotalMemoryFootprint(); return copyPacket; } std::shared_ptr QoSPacketQueue::queuePacket(const Publish &pub, uint16_t id) { assert(pub.qos > 0); std::shared_ptr copyPacket(new MqttPacket(pub)); copyPacket->setPacketId(id); queue.push_back(copyPacket); qosQueueBytes += copyPacket->getTotalMemoryFootprint(); return copyPacket; } std::list>::const_iterator QoSPacketQueue::begin() const { return queue.cbegin(); } std::list>::const_iterator QoSPacketQueue::end() const { return queue.cend(); }