From fb7cb7ff5368b8c0077055835c372204c8070207 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 14 Jul 2015 15:49:54 -0700 Subject: [PATCH] add the option for PacketReceiver to drop packets --- assignment-client/src/AssignmentClient.cpp | 3 +++ interface/src/Application.cpp | 9 ++++----- libraries/networking/src/PacketReceiver.cpp | 5 +++++ libraries/networking/src/PacketReceiver.h | 4 +++- libraries/networking/src/ThreadedAssignment.cpp | 7 ++++++- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/assignment-client/src/AssignmentClient.cpp b/assignment-client/src/AssignmentClient.cpp index c81a521552..bc2b18cb53 100644 --- a/assignment-client/src/AssignmentClient.cpp +++ b/assignment-client/src/AssignmentClient.cpp @@ -330,6 +330,9 @@ void AssignmentClient::assignmentCompleted() { auto nodeList = DependencyManager::get(); + // tell the packet receiver to stop dropping packets + nodeList->getPacketReceiver().setShouldDropPackets(false); + // reset our NodeList by switching back to unassigned and clearing the list nodeList->setOwnerType(NodeType::Unassigned); nodeList->reset(); diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 57f2b7c523..824de916d5 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -663,12 +663,11 @@ void Application::aboutToQuit() { void Application::cleanupBeforeQuit() { - // stop handling packets we've asked to handle - DependencyManager::get()->getPacketReceiver().unregisterListener(this); - _entities.clear(); // this will allow entity scripts to properly shutdown - - //_datagramProcessor->shutdown(); // tell the datagram processor we're shutting down, so it can short circuit + + // tell the packet receiver we're shutting down, so it can drop packets + DependencyManager::get()->getPacketReceiver().setShouldDropPackets(true); + _entities.shutdown(); // tell the entities system we're shutting down, so it will stop running scripts ScriptEngine::stopAllScripts(this); // stop all currently running global scripts diff --git a/libraries/networking/src/PacketReceiver.cpp b/libraries/networking/src/PacketReceiver.cpp index 1f163cdaf3..4c3f268d06 100644 --- a/libraries/networking/src/PacketReceiver.cpp +++ b/libraries/networking/src/PacketReceiver.cpp @@ -180,6 +180,11 @@ void PacketReceiver::processDatagrams() { // setup a buffer to read the packet into int packetSizeWithHeader = nodeList->getNodeSocket().pendingDatagramSize(); std::unique_ptr buffer = std::unique_ptr(new char[packetSizeWithHeader]); + + // if we're supposed to drop this packet then break out here + if (_shouldDropPackets) { + break; + } // setup a HifiSockAddr to read into HifiSockAddr senderSockAddr; diff --git a/libraries/networking/src/PacketReceiver.h b/libraries/networking/src/PacketReceiver.h index bf5889af1e..448ca07876 100644 --- a/libraries/networking/src/PacketReceiver.h +++ b/libraries/networking/src/PacketReceiver.h @@ -34,6 +34,8 @@ public: int getInPacketCount() const { return _inPacketCount; } int getInByteCount() const { return _inByteCount; } + + void setShouldDropPackets(bool shouldDropPackets) { _shouldDropPackets = shouldDropPackets; } void resetCounters() { _inPacketCount = 0; _inByteCount = 0; } @@ -61,7 +63,7 @@ private: QHash _packetListenerMap; int _inPacketCount = 0; int _inByteCount = 0; - bool _isShuttingDown = false; + bool _shouldDropPackets = false; }; #endif // hifi_PacketReceiver_h diff --git a/libraries/networking/src/ThreadedAssignment.cpp b/libraries/networking/src/ThreadedAssignment.cpp index 44d1aaf280..1c425806c9 100644 --- a/libraries/networking/src/ThreadedAssignment.cpp +++ b/libraries/networking/src/ThreadedAssignment.cpp @@ -34,8 +34,13 @@ void ThreadedAssignment::setFinished(bool isFinished) { qDebug() << "ThreadedAssignment::setFinished(true) called - finishing up."; + auto& packetReceiver = DependencyManager::get()->getPacketReceiver(); + // we should de-register immediately for any of our packets - DependencyManager::get()->getPacketReceiver().unregisterListener(this); + packetReceiver.unregisterListener(this); + + // we should also tell the packet receiver to drop packets while we're cleaning up + packetReceiver.setShouldDropPackets(true); if (_domainServerTimer) { _domainServerTimer->stop();