Add packet listeners to DatagramProcessor

This commit is contained in:
Ryan Huffman 2015-07-08 10:02:34 -07:00
parent 74940a2132
commit b1c09d71f7
2 changed files with 28 additions and 2 deletions

View file

@ -23,11 +23,20 @@
#include "DatagramProcessor.h" #include "DatagramProcessor.h"
DatagramProcessor::DatagramProcessor(QObject* parent) : DatagramProcessor::DatagramProcessor(QObject* parent) :
QObject(parent) QObject(parent),
_packetListenerMap()
{ {
} }
void DatagramProcessor::registerPacketListener(Packet::Type type, QObject* object, QString methodName) {
if (packetListenerMap.contains(type)) {
qDebug() << "Warning: Registering a packet listener for packet type " << type
<< " that will remove a previously registered listener";
}
packetListenerMap[type] = QPair<QObject*, QString>(object, methodName);
}
void DatagramProcessor::processDatagrams() { void DatagramProcessor::processDatagrams() {
PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings),
"DatagramProcessor::processDatagrams()"); "DatagramProcessor::processDatagrams()");
@ -160,7 +169,20 @@ void DatagramProcessor::processDatagrams() {
} }
break; break;
default: default:
nodeList->processNodeData(senderSockAddr, incomingPacket); //nodeList->processNodeData(senderSockAddr, incomingPacket);
if (packetListenerMap.contains(incomingType)) {
auto& listener = packetListenerMap[incomingType];
NLPacket packet;
bool success = QMetaObject::invokeMethod(listener.first, listener.second,
Q_ARG(std::unique_ptr<NLPacket>, packet),
Q_ARG(HifiSockAddr, senderSockAddr));
if (!success) {
qDebug() << "Error sending packet " << incomingType << " to listener: " << listener.first.name() << "::" << listener.second;
}
} else {
QDebug() << "No listener found for packet type: " << incomingType;
}
break; break;
} }
} }

View file

@ -27,10 +27,14 @@ public:
void resetCounters() { _inPacketCount = 0; _outPacketCount = 0; _inByteCount = 0; _outByteCount = 0; } void resetCounters() { _inPacketCount = 0; _outPacketCount = 0; _inByteCount = 0; _outByteCount = 0; }
void shutdown() { _isShuttingDown = true; } void shutdown() { _isShuttingDown = true; }
void registerPacketListener(Packet::Type type, QObject* object, QString methodName);
public slots: public slots:
void processDatagrams(); void processDatagrams();
private: private:
QMap<Packet::Type, QPair<QObject*, QString>> packetListenerMap;
int _inPacketCount = 0; int _inPacketCount = 0;
int _outPacketCount = 0; int _outPacketCount = 0;
int _inByteCount = 0; int _inByteCount = 0;