From f2a198dfddfbba06be2c7c74d60f2a99a0350671 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 8 Jul 2015 22:46:43 -0700 Subject: [PATCH] add O_OBJECT macros to appropriate classes, fix PacketList errors --- libraries/networking/src/LimitedNodeList.h | 3 +- libraries/networking/src/NLPacket.h | 11 ++--- .../src/{PacketList.cpp => NLPacketList.cpp} | 40 ++++++++--------- .../src/{PacketList.h => NLPacketList.h} | 43 ++++++++++--------- libraries/networking/src/Packet.h | 1 + 5 files changed, 50 insertions(+), 48 deletions(-) rename libraries/networking/src/{PacketList.cpp => NLPacketList.cpp} (71%) rename libraries/networking/src/{PacketList.h => NLPacketList.h} (55%) diff --git a/libraries/networking/src/LimitedNodeList.h b/libraries/networking/src/LimitedNodeList.h index 3aadb8373d..3d674516d6 100644 --- a/libraries/networking/src/LimitedNodeList.h +++ b/libraries/networking/src/LimitedNodeList.h @@ -39,7 +39,7 @@ #include "Node.h" #include "NLPacket.h" #include "PacketHeaders.h" -#include "PacketList.h" +#include "NLPacketList.h" #include "UUIDHasher.h" const int MAX_PACKET_SIZE = 1450; @@ -69,7 +69,6 @@ Q_DECLARE_METATYPE(SharedNodePointer) using namespace tbb; typedef std::pair UUIDNodePair; typedef concurrent_unordered_map NodeHash; -using NLPacketList = PacketList; typedef quint8 PingType_t; namespace PingType { diff --git a/libraries/networking/src/NLPacket.h b/libraries/networking/src/NLPacket.h index 9105418492..744056c103 100644 --- a/libraries/networking/src/NLPacket.h +++ b/libraries/networking/src/NLPacket.h @@ -15,23 +15,24 @@ #include "Packet.h" class NLPacket : public Packet { + Q_OBJECT public: static std::unique_ptr create(PacketType::Value type, int64_t size = -1); // Provided for convenience, try to limit use static std::unique_ptr createCopy(const std::unique_ptr& other); - + static int64_t localHeaderSize(PacketType::Value type); static int64_t maxPayloadSize(PacketType::Value type); - + virtual qint64 totalHeadersSize() const; // Cumulated size of all the headers virtual qint64 localHeaderSize() const; // Current level's header size - + protected: NLPacket(PacketType::Value type, int64_t size); NLPacket(NLPacket& other); - + void setSourceUuid(QUuid sourceUuid); void setConnectionUuid(QUuid connectionUuid); }; -#endif // hifi_NLPacket_h \ No newline at end of file +#endif // hifi_NLPacket_h diff --git a/libraries/networking/src/PacketList.cpp b/libraries/networking/src/NLPacketList.cpp similarity index 71% rename from libraries/networking/src/PacketList.cpp rename to libraries/networking/src/NLPacketList.cpp index 7eccc45200..785e3c3f9f 100644 --- a/libraries/networking/src/PacketList.cpp +++ b/libraries/networking/src/NLPacketList.cpp @@ -1,5 +1,5 @@ // -// PacketList.cpp +// NLPacketList.cpp // libraries/networking/src // // Created by Stephen Birarda on 07/06/15. @@ -9,19 +9,19 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include "PacketList.h" +#include "NLPacketList.h" #include -template PacketList::PacketList(PacketType::Value packetType) : +NLPacketList::NLPacketList(PacketType::Value packetType) : _packetType(packetType) { } -template std::unique_ptr PacketList::createPacketWithExtendedHeader() { +std::unique_ptr NLPacketList::createPacketWithExtendedHeader() { // use the static create method to create a new packet - auto packet = T::create(_packetType); + auto packet = NLPacket::create(_packetType); // add the extended header to the front of the packet if (packet->write(_extendedHeader) == -1) { @@ -32,16 +32,16 @@ template std::unique_ptr PacketList::createPacketWithExtended return packet; } -template qint64 PacketList::writeData(const char* data, qint64 maxSize) { +qint64 NLPacketList::writeData(const char* data, qint64 maxSize) { if (!_currentPacket) { // we don't have a current packet, time to set one up _currentPacket = createPacketWithExtendedHeader(); } // check if this block of data can fit into the currentPacket - if (maxSize <= _currentPacket.size()) { + if (maxSize <= _currentPacket->bytesAvailable()) { // it fits, just write it to the current packet - _currentPacket.write(data, maxSize); + _currentPacket->write(data, maxSize); // return the number of bytes written return maxSize; @@ -56,9 +56,9 @@ template qint64 PacketList::writeData(const char* data, qint64 m // We need to try and pull the first part of the segment out to our new packet // check now to see if this is an unsupported write - int numBytesToEnd = _currentPacket.size() - _segmentStartIndex; + int numBytesToEnd = _currentPacket->bytesAvailable(); - if ((newPacket.size() - numBytesToEnd) < maxSize) { + if ((newPacket->size() - numBytesToEnd) < maxSize) { // this is an unsupported case - the segment is bigger than the size of an individual packet // but the PacketList is not going to be sent ordered qDebug() << "Error in PacketList::writeData - attempted to write a segment to an unordered packet that is" @@ -67,23 +67,23 @@ template qint64 PacketList::writeData(const char* data, qint64 m } // copy from currentPacket where the segment started to the beginning of the newPacket - newPacket.write(_currentPacket->getPayload() + _segmentStartIndex, numBytesToEnd); + newPacket->write(_currentPacket->getPayload() + _segmentStartIndex, numBytesToEnd); // the current segment now starts at the beginning of the new packet _segmentStartIndex = 0; // shrink the current payload to the actual size of the packet - _currentPacket.setSizeUsed(_segmentStartIndex); + _currentPacket->setSizeUsed(_segmentStartIndex); } // move the current packet to our list of packets - _packets.insert(std::move(_currentPacket)); + _packets.push_back(std::move(_currentPacket)); // write the data to the newPacket - newPacket.write(data, maxSize); + newPacket->write(data, maxSize); - // set our current packet to the new packet - _currentPacket = newPacket; + // swap our current packet with the new packet + _currentPacket.swap(newPacket); // return the number of bytes written to the new packet return maxSize; @@ -91,11 +91,11 @@ template qint64 PacketList::writeData(const char* data, qint64 m // we're an ordered PacketList - let's fit what we can into the current packet and then put the leftover // into a new packet - int numBytesToEnd = _currentPacket.size() - _currentPacket.sizeUsed(); + int numBytesToEnd = _currentPacket->bytesAvailable(); _currentPacket->write(data, numBytesToEnd); // move the current packet to our list of packets - _packets.insert(std::move(_currentPacket)); + _packets.push_back(std::move(_currentPacket)); // recursively call our writeData method for the remaining data to write to a new packet return numBytesToEnd + writeData(data + numBytesToEnd, maxSize - numBytesToEnd); @@ -103,8 +103,8 @@ template qint64 PacketList::writeData(const char* data, qint64 m } } -template void PacketList::closeCurrentPacket() { +void NLPacketList::closeCurrentPacket() { // move the current packet to our list of packets - _packets.insert(std::move(_currentPacket)); + _packets.push_back(std::move(_currentPacket)); } diff --git a/libraries/networking/src/PacketList.h b/libraries/networking/src/NLPacketList.h similarity index 55% rename from libraries/networking/src/PacketList.h rename to libraries/networking/src/NLPacketList.h index 11dd3cb225..a0a6cdd5eb 100644 --- a/libraries/networking/src/PacketList.h +++ b/libraries/networking/src/NLPacketList.h @@ -1,5 +1,5 @@ // -// PacketList.h +// NLPacketList.h // libraries/networking/src // // Created by Stephen Birarda on 07/06/15. @@ -9,60 +9,61 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#ifndef hifi_PacketList_h -#define hifi_PacketList_h +#ifndef hifi_NLPacketList_h +#define hifi_NLPacketList_h -#include +#include #include "PacketHeaders.h" -template class PacketList : public QIODevice { +#include "NLPacket.h" + +class NLPacketList : public QIODevice { + Q_OBJECT public: - PacketList(PacketType::Value packetType); + NLPacketList(PacketType::Value packetType); virtual bool isSequential() const { return true; } - void startSegment() { _segmentStartIndex = _currentPacket->payload().pos(); } + void startSegment() { _segmentStartIndex = _currentPacket->pos(); } void endSegment() { _segmentStartIndex = -1; } int getNumPackets() const { return _packets.size() + (_currentPacket ? 1 : 0); } - void getCurrentPacketCapacity() const { return _currentPacket->bytesAvailable(); } - void closeCurrentPacket(); void setExtendedHeader(const QByteArray& extendedHeader) { _extendedHeader = extendedHeader; } - template qint64 readPrimitive(U* data); - template qint64 writePrimitive(const U& data); + template qint64 readPrimitive(T* data); + template qint64 writePrimitive(const T& data); protected: virtual qint64 writeData(const char* data, qint64 maxSize); virtual qint64 readData(char* data, qint64 maxSize) { return 0; } private: - PacketList(const PacketList& other) = delete; - PacketList& operator=(const PacketList& other) = delete; + NLPacketList(const NLPacketList& other) = delete; + NLPacketList& operator=(const NLPacketList& other) = delete; - std::unique_ptr createPacketWithExtendedHeader(); + std::unique_ptr createPacketWithExtendedHeader(); PacketType::Value _packetType; bool _isOrdered = false; - std::unique_ptr _currentPacket; - std::list> _packets; + std::unique_ptr _currentPacket; + std::list> _packets; int _segmentStartIndex = -1; QByteArray _extendedHeader; }; -template template qint64 PacketList::readPrimitive(U* data) { - return QIODevice::read(reinterpret_cast(data), sizeof(U)); +template qint64 NLPacketList::readPrimitive(T* data) { + return QIODevice::read(reinterpret_cast(data), sizeof(T)); } -template template qint64 PacketList::writePrimitive(const U& data) { - static_assert(!std::is_pointer::value, "U must not be a pointer"); - return QIODevice::write(reinterpret_cast(&data), sizeof(U)); +template qint64 NLPacketList::writePrimitive(const T& data) { + static_assert(!std::is_pointer::value, "T must not be a pointer"); + return QIODevice::write(reinterpret_cast(&data), sizeof(T)); } diff --git a/libraries/networking/src/Packet.h b/libraries/networking/src/Packet.h index 373c12356a..7bd8d81194 100644 --- a/libraries/networking/src/Packet.h +++ b/libraries/networking/src/Packet.h @@ -19,6 +19,7 @@ #include "PacketHeaders.h" class Packet : public QIODevice { + Q_OBJECT public: using SequenceNumber = uint16_t;