From c05105e2f9d556358d529c5798e03edb10f22fd7 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 14 Jul 2015 15:29:49 -0700 Subject: [PATCH] Added takeFront method to PacketList --- libraries/networking/src/LimitedNodeList.cpp | 6 ++---- libraries/networking/src/udt/PacketList.h | 13 +++++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/libraries/networking/src/LimitedNodeList.cpp b/libraries/networking/src/LimitedNodeList.cpp index d54fbd093b..005499cc53 100644 --- a/libraries/networking/src/LimitedNodeList.cpp +++ b/libraries/networking/src/LimitedNodeList.cpp @@ -288,10 +288,8 @@ qint64 LimitedNodeList::sendPacketList(NLPacketList& packetList, const Node& des qint64 LimitedNodeList::sendPacketList(NLPacketList& packetList, const HifiSockAddr& sockAddr) { qint64 bytesSent{ 0 }; - auto& packets = packetList._packets; - while (!packets.empty()) { - bytesSent += sendPacket(std::move(packets.front()), sockAddr); - packets.pop_front(); + while (!packetList._packets.empty()) { + bytesSent += sendPacket(std::move(packetList.takeFront()), sockAddr); } return bytesSent; diff --git a/libraries/networking/src/udt/PacketList.h b/libraries/networking/src/udt/PacketList.h index cbd2870ed8..173e8c75fb 100644 --- a/libraries/networking/src/udt/PacketList.h +++ b/libraries/networking/src/udt/PacketList.h @@ -47,6 +47,10 @@ private: PacketList(const PacketList& other) = delete; PacketList& operator=(const PacketList& other) = delete; + // Takes the first packet of the list and returns it. + template std::unique_ptr takeFront(); + + // Creates a new packet, can be overriden to change return underlying type virtual std::unique_ptr createPacket(); std::unique_ptr createPacketWithExtendedHeader(); @@ -62,6 +66,7 @@ private: }; template qint64 PacketList::readPrimitive(T* data) { + static_assert(!std::is_pointer::value, "T must not be a pointer"); return QIODevice::read(reinterpret_cast(data), sizeof(T)); } @@ -70,4 +75,12 @@ template qint64 PacketList::writePrimitive(const T& data) { return QIODevice::write(reinterpret_cast(&data), sizeof(T)); } +template std::unique_ptr PacketList::takeFront() { + static_assert(std::is_base_of::value, "T must derive from Packet."); + + auto packet = std::move(_packets.front()); + _packets.pop_front(); + return std::move(std::unique_ptr(dynamic_cast(packet.release()))); +} + #endif // hifi_PacketList_h \ No newline at end of file