diff --git a/assignment-client/src/Agent.cpp b/assignment-client/src/Agent.cpp index a67540738d..c25a0dc434 100644 --- a/assignment-client/src/Agent.cpp +++ b/assignment-client/src/Agent.cpp @@ -50,13 +50,13 @@ Agent::Agent(NLPacket& packet) : auto& packetReceiver = DependencyManager::get()->getPacketReceiver(); - packetReceiver.registerPacketListenerForTypes( + packetReceiver.registerListenerForTypes( { PacketType::MixedAudio, PacketType::SilentAudioFrame }, this, "handleAudioPacket"); - packetReceiver.registerPacketListenerForTypes( + packetReceiver.registerListenerForTypes( { PacketType::OctreeStats, PacketType::EntityData, PacketType::EntityErase }, this, "handleOctreePacket"); - packetReceiver.registerPacketListener(PacketType::Jurisdiction, this, "handleJurisdictionPacket"); + packetReceiver.registerListener(PacketType::Jurisdiction, this, "handleJurisdictionPacket"); } void Agent::handleOctreePacket(QSharedPointer packet, SharedNodePointer senderNode) { diff --git a/assignment-client/src/AssignmentClient.cpp b/assignment-client/src/AssignmentClient.cpp index 9957619134..aab5d6af3b 100644 --- a/assignment-client/src/AssignmentClient.cpp +++ b/assignment-client/src/AssignmentClient.cpp @@ -121,11 +121,10 @@ AssignmentClient::AssignmentClient(Assignment::Type requestAssignmentType, QStri setUpStatusToMonitor(); } auto& packetReceiver = DependencyManager::get()->getPacketReceiver(); - packetReceiver.registerPacketListener(PacketType::CreateAssignment, this, "handleCreateAssignmentPacket"); - packetReceiver.registerPacketListener(PacketType::StopNode, this, "handleStopNodePacket"); + packetReceiver.registerListener(PacketType::CreateAssignment, this, "handleCreateAssignmentPacket"); + packetReceiver.registerListener(PacketType::StopNode, this, "handleStopNodePacket"); } - void AssignmentClient::stopAssignmentClient() { qDebug() << "Forced stop of assignment-client."; diff --git a/assignment-client/src/AssignmentClient.h b/assignment-client/src/AssignmentClient.h index 23e28968ed..e18e8eea4d 100644 --- a/assignment-client/src/AssignmentClient.h +++ b/assignment-client/src/AssignmentClient.h @@ -15,11 +15,13 @@ #include #include +#include + #include "ThreadedAssignment.h" class QSharedMemory; -class AssignmentClient : public QObject { +class AssignmentClient : public QObject, public PacketListener { Q_OBJECT public: diff --git a/assignment-client/src/AssignmentClientMonitor.cpp b/assignment-client/src/AssignmentClientMonitor.cpp index df912ef519..fc7697fc15 100644 --- a/assignment-client/src/AssignmentClientMonitor.cpp +++ b/assignment-client/src/AssignmentClientMonitor.cpp @@ -54,7 +54,7 @@ AssignmentClientMonitor::AssignmentClientMonitor(const unsigned int numAssignmen auto nodeList = DependencyManager::set(); auto& packetReceiver = DependencyManager::get()->getPacketReceiver(); - packetReceiver.registerPacketListener(PacketType::AssignmentClientStatus, this, "handleChildStatusPacket"); + packetReceiver.registerListener(PacketType::AssignmentClientStatus, this, "handleChildStatusPacket"); // use QProcess to fork off a process for each of the child assignment clients for (unsigned int i = 0; i < _numAssignmentClientForks; i++) { diff --git a/assignment-client/src/AssignmentClientMonitor.h b/assignment-client/src/AssignmentClientMonitor.h index 895e0defb2..a75e876581 100644 --- a/assignment-client/src/AssignmentClientMonitor.h +++ b/assignment-client/src/AssignmentClientMonitor.h @@ -24,7 +24,7 @@ extern const char* NUM_FORKS_PARAMETER; -class AssignmentClientMonitor : public QObject { +class AssignmentClientMonitor : public QObject, public PacketListener { Q_OBJECT public: AssignmentClientMonitor(const unsigned int numAssignmentClientForks, const unsigned int minAssignmentClientForks, diff --git a/assignment-client/src/audio/AudioMixer.cpp b/assignment-client/src/audio/AudioMixer.cpp index 19b9ebe868..a44759ae8d 100644 --- a/assignment-client/src/audio/AudioMixer.cpp +++ b/assignment-client/src/audio/AudioMixer.cpp @@ -104,8 +104,8 @@ AudioMixer::AudioMixer(NLPacket& packet) : PacketType::AudioStreamStats }; - packetReceiver.registerPacketListenerForTypes(nodeAudioPackets, this, "handleNodeAudioPacket"); - packetReceiver.registerPacketListener(PacketType::MuteEnvironment, this, "handleMuteEnvironmentPacket"); + packetReceiver.registerListenerForTypes(nodeAudioPackets, this, "handleNodeAudioPacket"); + packetReceiver.registerListener(PacketType::MuteEnvironment, this, "handleMuteEnvironmentPacket"); } const float ATTENUATION_BEGINS_AT_DISTANCE = 1.0f; diff --git a/assignment-client/src/avatars/AvatarMixer.cpp b/assignment-client/src/avatars/AvatarMixer.cpp index 35cd3d4885..02202100be 100644 --- a/assignment-client/src/avatars/AvatarMixer.cpp +++ b/assignment-client/src/avatars/AvatarMixer.cpp @@ -48,10 +48,10 @@ AvatarMixer::AvatarMixer(NLPacket& packet) : connect(DependencyManager::get().data(), &NodeList::nodeKilled, this, &AvatarMixer::nodeKilled); auto& packetReceiver = DependencyManager::get()->getPacketReceiver(); - packetReceiver.registerPacketListener(PacketType::AvatarData, this, "handleAvatarDataPacket"); - packetReceiver.registerPacketListener(PacketType::AvatarIdentity, this, "handleAvatarIdentityPacket"); - packetReceiver.registerPacketListener(PacketType::AvatarBillboard, this, "handleAvatarBillboardPacket"); - packetReceiver.registerPacketListener(PacketType::KillAvatar, this, "handleKillAvatarPacket"); + packetReceiver.registerListener(PacketType::AvatarData, this, "handleAvatarDataPacket"); + packetReceiver.registerListener(PacketType::AvatarIdentity, this, "handleAvatarIdentityPacket"); + packetReceiver.registerListener(PacketType::AvatarBillboard, this, "handleAvatarBillboardPacket"); + packetReceiver.registerListener(PacketType::KillAvatar, this, "handleKillAvatarPacket"); } AvatarMixer::~AvatarMixer() { diff --git a/assignment-client/src/entities/EntityServer.cpp b/assignment-client/src/entities/EntityServer.cpp index 3dee2af464..2a6815c2d5 100644 --- a/assignment-client/src/entities/EntityServer.cpp +++ b/assignment-client/src/entities/EntityServer.cpp @@ -26,9 +26,9 @@ EntityServer::EntityServer(NLPacket& packet) : _entitySimulation(NULL) { auto& packetReceiver = DependencyManager::get()->getPacketReceiver(); - packetReceiver.registerPacketListener(PacketType::EntityAdd, this, "handleEntityAddPacket"); - packetReceiver.registerPacketListener(PacketType::EntityEdit, this, "handleEntityEditPacket"); - packetReceiver.registerPacketListener(PacketType::EntityErase, this, "handleEntityErasePacket"); + packetReceiver.registerListener(PacketType::EntityAdd, this, "handleEntityAddPacket"); + packetReceiver.registerListener(PacketType::EntityEdit, this, "handleEntityEditPacket"); + packetReceiver.registerListener(PacketType::EntityErase, this, "handleEntityErasePacket"); } EntityServer::~EntityServer() { diff --git a/assignment-client/src/octree/OctreeServer.cpp b/assignment-client/src/octree/OctreeServer.cpp index 9b93b3895e..efcde790f9 100644 --- a/assignment-client/src/octree/OctreeServer.cpp +++ b/assignment-client/src/octree/OctreeServer.cpp @@ -1060,9 +1060,9 @@ void OctreeServer::readConfiguration() { void OctreeServer::run() { auto& packetReceiver = DependencyManager::get()->getPacketReceiver(); - packetReceiver.registerPacketListener(getMyQueryMessageType(), this, "handleOctreeQueryPacket"); - packetReceiver.registerPacketListener(PacketType::OctreeDataNack, this, "handleOctreeDataNackPacket"); - packetReceiver.registerPacketListener(PacketType::JurisdictionRequest, this, "handleJurisdictionRequestPacket"); + packetReceiver.registerListener(getMyQueryMessageType(), this, "handleOctreeQueryPacket"); + packetReceiver.registerListener(PacketType::OctreeDataNack, this, "handleOctreeDataNackPacket"); + packetReceiver.registerListener(PacketType::JurisdictionRequest, this, "handleJurisdictionRequestPacket"); _safeServerName = getMyServerName(); diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index d6d6956a12..579ec1a002 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -111,6 +111,7 @@ DomainServer::DomainServer(int argc, char* argv[]) : } void DomainServer::aboutToQuit() { + // clear the log handler so that Qt doesn't call the destructor on LogHandler qInstallMessageHandler(0); } @@ -280,14 +281,14 @@ void DomainServer::setupNodeListAndAssignments(const QUuid& sessionUUID) { // register as the packet receiver for the types we want PacketReceiver& packetReceiver = nodeList->getPacketReceiver(); - packetReceiver.registerPacketListener(PacketType::RequestAssignment, this, "processRequestAssignmentPacket"); - packetReceiver.registerPacketListener(PacketType::DomainConnectRequest, this, "processConnectRequestPackets"); - packetReceiver.registerPacketListener(PacketType::DomainListRequest, this, "processListRequestPacket"); - packetReceiver.registerPacketListener(PacketType::DomainServerPathQuery, this, "processPathQueryPacket"); - packetReceiver.registerPacketListener(PacketType::NodeJsonStats, this, "processNodeJSONStatsPacket"); - packetReceiver.registerPacketListener(PacketType::ICEPing, this, "processICEPingPacket"); - packetReceiver.registerPacketListener(PacketType::ICEPingReply, this, "processICEPingReplyPacket"); - packetReceiver.registerPacketListener(PacketType::ICEServerPeerInformation, this, "processICEPeerInformationPacket"); + packetReceiver.registerListener(PacketType::RequestAssignment, this, "processRequestAssignmentPacket"); + packetReceiver.registerListener(PacketType::DomainConnectRequest, this, "processConnectRequestPackets"); + packetReceiver.registerListener(PacketType::DomainListRequest, this, "processListRequestPacket"); + packetReceiver.registerListener(PacketType::DomainServerPathQuery, this, "processPathQueryPacket"); + packetReceiver.registerListener(PacketType::NodeJsonStats, this, "processNodeJSONStatsPacket"); + packetReceiver.registerListener(PacketType::ICEPing, this, "processICEPingPacket"); + packetReceiver.registerListener(PacketType::ICEPingReply, this, "processICEPingReplyPacket"); + packetReceiver.registerListener(PacketType::ICEServerPeerInformation, this, "processICEPeerInformationPacket"); // add whatever static assignments that have been parsed to the queue addStaticAssignmentsToQueue(); diff --git a/domain-server/src/DomainServer.h b/domain-server/src/DomainServer.h index 0c287c4c55..7f7cba7445 100644 --- a/domain-server/src/DomainServer.h +++ b/domain-server/src/DomainServer.h @@ -24,6 +24,7 @@ #include #include #include +#include #include "DomainServerSettingsManager.h" #include "DomainServerWebSessionData.h" @@ -34,7 +35,7 @@ typedef QSharedPointer SharedAssignmentPointer; typedef QMultiHash TransactionHash; -class DomainServer : public QCoreApplication, public HTTPSRequestHandler { +class DomainServer : public QCoreApplication, public HTTPSRequestHandler, public PacketListener { Q_OBJECT public: DomainServer(int argc, char* argv[]); diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 80a4e7bcca..7886041ff6 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -654,7 +654,7 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) : applicationUpdater->checkForUpdate(); auto& packetReceiver = nodeList->getPacketReceiver(); - packetReceiver.registerPacketListener(PacketType::DomainConnectionDenied, this, "handleDomainConnectionDeniedPacket"); + packetReceiver.registerListener(PacketType::DomainConnectionDenied, this, "handleDomainConnectionDeniedPacket"); } void Application::aboutToQuit() { @@ -666,6 +666,9 @@ 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 diff --git a/interface/src/Application.h b/interface/src/Application.h index 1326ac5180..4580e762e6 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -134,7 +135,10 @@ class Application; typedef bool (Application::* AcceptURLMethod)(const QString &); -class Application : public QApplication, public AbstractViewStateInterface, AbstractScriptingServicesInterface { +class Application : public QApplication, + public AbstractViewStateInterface, + AbstractScriptingServicesInterface, + public PacketListener { Q_OBJECT friend class OctreePacketProcessor; diff --git a/interface/src/octree/OctreePacketProcessor.cpp b/interface/src/octree/OctreePacketProcessor.cpp index 80528612b6..64cf0696da 100644 --- a/interface/src/octree/OctreePacketProcessor.cpp +++ b/interface/src/octree/OctreePacketProcessor.cpp @@ -24,7 +24,11 @@ OctreePacketProcessor::OctreePacketProcessor() { PacketType::EntityErase, PacketType::OctreeStats, PacketType::EnvironmentData }; - packetReceiver.registerPacketListenerForTypes(types, this, "handleOctreePacket"); + packetReceiver.registerListenerForTypes(types, this, "handleOctreePacket"); +} + +OctreePacketProcessor::~OctreePacketProcessor() { + DependencyManager::get()->getPacketReceiver().unregisterListener(this); } void OctreePacketProcessor::handleOctreePacket(QSharedPointer packet, SharedNodePointer senderNode) { diff --git a/interface/src/octree/OctreePacketProcessor.h b/interface/src/octree/OctreePacketProcessor.h index 47ebdc73bc..3476cd383e 100644 --- a/interface/src/octree/OctreePacketProcessor.h +++ b/interface/src/octree/OctreePacketProcessor.h @@ -13,13 +13,15 @@ #define hifi_OctreePacketProcessor_h #include +#include /// Handles processing of incoming voxel packets for the interface application. As with other ReceivedPacketProcessor classes /// the user is responsible for reading inbound packets and adding them to the processing queue by calling queueReceivedPacket() -class OctreePacketProcessor : public ReceivedPacketProcessor { +class OctreePacketProcessor : public ReceivedPacketProcessor, public PacketListener { Q_OBJECT public: OctreePacketProcessor(); + ~OctreePacketProcessor(); signals: void packetVersionMismatch(); diff --git a/libraries/audio-client/src/AudioClient.cpp b/libraries/audio-client/src/AudioClient.cpp index fc515c0c4c..a9e60f85e2 100644 --- a/libraries/audio-client/src/AudioClient.cpp +++ b/libraries/audio-client/src/AudioClient.cpp @@ -142,12 +142,12 @@ AudioClient::AudioClient() : configureGverbFilter(_gverb); auto& packetReceiver = DependencyManager::get()->getPacketReceiver(); - packetReceiver.registerPacketListener(PacketType::AudioStreamStats, &_stats, "handleAudioStreamStatsPacket"); - packetReceiver.registerPacketListener(PacketType::AudioEnvironment, this, "handleAudioEnvironmentDataPacket"); - packetReceiver.registerPacketListener(PacketType::SilentAudioFrame, this, "handleAudioDataPacket"); - packetReceiver.registerPacketListener(PacketType::MixedAudio, this, "handleAudioDataPacket"); - packetReceiver.registerPacketListener(PacketType::NoisyMute, this, "handleNoisyMutePacket"); - packetReceiver.registerPacketListener(PacketType::MuteEnvironment, this, "handleMuteEnvironmentPacket"); + packetReceiver.registerListener(PacketType::AudioStreamStats, &_stats, "handleAudioStreamStatsPacket"); + packetReceiver.registerListener(PacketType::AudioEnvironment, this, "handleAudioEnvironmentDataPacket"); + packetReceiver.registerListener(PacketType::SilentAudioFrame, this, "handleAudioDataPacket"); + packetReceiver.registerListener(PacketType::MixedAudio, this, "handleAudioDataPacket"); + packetReceiver.registerListener(PacketType::NoisyMute, this, "handleNoisyMutePacket"); + packetReceiver.registerListener(PacketType::MuteEnvironment, this, "handleMuteEnvironmentPacket"); } AudioClient::~AudioClient() { diff --git a/libraries/audio-client/src/AudioClient.h b/libraries/audio-client/src/AudioClient.h index cf57b9dca1..dc9c40acf7 100644 --- a/libraries/audio-client/src/AudioClient.h +++ b/libraries/audio-client/src/AudioClient.h @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -80,7 +81,7 @@ typedef glm::quat (*AudioOrientationGetter)(); class NLPacket; -class AudioClient : public AbstractAudioInterface, public Dependency { +class AudioClient : public AbstractAudioInterface, public Dependency, public PacketListener { Q_OBJECT SINGLETON_DEPENDENCY public: diff --git a/libraries/audio-client/src/AudioIOStats.h b/libraries/audio-client/src/AudioIOStats.h index da0acf2fa3..c5c2a47ff4 100644 --- a/libraries/audio-client/src/AudioIOStats.h +++ b/libraries/audio-client/src/AudioIOStats.h @@ -19,10 +19,11 @@ #include #include #include +#include class MixedProcessedAudioStream; -class AudioIOStats : public QObject { +class AudioIOStats : public QObject, public PacketListener { Q_OBJECT public: AudioIOStats(MixedProcessedAudioStream* receivedAudioStream); diff --git a/libraries/avatars/src/AvatarHashMap.cpp b/libraries/avatars/src/AvatarHashMap.cpp index c00ba1f08c..b219089f07 100644 --- a/libraries/avatars/src/AvatarHashMap.cpp +++ b/libraries/avatars/src/AvatarHashMap.cpp @@ -20,10 +20,10 @@ AvatarHashMap::AvatarHashMap() { connect(DependencyManager::get().data(), &NodeList::uuidChanged, this, &AvatarHashMap::sessionUUIDChanged); auto& packetReceiver = DependencyManager::get()->getPacketReceiver(); - packetReceiver.registerPacketListener(PacketType::BulkAvatarData, this, "processAvatarDataPacket"); - packetReceiver.registerPacketListener(PacketType::KillAvatar, this, "processKillAvatar"); - packetReceiver.registerPacketListener(PacketType::AvatarIdentity, this, "processAvatarIdentityPacket"); - packetReceiver.registerPacketListener(PacketType::AvatarBillboard, this, "processAvatarBillboardPacket"); + packetReceiver.registerListener(PacketType::BulkAvatarData, this, "processAvatarDataPacket"); + packetReceiver.registerListener(PacketType::KillAvatar, this, "processKillAvatar"); + packetReceiver.registerListener(PacketType::AvatarIdentity, this, "processAvatarIdentityPacket"); + packetReceiver.registerListener(PacketType::AvatarBillboard, this, "processAvatarBillboardPacket"); } bool AvatarHashMap::isAvatarInRange(const glm::vec3& position, const float range) { diff --git a/libraries/avatars/src/AvatarHashMap.h b/libraries/avatars/src/AvatarHashMap.h index 071798e4f8..04988a81e2 100644 --- a/libraries/avatars/src/AvatarHashMap.h +++ b/libraries/avatars/src/AvatarHashMap.h @@ -21,11 +21,12 @@ #include #include #include +#include #include "AvatarData.h" #include -class AvatarHashMap : public QObject, public Dependency { +class AvatarHashMap : public QObject, public Dependency, public PacketListener { Q_OBJECT SINGLETON_DEPENDENCY diff --git a/libraries/entities/src/EntityEditPacketSender.cpp b/libraries/entities/src/EntityEditPacketSender.cpp index a798b83412..0dd3465b33 100644 --- a/libraries/entities/src/EntityEditPacketSender.cpp +++ b/libraries/entities/src/EntityEditPacketSender.cpp @@ -19,7 +19,7 @@ EntityEditPacketSender::EntityEditPacketSender() { auto& packetReceiver = DependencyManager::get()->getPacketReceiver(); - packetReceiver.registerPacketListener(PacketType::EntityEditNack, this, "processEntityEditNackPacket"); + packetReceiver.registerListener(PacketType::EntityEditNack, this, "processEntityEditNackPacket"); } void EntityEditPacketSender::processEntityEditNackPacket(QSharedPointer packet, HifiSockAddr senderSockAddr) { diff --git a/libraries/entities/src/EntityEditPacketSender.h b/libraries/entities/src/EntityEditPacketSender.h index 667040c1df..39bd4cdffc 100644 --- a/libraries/entities/src/EntityEditPacketSender.h +++ b/libraries/entities/src/EntityEditPacketSender.h @@ -14,10 +14,12 @@ #include +#include + #include "EntityItem.h" /// Utility for processing, packing, queueing and sending of outbound edit voxel messages. -class EntityEditPacketSender : public OctreeEditPacketSender { +class EntityEditPacketSender : public OctreeEditPacketSender, public PacketListener { Q_OBJECT public: EntityEditPacketSender(); diff --git a/libraries/networking/src/DomainHandler.cpp b/libraries/networking/src/DomainHandler.cpp index 6d76e09028..174fbe8cb3 100644 --- a/libraries/networking/src/DomainHandler.cpp +++ b/libraries/networking/src/DomainHandler.cpp @@ -38,6 +38,11 @@ DomainHandler::DomainHandler(QObject* parent) : { // if we get a socket that make sure our NetworkPeer ping timer stops connect(this, &DomainHandler::completedSocketDiscovery, &_icePeer, &NetworkPeer::stopPingTimer); + + auto& packetReceiver = DependencyManager::get()->getPacketReceiver(); + + packetReceiver.registerListener(PacketType::ICEServerPeerInformation, this, "processICEResponsePacket"); + packetReceiver.registerListener(PacketType::DomainServerRequireDTLS, this, "processDTLSRequirementPacket"); } void DomainHandler::clearConnectionInfo() { diff --git a/libraries/networking/src/DomainHandler.h b/libraries/networking/src/DomainHandler.h index 716138a4f1..82b0c163de 100644 --- a/libraries/networking/src/DomainHandler.h +++ b/libraries/networking/src/DomainHandler.h @@ -22,17 +22,18 @@ #include "HifiSockAddr.h" #include "NetworkPeer.h" #include "NLPacket.h" +#include "PacketListener.h" const unsigned short DEFAULT_DOMAIN_SERVER_PORT = 40102; const unsigned short DEFAULT_DOMAIN_SERVER_DTLS_PORT = 40103; const quint16 DOMAIN_SERVER_HTTP_PORT = 40100; const quint16 DOMAIN_SERVER_HTTPS_PORT = 40101; -class DomainHandler : public QObject { +class DomainHandler : public QObject, public PacketListener { Q_OBJECT public: DomainHandler(QObject* parent = 0); - + void clearConnectionInfo(); void clearSettings(); diff --git a/libraries/networking/src/LimitedNodeList.cpp b/libraries/networking/src/LimitedNodeList.cpp index a91076d6ca..d03a3c9f07 100644 --- a/libraries/networking/src/LimitedNodeList.cpp +++ b/libraries/networking/src/LimitedNodeList.cpp @@ -100,7 +100,7 @@ LimitedNodeList::LimitedNodeList(unsigned short socketListenPort, unsigned short _packetStatTimer.start(); // make sure we handle STUN response packets - _packetReceiver.registerPacketListener(PacketType::StunResponse, this, "processSTUNResponse"); + _packetReceiver.registerListener(PacketType::StunResponse, this, "processSTUNResponse"); } void LimitedNodeList::setSessionUUID(const QUuid& sessionUUID) { diff --git a/libraries/networking/src/LimitedNodeList.h b/libraries/networking/src/LimitedNodeList.h index 07d1eed644..5e23b2e93c 100644 --- a/libraries/networking/src/LimitedNodeList.h +++ b/libraries/networking/src/LimitedNodeList.h @@ -39,6 +39,7 @@ #include "NLPacket.h" #include "PacketHeaders.h" #include "PacketReceiver.h" +#include "PacketListener.h" #include "NLPacketList.h" #include "UUIDHasher.h" @@ -75,7 +76,7 @@ namespace PingType { const PingType_t Symmetric = 3; } -class LimitedNodeList : public QObject, public Dependency { +class LimitedNodeList : public QObject, public Dependency, public PacketListener { Q_OBJECT SINGLETON_DEPENDENCY public: diff --git a/libraries/networking/src/NodeList.cpp b/libraries/networking/src/NodeList.cpp index 1daf8a1c02..b0de65ab85 100644 --- a/libraries/networking/src/NodeList.cpp +++ b/libraries/networking/src/NodeList.cpp @@ -92,15 +92,13 @@ NodeList::NodeList(char newOwnerType, unsigned short socketListenPort, unsigned startSTUNPublicSocketUpdate(); auto& packetReceiver = getPacketReceiver(); - packetReceiver.registerPacketListener(PacketType::DomainList, this, "processDomainServerList"); - packetReceiver.registerPacketListener(PacketType::DomainServerAddedNode, this, "processDomainServerAddedNode"); - packetReceiver.registerPacketListener(PacketType::DomainServerRequireDTLS, &_domainHandler, "processDTLSRequirementPacket"); - packetReceiver.registerPacketListener(PacketType::DomainServerPathResponse, this, "processDomainServerPathQueryResponse"); - packetReceiver.registerPacketListener(PacketType::ICEServerPeerInformation, &_domainHandler, "processICEResponsePacket"); - packetReceiver.registerPacketListener(PacketType::Ping, this, "processPingPacket"); - packetReceiver.registerPacketListener(PacketType::PingReply, this, "processPingReplyPacket"); - packetReceiver.registerPacketListener(PacketType::ICEPing, this, "processICEPingPacket"); - packetReceiver.registerPacketListener(PacketType::ICEPingReply, this, "processICEPingReplyPacket"); + packetReceiver.registerListener(PacketType::DomainList, this, "processDomainServerList"); + packetReceiver.registerListener(PacketType::DomainServerAddedNode, this, "processDomainServerAddedNode"); + packetReceiver.registerListener(PacketType::DomainServerPathResponse, this, "processDomainServerPathQueryResponse"); + packetReceiver.registerListener(PacketType::Ping, this, "processPingPacket"); + packetReceiver.registerListener(PacketType::PingReply, this, "processPingReplyPacket"); + packetReceiver.registerListener(PacketType::ICEPing, this, "processICEPingPacket"); + packetReceiver.registerListener(PacketType::ICEPingReply, this, "processICEPingReplyPacket"); } qint64 NodeList::sendStats(const QJsonObject& statsObject, const HifiSockAddr& destination) { diff --git a/libraries/networking/src/PacketListener.cpp b/libraries/networking/src/PacketListener.cpp new file mode 100644 index 0000000000..a3d628c05b --- /dev/null +++ b/libraries/networking/src/PacketListener.cpp @@ -0,0 +1,18 @@ +// +// PacketListener.cpp +// libraries/networking/src +// +// Created by Stephen Birarda on 07/14/15. +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "PacketListener.h" + +#include "NodeList.h" + +PacketListener::~PacketListener() { + DependencyManager::get()->getPacketReceiver().unregisterListener(this); +} diff --git a/libraries/networking/src/PacketListener.h b/libraries/networking/src/PacketListener.h new file mode 100644 index 0000000000..ad0f1c6f7f --- /dev/null +++ b/libraries/networking/src/PacketListener.h @@ -0,0 +1,22 @@ +// +// PacketListener.h +// libraries/networking/src +// +// Created by Stephen Birarda on 07/14/15. +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_PacketListener_h +#define hifi_PacketListener_h + +#pragma once + +class PacketListener { +public: + virtual ~PacketListener(); +}; + +#endif // hifi_PacketListener_h diff --git a/libraries/networking/src/PacketReceiver.cpp b/libraries/networking/src/PacketReceiver.cpp index 7d40ba9187..20cc5c5b15 100644 --- a/libraries/networking/src/PacketReceiver.cpp +++ b/libraries/networking/src/PacketReceiver.cpp @@ -24,7 +24,7 @@ PacketReceiver::PacketReceiver(QObject* parent) : } -void PacketReceiver::registerPacketListenerForTypes(const QSet& types, QObject* object, const char* slot) { +void PacketReceiver::registerListenerForTypes(const QSet& types, PacketListener* listener, const char* slot) { QSet nonSourcedTypes; QSet sourcedTypes; @@ -36,6 +36,9 @@ void PacketReceiver::registerPacketListenerForTypes(const QSet(listener); + Q_ASSERT(object); + if (nonSourcedTypes.size() > 0) { QMetaMethod nonSourcedMethod = matchingMethodForListener(*nonSourcedTypes.begin(), object, slot); foreach(PacketType::Value type, nonSourcedTypes) { @@ -51,8 +54,12 @@ void PacketReceiver::registerPacketListenerForTypes(const QSet(listener); + Q_ASSERT(object); + QMetaMethod matchingMethod = matchingMethodForListener(type, object, slot); + if (matchingMethod.isValid()) { registerVerifiedListener(type, object, matchingMethod); } @@ -62,28 +69,35 @@ QMetaMethod PacketReceiver::matchingMethodForListener(PacketType::Value type, QO Q_ASSERT(object); // normalize the slot with the expected parameters - QString normalizedSlot; - - if (NON_SOURCED_PACKETS.contains(type)) { - const QString NON_SOURCED_PACKET_LISTENER_PARAMETERS = "QSharedPointer"; - - QString nonNormalizedSignature = QString("%1(%2)").arg(slot).arg(NON_SOURCED_PACKET_LISTENER_PARAMETERS); - normalizedSlot = - QMetaObject::normalizedSignature(nonNormalizedSignature.toStdString().c_str()); - } else { - const QString SOURCED_PACKET_LISTENER_PARAMETERS = "QSharedPointer,QSharedPointer"; - QString nonNormalizedSignature = QString("%1(%2)").arg(slot).arg(SOURCED_PACKET_LISTENER_PARAMETERS); - normalizedSlot = - QMetaObject::normalizedSignature(nonNormalizedSignature.toStdString().c_str()); + const QString NON_SOURCED_PACKET_LISTENER_PARAMETERS = "QSharedPointer"; + + QSet possibleSignatures { QString("%1(%2)").arg(slot).arg(NON_SOURCED_PACKET_LISTENER_PARAMETERS) }; + + if (!NON_SOURCED_PACKETS.contains(type)) { + const QString SOURCED_PACKET_LISTENER_PARAMETERS = "QSharedPointer,QSharedPointer"; + + // a sourced packet must take the shared pointer to the packet but optionally could include + // a shared pointer to the node + + possibleSignatures << QString("%1(%2)").arg(slot).arg(SOURCED_PACKET_LISTENER_PARAMETERS); } - // does the constructed normalized method exist? - int methodIndex = object->metaObject()->indexOfSlot(normalizedSlot.toStdString().c_str()); - + int methodIndex = -1; + + foreach(const QString& signature, possibleSignatures) { + QByteArray normalizedSlot = + QMetaObject::normalizedSignature(signature.toStdString().c_str()); + + // does the constructed normalized method exist? + methodIndex = object->metaObject()->indexOfSlot(normalizedSlot.toStdString().c_str()); + + break; + } + if (methodIndex < 0) { - qDebug() << "PacketReceiver::registerPacketListener expected a method with a normalized signature of" - << normalizedSlot << "but such a method was not found."; + qDebug() << "PacketReceiver::registerListener expected a method with one of the following signatures:" + << possibleSignatures << "- but such a method was not found."; } Q_ASSERT(methodIndex >= 0); @@ -107,12 +121,29 @@ void PacketReceiver::registerVerifiedListener(PacketType::Value type, QObject* o } // add the mapping - _packetListenerMap[type] = ObjectMethodPair(QPointer(object), slot); + _packetListenerMap[type] = ObjectMethodPair(object, slot); _packetListenerLock.unlock(); } +void PacketReceiver::unregisterListener(PacketListener* listener) { + _packetListenerLock.lock(); + + auto it = _packetListenerMap.begin(); + + while (it != _packetListenerMap.end()) { + if (it.value().first == dynamic_cast(listener)) { + // this listener matches - erase it + it = _packetListenerMap.erase(it); + } else { + ++it; + } + } + + _packetListenerLock.unlock(); +} + bool PacketReceiver::packetVersionMatch(const NLPacket& packet) { if (packet.getVersion() != versionForPacketType(packet.getType()) @@ -149,7 +180,7 @@ void PacketReceiver::processDatagrams() { auto nodeList = DependencyManager::get(); - while (DependencyManager::get()->getNodeSocket().hasPendingDatagrams()) { + while (nodeList->getNodeSocket().hasPendingDatagrams()) { // setup a buffer to read the packet into int packetSizeWithHeader = nodeList->getNodeSocket().pendingDatagramSize(); std::unique_ptr buffer = std::unique_ptr(new char[packetSizeWithHeader]); @@ -186,7 +217,7 @@ void PacketReceiver::processDatagrams() { auto listener = it.value(); - if (!listener.first.isNull()) { + if (listener.first) { if (matchingNode) { emit dataReceived(matchingNode->getType(), packet->getSizeWithHeader()); diff --git a/libraries/networking/src/PacketReceiver.h b/libraries/networking/src/PacketReceiver.h index 21c27bcdac..c84b5ef432 100644 --- a/libraries/networking/src/PacketReceiver.h +++ b/libraries/networking/src/PacketReceiver.h @@ -22,6 +22,8 @@ #include "NLPacket.h" #include "PacketHeaders.h" +class PacketListener; + class PacketReceiver : public QObject { Q_OBJECT public: @@ -33,12 +35,13 @@ public: int getInPacketCount() const { return _inPacketCount; } int getInByteCount() const { return _inByteCount; } - void resetCounters() { _inPacketCount = 0; _outPacketCount = 0; _inByteCount = 0; _outByteCount = 0; } + void resetCounters() { _inPacketCount = 0; _inByteCount = 0; } void shutdown() { _isShuttingDown = true; } - void registerPacketListenerForTypes(const QSet& types, QObject* listener, const char* slot); - void registerPacketListener(PacketType::Value type, QObject* listener, const char* slot); + void registerListenerForTypes(const QSet& types, PacketListener* listener, const char* slot); + void registerListener(PacketType::Value type, PacketListener* listener, const char* slot); + void unregisterListener(PacketListener* listener); public slots: void processDatagrams(); @@ -54,7 +57,7 @@ private: QMetaMethod matchingMethodForListener(PacketType::Value type, QObject* object, const char* slot) const; void registerVerifiedListener(PacketType::Value type, QObject* listener, const QMetaMethod& slot); - using ObjectMethodPair = std::pair, QMetaMethod>; + using ObjectMethodPair = std::pair; QMutex _packetListenerLock; QHash _packetListenerMap; diff --git a/libraries/networking/src/PacketReceiverListener.cpp b/libraries/networking/src/PacketReceiverListener.cpp new file mode 100644 index 0000000000..e69de29bb2 diff --git a/libraries/networking/src/PacketReceiverListener.h b/libraries/networking/src/PacketReceiverListener.h new file mode 100644 index 0000000000..ad0f1c6f7f --- /dev/null +++ b/libraries/networking/src/PacketReceiverListener.h @@ -0,0 +1,22 @@ +// +// PacketListener.h +// libraries/networking/src +// +// Created by Stephen Birarda on 07/14/15. +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_PacketListener_h +#define hifi_PacketListener_h + +#pragma once + +class PacketListener { +public: + virtual ~PacketListener(); +}; + +#endif // hifi_PacketListener_h diff --git a/libraries/networking/src/ThreadedAssignment.h b/libraries/networking/src/ThreadedAssignment.h index c4c6127b7b..922a34b3e4 100644 --- a/libraries/networking/src/ThreadedAssignment.h +++ b/libraries/networking/src/ThreadedAssignment.h @@ -1,6 +1,6 @@ // // ThreadedAssignment.h -// libraries/shared/src +// libraries/networking/src // // Created by Stephen Birarda on 12/3/2013. // Copyright 2013 High Fidelity, Inc. @@ -14,9 +14,11 @@ #include +#include "PacketListener.h" + #include "Assignment.h" -class ThreadedAssignment : public Assignment { +class ThreadedAssignment : public Assignment, public PacketListener { Q_OBJECT public: ThreadedAssignment(NLPacket& packet);