From beec5f60d8fc254028ba636e5d8b53c872cfa3ea Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Thu, 15 Aug 2013 08:36:06 -0700 Subject: [PATCH] renamed class to be more appropriate, added doxygen comments --- interface/src/Application.cpp | 12 +++--- interface/src/Application.h | 6 +-- ...tReceiver.cpp => VoxelPacketProcessor.cpp} | 10 ++--- ...acketReceiver.h => VoxelPacketProcessor.h} | 14 +++--- libraries/shared/src/GenericThread.h | 18 +++++--- libraries/shared/src/NetworkPacket.h | 1 + libraries/shared/src/PacketReceiver.h | 32 -------------- ...ceiver.cpp => ReceivedPacketProcessor.cpp} | 8 ++-- .../shared/src/ReceivedPacketProcessor.h | 43 +++++++++++++++++++ 9 files changed, 80 insertions(+), 64 deletions(-) rename interface/src/{VoxelPacketReceiver.cpp => VoxelPacketProcessor.cpp} (88%) rename interface/src/{VoxelPacketReceiver.h => VoxelPacketProcessor.h} (53%) delete mode 100644 libraries/shared/src/PacketReceiver.h rename libraries/shared/src/{PacketReceiver.cpp => ReceivedPacketProcessor.cpp} (73%) create mode 100644 libraries/shared/src/ReceivedPacketProcessor.h diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 7e60b2fac2..710ed6446b 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -221,7 +221,7 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : _audio(&_audioScope, STARTUP_JITTER_SAMPLES), #endif _stopNetworkReceiveThread(false), - _voxelReceiver(this), + _voxelProcessor(this), _voxelEditSender(this), _packetCount(0), _packetsPerSecond(0), @@ -374,7 +374,7 @@ void Application::initializeGL() { } // create thread for parsing of voxel data independent of the main network and rendering threads - _voxelReceiver.initialize(_enableProcessVoxelsThread); + _voxelProcessor.initialize(_enableProcessVoxelsThread); _voxelEditSender.initialize(_enableProcessVoxelsThread); if (_enableProcessVoxelsThread) { qDebug("Voxel parsing thread created.\n"); @@ -1164,7 +1164,7 @@ void Application::terminate() { pthread_join(_networkReceiveThread, NULL); } - _voxelReceiver.terminate(); + _voxelProcessor.terminate(); _voxelEditSender.terminate(); } @@ -2493,8 +2493,8 @@ void Application::update(float deltaTime) { // parse voxel packets if (!_enableProcessVoxelsThread) { - _voxelReceiver.process(); - _voxelEditSender.process(); + _voxelProcessor.threadRoutine(); + _voxelEditSender.threadRoutine(); } //loop through all the other avatars and simulate them... @@ -3942,7 +3942,7 @@ void* Application::networkReceive(void* args) { case PACKET_TYPE_VOXEL_STATS: case PACKET_TYPE_ENVIRONMENT_DATA: { // add this packet to our list of voxel packets and process them on the voxel processing - app->_voxelReceiver.queuePacket(senderAddress, app->_incomingPacket, bytesReceived); + app->_voxelProcessor.queuePacket(senderAddress, app->_incomingPacket, bytesReceived); break; } case PACKET_TYPE_BULK_AVATAR_DATA: diff --git a/interface/src/Application.h b/interface/src/Application.h index bfa4c53137..bb7eff8295 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -39,7 +39,7 @@ #include "ViewFrustum.h" #include "VoxelFade.h" #include "VoxelEditPacketSender.h" -#include "VoxelPacketReceiver.h" +#include "VoxelPacketProcessor.h" #include "VoxelSystem.h" #include "Webcam.h" #include "PieMenu.h" @@ -75,7 +75,7 @@ static const float NODE_KILLED_BLUE = 0.0f; class Application : public QApplication, public NodeListHook { Q_OBJECT - friend class VoxelPacketReceiver; + friend class VoxelPacketProcessor; friend class VoxelEditPacketSender; public: @@ -454,7 +454,7 @@ private: bool _stopNetworkReceiveThread; bool _enableProcessVoxelsThread; - VoxelPacketReceiver _voxelReceiver; + VoxelPacketProcessor _voxelProcessor; VoxelEditPacketSender _voxelEditSender; unsigned char _incomingPacket[MAX_PACKET_SIZE]; diff --git a/interface/src/VoxelPacketReceiver.cpp b/interface/src/VoxelPacketProcessor.cpp similarity index 88% rename from interface/src/VoxelPacketReceiver.cpp rename to interface/src/VoxelPacketProcessor.cpp index fdaa96e8dc..558037b0bc 100644 --- a/interface/src/VoxelPacketReceiver.cpp +++ b/interface/src/VoxelPacketProcessor.cpp @@ -1,5 +1,5 @@ // -// VoxelPacketReceiver.cpp +// VoxelPacketProcessor.cpp // interface // // Created by Brad Hefta-Gaub on 8/12/13. @@ -11,14 +11,14 @@ #include #include "Application.h" -#include "VoxelPacketReceiver.h" +#include "VoxelPacketProcessor.h" -VoxelPacketReceiver::VoxelPacketReceiver(Application* app) : +VoxelPacketProcessor::VoxelPacketProcessor(Application* app) : _app(app) { } -void VoxelPacketReceiver::processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { - PerformanceWarning warn(_app->_renderPipelineWarnings->isChecked(),"processVoxelPacket()"); +void VoxelPacketProcessor::processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { + PerformanceWarning warn(_app->_renderPipelineWarnings->isChecked(),"VoxelPacketProcessor::processPacket()"); ssize_t messageLength = packetLength; // check to see if the UI thread asked us to kill the voxel tree. since we're the only thread allowed to do that diff --git a/interface/src/VoxelPacketReceiver.h b/interface/src/VoxelPacketProcessor.h similarity index 53% rename from interface/src/VoxelPacketReceiver.h rename to interface/src/VoxelPacketProcessor.h index 9d27b37fba..725d69e181 100644 --- a/interface/src/VoxelPacketReceiver.h +++ b/interface/src/VoxelPacketProcessor.h @@ -1,5 +1,5 @@ // -// VoxelPacketReceiver.h +// VoxelPacketProcessor.h // interface // // Created by Brad Hefta-Gaub on 8/12/13. @@ -8,19 +8,19 @@ // Voxel Packet Receiver // -#ifndef __shared__VoxelPacketReceiver__ -#define __shared__VoxelPacketReceiver__ +#ifndef __shared__VoxelPacketProcessor__ +#define __shared__VoxelPacketProcessor__ -#include +#include class Application; -class VoxelPacketReceiver : public PacketReceiver { +class VoxelPacketProcessor : public ReceivedPacketProcessor { public: - VoxelPacketReceiver(Application* app); + VoxelPacketProcessor(Application* app); virtual void processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); private: Application* _app; }; -#endif // __shared__VoxelPacketReceiver__ +#endif // __shared__VoxelPacketProcessor__ diff --git a/libraries/shared/src/GenericThread.h b/libraries/shared/src/GenericThread.h index c41e5b29cb..aa55adf4bf 100644 --- a/libraries/shared/src/GenericThread.h +++ b/libraries/shared/src/GenericThread.h @@ -13,24 +13,28 @@ #include +/// A basic generic "thread" class. Handles a single thread of control within the application. Can operate in non-threaded +/// mode but caller must regularly call threadRoutine() method. class GenericThread { public: GenericThread(); virtual ~GenericThread(); - // Call to start the thread - void initialize(bool isThreaded); + /// Call to start the thread. + /// \param bool isThreaded true by default. false for non-threaded mode and caller must call threadRoutine() regularly. + void initialize(bool isThreaded = true); - // override this function to do whatever your class actually does, return false to exit thread early - virtual bool process() = 0; - - // Call when you're ready to stop the thread + /// Call to stop the thread void terminate(); - // If you're running in non-threaded mode, you must call this regularly + /// If you're running in non-threaded mode, you must call this regularly void* threadRoutine(); protected: + /// Override this function to do whatever your class actually does, return false to exit thread early. + virtual bool process() = 0; + + /// Locks all the resources of the thread. void lock() { pthread_mutex_lock(&_mutex); } void unlock() { pthread_mutex_unlock(&_mutex); } diff --git a/libraries/shared/src/NetworkPacket.h b/libraries/shared/src/NetworkPacket.h index a2fe2e63cd..16b6f7b261 100644 --- a/libraries/shared/src/NetworkPacket.h +++ b/libraries/shared/src/NetworkPacket.h @@ -17,6 +17,7 @@ #include "NodeList.h" // for MAX_PACKET_SIZE +/// Storage of not-yet processed inbound, or not yet sent outbound generic UDP network packet class NetworkPacket { public: NetworkPacket(); diff --git a/libraries/shared/src/PacketReceiver.h b/libraries/shared/src/PacketReceiver.h deleted file mode 100644 index 7b7f792862..0000000000 --- a/libraries/shared/src/PacketReceiver.h +++ /dev/null @@ -1,32 +0,0 @@ -// -// PacketReceiver.h -// shared -// -// Created by Brad Hefta-Gaub on 8/12/13. -// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. -// -// Threaded or non-threaded packet receiver. -// - -#ifndef __shared__PacketReceiver__ -#define __shared__PacketReceiver__ - -#include "GenericThread.h" -#include "NetworkPacket.h" - -class PacketReceiver : public GenericThread { -public: - // Call this when your network receive gets a packet - void queuePacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); - - // implement this to process the incoming packets - virtual void processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) = 0; - - virtual bool process(); - -private: - - std::vector _packets; -}; - -#endif // __shared__PacketReceiver__ diff --git a/libraries/shared/src/PacketReceiver.cpp b/libraries/shared/src/ReceivedPacketProcessor.cpp similarity index 73% rename from libraries/shared/src/PacketReceiver.cpp rename to libraries/shared/src/ReceivedPacketProcessor.cpp index 801243eb5c..8ab76397a2 100644 --- a/libraries/shared/src/PacketReceiver.cpp +++ b/libraries/shared/src/ReceivedPacketProcessor.cpp @@ -1,5 +1,5 @@ // -// PacketReceiver.cpp +// ReceivedPacketProcessor.cpp // shared // // Created by Brad Hefta-Gaub on 8/12/13. @@ -8,16 +8,16 @@ // Threaded or non-threaded packet receiver. // -#include "PacketReceiver.h" +#include "ReceivedPacketProcessor.h" -void PacketReceiver::queuePacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength) { +void ReceivedPacketProcessor::queuePacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength) { NetworkPacket packet(address, packetData, packetLength); lock(); _packets.push_back(packet); unlock(); } -bool PacketReceiver::process() { +bool ReceivedPacketProcessor::process() { while (_packets.size() > 0) { NetworkPacket& packet = _packets.front(); processPacket(packet.getAddress(), packet.getData(), packet.getLength()); diff --git a/libraries/shared/src/ReceivedPacketProcessor.h b/libraries/shared/src/ReceivedPacketProcessor.h new file mode 100644 index 0000000000..a132267482 --- /dev/null +++ b/libraries/shared/src/ReceivedPacketProcessor.h @@ -0,0 +1,43 @@ +// +// ReceivedPacketProcessor.h +// shared +// +// Created by Brad Hefta-Gaub on 8/12/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// +// Threaded or non-threaded received packet processor. +// + +#ifndef __shared__ReceivedPacketProcessor__ +#define __shared__ReceivedPacketProcessor__ + +#include "GenericThread.h" +#include "NetworkPacket.h" + +/// Generalized threaded processor for handler received inbound packets. +class ReceivedPacketProcessor : public GenericThread { +public: + + /// Add packet from network receive thread to the processing queue. + /// \param sockaddr& senderAddress the address of the sender + /// \param packetData pointer to received data + /// \param ssize_t packetLength size of received data + /// \thread network receive thread + void queuePacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); + + /// Callback for processing of recieved packets. Implement this to process the incoming packets. + /// \param sockaddr& senderAddress the address of the sender + /// \param packetData pointer to received data + /// \param ssize_t packetLength size of received data + /// \thread "this" individual processing thread + virtual void processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) = 0; + +protected: + /// Implements generic processing behavior for this thread. + virtual bool process(); +private: + + std::vector _packets; +}; + +#endif // __shared__PacketReceiver__