Added takeFront method to PacketList

This commit is contained in:
Atlante45 2015-07-14 15:29:49 -07:00
parent bbb00b9d3a
commit c05105e2f9
2 changed files with 15 additions and 4 deletions

View file

@ -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<NLPacket>()), sockAddr);
}
return bytesSent;

View file

@ -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<typename T> std::unique_ptr<T> takeFront();
// Creates a new packet, can be overriden to change return underlying type
virtual std::unique_ptr<Packet> createPacket();
std::unique_ptr<Packet> createPacketWithExtendedHeader();
@ -62,6 +66,7 @@ private:
};
template <typename T> qint64 PacketList::readPrimitive(T* data) {
static_assert(!std::is_pointer<T>::value, "T must not be a pointer");
return QIODevice::read(reinterpret_cast<char*>(data), sizeof(T));
}
@ -70,4 +75,12 @@ template <typename T> qint64 PacketList::writePrimitive(const T& data) {
return QIODevice::write(reinterpret_cast<const char*>(&data), sizeof(T));
}
template<typename T> std::unique_ptr<T> PacketList::takeFront() {
static_assert(std::is_base_of<Packet, T>::value, "T must derive from Packet.");
auto packet = std::move(_packets.front());
_packets.pop_front();
return std::move(std::unique_ptr<T>(dynamic_cast<T*>(packet.release())));
}
#endif // hifi_PacketList_h