add PacketListener to help unregister for packets

This commit is contained in:
Stephen Birarda 2015-07-14 13:52:38 -07:00
parent c63d714400
commit 0637f589b5
34 changed files with 215 additions and 91 deletions

View file

@ -50,13 +50,13 @@ Agent::Agent(NLPacket& packet) :
auto& packetReceiver = DependencyManager::get<NodeList>()->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<NLPacket> packet, SharedNodePointer senderNode) {

View file

@ -121,11 +121,10 @@ AssignmentClient::AssignmentClient(Assignment::Type requestAssignmentType, QStri
setUpStatusToMonitor();
}
auto& packetReceiver = DependencyManager::get<NodeList>()->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.";

View file

@ -15,11 +15,13 @@
#include <QtCore/QCoreApplication>
#include <QtCore/QPointer>
#include <PacketListener.h>
#include "ThreadedAssignment.h"
class QSharedMemory;
class AssignmentClient : public QObject {
class AssignmentClient : public QObject, public PacketListener {
Q_OBJECT
public:

View file

@ -54,7 +54,7 @@ AssignmentClientMonitor::AssignmentClientMonitor(const unsigned int numAssignmen
auto nodeList = DependencyManager::set<LimitedNodeList>();
auto& packetReceiver = DependencyManager::get<NodeList>()->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++) {

View file

@ -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,

View file

@ -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;

View file

@ -48,10 +48,10 @@ AvatarMixer::AvatarMixer(NLPacket& packet) :
connect(DependencyManager::get<NodeList>().data(), &NodeList::nodeKilled, this, &AvatarMixer::nodeKilled);
auto& packetReceiver = DependencyManager::get<NodeList>()->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() {

View file

@ -26,9 +26,9 @@ EntityServer::EntityServer(NLPacket& packet) :
_entitySimulation(NULL)
{
auto& packetReceiver = DependencyManager::get<NodeList>()->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() {

View file

@ -1060,9 +1060,9 @@ void OctreeServer::readConfiguration() {
void OctreeServer::run() {
auto& packetReceiver = DependencyManager::get<NodeList>()->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();

View file

@ -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();

View file

@ -24,6 +24,7 @@
#include <Assignment.h>
#include <HTTPSConnection.h>
#include <LimitedNodeList.h>
#include <PacketListener.h>
#include "DomainServerSettingsManager.h"
#include "DomainServerWebSessionData.h"
@ -34,7 +35,7 @@
typedef QSharedPointer<Assignment> SharedAssignmentPointer;
typedef QMultiHash<QUuid, WalletTransaction*> TransactionHash;
class DomainServer : public QCoreApplication, public HTTPSRequestHandler {
class DomainServer : public QCoreApplication, public HTTPSRequestHandler, public PacketListener {
Q_OBJECT
public:
DomainServer(int argc, char* argv[]);

View file

@ -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<LimitedNodeList>()->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

View file

@ -32,6 +32,7 @@
#include <OctreeQuery.h>
#include <OffscreenUi.h>
#include <PacketHeaders.h>
#include <PacketListener.h>
#include <PhysicalEntitySimulation.h>
#include <PhysicsEngine.h>
#include <ScriptEngine.h>
@ -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;

View file

@ -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<NodeList>()->getPacketReceiver().unregisterListener(this);
}
void OctreePacketProcessor::handleOctreePacket(QSharedPointer<NLPacket> packet, SharedNodePointer senderNode) {

View file

@ -13,13 +13,15 @@
#define hifi_OctreePacketProcessor_h
#include <ReceivedPacketProcessor.h>
#include <PacketListener.h>
/// 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();

View file

@ -142,12 +142,12 @@ AudioClient::AudioClient() :
configureGverbFilter(_gverb);
auto& packetReceiver = DependencyManager::get<NodeList>()->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() {

View file

@ -39,6 +39,7 @@
#include <NLPacket.h>
#include <MixedProcessedAudioStream.h>
#include <RingBufferHistory.h>
#include <PacketListener.h>
#include <SettingHandle.h>
#include <Sound.h>
#include <StDev.h>
@ -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:

View file

@ -19,10 +19,11 @@
#include <AudioStreamStats.h>
#include <Node.h>
#include <NLPacket.h>
#include <PacketListener.h>
class MixedProcessedAudioStream;
class AudioIOStats : public QObject {
class AudioIOStats : public QObject, public PacketListener {
Q_OBJECT
public:
AudioIOStats(MixedProcessedAudioStream* receivedAudioStream);

View file

@ -20,10 +20,10 @@ AvatarHashMap::AvatarHashMap() {
connect(DependencyManager::get<NodeList>().data(), &NodeList::uuidChanged, this, &AvatarHashMap::sessionUUIDChanged);
auto& packetReceiver = DependencyManager::get<NodeList>()->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) {

View file

@ -21,11 +21,12 @@
#include <DependencyManager.h>
#include <NLPacket.h>
#include <Node.h>
#include <PacketListener.h>
#include "AvatarData.h"
#include <glm/glm.hpp>
class AvatarHashMap : public QObject, public Dependency {
class AvatarHashMap : public QObject, public Dependency, public PacketListener {
Q_OBJECT
SINGLETON_DEPENDENCY

View file

@ -19,7 +19,7 @@
EntityEditPacketSender::EntityEditPacketSender() {
auto& packetReceiver = DependencyManager::get<NodeList>()->getPacketReceiver();
packetReceiver.registerPacketListener(PacketType::EntityEditNack, this, "processEntityEditNackPacket");
packetReceiver.registerListener(PacketType::EntityEditNack, this, "processEntityEditNackPacket");
}
void EntityEditPacketSender::processEntityEditNackPacket(QSharedPointer<NLPacket> packet, HifiSockAddr senderSockAddr) {

View file

@ -14,10 +14,12 @@
#include <OctreeEditPacketSender.h>
#include <PacketListener.h>
#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();

View file

@ -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<NodeList>()->getPacketReceiver();
packetReceiver.registerListener(PacketType::ICEServerPeerInformation, this, "processICEResponsePacket");
packetReceiver.registerListener(PacketType::DomainServerRequireDTLS, this, "processDTLSRequirementPacket");
}
void DomainHandler::clearConnectionInfo() {

View file

@ -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();

View file

@ -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) {

View file

@ -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:

View file

@ -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) {

View file

@ -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<NodeList>()->getPacketReceiver().unregisterListener(this);
}

View file

@ -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

View file

@ -24,7 +24,7 @@ PacketReceiver::PacketReceiver(QObject* parent) :
}
void PacketReceiver::registerPacketListenerForTypes(const QSet<PacketType::Value>& types, QObject* object, const char* slot) {
void PacketReceiver::registerListenerForTypes(const QSet<PacketType::Value>& types, PacketListener* listener, const char* slot) {
QSet<PacketType::Value> nonSourcedTypes;
QSet<PacketType::Value> sourcedTypes;
@ -36,6 +36,9 @@ void PacketReceiver::registerPacketListenerForTypes(const QSet<PacketType::Value
}
}
QObject* object = dynamic_cast<QObject*>(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<PacketType::Value
}
}
void PacketReceiver::registerPacketListener(PacketType::Value type, QObject* object, const char* slot) {
void PacketReceiver::registerListener(PacketType::Value type, PacketListener* listener, const char* slot) {
QObject* object = dynamic_cast<QObject*>(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<NLPacket>";
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<NLPacket>,QSharedPointer<Node>";
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<NLPacket>";
QSet<QString> 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<NLPacket>,QSharedPointer<Node>";
// 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<QObject>(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<QObject*>(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<NodeList>();
while (DependencyManager::get<NodeList>()->getNodeSocket().hasPendingDatagrams()) {
while (nodeList->getNodeSocket().hasPendingDatagrams()) {
// setup a buffer to read the packet into
int packetSizeWithHeader = nodeList->getNodeSocket().pendingDatagramSize();
std::unique_ptr<char> buffer = std::unique_ptr<char>(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());

View file

@ -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<PacketType::Value>& types, QObject* listener, const char* slot);
void registerPacketListener(PacketType::Value type, QObject* listener, const char* slot);
void registerListenerForTypes(const QSet<PacketType::Value>& 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<QPointer<QObject>, QMetaMethod>;
using ObjectMethodPair = std::pair<QObject*, QMetaMethod>;
QMutex _packetListenerLock;
QHash<PacketType::Value, ObjectMethodPair> _packetListenerMap;

View file

@ -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

View file

@ -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 <QtCore/QSharedPointer>
#include "PacketListener.h"
#include "Assignment.h"
class ThreadedAssignment : public Assignment {
class ThreadedAssignment : public Assignment, public PacketListener {
Q_OBJECT
public:
ThreadedAssignment(NLPacket& packet);