diff --git a/libraries/networking/src/Packet.cpp b/libraries/networking/src/Packet.cpp index ca49100efe..6a02af803b 100644 --- a/libraries/networking/src/Packet.cpp +++ b/libraries/networking/src/Packet.cpp @@ -38,6 +38,12 @@ std::unique_ptr Packet::create(PacketType::Value type, qint64 size) { return std::unique_ptr(new Packet(type, size)); } + +std::unique_ptr Packet::createCopy(const std::unique_ptr& other) { + Q_ASSERT(other); + return std::unique_ptr(new Packet(*other)); +} + qint64 Packet::totalHeadersSize() const { return localHeaderSize(); } @@ -74,7 +80,6 @@ Packet& Packet::operator=(const Packet& other) { memcpy(_packet.get(), other._packet.get(), _packetSize); _payloadStart = _packet.get() + (other._payloadStart - other._packet.get()); - _position = other._position; _capacity = other._capacity; _sizeUsed = other._sizeUsed; @@ -91,7 +96,6 @@ Packet& Packet::operator=(Packet&& other) { _packet = std::move(other._packet); _payloadStart = other._payloadStart; - _position = other._position; _capacity = other._capacity; _sizeUsed = other._sizeUsed; @@ -132,7 +136,7 @@ void Packet::setPacketTypeAndVersion(PacketType::Value type) { auto offset = packArithmeticallyCodedValue(type, _packet.get()); // Pack the packet version - auto version { versionForPacketType(type) }; + auto version = versionForPacketType(type); memcpy(_packet.get() + offset, &version, sizeof(version)); } @@ -144,14 +148,6 @@ void Packet::setSequenceNumber(SequenceNumber seqNum) { &seqNum, sizeof(seqNum)); } -bool Packet::seek(qint64 pos) { - bool valid = (pos >= 0) && (pos < size()); - if (valid) { - _position = pos; - } - return valid; -} - static const qint64 PACKET_WRITE_ERROR = -1; qint64 Packet::writeData(const char* data, qint64 maxSize) { // make sure we have the space required to write this block diff --git a/libraries/networking/src/Packet.h b/libraries/networking/src/Packet.h index 6177cbd60e..63f6db8fda 100644 --- a/libraries/networking/src/Packet.h +++ b/libraries/networking/src/Packet.h @@ -23,6 +23,8 @@ public: using SequenceNumber = uint16_t; 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 qint64 localHeaderSize(PacketType::Value type); static qint64 maxPayloadSize(PacketType::Value type); @@ -30,10 +32,14 @@ public: virtual qint64 totalHeadersSize() const; // Cumulated size of all the headers virtual qint64 localHeaderSize() const; // Current level's header size - // Payload direct access, use responsibly! + // Payload direct access to the payload, use responsibly! char* getPayload() { return _payloadStart; } const char* getPayload() const { return _payloadStart; } + // Return direct access to the entire packet, use responsibly! + char* getData() { return _packet.get(); } + const char* getData() const { return _packet.get(); } + qint64 getSizeWithHeader() const { return localHeaderSize() + getSizeUsed(); } qint64 getSizeUsed() const { return _sizeUsed; } void setSizeUsed(qint64 sizeUsed) { _sizeUsed = sizeUsed; } @@ -46,21 +52,12 @@ public: // QIODevice virtual functions // WARNING: Those methods all refer to the payload ONLY and NOT the entire packet - virtual bool atEnd() const { return _position == _capacity; } - virtual qint64 bytesAvailable() const { return size() - pos(); } - virtual bool canReadLine() const { return false; } virtual bool isSequential() const { return false; } - virtual qint64 pos() const { return _position; } - virtual bool reset() { return seek(0); } - virtual bool seek(qint64 pos); + virtual bool reset() { setSizeUsed(0); return QIODevice::reset(); } virtual qint64 size() const { return _capacity; } protected: Packet(PacketType::Value type, int64_t size); - Packet(const Packet& other); - Packet& operator=(const Packet& other); - Packet(Packet&& other); - Packet& operator=(Packet&& other); // QIODevice virtual functions virtual qint64 writeData(const char* data, qint64 maxSize); @@ -70,16 +67,21 @@ protected: void setPacketTypeAndVersion(PacketType::Value type); void setSequenceNumber(SequenceNumber seqNum); - PacketType::Value _type; + PacketType::Value _type; // Packet type qint64 _packetSize = 0; // Total size of the allocated memory std::unique_ptr _packet; // Allocated memory char* _payloadStart = nullptr; // Start of the payload - qint64 _position = 0; // Current position in the payload qint64 _capacity = 0; // Total capacity of the payload qint64 _sizeUsed = 0; // How much of the payload is actually used + +private: + Packet(const Packet& other); + Packet& operator=(const Packet& other); + Packet(Packet&& other); + Packet& operator=(Packet&& other); }; #endif // hifi_Packet_h \ No newline at end of file