diff --git a/libraries/networking/src/NetworkPacket.cpp b/libraries/networking/src/NetworkPacket.cpp index 69bd0962bf..fb1c90e392 100644 --- a/libraries/networking/src/NetworkPacket.cpp +++ b/libraries/networking/src/NetworkPacket.cpp @@ -18,38 +18,38 @@ #include "NetworkPacket.h" -void NetworkPacket::copyContents(const SharedNodePointer& node, const QByteArray& packet) { - if (packet.size() && packet.size() <= MAX_PACKET_SIZE) { +NetworkPacket::NetworkPacket(const NetworkPacket& other) : + _node(other._node), + _nlPacket(other._nlPacket) { +} + +NetworkPacket::NetworkPacket(const SharedNodePointer& node, const NLPacket& packet) { + if (packet.getSizeWitHeader() && packet.getSizeWithHeader() <= MAX_nlPacket_SIZE) { _node = node; - _byteArray = packet; + _nlPacket = packet; } else { qCDebug(networking, ">>> NetworkPacket::copyContents() unexpected length = %d", packet.size()); } -} - -NetworkPacket::NetworkPacket(const NetworkPacket& packet) { - copyContents(packet.getNode(), packet.getByteArray()); -} - -NetworkPacket::NetworkPacket(const SharedNodePointer& node, const QByteArray& packet) { - copyContents(node, packet); }; // copy assignment NetworkPacket& NetworkPacket::operator=(NetworkPacket const& other) { - copyContents(other.getNode(), other.getByteArray()); + _node = other._node; + _nlPacket = other._nlPacket; return *this; } #ifdef HAS_MOVE_SEMANTICS // move, same as copy, but other packet won't be used further -NetworkPacket::NetworkPacket(NetworkPacket && packet) { - copyContents(packet.getNode(), packet.getByteArray()); +NetworkPacket::NetworkPacket(NetworkPacket&& other) : + _node(std::move(other._node)), + _nlPacket(std::move(other._nlPacket)) { } // move assignment NetworkPacket& NetworkPacket::operator=(NetworkPacket&& other) { - copyContents(other.getNode(), other.getByteArray()); + _node = std::move(other._node); + _nlPacket = std::move(other._nlPacket); return *this; } #endif diff --git a/libraries/networking/src/NetworkPacket.h b/libraries/networking/src/NetworkPacket.h index caee42f126..c42ad286ae 100644 --- a/libraries/networking/src/NetworkPacket.h +++ b/libraries/networking/src/NetworkPacket.h @@ -24,20 +24,19 @@ public: NetworkPacket& operator= (const NetworkPacket& other); // copy assignment #ifdef HAS_MOVE_SEMANTICS - NetworkPacket(NetworkPacket&& packet); // move?? // same as copy, but other packet won't be used further + NetworkPacket(NetworkPacket&& other); // move?? // same as copy, but other packet won't be used further NetworkPacket& operator= (NetworkPacket&& other); // move assignment #endif - NetworkPacket(const SharedNodePointer& node, const QByteArray& byteArray); + NetworkPacket(const SharedNodePointer& node, const NLPacket& nlPacket); const SharedNodePointer& getNode() const { return _node; } - const QByteArray& getByteArray() const { return _byteArray; } + const NLPacket& getPacket() const { return _nlPacket; } private: - void copyContents(const SharedNodePointer& node, const QByteArray& byteArray); SharedNodePointer _node; - QByteArray _byteArray; + NLPacket _nlPacket; }; #endif // hifi_NetworkPacket_h diff --git a/libraries/networking/src/PacketSender.cpp b/libraries/networking/src/PacketSender.cpp index f560d94b86..4481cd716c 100644 --- a/libraries/networking/src/PacketSender.cpp +++ b/libraries/networking/src/PacketSender.cpp @@ -48,13 +48,13 @@ PacketSender::~PacketSender() { } -void PacketSender::queuePacketForSending(const SharedNodePointer& destinationNode, const QByteArray& packet) { +void PacketSender::queuePacketForSending(const SharedNodePointer& destinationNode, const NLPacket& packet) { NetworkPacket networkPacket(destinationNode, packet); lock(); _packets.push_back(networkPacket); unlock(); _totalPacketsQueued++; - _totalBytesQueued += packet.size(); + _totalBytesQueued += packet.getSizeWithHeader(); // Make sure to wake our actual processing thread because we now have packets for it to process. _hasPackets.wakeAll(); @@ -271,13 +271,13 @@ bool PacketSender::nonThreadedProcess() { unlock(); // send the packet through the NodeList... - DependencyManager::get()->writeDatagram(temporary.getByteArray(), temporary.getNode()); + DependencyManager::get()->sendUnreliablePacket(temporary.getPacket(), temporary.getNode()); packetsSentThisCall++; _packetsOverCheckInterval++; _totalPacketsSent++; - _totalBytesSent += temporary.getByteArray().size(); + _totalBytesSent += temporary.getPacket().getSizeWithHeader(); - emit packetSent(temporary.getByteArray().size()); + emit packetSent(temporary.getPacket().getSizeWithHeader()); _lastSendTime = now; } diff --git a/libraries/networking/src/PacketSender.h b/libraries/networking/src/PacketSender.h index 29d9287127..6754da9825 100644 --- a/libraries/networking/src/PacketSender.h +++ b/libraries/networking/src/PacketSender.h @@ -39,7 +39,7 @@ public: ~PacketSender(); /// Add packet to outbound queue. - void queuePacketForSending(const SharedNodePointer& destinationNode, const QByteArray& packet); + void queuePacketForSending(const SharedNodePointer& destinationNode, const NLPacket& packet); void setPacketsPerSecond(int packetsPerSecond); int getPacketsPerSecond() const { return _packetsPerSecond; } diff --git a/libraries/octree/src/JurisdictionListener.cpp b/libraries/octree/src/JurisdictionListener.cpp index 71c4feda96..a86130b005 100644 --- a/libraries/octree/src/JurisdictionListener.cpp +++ b/libraries/octree/src/JurisdictionListener.cpp @@ -33,17 +33,15 @@ void JurisdictionListener::nodeKilled(SharedNodePointer node) { } bool JurisdictionListener::queueJurisdictionRequest() { - static unsigned char buffer[MAX_PACKET_SIZE]; - unsigned char* bufferOut = &buffer[0]; + auto packet = NLPacket::create(PacketType::JurisdictionRequest, 0); auto nodeList = DependencyManager::get(); - int sizeOut = nodeList->populatePacketHeader(reinterpret_cast(bufferOut), PacketTypeJurisdictionRequest); int nodeCount = 0; nodeList->eachNode([&](const SharedNodePointer& node) { if (node->getType() == getNodeType() && node->getActiveSocket()) { - _packetSender.queuePacketForSending(node, QByteArray(reinterpret_cast(bufferOut), sizeOut)); + _packetSender.queuePacketForSending(node, packet); nodeCount++; } });