From 3a9aedea68daf7ace5d86c9b848916db0a1e7f27 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 30 Jul 2015 15:56:42 -0700 Subject: [PATCH 1/2] Fix packet ctors size param --- libraries/networking/src/NLPacket.cpp | 25 ++----------------------- libraries/networking/src/NLPacket.h | 3 +-- libraries/networking/src/udt/Packet.cpp | 2 +- 3 files changed, 4 insertions(+), 26 deletions(-) diff --git a/libraries/networking/src/NLPacket.cpp b/libraries/networking/src/NLPacket.cpp index a5e7feb6f5..146054ccc7 100644 --- a/libraries/networking/src/NLPacket.cpp +++ b/libraries/networking/src/NLPacket.cpp @@ -25,16 +25,7 @@ int NLPacket::maxPayloadSize(PacketType type, bool isPartOfMessage) { } std::unique_ptr NLPacket::create(PacketType type, qint64 size, bool isReliable, bool isPartOfMessage) { - std::unique_ptr packet; - - if (size == -1) { - packet = std::unique_ptr(new NLPacket(type, isReliable, isPartOfMessage)); - } else { - // Fail with invalid size - Q_ASSERT(size >= 0); - - packet = std::unique_ptr(new NLPacket(type, size, isReliable, isPartOfMessage)); - } + auto packet = std::unique_ptr(new NLPacket(type, size, isReliable, isPartOfMessage)); packet->open(QIODevice::ReadWrite); @@ -70,23 +61,11 @@ std::unique_ptr NLPacket::createCopy(const NLPacket& other) { return std::unique_ptr(new NLPacket(other)); } -NLPacket::NLPacket(PacketType type, bool isReliable, bool isPartOfMessage) : - Packet(-1, isReliable, isPartOfMessage), - _type(type), - _version(versionForPacketType(type)) -{ - adjustPayloadStartAndCapacity(NLPacket::localHeaderSize(_type)); - - writeTypeAndVersion(); -} - NLPacket::NLPacket(PacketType type, qint64 size, bool isReliable, bool isPartOfMessage) : - Packet(NLPacket::localHeaderSize(type) + size, isReliable, isPartOfMessage), + Packet((size == -1) ? -1 : NLPacket::localHeaderSize(type) + size, isReliable, isPartOfMessage), _type(type), _version(versionForPacketType(type)) { - Q_ASSERT(size >= 0); - adjustPayloadStartAndCapacity(NLPacket::localHeaderSize(_type)); writeTypeAndVersion(); diff --git a/libraries/networking/src/NLPacket.h b/libraries/networking/src/NLPacket.h index 25efd673aa..2303150e66 100644 --- a/libraries/networking/src/NLPacket.h +++ b/libraries/networking/src/NLPacket.h @@ -61,8 +61,7 @@ public: protected: - NLPacket(PacketType type, bool forceReliable = false, bool isPartOfMessage = false); - NLPacket(PacketType type, qint64 size, bool forceReliable = false, bool isPartOfMessage = false); + NLPacket(PacketType type, qint64 size = -1, bool forceReliable = false, bool isPartOfMessage = false); NLPacket(std::unique_ptr data, qint64 size, const HifiSockAddr& senderSockAddr); NLPacket(std::unique_ptr packet); NLPacket(const NLPacket& other); diff --git a/libraries/networking/src/udt/Packet.cpp b/libraries/networking/src/udt/Packet.cpp index 67fb3b8378..4e77079a22 100644 --- a/libraries/networking/src/udt/Packet.cpp +++ b/libraries/networking/src/udt/Packet.cpp @@ -52,7 +52,7 @@ std::unique_ptr Packet::createCopy(const Packet& other) { } Packet::Packet(qint64 size, bool isReliable, bool isPartOfMessage) : - BasePacket(Packet::localHeaderSize() + size), + BasePacket((size == -1) ? -1 : (Packet::localHeaderSize() + size)), _isReliable(isReliable), _isPartOfMessage(isPartOfMessage) { From e8ffe962325d261de9430e83355c2fc8649be906 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 30 Jul 2015 15:58:05 -0700 Subject: [PATCH 2/2] Remove const_cast --- libraries/networking/src/Assignment.cpp | 4 +--- libraries/networking/src/LimitedNodeList.cpp | 4 ++-- libraries/networking/src/NLPacket.cpp | 4 ++-- libraries/networking/src/NLPacket.h | 6 +++--- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/libraries/networking/src/Assignment.cpp b/libraries/networking/src/Assignment.cpp index 293d86475f..e8ba67c4a6 100644 --- a/libraries/networking/src/Assignment.cpp +++ b/libraries/networking/src/Assignment.cpp @@ -150,11 +150,9 @@ QDataStream& operator<<(QDataStream &out, const Assignment& assignment) { QDataStream& operator>>(QDataStream &in, Assignment& assignment) { quint8 packedType; - in >> packedType; + in >> packedType >> assignment._uuid >> assignment._pool >> assignment._payload; assignment._type = (Assignment::Type) packedType; - in >> assignment._uuid >> assignment._pool >> assignment._payload; - if (assignment._command == Assignment::RequestCommand) { in >> assignment._walletUUID; } diff --git a/libraries/networking/src/LimitedNodeList.cpp b/libraries/networking/src/LimitedNodeList.cpp index 2ca4e89c12..5f547ecaf3 100644 --- a/libraries/networking/src/LimitedNodeList.cpp +++ b/libraries/networking/src/LimitedNodeList.cpp @@ -247,13 +247,13 @@ void LimitedNodeList::collectPacketStats(const NLPacket& packet) { void LimitedNodeList::fillPacketHeader(const NLPacket& packet, const QUuid& connectionSecret) { if (!NON_SOURCED_PACKETS.contains(packet.getType())) { - const_cast(packet).writeSourceID(getSessionUUID()); + packet.writeSourceID(getSessionUUID()); } if (!connectionSecret.isNull() && !NON_SOURCED_PACKETS.contains(packet.getType()) && !NON_VERIFIED_PACKETS.contains(packet.getType())) { - const_cast(packet).writeVerificationHashGivenSecret(connectionSecret); + packet.writeVerificationHashGivenSecret(connectionSecret); } } diff --git a/libraries/networking/src/NLPacket.cpp b/libraries/networking/src/NLPacket.cpp index 146054ccc7..84330b0d77 100644 --- a/libraries/networking/src/NLPacket.cpp +++ b/libraries/networking/src/NLPacket.cpp @@ -198,7 +198,7 @@ void NLPacket::readSourceID() { } } -void NLPacket::writeSourceID(const QUuid& sourceID) { +void NLPacket::writeSourceID(const QUuid& sourceID) const { Q_ASSERT(!NON_SOURCED_PACKETS.contains(_type)); auto offset = Packet::totalHeaderSize(isPartOfMessage()) + sizeof(PacketType) + sizeof(PacketVersion); @@ -207,7 +207,7 @@ void NLPacket::writeSourceID(const QUuid& sourceID) { _sourceID = sourceID; } -void NLPacket::writeVerificationHashGivenSecret(const QUuid& connectionSecret) { +void NLPacket::writeVerificationHashGivenSecret(const QUuid& connectionSecret) const { Q_ASSERT(!NON_SOURCED_PACKETS.contains(_type) && !NON_VERIFIED_PACKETS.contains(_type)); auto offset = Packet::totalHeaderSize(isPartOfMessage()) + sizeof(PacketType) + sizeof(PacketVersion) diff --git a/libraries/networking/src/NLPacket.h b/libraries/networking/src/NLPacket.h index 2303150e66..f5c08b308a 100644 --- a/libraries/networking/src/NLPacket.h +++ b/libraries/networking/src/NLPacket.h @@ -56,8 +56,8 @@ public: const QUuid& getSourceID() const { return _sourceID; } - void writeSourceID(const QUuid& sourceID); - void writeVerificationHashGivenSecret(const QUuid& connectionSecret); + void writeSourceID(const QUuid& sourceID) const; + void writeVerificationHashGivenSecret(const QUuid& connectionSecret) const; protected: @@ -80,7 +80,7 @@ protected: PacketType _type; PacketVersion _version; - QUuid _sourceID; + mutable QUuid _sourceID; }; #endif // hifi_NLPacket_h