From 70344cdaf27232ababd1a70dbb70f3d6baf68074 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 12 Aug 2013 11:46:57 -0700 Subject: [PATCH 01/26] move voxel receiving into class --- interface/src/Application.cpp | 90 ++----------------------- interface/src/Application.h | 11 ++- interface/src/VoxelPacketReceiver.cpp | 62 +++++++++++++++++ interface/src/VoxelPacketReceiver.h | 26 +++++++ libraries/shared/src/PacketReceiver.cpp | 70 +++++++++++++++++++ libraries/shared/src/PacketReceiver.h | 45 +++++++++++++ 6 files changed, 213 insertions(+), 91 deletions(-) create mode 100644 interface/src/VoxelPacketReceiver.cpp create mode 100644 interface/src/VoxelPacketReceiver.h create mode 100644 libraries/shared/src/PacketReceiver.cpp create mode 100644 libraries/shared/src/PacketReceiver.h diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 174dfc2527..46b2a2243c 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -216,7 +216,7 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : _audio(&_audioScope, STARTUP_JITTER_SAMPLES), #endif _stopNetworkReceiveThread(false), - _stopProcessVoxelsThread(false), + _voxelReceiver(this), _packetCount(0), _packetsPerSecond(0), _bytesPerSecond(0), @@ -368,9 +368,9 @@ void Application::initializeGL() { } // create thread for parsing of voxel data independent of the main network and rendering threads + _voxelReceiver.initialize(_enableProcessVoxelsThread); if (_enableProcessVoxelsThread) { - pthread_create(&_processVoxelsThread, NULL, processVoxels, NULL); - qDebug("Voxel parsing thread created.\n"); + qDebug("Voxel parsing thread created.\n"); } // call terminate before exiting @@ -1178,10 +1178,7 @@ void Application::terminate() { pthread_join(_networkReceiveThread, NULL); } - if (_enableProcessVoxelsThread) { - _stopProcessVoxelsThread = true; - pthread_join(_processVoxelsThread, NULL); - } + _voxelReceiver.terminate(); } void Application::sendAvatarVoxelURLMessage(const QUrl& url) { @@ -2580,7 +2577,7 @@ void Application::update(float deltaTime) { // parse voxel packets if (!_enableProcessVoxelsThread) { - processVoxels(0); + _voxelReceiver.threadRoutine(); } //loop through all the other avatars and simulate them... @@ -4034,75 +4031,6 @@ int Application::parseVoxelStats(unsigned char* messageData, ssize_t messageLeng return statsMessageLength; } -// Receive packets from other nodes/servers and decide what to do with them! -void* Application::processVoxels(void* args) { - Application* app = Application::getInstance(); - while (!app->_stopProcessVoxelsThread) { - - // check to see if the UI thread asked us to kill the voxel tree. since we're the only thread allowed to do that - if (app->_wantToKillLocalVoxels) { - app->_voxels.killLocalVoxels(); - app->_wantToKillLocalVoxels = false; - } - - while (app->_voxelPackets.size() > 0) { - NetworkPacket& packet = app->_voxelPackets.front(); - app->processVoxelPacket(packet.getSenderAddress(), packet.getData(), packet.getLength()); - app->_voxelPackets.erase(app->_voxelPackets.begin()); - } - - if (!app->_enableProcessVoxelsThread) { - break; - } - } - - if (app->_enableProcessVoxelsThread) { - pthread_exit(0); - } - return NULL; -} - -void Application::queueVoxelPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { - _voxelPackets.push_back(NetworkPacket(senderAddress, packetData, packetLength)); -} - -void Application::processVoxelPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { - PerformanceWarning warn(_renderPipelineWarnings->isChecked(),"processVoxelPacket()"); - ssize_t messageLength = packetLength; - - // note: PACKET_TYPE_VOXEL_STATS can have PACKET_TYPE_VOXEL_DATA or PACKET_TYPE_VOXEL_DATA_MONOCHROME - // immediately following them inside the same packet. So, we process the PACKET_TYPE_VOXEL_STATS first - // then process any remaining bytes as if it was another packet - if (packetData[0] == PACKET_TYPE_VOXEL_STATS) { - - int statsMessageLength = parseVoxelStats(packetData, messageLength, senderAddress); - if (messageLength > statsMessageLength) { - packetData += statsMessageLength; - messageLength -= statsMessageLength; - if (!packetVersionMatch(packetData)) { - return; // bail since piggyback data doesn't match our versioning - } - } else { - return; // bail since no piggyback data - } - } // fall through to piggyback message - - if (_renderVoxels->isChecked()) { - Node* voxelServer = NodeList::getInstance()->nodeWithAddress(&senderAddress); - if (voxelServer && socketMatch(voxelServer->getActiveSocket(), &senderAddress)) { - voxelServer->lock(); - if (packetData[0] == PACKET_TYPE_ENVIRONMENT_DATA) { - _environment.parseData(&senderAddress, packetData, messageLength); - } else { - _voxels.setDataSourceID(voxelServer->getNodeID()); - _voxels.parseData(packetData, messageLength); - _voxels.setDataSourceID(UNKNOWN_NODE_ID); - } - voxelServer->unlock(); - } - } -} - // Receive packets from other nodes/servers and decide what to do with them! void* Application::networkReceive(void* args) { sockaddr senderAddress; @@ -4110,12 +4038,6 @@ void* Application::networkReceive(void* args) { Application* app = Application::getInstance(); while (!app->_stopNetworkReceiveThread) { - // check to see if the UI thread asked us to kill the voxel tree. since we're the only thread allowed to do that - if (app->_wantToKillLocalVoxels) { - app->_voxels.killLocalVoxels(); - app->_wantToKillLocalVoxels = false; - } - if (NodeList::getInstance()->getNodeSocket()->receive(&senderAddress, app->_incomingPacket, &bytesReceived)) { app->_packetCount++; @@ -4139,7 +4061,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->queueVoxelPacket(senderAddress, app->_incomingPacket, bytesReceived); + app->_voxelReceiver.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 29f7e8bea7..0a62fdd665 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -38,6 +38,7 @@ #include "ToolsPalette.h" #include "ViewFrustum.h" #include "VoxelFade.h" +#include "VoxelPacketReceiver.h" #include "VoxelSystem.h" #include "Webcam.h" #include "PieMenu.h" @@ -72,6 +73,8 @@ static const float NODE_KILLED_BLUE = 0.0f; class Application : public QApplication, public NodeListHook { Q_OBJECT + friend class VoxelPacketReceiver; + public: static Application* getInstance() { return static_cast(QCoreApplication::instance()); } @@ -263,10 +266,6 @@ private: static void attachNewHeadToNode(Node *newNode); static void* networkReceive(void* args); // network receive thread - static void* processVoxels(void* args); // voxel parsing thread - void processVoxelPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); - void queueVoxelPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); - // methodes handling menu settings typedef void(*settingsAction)(QSettings*, QAction*); static void loadAction(QSettings* set, QAction* action); @@ -457,9 +456,7 @@ private: bool _stopNetworkReceiveThread; bool _enableProcessVoxelsThread; - pthread_t _processVoxelsThread; - bool _stopProcessVoxelsThread; - std::vector _voxelPackets; + VoxelPacketReceiver _voxelReceiver; unsigned char _incomingPacket[MAX_PACKET_SIZE]; diff --git a/interface/src/VoxelPacketReceiver.cpp b/interface/src/VoxelPacketReceiver.cpp new file mode 100644 index 0000000000..fdaa96e8dc --- /dev/null +++ b/interface/src/VoxelPacketReceiver.cpp @@ -0,0 +1,62 @@ +// +// VoxelPacketReceiver.cpp +// interface +// +// Created by Brad Hefta-Gaub on 8/12/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// +// Threaded or non-threaded voxel packet receiver for the Application +// + +#include + +#include "Application.h" +#include "VoxelPacketReceiver.h" + +VoxelPacketReceiver::VoxelPacketReceiver(Application* app) : + _app(app) { +} + +void VoxelPacketReceiver::processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { + PerformanceWarning warn(_app->_renderPipelineWarnings->isChecked(),"processVoxelPacket()"); + 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 + if (_app->_wantToKillLocalVoxels) { + _app->_voxels.killLocalVoxels(); + _app->_wantToKillLocalVoxels = false; + } + + // note: PACKET_TYPE_VOXEL_STATS can have PACKET_TYPE_VOXEL_DATA or PACKET_TYPE_VOXEL_DATA_MONOCHROME + // immediately following them inside the same packet. So, we process the PACKET_TYPE_VOXEL_STATS first + // then process any remaining bytes as if it was another packet + if (packetData[0] == PACKET_TYPE_VOXEL_STATS) { + + int statsMessageLength = _app->parseVoxelStats(packetData, messageLength, senderAddress); + if (messageLength > statsMessageLength) { + packetData += statsMessageLength; + messageLength -= statsMessageLength; + if (!packetVersionMatch(packetData)) { + return; // bail since piggyback data doesn't match our versioning + } + } else { + return; // bail since no piggyback data + } + } // fall through to piggyback message + + if (_app->_renderVoxels->isChecked()) { + Node* voxelServer = NodeList::getInstance()->nodeWithAddress(&senderAddress); + if (voxelServer && socketMatch(voxelServer->getActiveSocket(), &senderAddress)) { + voxelServer->lock(); + if (packetData[0] == PACKET_TYPE_ENVIRONMENT_DATA) { + _app->_environment.parseData(&senderAddress, packetData, messageLength); + } else { + _app->_voxels.setDataSourceID(voxelServer->getNodeID()); + _app->_voxels.parseData(packetData, messageLength); + _app->_voxels.setDataSourceID(UNKNOWN_NODE_ID); + } + voxelServer->unlock(); + } + } +} + diff --git a/interface/src/VoxelPacketReceiver.h b/interface/src/VoxelPacketReceiver.h new file mode 100644 index 0000000000..9d27b37fba --- /dev/null +++ b/interface/src/VoxelPacketReceiver.h @@ -0,0 +1,26 @@ +// +// VoxelPacketReceiver.h +// interface +// +// Created by Brad Hefta-Gaub on 8/12/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// +// Voxel Packet Receiver +// + +#ifndef __shared__VoxelPacketReceiver__ +#define __shared__VoxelPacketReceiver__ + +#include + +class Application; + +class VoxelPacketReceiver : public PacketReceiver { +public: + VoxelPacketReceiver(Application* app); + virtual void processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); + +private: + Application* _app; +}; +#endif // __shared__VoxelPacketReceiver__ diff --git a/libraries/shared/src/PacketReceiver.cpp b/libraries/shared/src/PacketReceiver.cpp new file mode 100644 index 0000000000..9bf4764ebb --- /dev/null +++ b/libraries/shared/src/PacketReceiver.cpp @@ -0,0 +1,70 @@ +// +// PacketReceiver.cpp +// 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. +// + +#include "PacketReceiver.h" + +PacketReceiver::PacketReceiver() : + _stopThread(false), + _isThreaded(false) // assume non-threaded, must call initialize() +{ +} + +PacketReceiver::~PacketReceiver() { + terminate(); +} + +void PacketReceiver::initialize(bool isThreaded) { + _isThreaded = isThreaded; + if (_isThreaded) { + pthread_create(&_thread, NULL, PacketReceiverThreadEntry, this); + } +} + +void PacketReceiver::terminate() { + if (_isThreaded) { + _stopThread = true; + pthread_join(_thread, NULL); + _isThreaded = false; + } +} + +void PacketReceiver::queuePacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { + _packets.push_back(NetworkPacket(senderAddress, packetData, packetLength)); +} + +void PacketReceiver::processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { + // Default implementation does nothing... packet is discarded +} + +void* PacketReceiver::threadRoutine() { + while (!_stopThread) { + while (_packets.size() > 0) { + NetworkPacket& packet = _packets.front(); + processPacket(packet.getSenderAddress(), packet.getData(), packet.getLength()); + _packets.erase(_packets.begin()); + } + + // In non-threaded mode, this will break each time you call it so it's the + // callers responsibility to continuously call this method + if (!_isThreaded) { + break; + } + } + + if (_isThreaded) { + pthread_exit(0); + } + return NULL; +} + +extern "C" void* PacketReceiverThreadEntry(void* arg) { + PacketReceiver* packetReceiver = (PacketReceiver*)arg; + return packetReceiver->threadRoutine(); +} \ No newline at end of file diff --git a/libraries/shared/src/PacketReceiver.h b/libraries/shared/src/PacketReceiver.h new file mode 100644 index 0000000000..f236b362e6 --- /dev/null +++ b/libraries/shared/src/PacketReceiver.h @@ -0,0 +1,45 @@ +// +// 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 "NetworkPacket.h" + +class PacketReceiver { +public: + PacketReceiver(); + virtual ~PacketReceiver(); + + // 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); + + // Call to start the thread + void initialize(bool isThreaded); + + // Call when you're ready to stop the thread + void terminate(); + + // If you're running in non-threaded mode, you must call this regularly + void* threadRoutine(); + +private: + bool _stopThread; + bool _isThreaded; + pthread_t _thread; + std::vector _packets; +}; + +extern "C" void* PacketReceiverThreadEntry(void* arg); + +#endif // __shared__PacketReceiver__ From e7b3d41c330b4d65c5433a7120a74139519fb14f Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 12 Aug 2013 12:04:05 -0700 Subject: [PATCH 02/26] make PacketReceiver derive from GenericThread --- interface/src/Application.cpp | 2 +- libraries/shared/src/GenericThread.cpp | 62 +++++++++++++++++++++++++ libraries/shared/src/GenericThread.h | 41 ++++++++++++++++ libraries/shared/src/PacketReceiver.cpp | 58 +++-------------------- libraries/shared/src/PacketReceiver.h | 19 ++------ 5 files changed, 114 insertions(+), 68 deletions(-) create mode 100644 libraries/shared/src/GenericThread.cpp create mode 100644 libraries/shared/src/GenericThread.h diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 46b2a2243c..0c19d3f5c6 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2577,7 +2577,7 @@ void Application::update(float deltaTime) { // parse voxel packets if (!_enableProcessVoxelsThread) { - _voxelReceiver.threadRoutine(); + _voxelReceiver.process(); } //loop through all the other avatars and simulate them... diff --git a/libraries/shared/src/GenericThread.cpp b/libraries/shared/src/GenericThread.cpp new file mode 100644 index 0000000000..d75e16627c --- /dev/null +++ b/libraries/shared/src/GenericThread.cpp @@ -0,0 +1,62 @@ +// +// GenericThread.cpp +// shared +// +// Created by Brad Hefta-Gaub on 8/12/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// +// Generic Threaded or non-threaded processing class +// + +#include "GenericThread.h" + +GenericThread::GenericThread() : + _stopThread(false), + _isThreaded(false) // assume non-threaded, must call initialize() +{ +} + +GenericThread::~GenericThread() { + terminate(); +} + +void GenericThread::initialize(bool isThreaded) { + _isThreaded = isThreaded; + if (_isThreaded) { + pthread_create(&_thread, NULL, GenericThreadEntry, this); + } +} + +void GenericThread::terminate() { + if (_isThreaded) { + _stopThread = true; + pthread_join(_thread, NULL); + _isThreaded = false; + } +} + +void* GenericThread::threadRoutine() { + while (!_stopThread) { + + // override this function to do whatever your class actually does, return false to exit thread early + if (!process()) { + break; + } + + // In non-threaded mode, this will break each time you call it so it's the + // callers responsibility to continuously call this method + if (!_isThreaded) { + break; + } + } + + if (_isThreaded) { + pthread_exit(0); + } + return NULL; +} + +extern "C" void* GenericThreadEntry(void* arg) { + GenericThread* genericThread = (GenericThread*)arg; + return genericThread->threadRoutine(); +} \ No newline at end of file diff --git a/libraries/shared/src/GenericThread.h b/libraries/shared/src/GenericThread.h new file mode 100644 index 0000000000..e0c372fe00 --- /dev/null +++ b/libraries/shared/src/GenericThread.h @@ -0,0 +1,41 @@ +// +// GenericThread.h +// shared +// +// Created by Brad Hefta-Gaub on 8/12/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// +// Generic Threaded or non-threaded processing class. +// + +#ifndef __shared__GenericThread__ +#define __shared__GenericThread__ + +#include + +class GenericThread { +public: + GenericThread(); + virtual ~GenericThread(); + + // Call to start the thread + void initialize(bool isThreaded); + + // 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 + void terminate(); + + // If you're running in non-threaded mode, you must call this regularly + void* threadRoutine(); + +private: + bool _stopThread; + bool _isThreaded; + pthread_t _thread; +}; + +extern "C" void* GenericThreadEntry(void* arg); + +#endif // __shared__GenericThread__ diff --git a/libraries/shared/src/PacketReceiver.cpp b/libraries/shared/src/PacketReceiver.cpp index 9bf4764ebb..39542eb21e 100644 --- a/libraries/shared/src/PacketReceiver.cpp +++ b/libraries/shared/src/PacketReceiver.cpp @@ -10,61 +10,15 @@ #include "PacketReceiver.h" -PacketReceiver::PacketReceiver() : - _stopThread(false), - _isThreaded(false) // assume non-threaded, must call initialize() -{ -} - -PacketReceiver::~PacketReceiver() { - terminate(); -} - -void PacketReceiver::initialize(bool isThreaded) { - _isThreaded = isThreaded; - if (_isThreaded) { - pthread_create(&_thread, NULL, PacketReceiverThreadEntry, this); - } -} - -void PacketReceiver::terminate() { - if (_isThreaded) { - _stopThread = true; - pthread_join(_thread, NULL); - _isThreaded = false; - } -} - void PacketReceiver::queuePacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { _packets.push_back(NetworkPacket(senderAddress, packetData, packetLength)); } -void PacketReceiver::processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { - // Default implementation does nothing... packet is discarded -} - -void* PacketReceiver::threadRoutine() { - while (!_stopThread) { - while (_packets.size() > 0) { - NetworkPacket& packet = _packets.front(); - processPacket(packet.getSenderAddress(), packet.getData(), packet.getLength()); - _packets.erase(_packets.begin()); - } - - // In non-threaded mode, this will break each time you call it so it's the - // callers responsibility to continuously call this method - if (!_isThreaded) { - break; - } +bool PacketReceiver::process() { + while (_packets.size() > 0) { + NetworkPacket& packet = _packets.front(); + processPacket(packet.getSenderAddress(), packet.getData(), packet.getLength()); + _packets.erase(_packets.begin()); } - - if (_isThreaded) { - pthread_exit(0); - } - return NULL; + return true; // keep running till they terminate us } - -extern "C" void* PacketReceiverThreadEntry(void* arg) { - PacketReceiver* packetReceiver = (PacketReceiver*)arg; - return packetReceiver->threadRoutine(); -} \ No newline at end of file diff --git a/libraries/shared/src/PacketReceiver.h b/libraries/shared/src/PacketReceiver.h index f236b362e6..c4dabb4906 100644 --- a/libraries/shared/src/PacketReceiver.h +++ b/libraries/shared/src/PacketReceiver.h @@ -11,32 +11,21 @@ #ifndef __shared__PacketReceiver__ #define __shared__PacketReceiver__ +#include "GenericThread.h" #include "NetworkPacket.h" -class PacketReceiver { +class PacketReceiver : public GenericThread { public: - PacketReceiver(); - virtual ~PacketReceiver(); // 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); + virtual void processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) = 0; - // Call to start the thread - void initialize(bool isThreaded); - - // Call when you're ready to stop the thread - void terminate(); - - // If you're running in non-threaded mode, you must call this regularly - void* threadRoutine(); + virtual bool process(); private: - bool _stopThread; - bool _isThreaded; - pthread_t _thread; std::vector _packets; }; From 4ea0de16375ed7a2db71be59fa2636f8a7a18615 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 12 Aug 2013 13:39:01 -0700 Subject: [PATCH 03/26] deleting old dead code --- interface/src/Application.cpp | 89 ++++------------------------------- interface/src/Application.h | 7 --- 2 files changed, 9 insertions(+), 87 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 0c19d3f5c6..40eec6c471 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -205,8 +205,6 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : _justEditedVoxel(false), _isLookingAtOtherAvatar(false), _lookatIndicatorScale(1.0f), - _paintOn(false), - _dominantColor(0), _perfStatsOn(false), _chatEntryOn(false), _oculusTextureID(0), @@ -540,27 +538,20 @@ void Application::controlledBroadcastToNodes(unsigned char* broadcastData, size_ // Feed number of bytes to corresponding channel of the bandwidth meter, if any (done otherwise) BandwidthMeter::ChannelIndex channel; switch (nodeTypes[i]) { - case NODE_TYPE_AGENT: - case NODE_TYPE_AVATAR_MIXER: - channel = BandwidthMeter::AVATARS; - break; - case NODE_TYPE_VOXEL_SERVER: - channel = BandwidthMeter::VOXELS; - break; - default: - continue; + case NODE_TYPE_AGENT: + case NODE_TYPE_AVATAR_MIXER: + channel = BandwidthMeter::AVATARS; + break; + case NODE_TYPE_VOXEL_SERVER: + channel = BandwidthMeter::VOXELS; + break; + default: + continue; } self->_bandwidthMeter.outputStream(channel).updateValue(nReceivingNodes * dataBytes); } } -void Application::sendVoxelServerAddScene() { - char message[100]; - sprintf(message,"%c%s",'Z',"add scene"); - int messageSize = strlen(message) + 1; - controlledBroadcastToNodes((unsigned char*)message, messageSize, & NODE_TYPE_VOXEL_SERVER, 1); -} - void Application::keyPressEvent(QKeyEvent* event) { if (activeWindow() == _window) { if (_chatEntryOn) { @@ -626,19 +617,6 @@ void Application::keyPressEvent(QKeyEvent* event) { _viewFrustumOffsetUp += 0.05; break; - case Qt::Key_Ampersand: - _paintOn = !_paintOn; - setupPaintingVoxel(); - break; - - case Qt::Key_AsciiCircum: - shiftPaintingColor(); - break; - - case Qt::Key_Percent: - sendVoxelServerAddScene(); - break; - case Qt::Key_Semicolon: _audio.ping(); break; @@ -2761,26 +2739,6 @@ void Application::updateAvatar(float deltaTime) { sendAvatarVoxelURLMessage(_myAvatar.getVoxels()->getVoxelURL()); } } - - // If I'm in paint mode, send a voxel out to VOXEL server nodes. - if (_paintOn) { - - glm::vec3 avatarPos = _myAvatar.getPosition(); - - // For some reason, we don't want to flip X and Z here. - _paintingVoxel.x = avatarPos.x / 10.0; - _paintingVoxel.y = avatarPos.y / 10.0; - _paintingVoxel.z = avatarPos.z / 10.0; - - if (_paintingVoxel.x >= 0.0 && _paintingVoxel.x <= 1.0 && - _paintingVoxel.y >= 0.0 && _paintingVoxel.y <= 1.0 && - _paintingVoxel.z >= 0.0 && _paintingVoxel.z <= 1.0) { - - PACKET_TYPE message = (_destructiveAddVoxel->isChecked() ? - PACKET_TYPE_SET_VOXEL_DESTRUCTIVE : PACKET_TYPE_SET_VOXEL); - sendVoxelEditMessage(message, _paintingVoxel); - } - } } ///////////////////////////////////////////////////////////////////////////////////// @@ -3268,15 +3226,6 @@ void Application::displayOverlay() { sprintf(nodes, "Servers: %d, Avatars: %d\n", totalServers, totalAvatars); drawtext(_glWidget->width() - 150, 20, 0.10, 0, 1.0, 0, nodes, 1, 0, 0); - if (_paintOn) { - - char paintMessage[100]; - sprintf(paintMessage,"Painting (%.3f,%.3f,%.3f/%.3f/%d,%d,%d)", - _paintingVoxel.x, _paintingVoxel.y, _paintingVoxel.z, _paintingVoxel.s, - (unsigned int)_paintingVoxel.red, (unsigned int)_paintingVoxel.green, (unsigned int)_paintingVoxel.blue); - drawtext(_glWidget->width() - 350, 50, 0.10, 0, 1.0, 0, paintMessage, 1, 1, 0); - } - // render the webcam input frame _webcam.renderPreview(_glWidget->width(), _glWidget->height()); @@ -3734,26 +3683,6 @@ void Application::renderViewFrustum(ViewFrustum& viewFrustum) { } } -void Application::setupPaintingVoxel() { - glm::vec3 avatarPos = _myAvatar.getPosition(); - - _paintingVoxel.x = avatarPos.z/-10.0; // voxel space x is negative z head space - _paintingVoxel.y = avatarPos.y/-10.0; // voxel space y is negative y head space - _paintingVoxel.z = avatarPos.x/-10.0; // voxel space z is negative x head space - _paintingVoxel.s = 1.0/256; - - shiftPaintingColor(); -} - -void Application::shiftPaintingColor() { - // About the color of the paintbrush... first determine the dominant color - _dominantColor = (_dominantColor + 1) % 3; // 0=red,1=green,2=blue - _paintingVoxel.red = (_dominantColor == 0) ? randIntInRange(200, 255) : randIntInRange(40, 100); - _paintingVoxel.green = (_dominantColor == 1) ? randIntInRange(200, 255) : randIntInRange(40, 100); - _paintingVoxel.blue = (_dominantColor == 2) ? randIntInRange(200, 255) : randIntInRange(40, 100); -} - - void Application::injectVoxelAddedSoundEffect() { AudioInjector* voxelInjector = AudioInjectionManager::injectorWithCapacity(11025); diff --git a/interface/src/Application.h b/interface/src/Application.h index 0a62fdd665..66a40e80ca 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -218,7 +218,6 @@ private: static void controlledBroadcastToNodes(unsigned char* broadcastData, size_t dataBytes, const char* nodeTypes, int numNodeTypes); - static void sendVoxelServerAddScene(); static bool sendVoxelsOperation(VoxelNode* node, void* extraData); static void sendVoxelEditMessage(PACKET_TYPE type, VoxelDetail& detail); static void sendAvatarVoxelURLMessage(const QUrl& url); @@ -249,8 +248,6 @@ private: void checkBandwidthMeterClick(); - void setupPaintingVoxel(); - void shiftPaintingColor(); bool maybeEditVoxelUnderCursor(); void deleteVoxelUnderCursor(); void eyedropperVoxelUnderCursor(); @@ -423,10 +420,6 @@ private: glm::vec3 _lookatOtherPosition; float _lookatIndicatorScale; - bool _paintOn; // Whether to paint voxels as you fly around - unsigned char _dominantColor; // The dominant color of the voxel we're painting - VoxelDetail _paintingVoxel; // The voxel we're painting if we're painting - bool _perfStatsOn; // Do we want to display perfStats? ChatEntry _chatEntry; // chat entry field From ce0c868c89e713f977d25dadc15973cd09000f6e Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 12 Aug 2013 13:40:21 -0700 Subject: [PATCH 04/26] cleanup naming --- libraries/shared/src/NetworkPacket.cpp | 6 +++--- libraries/shared/src/NetworkPacket.h | 11 +++++------ libraries/shared/src/NodeList.cpp | 2 +- libraries/shared/src/PacketReceiver.cpp | 6 +++--- libraries/shared/src/PacketReceiver.h | 2 -- 5 files changed, 12 insertions(+), 15 deletions(-) diff --git a/libraries/shared/src/NetworkPacket.cpp b/libraries/shared/src/NetworkPacket.cpp index 99dc00e9e2..a3d6e5558d 100644 --- a/libraries/shared/src/NetworkPacket.cpp +++ b/libraries/shared/src/NetworkPacket.cpp @@ -17,13 +17,13 @@ NetworkPacket::NetworkPacket() : _packetLength(0) { } NetworkPacket::NetworkPacket(const NetworkPacket& packet) { - memcpy(&_senderAddress, &packet.getSenderAddress(), sizeof(_senderAddress)); + memcpy(&_address, &packet.getAddress(), sizeof(_address)); _packetLength = packet.getLength(); memcpy(&_packetData[0], packet.getData(), _packetLength); } -NetworkPacket::NetworkPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { - memcpy(&_senderAddress, &senderAddress, sizeof(_senderAddress)); +NetworkPacket::NetworkPacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength) { + memcpy(&_address, &address, sizeof(_address)); _packetLength = packetLength; memcpy(&_packetData[0], packetData, packetLength); }; diff --git a/libraries/shared/src/NetworkPacket.h b/libraries/shared/src/NetworkPacket.h index 9f35004443..4c7f2e4466 100644 --- a/libraries/shared/src/NetworkPacket.h +++ b/libraries/shared/src/NetworkPacket.h @@ -19,20 +19,19 @@ class NetworkPacket { public: - NetworkPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); + NetworkPacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength); NetworkPacket(const NetworkPacket& packet); NetworkPacket(); - //~NetworkPacket(); - sockaddr& getSenderAddress() { return _senderAddress; }; + sockaddr& getAddress() { return _address; }; ssize_t getLength() const { return _packetLength; }; unsigned char* getData() { return &_packetData[0]; }; - const sockaddr& getSenderAddress() const { return _senderAddress; }; - const unsigned char* getData() const { return &_packetData[0]; }; + const sockaddr& getAddress() const { return _address; }; + const unsigned char* getData() const { return &_packetData[0]; }; private: - sockaddr _senderAddress; + sockaddr _address; ssize_t _packetLength; unsigned char _packetData[MAX_PACKET_SIZE]; }; diff --git a/libraries/shared/src/NodeList.cpp b/libraries/shared/src/NodeList.cpp index 6c2647efd5..23b39df205 100644 --- a/libraries/shared/src/NodeList.cpp +++ b/libraries/shared/src/NodeList.cpp @@ -439,7 +439,7 @@ void NodeList::addNodeToList(Node* newNode) { notifyHooksOfAddedNode(newNode); } -unsigned NodeList::broadcastToNodes(unsigned char *broadcastData, size_t dataBytes, const char* nodeTypes, int numNodeTypes) { +unsigned NodeList::broadcastToNodes(unsigned char* broadcastData, size_t dataBytes, const char* nodeTypes, int numNodeTypes) { unsigned n = 0; for(NodeList::iterator node = begin(); node != end(); node++) { // only send to the NodeTypes we are asked to send to. diff --git a/libraries/shared/src/PacketReceiver.cpp b/libraries/shared/src/PacketReceiver.cpp index 39542eb21e..0f7d9ab1ab 100644 --- a/libraries/shared/src/PacketReceiver.cpp +++ b/libraries/shared/src/PacketReceiver.cpp @@ -10,14 +10,14 @@ #include "PacketReceiver.h" -void PacketReceiver::queuePacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { - _packets.push_back(NetworkPacket(senderAddress, packetData, packetLength)); +void PacketReceiver::queuePacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength) { + _packets.push_back(NetworkPacket(address, packetData, packetLength)); } bool PacketReceiver::process() { while (_packets.size() > 0) { NetworkPacket& packet = _packets.front(); - processPacket(packet.getSenderAddress(), packet.getData(), packet.getLength()); + processPacket(packet.getAddress(), packet.getData(), packet.getLength()); _packets.erase(_packets.begin()); } return true; // keep running till they terminate us diff --git a/libraries/shared/src/PacketReceiver.h b/libraries/shared/src/PacketReceiver.h index c4dabb4906..27ab68f5de 100644 --- a/libraries/shared/src/PacketReceiver.h +++ b/libraries/shared/src/PacketReceiver.h @@ -29,6 +29,4 @@ private: std::vector _packets; }; -extern "C" void* PacketReceiverThreadEntry(void* arg); - #endif // __shared__PacketReceiver__ From 7d2c69f530618331780bbd7c52af495f6bd00f9a Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 12 Aug 2013 16:55:58 -0700 Subject: [PATCH 05/26] latest work on threaded sending --- interface/src/Application.cpp | 112 +++--------------------- interface/src/Application.h | 6 +- interface/src/VoxelEditPacketSender.cpp | 84 ++++++++++++++++++ interface/src/VoxelEditPacketSender.h | 37 ++++++++ libraries/shared/src/GenericThread.cpp | 2 + libraries/shared/src/GenericThread.h | 6 ++ libraries/shared/src/NetworkPacket.cpp | 46 ++++++++-- libraries/shared/src/NetworkPacket.h | 13 ++- libraries/shared/src/PacketHeaders.h | 1 + libraries/shared/src/PacketReceiver.cpp | 8 +- libraries/shared/src/PacketReceiver.h | 2 +- libraries/shared/src/PacketSender.cpp | 58 ++++++++++++ libraries/shared/src/PacketSender.h | 32 +++++++ 13 files changed, 295 insertions(+), 112 deletions(-) create mode 100644 interface/src/VoxelEditPacketSender.cpp create mode 100644 interface/src/VoxelEditPacketSender.h create mode 100644 libraries/shared/src/PacketSender.cpp create mode 100644 libraries/shared/src/PacketSender.h diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 40eec6c471..dfe548afd4 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -215,6 +215,7 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : #endif _stopNetworkReceiveThread(false), _voxelReceiver(this), + _voxelEditSender(this), _packetCount(0), _packetsPerSecond(0), _bytesPerSecond(0), @@ -367,6 +368,7 @@ void Application::initializeGL() { // create thread for parsing of voxel data independent of the main network and rendering threads _voxelReceiver.initialize(_enableProcessVoxelsThread); + _voxelEditSender.initialize(_enableProcessVoxelsThread); if (_enableProcessVoxelsThread) { qDebug("Voxel parsing thread created.\n"); } @@ -1157,6 +1159,7 @@ void Application::terminate() { } _voxelReceiver.terminate(); + _voxelEditSender.terminate(); } void Application::sendAvatarVoxelURLMessage(const QUrl& url) { @@ -1506,16 +1509,6 @@ void Application::updateVoxelModeActions() { } } -void Application::sendVoxelEditMessage(PACKET_TYPE type, VoxelDetail& detail) { - unsigned char* bufferOut; - int sizeOut; - - if (createVoxelEditMessage(type, 0, 1, &detail, bufferOut, sizeOut)){ - Application::controlledBroadcastToNodes(bufferOut, sizeOut, & NODE_TYPE_VOXEL_SERVER, 1); - delete[] bufferOut; - } -} - const glm::vec3 Application::getMouseVoxelWorldCoordinates(const VoxelDetail _mouseVoxel) { return glm::vec3((_mouseVoxel.x + _mouseVoxel.s / 2.f) * TREE_SCALE, (_mouseVoxel.y + _mouseVoxel.s / 2.f) * TREE_SCALE, @@ -1553,12 +1546,8 @@ void Application::chooseVoxelPaintColor() { const int MAXIMUM_EDIT_VOXEL_MESSAGE_SIZE = 1500; struct SendVoxelsOperationArgs { - unsigned char* newBaseOctCode; - unsigned char messageBuffer[MAXIMUM_EDIT_VOXEL_MESSAGE_SIZE]; - int bufferInUse; - uint64_t lastSendTime; - int packetsSent; - uint64_t bytesSent; + unsigned char* newBaseOctCode; + Application* app; }; bool Application::sendVoxelsOperation(VoxelNode* node, void* extraData) { @@ -1590,37 +1579,9 @@ bool Application::sendVoxelsOperation(VoxelNode* node, void* extraData) { codeColorBuffer[bytesInCode + GREEN_INDEX] = node->getColor()[GREEN_INDEX]; codeColorBuffer[bytesInCode + BLUE_INDEX ] = node->getColor()[BLUE_INDEX ]; - // if we have room don't have room in the buffer, then send the previously generated message first - if (args->bufferInUse + codeAndColorLength > MAXIMUM_EDIT_VOXEL_MESSAGE_SIZE) { - - args->packetsSent++; - args->bytesSent += args->bufferInUse; - - controlledBroadcastToNodes(args->messageBuffer, args->bufferInUse, & NODE_TYPE_VOXEL_SERVER, 1); - args->bufferInUse = numBytesForPacketHeader((unsigned char*) &PACKET_TYPE_SET_VOXEL_DESTRUCTIVE) - + sizeof(unsigned short int); // reset - - uint64_t now = usecTimestampNow(); - // dynamically sleep until we need to fire off the next set of voxels - uint64_t elapsed = now - args->lastSendTime; - int usecToSleep = CLIENT_TO_SERVER_VOXEL_SEND_INTERVAL_USECS - elapsed; - if (usecToSleep > 0) { - //qDebug("sendVoxelsOperation: packet: %d bytes:%lld elapsed %lld usecs, sleeping for %d usecs!\n", - // args->packetsSent, (long long int)args->bytesSent, (long long int)elapsed, usecToSleep); - - Application::getInstance()->timer(); - - usleep(usecToSleep); - } else { - //qDebug("sendVoxelsOperation: packet: %d bytes:%lld elapsed %lld usecs, no need to sleep!\n", - // args->packetsSent, (long long int)args->bytesSent, (long long int)elapsed); - } - args->lastSendTime = now; - } + args->app->_voxelEditSender.queueVoxelEditMessage(PACKET_TYPE_SET_VOXEL_DESTRUCTIVE, codeColorBuffer, codeAndColorLength); - // copy this node's code color details into our buffer. - memcpy(&args->messageBuffer[args->bufferInUse], codeColorBuffer, codeAndColorLength); - args->bufferInUse += codeAndColorLength; + delete[] codeColorBuffer; } return true; // keep going } @@ -1802,15 +1763,7 @@ void Application::importVoxels() { // the server as an set voxel message, this will also rebase the voxels to the new location unsigned char* calculatedOctCode = NULL; SendVoxelsOperationArgs args; - args.lastSendTime = usecTimestampNow(); - args.packetsSent = 0; - args.bytesSent = 0; - - int numBytesPacketHeader = populateTypeAndVersion(args.messageBuffer, PACKET_TYPE_SET_VOXEL_DESTRUCTIVE); - - unsigned short int* sequenceAt = (unsigned short int*)&args.messageBuffer[numBytesPacketHeader]; - *sequenceAt = 0; - args.bufferInUse = numBytesPacketHeader + sizeof(unsigned short int); // set to command + sequence + args.app = this; // we only need the selected voxel to get the newBaseOctCode, which we can actually calculate from the // voxel size/position details. @@ -1824,31 +1777,8 @@ void Application::importVoxels() { // send the insert/paste of these voxels importVoxels.recurseTreeWithOperation(sendVoxelsOperation, &args); + _voxelEditSender.flushQueue(); - // If we have voxels left in the packet, then send the packet - if (args.bufferInUse > (numBytesPacketHeader + sizeof(unsigned short int))) { - controlledBroadcastToNodes(args.messageBuffer, args.bufferInUse, & NODE_TYPE_VOXEL_SERVER, 1); - - - args.packetsSent++; - args.bytesSent += args.bufferInUse; - - uint64_t now = usecTimestampNow(); - // dynamically sleep until we need to fire off the next set of voxels - uint64_t elapsed = now - args.lastSendTime; - int usecToSleep = CLIENT_TO_SERVER_VOXEL_SEND_INTERVAL_USECS - elapsed; - - if (usecToSleep > 0) { - //qDebug("after sendVoxelsOperation: packet: %d bytes:%lld elapsed %lld usecs, sleeping for %d usecs!\n", - // args.packetsSent, (long long int)args.bytesSent, (long long int)elapsed, usecToSleep); - usleep(usecToSleep); - } else { - //qDebug("after sendVoxelsOperation: packet: %d bytes:%lld elapsed %lld usecs, no need to sleep!\n", - // args.packetsSent, (long long int)args.bytesSent, (long long int)elapsed); - } - args.lastSendTime = now; - } - if (calculatedOctCode) { delete[] calculatedOctCode; } @@ -1882,15 +1812,7 @@ void Application::pasteVoxels() { // Recurse the clipboard tree, where everything is root relative, and send all the colored voxels to // the server as an set voxel message, this will also rebase the voxels to the new location SendVoxelsOperationArgs args; - args.lastSendTime = usecTimestampNow(); - args.packetsSent = 0; - args.bytesSent = 0; - - int numBytesPacketHeader = populateTypeAndVersion(args.messageBuffer, PACKET_TYPE_SET_VOXEL_DESTRUCTIVE); - - unsigned short int* sequenceAt = (unsigned short int*)&args.messageBuffer[numBytesPacketHeader]; - *sequenceAt = 0; - args.bufferInUse = numBytesPacketHeader + sizeof(unsigned short int); // set to command + sequence + args.app = this; // we only need the selected voxel to get the newBaseOctCode, which we can actually calculate from the // voxel size/position details. If we don't have an actual selectedNode then use the mouseVoxel to create a @@ -1902,14 +1824,7 @@ void Application::pasteVoxels() { } _clipboardTree.recurseTreeWithOperation(sendVoxelsOperation, &args); - - // If we have voxels left in the packet, then send the packet - if (args.bufferInUse > (numBytesPacketHeader + sizeof(unsigned short int))) { - controlledBroadcastToNodes(args.messageBuffer, args.bufferInUse, & NODE_TYPE_VOXEL_SERVER, 1); - qDebug("sending packet: %d\n", ++args.packetsSent); - args.bytesSent += args.bufferInUse; - qDebug("total bytes sent: %lld\n", (long long int)args.bytesSent); - } + _voxelEditSender.flushQueue(); if (calculatedOctCode) { delete[] calculatedOctCode; @@ -2556,6 +2471,7 @@ void Application::update(float deltaTime) { // parse voxel packets if (!_enableProcessVoxelsThread) { _voxelReceiver.process(); + _voxelEditSender.process(); } //loop through all the other avatars and simulate them... @@ -3744,7 +3660,7 @@ bool Application::maybeEditVoxelUnderCursor() { if (_mouseVoxel.s != 0) { PACKET_TYPE message = (_destructiveAddVoxel->isChecked() ? PACKET_TYPE_SET_VOXEL_DESTRUCTIVE : PACKET_TYPE_SET_VOXEL); - sendVoxelEditMessage(message, _mouseVoxel); + _voxelEditSender.sendVoxelEditMessage(message, _mouseVoxel); // create the voxel locally so it appears immediately _voxels.createVoxel(_mouseVoxel.x, _mouseVoxel.y, _mouseVoxel.z, _mouseVoxel.s, @@ -3790,7 +3706,7 @@ bool Application::maybeEditVoxelUnderCursor() { void Application::deleteVoxelUnderCursor() { if (_mouseVoxel.s != 0) { // sending delete to the server is sufficient, server will send new version so we see updates soon enough - sendVoxelEditMessage(PACKET_TYPE_ERASE_VOXEL, _mouseVoxel); + _voxelEditSender.sendVoxelEditMessage(PACKET_TYPE_ERASE_VOXEL, _mouseVoxel); AudioInjector* voxelInjector = AudioInjectionManager::injectorWithCapacity(5000); if (voxelInjector) { diff --git a/interface/src/Application.h b/interface/src/Application.h index 66a40e80ca..89b8f9d28b 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -38,6 +38,7 @@ #include "ToolsPalette.h" #include "ViewFrustum.h" #include "VoxelFade.h" +#include "VoxelEditPacketSender.h" #include "VoxelPacketReceiver.h" #include "VoxelSystem.h" #include "Webcam.h" @@ -74,6 +75,7 @@ class Application : public QApplication, public NodeListHook { Q_OBJECT friend class VoxelPacketReceiver; + friend class VoxelEditPacketSender; public: static Application* getInstance() { return static_cast(QCoreApplication::instance()); } @@ -219,7 +221,6 @@ private: const char* nodeTypes, int numNodeTypes); static bool sendVoxelsOperation(VoxelNode* node, void* extraData); - static void sendVoxelEditMessage(PACKET_TYPE type, VoxelDetail& detail); static void sendAvatarVoxelURLMessage(const QUrl& url); static void processAvatarVoxelURLMessage(unsigned char* packetData, size_t dataBytes); static void processAvatarFaceVideoMessage(unsigned char* packetData, size_t dataBytes); @@ -449,7 +450,8 @@ private: bool _stopNetworkReceiveThread; bool _enableProcessVoxelsThread; - VoxelPacketReceiver _voxelReceiver; + VoxelPacketReceiver _voxelReceiver; + VoxelEditPacketSender _voxelEditSender; unsigned char _incomingPacket[MAX_PACKET_SIZE]; diff --git a/interface/src/VoxelEditPacketSender.cpp b/interface/src/VoxelEditPacketSender.cpp new file mode 100644 index 0000000000..0e1a2b3343 --- /dev/null +++ b/interface/src/VoxelEditPacketSender.cpp @@ -0,0 +1,84 @@ +// +// VoxelEditPacketSender.cpp +// interface +// +// Created by Brad Hefta-Gaub on 8/12/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// +// Threaded or non-threaded voxel packet Sender for the Application +// + +#include + +#include "Application.h" +#include "VoxelEditPacketSender.h" + +VoxelEditPacketSender::VoxelEditPacketSender(Application* app) : + _app(app), + _currentType(PACKET_TYPE_UNKNOWN), + _currentSize(0) +{ +} + +void VoxelEditPacketSender::sendVoxelEditMessage(PACKET_TYPE type, VoxelDetail& detail) { + + // if the app has Voxels disabled, we don't do any of this... + if (!_app->_renderVoxels->isChecked()) { + return; // bail early + } + + unsigned char* bufferOut; + int sizeOut; + int totalBytesSent = 0; + + if (createVoxelEditMessage(type, 0, 1, &detail, bufferOut, sizeOut)){ + actuallySendMessage(bufferOut, sizeOut); + delete[] bufferOut; + } + + // Tell the application's bandwidth meters about what we've sent + _app->_bandwidthMeter.outputStream(BandwidthMeter::VOXELS).updateValue(totalBytesSent); +} + +void VoxelEditPacketSender::actuallySendMessage(unsigned char* bufferOut, ssize_t sizeOut) { + qDebug("VoxelEditPacketSender::actuallySendMessage() sizeOut=%lu\n", sizeOut); + NodeList* nodeList = NodeList::getInstance(); + for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) { + // only send to the NodeTypes we are asked to send to. + if (node->getActiveSocket() != NULL && node->getType() == NODE_TYPE_VOXEL_SERVER) { + // we know which socket is good for this node, send there + sockaddr* nodeAddress = node->getActiveSocket(); + queuePacket(*nodeAddress, bufferOut, sizeOut); + } + } +} + +void VoxelEditPacketSender::queueVoxelEditMessage(PACKET_TYPE type, unsigned char* codeColorBuffer, ssize_t length) { + // If we're switching type, then we send the last one and start over + if ((type != _currentType && _currentSize > 0) || (_currentSize + length >= MAX_PACKET_SIZE)) { + flushQueue(); + initializePacket(type); + } + + // If the buffer is empty and not correctly initialized for our type... + if (type != _currentType && _currentSize == 0) { + initializePacket(type); + } + + memcpy(&_currentBuffer[_currentSize], codeColorBuffer, length); + _currentSize += length; +} + +void VoxelEditPacketSender::flushQueue() { + actuallySendMessage(&_currentBuffer[0], _currentSize); + _currentSize = 0; + _currentType = PACKET_TYPE_UNKNOWN; +} + +void VoxelEditPacketSender::initializePacket(PACKET_TYPE type) { + _currentSize = populateTypeAndVersion(&_currentBuffer[0], type); + unsigned short int* sequenceAt = (unsigned short int*)&_currentBuffer[_currentSize]; + *sequenceAt = 0; + _currentSize += sizeof(unsigned short int); // set to command + sequence + _currentType = type; +} diff --git a/interface/src/VoxelEditPacketSender.h b/interface/src/VoxelEditPacketSender.h new file mode 100644 index 0000000000..5bcc73e7a5 --- /dev/null +++ b/interface/src/VoxelEditPacketSender.h @@ -0,0 +1,37 @@ +// +// VoxelEditPacketSender.h +// interface +// +// Created by Brad Hefta-Gaub on 8/12/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// +// Voxel Packet Sender +// + +#ifndef __shared__VoxelEditPacketSender__ +#define __shared__VoxelEditPacketSender__ + +#include +#include // for VoxelDetail + +class Application; + +class VoxelEditPacketSender : public PacketSender { +public: + VoxelEditPacketSender(Application* app); + + // Some ways you can send voxel edit messages... + void sendVoxelEditMessage(PACKET_TYPE type, VoxelDetail& detail); // sends it right away + void queueVoxelEditMessage(PACKET_TYPE type, unsigned char* codeColorBuffer, ssize_t length); // queues it into a multi-command packet + void flushQueue(); // flushes any queued packets + +private: + void actuallySendMessage(unsigned char* bufferOut, ssize_t sizeOut); + void initializePacket(PACKET_TYPE type); + + Application* _app; + PACKET_TYPE _currentType ; + unsigned char _currentBuffer[MAX_PACKET_SIZE]; + ssize_t _currentSize; +}; +#endif // __shared__VoxelEditPacketSender__ diff --git a/libraries/shared/src/GenericThread.cpp b/libraries/shared/src/GenericThread.cpp index d75e16627c..9f43473278 100644 --- a/libraries/shared/src/GenericThread.cpp +++ b/libraries/shared/src/GenericThread.cpp @@ -14,10 +14,12 @@ GenericThread::GenericThread() : _stopThread(false), _isThreaded(false) // assume non-threaded, must call initialize() { + pthread_mutex_init(&_mutex, 0); } GenericThread::~GenericThread() { terminate(); + pthread_mutex_destroy(&_mutex); } void GenericThread::initialize(bool isThreaded) { diff --git a/libraries/shared/src/GenericThread.h b/libraries/shared/src/GenericThread.h index e0c372fe00..c41e5b29cb 100644 --- a/libraries/shared/src/GenericThread.h +++ b/libraries/shared/src/GenericThread.h @@ -29,8 +29,14 @@ public: // If you're running in non-threaded mode, you must call this regularly void* threadRoutine(); + +protected: + void lock() { pthread_mutex_lock(&_mutex); } + void unlock() { pthread_mutex_unlock(&_mutex); } private: + pthread_mutex_t _mutex; + bool _stopThread; bool _isThreaded; pthread_t _thread; diff --git a/libraries/shared/src/NetworkPacket.cpp b/libraries/shared/src/NetworkPacket.cpp index a3d6e5558d..803972d502 100644 --- a/libraries/shared/src/NetworkPacket.cpp +++ b/libraries/shared/src/NetworkPacket.cpp @@ -10,20 +10,52 @@ #include #include +#include #include "NetworkPacket.h" -NetworkPacket::NetworkPacket() : _packetLength(0) { +NetworkPacket::NetworkPacket() { + _packetLength = 0; +} + +NetworkPacket::~NetworkPacket() { + // nothing to do +} + +void NetworkPacket::copyContents(const sockaddr& address, const unsigned char* packetData, ssize_t packetLength) { + _packetLength = 0; + if (packetLength >=0 && packetLength <= MAX_PACKET_SIZE) { + memcpy(&_address, &address, sizeof(_address)); + _packetLength = packetLength; + memcpy(&_packetData[0], packetData, packetLength); + } else { + qDebug(">>> NetworkPacket::copyContents() unexpected length=%lu\n",packetLength); + } } NetworkPacket::NetworkPacket(const NetworkPacket& packet) { - memcpy(&_address, &packet.getAddress(), sizeof(_address)); - _packetLength = packet.getLength(); - memcpy(&_packetData[0], packet.getData(), _packetLength); + copyContents(packet.getAddress(), packet.getData(), packet.getLength()); } +// move?? // same as copy, but other packet won't be used further +NetworkPacket::NetworkPacket(NetworkPacket && packet) { + copyContents(packet.getAddress(), packet.getData(), packet.getLength()); +} + + NetworkPacket::NetworkPacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength) { - memcpy(&_address, &address, sizeof(_address)); - _packetLength = packetLength; - memcpy(&_packetData[0], packetData, packetLength); + copyContents(address, packetData, packetLength); }; + +// copy assignment +NetworkPacket& NetworkPacket::operator=(NetworkPacket const& other) { + copyContents(other.getAddress(), other.getData(), other.getLength()); + return *this; +} + +// move assignment +NetworkPacket& NetworkPacket::operator=(NetworkPacket&& other) { + _packetLength = 0; + copyContents(other.getAddress(), other.getData(), other.getLength()); + return *this; +} diff --git a/libraries/shared/src/NetworkPacket.h b/libraries/shared/src/NetworkPacket.h index 4c7f2e4466..d82a254591 100644 --- a/libraries/shared/src/NetworkPacket.h +++ b/libraries/shared/src/NetworkPacket.h @@ -19,10 +19,15 @@ class NetworkPacket { public: - NetworkPacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength); - NetworkPacket(const NetworkPacket& packet); NetworkPacket(); - + NetworkPacket(const NetworkPacket& packet); // copy constructor + NetworkPacket(NetworkPacket && packet); // move?? // same as copy, but other packet won't be used further + ~NetworkPacket(); // destructor + NetworkPacket& operator= (NetworkPacket const &other); // copy assignment + NetworkPacket& operator= (NetworkPacket&& other); // move assignment + + NetworkPacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength); + sockaddr& getAddress() { return _address; }; ssize_t getLength() const { return _packetLength; }; unsigned char* getData() { return &_packetData[0]; }; @@ -31,6 +36,8 @@ public: const unsigned char* getData() const { return &_packetData[0]; }; private: + void copyContents(const sockaddr& address, const unsigned char* packetData, ssize_t packetLength); + sockaddr _address; ssize_t _packetLength; unsigned char _packetData[MAX_PACKET_SIZE]; diff --git a/libraries/shared/src/PacketHeaders.h b/libraries/shared/src/PacketHeaders.h index 2b021db82c..3d49b6b066 100644 --- a/libraries/shared/src/PacketHeaders.h +++ b/libraries/shared/src/PacketHeaders.h @@ -13,6 +13,7 @@ #define hifi_PacketHeaders_h typedef char PACKET_TYPE; +const PACKET_TYPE PACKET_TYPE_UNKNOWN = 0; const PACKET_TYPE PACKET_TYPE_DOMAIN = 'D'; const PACKET_TYPE PACKET_TYPE_PING = 'P'; const PACKET_TYPE PACKET_TYPE_PING_REPLY = 'R'; diff --git a/libraries/shared/src/PacketReceiver.cpp b/libraries/shared/src/PacketReceiver.cpp index 0f7d9ab1ab..801243eb5c 100644 --- a/libraries/shared/src/PacketReceiver.cpp +++ b/libraries/shared/src/PacketReceiver.cpp @@ -11,14 +11,20 @@ #include "PacketReceiver.h" void PacketReceiver::queuePacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength) { - _packets.push_back(NetworkPacket(address, packetData, packetLength)); + NetworkPacket packet(address, packetData, packetLength); + lock(); + _packets.push_back(packet); + unlock(); } bool PacketReceiver::process() { while (_packets.size() > 0) { NetworkPacket& packet = _packets.front(); processPacket(packet.getAddress(), packet.getData(), packet.getLength()); + + lock(); _packets.erase(_packets.begin()); + unlock(); } return true; // keep running till they terminate us } diff --git a/libraries/shared/src/PacketReceiver.h b/libraries/shared/src/PacketReceiver.h index 27ab68f5de..7b7f792862 100644 --- a/libraries/shared/src/PacketReceiver.h +++ b/libraries/shared/src/PacketReceiver.h @@ -16,7 +16,6 @@ class PacketReceiver : public GenericThread { public: - // Call this when your network receive gets a packet void queuePacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); @@ -26,6 +25,7 @@ public: virtual bool process(); private: + std::vector _packets; }; diff --git a/libraries/shared/src/PacketSender.cpp b/libraries/shared/src/PacketSender.cpp new file mode 100644 index 0000000000..61b173c5ee --- /dev/null +++ b/libraries/shared/src/PacketSender.cpp @@ -0,0 +1,58 @@ +// +// PacketSender.cpp +// shared +// +// Created by Brad Hefta-Gaub on 8/12/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// +// Threaded or non-threaded packet sender. +// + +#include + +const uint64_t SEND_INTERVAL_USECS = 1000 * 5; // no more than 200pps... should be settable + +#include "NodeList.h" +#include "PacketSender.h" +#include "SharedUtil.h" + +PacketSender::PacketSender() { + _lastSendTime = usecTimestampNow(); +} + + +void PacketSender::queuePacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength) { + NetworkPacket packet(address, packetData, packetLength); + lock(); + _packets.push_back(packet); + unlock(); +} + +bool PacketSender::process() { + while (_packets.size() > 0) { + NetworkPacket& packet = _packets.front(); + + // send the packet through the NodeList... + UDPSocket* nodeSocket = NodeList::getInstance()->getNodeSocket(); + + qDebug("PacketSender::process()... nodeSocket->send() length=%lu\n", packet.getLength()); + + nodeSocket->send(&packet.getAddress(), packet.getData(), packet.getLength()); + + lock(); + _packets.erase(_packets.begin()); + unlock(); + + uint64_t now = usecTimestampNow(); + // dynamically sleep until we need to fire off the next set of voxels + uint64_t elapsed = now - _lastSendTime; + int usecToSleep = SEND_INTERVAL_USECS - elapsed; + _lastSendTime = now; + if (usecToSleep > 0) { + //qDebug("PacketSender::process()... sleeping for %d useconds\n", usecToSleep); + usleep(usecToSleep); + } + + } + return true; // keep running till they terminate us +} diff --git a/libraries/shared/src/PacketSender.h b/libraries/shared/src/PacketSender.h new file mode 100644 index 0000000000..72b61011cc --- /dev/null +++ b/libraries/shared/src/PacketSender.h @@ -0,0 +1,32 @@ +// +// PacketSender.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 sender. +// + +#ifndef __shared__PacketSender__ +#define __shared__PacketSender__ + +#include "GenericThread.h" +#include "NetworkPacket.h" + +class PacketSender : public GenericThread { +public: + + PacketSender(); + // Call this when you have a packet you'd like sent... + void queuePacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength); + + virtual bool process(); + +private: + std::vector _packets; + uint64_t _lastSendTime; + +}; + +#endif // __shared__PacketSender__ From 60dedee7391e5390f3f6dc16bcd7d8f9cd227a82 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 13 Aug 2013 11:08:43 -0700 Subject: [PATCH 06/26] make JurisdictionMap handle copy/move/assigment so that it will work in std::vector<> and std::map<>, switch application to have map of JurisdictionMap objects instead of just root codes --- interface/src/Application.cpp | 20 ++++---- interface/src/Application.h | 2 +- interface/src/VoxelEditPacketSender.cpp | 35 +++++++++++++- libraries/shared/src/OctalCode.h | 2 +- libraries/voxels/src/JurisdictionMap.cpp | 60 ++++++++++++++++++++++++ libraries/voxels/src/JurisdictionMap.h | 12 ++++- libraries/voxels/src/VoxelSceneStats.h | 5 +- 7 files changed, 120 insertions(+), 16 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index dfe548afd4..8ee1fd2b24 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3826,15 +3826,16 @@ void Application::nodeKilled(Node* node) { uint16_t nodeID = node->getNodeID(); // see if this is the first we've heard of this node... if (_voxelServerJurisdictions.find(nodeID) != _voxelServerJurisdictions.end()) { - VoxelPositionSize jurisditionDetails; - jurisditionDetails = _voxelServerJurisdictions[nodeID]; + unsigned char* rootCode = _voxelServerJurisdictions[nodeID].getRootOctalCode(); + VoxelPositionSize rootDetails; + voxelDetailsForCode(rootCode, rootDetails); printf("voxel server going away...... v[%f, %f, %f, %f]\n", - jurisditionDetails.x, jurisditionDetails.y, jurisditionDetails.z, jurisditionDetails.s); + rootDetails.x, rootDetails.y, rootDetails.z, rootDetails.s); // Add the jurisditionDetails object to the list of "fade outs" VoxelFade fade(VoxelFade::FADE_OUT, NODE_KILLED_RED, NODE_KILLED_GREEN, NODE_KILLED_BLUE); - fade.voxelDetails = jurisditionDetails; + fade.voxelDetails = rootDetails; const float slightly_smaller = 0.99; fade.voxelDetails.s = fade.voxelDetails.s * slightly_smaller; _voxelFades.push_back(fade); @@ -3855,23 +3856,24 @@ int Application::parseVoxelStats(unsigned char* messageData, ssize_t messageLeng if (voxelServer) { uint16_t nodeID = voxelServer->getNodeID(); - VoxelPositionSize jurisditionDetails; - voxelDetailsForCode(_voxelSceneStats.getJurisdictionRoot(), jurisditionDetails); + VoxelPositionSize rootDetails; + voxelDetailsForCode(_voxelSceneStats.getJurisdictionRoot(), rootDetails); // see if this is the first we've heard of this node... if (_voxelServerJurisdictions.find(nodeID) == _voxelServerJurisdictions.end()) { printf("stats from new voxel server... v[%f, %f, %f, %f]\n", - jurisditionDetails.x, jurisditionDetails.y, jurisditionDetails.z, jurisditionDetails.s); + rootDetails.x, rootDetails.y, rootDetails.z, rootDetails.s); // Add the jurisditionDetails object to the list of "fade outs" VoxelFade fade(VoxelFade::FADE_OUT, NODE_ADDED_RED, NODE_ADDED_GREEN, NODE_ADDED_BLUE); - fade.voxelDetails = jurisditionDetails; + fade.voxelDetails = rootDetails; const float slightly_smaller = 0.99; fade.voxelDetails.s = fade.voxelDetails.s * slightly_smaller; _voxelFades.push_back(fade); } // store jurisdiction details for later use - _voxelServerJurisdictions[nodeID] = jurisditionDetails; + _voxelServerJurisdictions[nodeID] = JurisdictionMap(_voxelSceneStats.getJurisdictionRoot(), + _voxelSceneStats.getJurisdictionEndNodes()); } return statsMessageLength; } diff --git a/interface/src/Application.h b/interface/src/Application.h index 89b8f9d28b..9ef808f603 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -471,7 +471,7 @@ private: VoxelSceneStats _voxelSceneStats; int parseVoxelStats(unsigned char* messageData, ssize_t messageLength, sockaddr senderAddress); - std::map _voxelServerJurisdictions; + std::map _voxelServerJurisdictions; std::vector _voxelFades; }; diff --git a/interface/src/VoxelEditPacketSender.cpp b/interface/src/VoxelEditPacketSender.cpp index 0e1a2b3343..64016bc592 100644 --- a/interface/src/VoxelEditPacketSender.cpp +++ b/interface/src/VoxelEditPacketSender.cpp @@ -44,9 +44,12 @@ void VoxelEditPacketSender::actuallySendMessage(unsigned char* bufferOut, ssize_ qDebug("VoxelEditPacketSender::actuallySendMessage() sizeOut=%lu\n", sizeOut); NodeList* nodeList = NodeList::getInstance(); for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) { - // only send to the NodeTypes we are asked to send to. + // only send to the NodeTypes that are NODE_TYPE_VOXEL_SERVER if (node->getActiveSocket() != NULL && node->getType() == NODE_TYPE_VOXEL_SERVER) { - // we know which socket is good for this node, send there + + // We want to filter out edit messages for voxel servers based on the server's Jurisdiction + // But we can't really do that with a packed message, since each edit message could be destined + // for a different voxel server... sockaddr* nodeAddress = node->getActiveSocket(); queuePacket(*nodeAddress, bufferOut, sizeOut); } @@ -54,6 +57,34 @@ void VoxelEditPacketSender::actuallySendMessage(unsigned char* bufferOut, ssize_ } void VoxelEditPacketSender::queueVoxelEditMessage(PACKET_TYPE type, unsigned char* codeColorBuffer, ssize_t length) { +/**** + // We want to filter out edit messages for voxel servers based on the server's Jurisdiction + // But we can't really do that with a packed message, since each edit message could be destined + // for a different voxel server... So we need to actually manage multiple queued packets... one + // for each voxel server + for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) { + // only send to the NodeTypes that are NODE_TYPE_VOXEL_SERVER + if (node->getActiveSocket() != NULL && node->getType() == NODE_TYPE_VOXEL_SERVER) { + + // we need to get the jurisdiction for this + // here we need to get the "pending packet" for this server + + // If we're switching type, then we send the last one and start over + if ((type != _currentType && _currentSize > 0) || (_currentSize + length >= MAX_PACKET_SIZE)) { + flushQueue(); + initializePacket(type); + } + + // If the buffer is empty and not correctly initialized for our type... + if (type != _currentType && _currentSize == 0) { + initializePacket(type); + } + + memcpy(&_currentBuffer[_currentSize], codeColorBuffer, length); + _currentSize += length; + } + } +****/ // If we're switching type, then we send the last one and start over if ((type != _currentType && _currentSize > 0) || (_currentSize + length >= MAX_PACKET_SIZE)) { flushQueue(); diff --git a/libraries/shared/src/OctalCode.h b/libraries/shared/src/OctalCode.h index 405b85164d..846b65203b 100644 --- a/libraries/shared/src/OctalCode.h +++ b/libraries/shared/src/OctalCode.h @@ -39,7 +39,7 @@ void copyFirstVertexForCode(unsigned char * octalCode, float* output); struct VoxelPositionSize { float x, y, z, s; }; -void voxelDetailsForCode(unsigned char * octalCode, VoxelPositionSize& voxelPositionSize); +void voxelDetailsForCode(unsigned char* octalCode, VoxelPositionSize& voxelPositionSize); typedef enum { ILLEGAL_CODE = -2, diff --git a/libraries/voxels/src/JurisdictionMap.cpp b/libraries/voxels/src/JurisdictionMap.cpp index b452c12d14..eec78fcda9 100644 --- a/libraries/voxels/src/JurisdictionMap.cpp +++ b/libraries/voxels/src/JurisdictionMap.cpp @@ -13,8 +13,64 @@ #include "JurisdictionMap.h" #include "VoxelNode.h" + +// standard assignment +// copy assignment +JurisdictionMap& JurisdictionMap::operator=(const JurisdictionMap& other) { + copyContents(other); + printf("JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap const& other) COPY ASSIGNMENT %p from %p\n", this, &other); + return *this; +} + +// move assignment +JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap&& other) { + init(other._rootOctalCode, other._endNodes); + other._rootOctalCode = NULL; + other._endNodes.clear(); + printf("JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap&& other) MOVE ASSIGNMENT %p from %p\n", this, &other); + return *this; +} + +// Move constructor +JurisdictionMap::JurisdictionMap(JurisdictionMap&& other) : _rootOctalCode(NULL) { + init(other._rootOctalCode, other._endNodes); + other._rootOctalCode = NULL; + other._endNodes.clear(); + printf("JurisdictionMap::JurisdictionMap(JurisdictionMap&& other) MOVE CONSTRUCTOR %p from %p\n", this, &other); +} + +// Copy constructor +JurisdictionMap::JurisdictionMap(const JurisdictionMap& other) : _rootOctalCode(NULL) { + copyContents(other); + printf("JurisdictionMap::JurisdictionMap(const JurisdictionMap& other) COPY CONSTRUCTOR %p from %p\n", this, &other); +} + +void JurisdictionMap::copyContents(const JurisdictionMap& other) { + unsigned char* rootCode; + std::vector endNodes; + if (other._rootOctalCode) { + int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(other._rootOctalCode)); + rootCode = new unsigned char[bytes]; + memcpy(rootCode, other._rootOctalCode, bytes); + } else { + rootCode = new unsigned char[1]; + *rootCode = 0; + } + + for (int i = 0; i < other._endNodes.size(); i++) { + if (other._endNodes[i]) { + int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(other._endNodes[i])); + unsigned char* endNodeCode = new unsigned char[bytes]; + memcpy(endNodeCode, other._endNodes[i], bytes); + endNodes.push_back(endNodeCode); + } + } + init(rootCode, endNodes); +} + JurisdictionMap::~JurisdictionMap() { clear(); + printf("JurisdictionMap::~JurisdictionMap() DESTRUCTOR %p\n",this); } void JurisdictionMap::clear() { @@ -37,16 +93,19 @@ JurisdictionMap::JurisdictionMap() : _rootOctalCode(NULL) { std::vector emptyEndNodes; init(rootCode, emptyEndNodes); + printf("JurisdictionMap::~JurisdictionMap() DEFAULT CONSTRUCTOR %p\n",this); } JurisdictionMap::JurisdictionMap(const char* filename) : _rootOctalCode(NULL) { clear(); // clean up our own memory readFromFile(filename); + printf("JurisdictionMap::~JurisdictionMap() INI FILE CONSTRUCTOR %p\n",this); } JurisdictionMap::JurisdictionMap(unsigned char* rootOctalCode, const std::vector& endNodes) : _rootOctalCode(NULL) { init(rootOctalCode, endNodes); + printf("JurisdictionMap::~JurisdictionMap() OCTCODE CONSTRUCTOR %p\n",this); } JurisdictionMap::JurisdictionMap(const char* rootHexCode, const char* endNodesHexCodes) { @@ -63,6 +122,7 @@ JurisdictionMap::JurisdictionMap(const char* rootHexCode, const char* endNodesHe //printOctalCode(endNodeOctcode); _endNodes.push_back(endNodeOctcode); } + printf("JurisdictionMap::~JurisdictionMap() HEX STRING CONSTRUCTOR %p\n",this); } diff --git a/libraries/voxels/src/JurisdictionMap.h b/libraries/voxels/src/JurisdictionMap.h index 6622292df6..d54f5147d0 100644 --- a/libraries/voxels/src/JurisdictionMap.h +++ b/libraries/voxels/src/JurisdictionMap.h @@ -20,7 +20,16 @@ public: BELOW }; - JurisdictionMap(); + // standard constructors + JurisdictionMap(); // default constructor + JurisdictionMap(const JurisdictionMap& other); // copy constructor + JurisdictionMap(JurisdictionMap&& other); // move constructor + + // standard assignment + JurisdictionMap& operator= (JurisdictionMap const &other); // copy assignment + JurisdictionMap& operator= (JurisdictionMap&& other); // move assignment + + // application constructors JurisdictionMap(const char* filename); JurisdictionMap(unsigned char* rootOctalCode, const std::vector& endNodes); JurisdictionMap(const char* rootHextString, const char* endNodesHextString); @@ -36,6 +45,7 @@ public: int getEndNodeCount() const { return _endNodes.size(); } private: + void copyContents(const JurisdictionMap& other); void clear(); void init(unsigned char* rootOctalCode, const std::vector& endNodes); diff --git a/libraries/voxels/src/VoxelSceneStats.h b/libraries/voxels/src/VoxelSceneStats.h index 910859ff3e..ba9fa559b1 100644 --- a/libraries/voxels/src/VoxelSceneStats.h +++ b/libraries/voxels/src/VoxelSceneStats.h @@ -81,7 +81,7 @@ public: char* getItemValue(int item); unsigned char* getJurisdictionRoot() const { return _jurisdictionRoot; } - + const std::vector& getJurisdictionEndNodes() const { return _jurisdictionEndNodes; } private: bool _isReadyToSend; @@ -171,7 +171,8 @@ private: static int const MAX_ITEM_VALUE_LENGTH = 128; char _itemValueBuffer[MAX_ITEM_VALUE_LENGTH]; - unsigned char* _jurisdictionRoot; + unsigned char* _jurisdictionRoot; + std::vector _jurisdictionEndNodes; }; #endif /* defined(__hifi__VoxelSceneStats__) */ From d3ce3e4e605a8508b0d2d9dd82d3a1291892296e Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 13 Aug 2013 11:37:57 -0700 Subject: [PATCH 07/26] some cleanup and fixing of memory issue --- interface/src/Application.cpp | 9 ++- libraries/shared/src/OctalCode.cpp | 45 +++++++++++++ libraries/shared/src/OctalCode.h | 5 ++ libraries/voxels/src/JurisdictionMap.cpp | 83 ++++++------------------ libraries/voxels/src/JurisdictionMap.h | 7 +- 5 files changed, 81 insertions(+), 68 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 8ee1fd2b24..082d942ee7 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3872,8 +3872,13 @@ int Application::parseVoxelStats(unsigned char* messageData, ssize_t messageLeng _voxelFades.push_back(fade); } // store jurisdiction details for later use - _voxelServerJurisdictions[nodeID] = JurisdictionMap(_voxelSceneStats.getJurisdictionRoot(), - _voxelSceneStats.getJurisdictionEndNodes()); + + // This is bit of fiddling is because JurisdictionMap assumes it is the owner of the values used to construct it + // but VoxelSceneStats thinks it's just returning a reference to it's contents. So we need to make a copy of the + // details from the VoxelSceneStats to construct the JurisdictionMap + JurisdictionMap jurisdictionMap; + jurisdictionMap.copyContents(_voxelSceneStats.getJurisdictionRoot(), _voxelSceneStats.getJurisdictionEndNodes()); + _voxelServerJurisdictions[nodeID] = jurisdictionMap; } return statsMessageLength; } diff --git a/libraries/shared/src/OctalCode.cpp b/libraries/shared/src/OctalCode.cpp index 5513d362ac..8146924cf8 100644 --- a/libraries/shared/src/OctalCode.cpp +++ b/libraries/shared/src/OctalCode.cpp @@ -319,3 +319,48 @@ bool isAncestorOf(unsigned char* possibleAncestor, unsigned char* possibleDescen // they all match, so we are an ancestor return true; } + +unsigned char* hexStringToOctalCode(const QString& input) { + const int HEX_NUMBER_BASE = 16; + const int HEX_BYTE_SIZE = 2; + int stringIndex = 0; + int byteArrayIndex = 0; + + // allocate byte array based on half of string length + unsigned char* bytes = new unsigned char[(input.length()) / HEX_BYTE_SIZE]; + + // loop through the string - 2 bytes at a time converting + // it to decimal equivalent and store in byte array + bool ok; + while (stringIndex < input.length()) { + uint value = input.mid(stringIndex, HEX_BYTE_SIZE).toUInt(&ok, HEX_NUMBER_BASE); + if (!ok) { + break; + } + bytes[byteArrayIndex] = (unsigned char)value; + stringIndex += HEX_BYTE_SIZE; + byteArrayIndex++; + } + + // something went wrong + if (!ok) { + delete[] bytes; + return NULL; + } + return bytes; +} + +QString octalCodeToHexString(unsigned char* octalCode) { + const int HEX_NUMBER_BASE = 16; + const int HEX_BYTE_SIZE = 2; + QString output; + if (!octalCode) { + output = "00"; + } else { + for (int i = 0; i < bytesRequiredForCodeLength(*octalCode); i++) { + output.append(QString("%1").arg(octalCode[i], HEX_BYTE_SIZE, HEX_NUMBER_BASE, QChar('0')).toUpper()); + } + } + return output; +} + diff --git a/libraries/shared/src/OctalCode.h b/libraries/shared/src/OctalCode.h index 846b65203b..4a43142bb8 100644 --- a/libraries/shared/src/OctalCode.h +++ b/libraries/shared/src/OctalCode.h @@ -10,6 +10,7 @@ #define __hifi__OctalCode__ #include +#include const int BITS_IN_BYTE = 8; const int BITS_IN_OCTAL = 3; @@ -49,4 +50,8 @@ typedef enum { } OctalCodeComparison; OctalCodeComparison compareOctalCodes(unsigned char* code1, unsigned char* code2); + +QString octalCodeToHexString(unsigned char* octalCode); +unsigned char* hexStringToOctalCode(const QString& input); + #endif /* defined(__hifi__OctalCode__) */ diff --git a/libraries/voxels/src/JurisdictionMap.cpp b/libraries/voxels/src/JurisdictionMap.cpp index eec78fcda9..a89d12fa6b 100644 --- a/libraries/voxels/src/JurisdictionMap.cpp +++ b/libraries/voxels/src/JurisdictionMap.cpp @@ -18,7 +18,7 @@ // copy assignment JurisdictionMap& JurisdictionMap::operator=(const JurisdictionMap& other) { copyContents(other); - printf("JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap const& other) COPY ASSIGNMENT %p from %p\n", this, &other); + //printf("JurisdictionMap COPY ASSIGNMENT %p from %p\n", this, &other); return *this; } @@ -27,7 +27,7 @@ JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap&& other) { init(other._rootOctalCode, other._endNodes); other._rootOctalCode = NULL; other._endNodes.clear(); - printf("JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap&& other) MOVE ASSIGNMENT %p from %p\n", this, &other); + //printf("JurisdictionMap MOVE ASSIGNMENT %p from %p\n", this, &other); return *this; } @@ -36,41 +36,45 @@ JurisdictionMap::JurisdictionMap(JurisdictionMap&& other) : _rootOctalCode(NULL) init(other._rootOctalCode, other._endNodes); other._rootOctalCode = NULL; other._endNodes.clear(); - printf("JurisdictionMap::JurisdictionMap(JurisdictionMap&& other) MOVE CONSTRUCTOR %p from %p\n", this, &other); + //printf("JurisdictionMap MOVE CONSTRUCTOR %p from %p\n", this, &other); } // Copy constructor JurisdictionMap::JurisdictionMap(const JurisdictionMap& other) : _rootOctalCode(NULL) { copyContents(other); - printf("JurisdictionMap::JurisdictionMap(const JurisdictionMap& other) COPY CONSTRUCTOR %p from %p\n", this, &other); + //printf("JurisdictionMap COPY CONSTRUCTOR %p from %p\n", this, &other); } -void JurisdictionMap::copyContents(const JurisdictionMap& other) { +void JurisdictionMap::copyContents(unsigned char* rootCodeIn, const std::vector endNodesIn) { unsigned char* rootCode; std::vector endNodes; - if (other._rootOctalCode) { - int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(other._rootOctalCode)); + if (rootCodeIn) { + int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(rootCodeIn)); rootCode = new unsigned char[bytes]; - memcpy(rootCode, other._rootOctalCode, bytes); + memcpy(rootCode, rootCodeIn, bytes); } else { rootCode = new unsigned char[1]; *rootCode = 0; } - for (int i = 0; i < other._endNodes.size(); i++) { - if (other._endNodes[i]) { - int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(other._endNodes[i])); + for (int i = 0; i < endNodesIn.size(); i++) { + if (endNodesIn[i]) { + int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(endNodesIn[i])); unsigned char* endNodeCode = new unsigned char[bytes]; - memcpy(endNodeCode, other._endNodes[i], bytes); + memcpy(endNodeCode, endNodesIn[i], bytes); endNodes.push_back(endNodeCode); } } init(rootCode, endNodes); } +void JurisdictionMap::copyContents(const JurisdictionMap& other) { + copyContents(other._rootOctalCode, other._endNodes); +} + JurisdictionMap::~JurisdictionMap() { clear(); - printf("JurisdictionMap::~JurisdictionMap() DESTRUCTOR %p\n",this); + //printf("JurisdictionMap DESTRUCTOR %p\n",this); } void JurisdictionMap::clear() { @@ -93,19 +97,19 @@ JurisdictionMap::JurisdictionMap() : _rootOctalCode(NULL) { std::vector emptyEndNodes; init(rootCode, emptyEndNodes); - printf("JurisdictionMap::~JurisdictionMap() DEFAULT CONSTRUCTOR %p\n",this); + //printf("JurisdictionMap DEFAULT CONSTRUCTOR %p\n",this); } JurisdictionMap::JurisdictionMap(const char* filename) : _rootOctalCode(NULL) { clear(); // clean up our own memory readFromFile(filename); - printf("JurisdictionMap::~JurisdictionMap() INI FILE CONSTRUCTOR %p\n",this); + //printf("JurisdictionMap INI FILE CONSTRUCTOR %p\n",this); } JurisdictionMap::JurisdictionMap(unsigned char* rootOctalCode, const std::vector& endNodes) : _rootOctalCode(NULL) { init(rootOctalCode, endNodes); - printf("JurisdictionMap::~JurisdictionMap() OCTCODE CONSTRUCTOR %p\n",this); + //printf("JurisdictionMap OCTCODE CONSTRUCTOR %p\n",this); } JurisdictionMap::JurisdictionMap(const char* rootHexCode, const char* endNodesHexCodes) { @@ -122,7 +126,7 @@ JurisdictionMap::JurisdictionMap(const char* rootHexCode, const char* endNodesHe //printOctalCode(endNodeOctcode); _endNodes.push_back(endNodeOctcode); } - printf("JurisdictionMap::~JurisdictionMap() HEX STRING CONSTRUCTOR %p\n",this); + //printf("JurisdictionMap HEX STRING CONSTRUCTOR %p\n",this); } @@ -207,48 +211,3 @@ bool JurisdictionMap::writeToFile(const char* filename) { settings.endGroup(); return true; } - - -unsigned char* JurisdictionMap::hexStringToOctalCode(const QString& input) const { - const int HEX_NUMBER_BASE = 16; - const int HEX_BYTE_SIZE = 2; - int stringIndex = 0; - int byteArrayIndex = 0; - - // allocate byte array based on half of string length - unsigned char* bytes = new unsigned char[(input.length()) / HEX_BYTE_SIZE]; - - // loop through the string - 2 bytes at a time converting - // it to decimal equivalent and store in byte array - bool ok; - while (stringIndex < input.length()) { - uint value = input.mid(stringIndex, HEX_BYTE_SIZE).toUInt(&ok, HEX_NUMBER_BASE); - if (!ok) { - break; - } - bytes[byteArrayIndex] = (unsigned char)value; - stringIndex += HEX_BYTE_SIZE; - byteArrayIndex++; - } - - // something went wrong - if (!ok) { - delete[] bytes; - return NULL; - } - return bytes; -} - -QString JurisdictionMap::octalCodeToHexString(unsigned char* octalCode) const { - const int HEX_NUMBER_BASE = 16; - const int HEX_BYTE_SIZE = 2; - QString output; - if (!octalCode) { - output = "00"; - } else { - for (int i = 0; i < bytesRequiredForCodeLength(*octalCode); i++) { - output.append(QString("%1").arg(octalCode[i], HEX_BYTE_SIZE, HEX_NUMBER_BASE, QChar('0')).toUpper()); - } - } - return output; -} diff --git a/libraries/voxels/src/JurisdictionMap.h b/libraries/voxels/src/JurisdictionMap.h index d54f5147d0..ff05c59584 100644 --- a/libraries/voxels/src/JurisdictionMap.h +++ b/libraries/voxels/src/JurisdictionMap.h @@ -43,15 +43,14 @@ public: unsigned char* getRootOctalCode() const { return _rootOctalCode; } unsigned char* getEndNodeOctalCode(int index) const { return _endNodes[index]; } int getEndNodeCount() const { return _endNodes.size(); } + + void copyContents(unsigned char* rootCodeIn, const std::vector endNodesIn); private: - void copyContents(const JurisdictionMap& other); + void copyContents(const JurisdictionMap& other); // use assignment instead void clear(); void init(unsigned char* rootOctalCode, const std::vector& endNodes); - unsigned char* hexStringToOctalCode(const QString& input) const; - QString octalCodeToHexString(unsigned char* octalCode) const; - unsigned char* _rootOctalCode; std::vector _endNodes; }; From 27d001a84dea4ff2f2039f4a6ef5bab4802b6a83 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 13 Aug 2013 13:29:55 -0700 Subject: [PATCH 08/26] copy the jurisdication end nodes into voxel scene stats --- libraries/voxels/src/VoxelSceneStats.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/libraries/voxels/src/VoxelSceneStats.cpp b/libraries/voxels/src/VoxelSceneStats.cpp index d525725371..d199caf9ef 100644 --- a/libraries/voxels/src/VoxelSceneStats.cpp +++ b/libraries/voxels/src/VoxelSceneStats.cpp @@ -29,9 +29,13 @@ VoxelSceneStats::~VoxelSceneStats() { if (_jurisdictionRoot) { delete[] _jurisdictionRoot; } + for (int i=0; i < _jurisdictionEndNodes.size(); i++) { + if (_jurisdictionEndNodes[i]) { + delete[] _jurisdictionEndNodes[i]; + } + } } - void VoxelSceneStats::sceneStarted(bool isFullScene, bool isMoving, VoxelNode* root, JurisdictionMap* jurisdictionMap) { reset(); // resets packet and voxel stats _isStarted = true; @@ -54,6 +58,16 @@ void VoxelSceneStats::sceneStarted(bool isFullScene, bool isMoving, VoxelNode* r _jurisdictionRoot = new unsigned char[bytes]; memcpy(_jurisdictionRoot, jurisdictionRoot, bytes); } + + for (int i=0; i < jurisdictionMap->getEndNodeCount(); i++) { + unsigned char* endNodeCode = jurisdictionMap->getEndNodeOctalCode(i); + if (endNodeCode) { + int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(endNodeCode)); + unsigned char* endNodeCodeCopy = new unsigned char[bytes]; + memcpy(endNodeCodeCopy, endNodeCode, bytes); + _jurisdictionEndNodes.push_back(endNodeCodeCopy); + } + } } } From 02f2de6101f5f3f49b1be97c51b61b3f78bf5e4f Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 13 Aug 2013 13:43:46 -0700 Subject: [PATCH 09/26] properly send full jurisdiction details in scene stats --- libraries/shared/src/PacketHeaders.cpp | 2 +- libraries/voxels/src/VoxelSceneStats.cpp | 54 +++++++++++++++++++----- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/libraries/shared/src/PacketHeaders.cpp b/libraries/shared/src/PacketHeaders.cpp index e8a459d633..5a1c3f9b4c 100644 --- a/libraries/shared/src/PacketHeaders.cpp +++ b/libraries/shared/src/PacketHeaders.cpp @@ -26,7 +26,7 @@ PACKET_VERSION versionForPacketType(PACKET_TYPE type) { return 1; case PACKET_TYPE_VOXEL_STATS: - return 1; + return 2; default: return 0; } diff --git a/libraries/voxels/src/VoxelSceneStats.cpp b/libraries/voxels/src/VoxelSceneStats.cpp index d199caf9ef..47e0beafba 100644 --- a/libraries/voxels/src/VoxelSceneStats.cpp +++ b/libraries/voxels/src/VoxelSceneStats.cpp @@ -17,23 +17,16 @@ const int samples = 100; VoxelSceneStats::VoxelSceneStats() : _elapsedAverage(samples), - _bitsPerVoxelAverage(samples) + _bitsPerVoxelAverage(samples), + _jurisdictionRoot(NULL) { reset(); _isReadyToSend = false; _isStarted = false; - _jurisdictionRoot = NULL; } VoxelSceneStats::~VoxelSceneStats() { - if (_jurisdictionRoot) { - delete[] _jurisdictionRoot; - } - for (int i=0; i < _jurisdictionEndNodes.size(); i++) { - if (_jurisdictionEndNodes[i]) { - delete[] _jurisdictionEndNodes[i]; - } - } + reset(); } void VoxelSceneStats::sceneStarted(bool isFullScene, bool isMoving, VoxelNode* root, JurisdictionMap* jurisdictionMap) { @@ -139,6 +132,17 @@ void VoxelSceneStats::reset() { _existsBitsWritten = 0; _existsInPacketBitsWritten = 0; _treesRemoved = 0; + + if (_jurisdictionRoot) { + delete[] _jurisdictionRoot; + _jurisdictionRoot = NULL; + } + for (int i=0; i < _jurisdictionEndNodes.size(); i++) { + if (_jurisdictionEndNodes[i]) { + delete[] _jurisdictionEndNodes[i]; + } + } + _jurisdictionEndNodes.clear(); } void VoxelSceneStats::packetSent(int bytes) { @@ -317,6 +321,21 @@ int VoxelSceneStats::packIntoMessage(unsigned char* destinationBuffer, int avail destinationBuffer += sizeof(bytes); memcpy(destinationBuffer, _jurisdictionRoot, bytes); destinationBuffer += bytes; + + // if and only if there's a root jurisdiction, also include the end nodes + int endNodeCount = _jurisdictionEndNodes.size(); + + memcpy(destinationBuffer, &endNodeCount, sizeof(endNodeCount)); + destinationBuffer += sizeof(endNodeCount); + + for (int i=0; i < endNodeCount; i++) { + unsigned char* endNodeCode = _jurisdictionEndNodes[i]; + int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(endNodeCode)); + memcpy(destinationBuffer, &bytes, sizeof(bytes)); + destinationBuffer += sizeof(bytes); + memcpy(destinationBuffer, endNodeCode, bytes); + destinationBuffer += bytes; + } } else { int bytes = 0; memcpy(destinationBuffer, &bytes, sizeof(bytes)); @@ -424,6 +443,21 @@ int VoxelSceneStats::unpackFromMessage(unsigned char* sourceBuffer, int availabl _jurisdictionRoot = new unsigned char[bytes]; memcpy(_jurisdictionRoot, sourceBuffer, bytes); sourceBuffer += bytes; + + // if and only if there's a root jurisdiction, also include the end nodes + int endNodeCount = 0; + memcpy(&endNodeCount, sourceBuffer, sizeof(endNodeCount)); + sourceBuffer += sizeof(endNodeCount); + + for (int i=0; i < endNodeCount; i++) { + int bytes = 0; + memcpy(&bytes, sourceBuffer, sizeof(bytes)); + sourceBuffer += sizeof(bytes); + unsigned char* endNodeCode = new unsigned char[bytes]; + memcpy(endNodeCode, sourceBuffer, bytes); + sourceBuffer += bytes; + _jurisdictionEndNodes.push_back(endNodeCode); + } } // running averages From 4477289501524bbbf983527715964c1a3916c2fd Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 14 Aug 2013 13:18:41 -0700 Subject: [PATCH 10/26] repair bad merge --- interface/src/Application.cpp | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 0b68887725..e0f8f87dbf 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -22,6 +22,9 @@ #include #include +// include this before QGLWidget, which includes an earlier version of OpenGL +#include "InterfaceConfig.h" + #include #include #include @@ -92,6 +95,8 @@ const int STARTUP_JITTER_SAMPLES = PACKET_LENGTH_SAMPLES_PER_CHANNEL / 2; // customized canvas that simply forwards requests/events to the singleton application class GLCanvas : public QGLWidget { +public: + GLCanvas(); protected: virtual void initializeGL(); @@ -110,6 +115,9 @@ protected: virtual void wheelEvent(QWheelEvent* event); }; +GLCanvas::GLCanvas() : QGLWidget(QGLFormat(QGL::NoDepthBuffer, QGL::NoStencilBuffer)) { +} + void GLCanvas::initializeGL() { Application::getInstance()->initializeGL(); setAttribute(Qt::WA_AcceptTouchEvents); @@ -2103,8 +2111,8 @@ void Application::runTests() { void Application::initDisplay() { glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glShadeModel (GL_SMOOTH); + glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE); + glShadeModel(GL_SMOOTH); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); @@ -2114,6 +2122,8 @@ void Application::init() { _voxels.init(); _environment.init(); + + _glowEffect.init(); _handControl.setScreenDimensions(_glWidget->width(), _glWidget->height()); @@ -2214,11 +2224,21 @@ void Application::renderLookatIndicator(glm::vec3 pointOfInterest, Camera& which renderCircle(haloOrigin, INDICATOR_RADIUS, IDENTITY_UP, NUM_SEGMENTS); } +void maybeBeginFollowIndicator(bool& began) { + if (!began) { + Application::getInstance()->getGlowEffect()->begin(); + glLineWidth(5); + glBegin(GL_LINES); + began = true; + } +} + void Application::renderFollowIndicator() { NodeList* nodeList = NodeList::getInstance(); - glLineWidth(5); - glBegin(GL_LINES); + // initialize lazily so that we don't enable the glow effect unnecessarily + bool began = false; + for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); ++node) { if (node->getLinkedData() != NULL && node->getType() == NODE_TYPE_AGENT) { Avatar* avatar = (Avatar *) node->getLinkedData(); @@ -2237,6 +2257,7 @@ void Application::renderFollowIndicator() { } if (leader != NULL) { + maybeBeginFollowIndicator(began); glColor3f(1.f, 0.f, 0.f); glVertex3f((avatar->getHead().getPosition().x + avatar->getPosition().x) / 2.f, (avatar->getHead().getPosition().y + avatar->getPosition().y) / 2.f, @@ -2251,6 +2272,7 @@ void Application::renderFollowIndicator() { } if (_myAvatar.getLeadingAvatar() != NULL) { + maybeBeginFollowIndicator(began); glColor3f(1.f, 0.f, 0.f); glVertex3f((_myAvatar.getHead().getPosition().x + _myAvatar.getPosition().x) / 2.f, (_myAvatar.getHead().getPosition().y + _myAvatar.getPosition().y) / 2.f, @@ -2261,7 +2283,10 @@ void Application::renderFollowIndicator() { (_myAvatar.getLeadingAvatar()->getHead().getPosition().z + _myAvatar.getLeadingAvatar()->getPosition().z) / 2.f); } - glEnd(); + if (began) { + glEnd(); + _glowEffect.end(); + } } void Application::update(float deltaTime) { From a43615e9dc16e13bd3f68a276fcbf71aa82b5b22 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 14 Aug 2013 13:20:22 -0700 Subject: [PATCH 11/26] repair bad merge --- interface/src/Application.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index e0f8f87dbf..828fd75a78 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2914,6 +2914,9 @@ void Application::displaySide(Camera& whichCamera) { // Setup 3D lights (after the camera transform, so that they are positioned in world space) setupWorldLight(whichCamera); + // prepare the glow effect + _glowEffect.prepare(); + if (_renderStarsOn->isChecked()) { if (!_stars.getFileLoaded()) { _stars.readInput(STAR_FILE, STAR_CACHE_FILE, 0); @@ -3042,6 +3045,7 @@ void Application::displaySide(Camera& whichCamera) { _myAvatar.getHead().setLookAtPosition(_myCamera.getPosition()); } _myAvatar.render(_lookingInMirror->isChecked(), _renderAvatarBalls->isChecked()); + _myAvatar.setDisplayingLookatVectors(_renderLookatOn->isChecked()); if (_renderLookatIndicatorOn->isChecked() && _isLookingAtOtherAvatar) { @@ -3076,6 +3080,9 @@ void Application::displaySide(Camera& whichCamera) { } renderFollowIndicator(); + + // render the glow effect + _glowEffect.render(); } void Application::displayOverlay() { From 4305ad552d22fccf5695bec8cccea65d3c70a712 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 14 Aug 2013 14:19:06 -0700 Subject: [PATCH 12/26] fix issue with JurisdictionMap being passed across wire --- interface/src/Application.cpp | 2 +- interface/src/VoxelEditPacketSender.cpp | 7 ------- libraries/voxels/src/JurisdictionMap.cpp | 9 +-------- libraries/voxels/src/VoxelSceneStats.cpp | 4 ++-- 4 files changed, 4 insertions(+), 18 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 828fd75a78..2da4abc242 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1586,7 +1586,7 @@ bool Application::sendVoxelsOperation(VoxelNode* node, void* extraData) { codeColorBuffer[bytesInCode + RED_INDEX ] = node->getColor()[RED_INDEX ]; codeColorBuffer[bytesInCode + GREEN_INDEX] = node->getColor()[GREEN_INDEX]; codeColorBuffer[bytesInCode + BLUE_INDEX ] = node->getColor()[BLUE_INDEX ]; - + args->app->_voxelEditSender.queueVoxelEditMessage(PACKET_TYPE_SET_VOXEL_DESTRUCTIVE, codeColorBuffer, codeAndColorLength); delete[] codeColorBuffer; diff --git a/interface/src/VoxelEditPacketSender.cpp b/interface/src/VoxelEditPacketSender.cpp index 5155619b7e..c663977268 100644 --- a/interface/src/VoxelEditPacketSender.cpp +++ b/interface/src/VoxelEditPacketSender.cpp @@ -39,7 +39,6 @@ void VoxelEditPacketSender::sendVoxelEditMessage(PACKET_TYPE type, VoxelDetail& } void VoxelEditPacketSender::actuallySendMessage(uint16_t nodeID, unsigned char* bufferOut, ssize_t sizeOut) { - qDebug("VoxelEditPacketSender::actuallySendMessage() sizeOut=%lu target NodeID=%d\n", sizeOut, nodeID); NodeList* nodeList = NodeList::getInstance(); for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) { // only send to the NodeTypes that are NODE_TYPE_VOXEL_SERVER @@ -65,13 +64,7 @@ void VoxelEditPacketSender::queueVoxelEditMessage(PACKET_TYPE type, unsigned cha // here we need to get the "pending packet" for this server uint16_t nodeID = node->getNodeID(); const JurisdictionMap& map = _app->_voxelServerJurisdictions[nodeID]; - if (map.isMyJurisdiction(codeColorBuffer, CHECK_NODE_ONLY) == JurisdictionMap::WITHIN) { - - // do I need this??? - //if (_pendingEditPackets.find(nodeID) == _pendingEditPackets.end()) { - // _pendingEditPackets[nodeID] = - //} EditPacketBuffer& packetBuffer = _pendingEditPackets[nodeID]; packetBuffer._nodeID = nodeID; diff --git a/libraries/voxels/src/JurisdictionMap.cpp b/libraries/voxels/src/JurisdictionMap.cpp index a89d12fa6b..5573bdf840 100644 --- a/libraries/voxels/src/JurisdictionMap.cpp +++ b/libraries/voxels/src/JurisdictionMap.cpp @@ -138,7 +138,7 @@ void JurisdictionMap::init(unsigned char* rootOctalCode, const std::vector Date: Wed, 14 Aug 2013 14:22:50 -0700 Subject: [PATCH 14/26] merge repair --- interface/src/Application.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index a51ead5ad5..00f4178b90 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -416,7 +416,6 @@ void Application::paintGL() { PerfStat("display"); glEnable(GL_LINE_SMOOTH); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (_myCamera.getMode() == CAMERA_MODE_MIRROR) { _myCamera.setTightness (100.0f); From d19e2d7490cd5b9e0938bcf3116175f981ab9001 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 14 Aug 2013 14:23:13 -0700 Subject: [PATCH 15/26] merge repair --- interface/src/Application.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 00f4178b90..7e60b2fac2 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -416,7 +416,7 @@ void Application::paintGL() { PerfStat("display"); glEnable(GL_LINE_SMOOTH); - + if (_myCamera.getMode() == CAMERA_MODE_MIRROR) { _myCamera.setTightness (100.0f); _myCamera.setTargetPosition(_myAvatar.getUprightHeadPosition()); From 21f521f3a53a5728f38df53873e4d6a237e445b0 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 14 Aug 2013 14:33:47 -0700 Subject: [PATCH 16/26] fix build buster --- libraries/shared/src/NetworkPacket.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/shared/src/NetworkPacket.h b/libraries/shared/src/NetworkPacket.h index d82a254591..a6383b4468 100644 --- a/libraries/shared/src/NetworkPacket.h +++ b/libraries/shared/src/NetworkPacket.h @@ -21,9 +21,9 @@ class NetworkPacket { public: NetworkPacket(); NetworkPacket(const NetworkPacket& packet); // copy constructor - NetworkPacket(NetworkPacket && packet); // move?? // same as copy, but other packet won't be used further + NetworkPacket(NetworkPacket&& packet); // move?? // same as copy, but other packet won't be used further ~NetworkPacket(); // destructor - NetworkPacket& operator= (NetworkPacket const &other); // copy assignment + NetworkPacket& operator= (const NetworkPacket& other); // copy assignment NetworkPacket& operator= (NetworkPacket&& other); // move assignment NetworkPacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength); From 18e5d49d758458a54eac6e3dcae6bc907c5698c0 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 14 Aug 2013 14:46:40 -0700 Subject: [PATCH 17/26] move move constructors and assignments into IFDEFS for now to fix build buster --- libraries/shared/src/NetworkPacket.cpp | 13 +++++++------ libraries/shared/src/NetworkPacket.h | 5 ++++- libraries/shared/src/PacketSender.cpp | 2 +- libraries/voxels/src/JurisdictionMap.cpp | 18 ++++++++++-------- libraries/voxels/src/JurisdictionMap.h | 6 +++++- 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/libraries/shared/src/NetworkPacket.cpp b/libraries/shared/src/NetworkPacket.cpp index 803972d502..0acabbac58 100644 --- a/libraries/shared/src/NetworkPacket.cpp +++ b/libraries/shared/src/NetworkPacket.cpp @@ -37,12 +37,6 @@ NetworkPacket::NetworkPacket(const NetworkPacket& packet) { copyContents(packet.getAddress(), packet.getData(), packet.getLength()); } -// move?? // same as copy, but other packet won't be used further -NetworkPacket::NetworkPacket(NetworkPacket && packet) { - copyContents(packet.getAddress(), packet.getData(), packet.getLength()); -} - - NetworkPacket::NetworkPacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength) { copyContents(address, packetData, packetLength); }; @@ -53,9 +47,16 @@ NetworkPacket& NetworkPacket::operator=(NetworkPacket const& other) { return *this; } +#ifdef HAS_MOVE_SEMANTICS +// move, same as copy, but other packet won't be used further +NetworkPacket::NetworkPacket(NetworkPacket && packet) { + copyContents(packet.getAddress(), packet.getData(), packet.getLength()); +} + // move assignment NetworkPacket& NetworkPacket::operator=(NetworkPacket&& other) { _packetLength = 0; copyContents(other.getAddress(), other.getData(), other.getLength()); return *this; } +#endif \ No newline at end of file diff --git a/libraries/shared/src/NetworkPacket.h b/libraries/shared/src/NetworkPacket.h index a6383b4468..a2fe2e63cd 100644 --- a/libraries/shared/src/NetworkPacket.h +++ b/libraries/shared/src/NetworkPacket.h @@ -21,10 +21,13 @@ class NetworkPacket { public: NetworkPacket(); NetworkPacket(const NetworkPacket& packet); // copy constructor - NetworkPacket(NetworkPacket&& packet); // move?? // same as copy, but other packet won't be used further ~NetworkPacket(); // destructor NetworkPacket& operator= (const NetworkPacket& other); // copy assignment + +#ifdef HAS_MOVE_SEMANTICS + NetworkPacket(NetworkPacket&& packet); // move?? // same as copy, but other packet won't be used further NetworkPacket& operator= (NetworkPacket&& other); // move assignment +#endif NetworkPacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength); diff --git a/libraries/shared/src/PacketSender.cpp b/libraries/shared/src/PacketSender.cpp index 61b173c5ee..858cd5c0e2 100644 --- a/libraries/shared/src/PacketSender.cpp +++ b/libraries/shared/src/PacketSender.cpp @@ -35,7 +35,7 @@ bool PacketSender::process() { // send the packet through the NodeList... UDPSocket* nodeSocket = NodeList::getInstance()->getNodeSocket(); - qDebug("PacketSender::process()... nodeSocket->send() length=%lu\n", packet.getLength()); + //qDebug("PacketSender::process()... nodeSocket->send() length=%lu\n", packet.getLength()); nodeSocket->send(&packet.getAddress(), packet.getData(), packet.getLength()); diff --git a/libraries/voxels/src/JurisdictionMap.cpp b/libraries/voxels/src/JurisdictionMap.cpp index 5573bdf840..377329caf2 100644 --- a/libraries/voxels/src/JurisdictionMap.cpp +++ b/libraries/voxels/src/JurisdictionMap.cpp @@ -22,6 +22,15 @@ JurisdictionMap& JurisdictionMap::operator=(const JurisdictionMap& other) { return *this; } +#ifdef HAS_MOVE_SEMANTICS +// Move constructor +JurisdictionMap::JurisdictionMap(JurisdictionMap&& other) : _rootOctalCode(NULL) { + init(other._rootOctalCode, other._endNodes); + other._rootOctalCode = NULL; + other._endNodes.clear(); + //printf("JurisdictionMap MOVE CONSTRUCTOR %p from %p\n", this, &other); +} + // move assignment JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap&& other) { init(other._rootOctalCode, other._endNodes); @@ -30,14 +39,7 @@ JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap&& other) { //printf("JurisdictionMap MOVE ASSIGNMENT %p from %p\n", this, &other); return *this; } - -// Move constructor -JurisdictionMap::JurisdictionMap(JurisdictionMap&& other) : _rootOctalCode(NULL) { - init(other._rootOctalCode, other._endNodes); - other._rootOctalCode = NULL; - other._endNodes.clear(); - //printf("JurisdictionMap MOVE CONSTRUCTOR %p from %p\n", this, &other); -} +#endif // Copy constructor JurisdictionMap::JurisdictionMap(const JurisdictionMap& other) : _rootOctalCode(NULL) { diff --git a/libraries/voxels/src/JurisdictionMap.h b/libraries/voxels/src/JurisdictionMap.h index ff05c59584..50aabb65f2 100644 --- a/libraries/voxels/src/JurisdictionMap.h +++ b/libraries/voxels/src/JurisdictionMap.h @@ -23,11 +23,15 @@ public: // standard constructors JurisdictionMap(); // default constructor JurisdictionMap(const JurisdictionMap& other); // copy constructor - JurisdictionMap(JurisdictionMap&& other); // move constructor // standard assignment JurisdictionMap& operator= (JurisdictionMap const &other); // copy assignment + +#ifdef HAS_MOVE_SEMANTICS + // move constructor and assignment + JurisdictionMap(JurisdictionMap&& other); // move constructor JurisdictionMap& operator= (JurisdictionMap&& other); // move assignment +#endif // application constructors JurisdictionMap(const char* filename); From 1730f7f2f6a0eb4e89b702d814d46e7314c4bc1b Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 14 Aug 2013 14:48:16 -0700 Subject: [PATCH 18/26] fix build buster --- libraries/shared/src/PacketSender.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/libraries/shared/src/PacketSender.cpp b/libraries/shared/src/PacketSender.cpp index 858cd5c0e2..99a79d9881 100644 --- a/libraries/shared/src/PacketSender.cpp +++ b/libraries/shared/src/PacketSender.cpp @@ -8,8 +8,6 @@ // Threaded or non-threaded packet sender. // -#include - const uint64_t SEND_INTERVAL_USECS = 1000 * 5; // no more than 200pps... should be settable #include "NodeList.h" @@ -35,8 +33,6 @@ bool PacketSender::process() { // send the packet through the NodeList... UDPSocket* nodeSocket = NodeList::getInstance()->getNodeSocket(); - //qDebug("PacketSender::process()... nodeSocket->send() length=%lu\n", packet.getLength()); - nodeSocket->send(&packet.getAddress(), packet.getData(), packet.getLength()); lock(); @@ -49,7 +45,6 @@ bool PacketSender::process() { int usecToSleep = SEND_INTERVAL_USECS - elapsed; _lastSendTime = now; if (usecToSleep > 0) { - //qDebug("PacketSender::process()... sleeping for %d useconds\n", usecToSleep); usleep(usecToSleep); } From 266d57264b118314f7486d193d3abd667742cd52 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 14 Aug 2013 14:57:23 -0700 Subject: [PATCH 19/26] fix build buster --- libraries/shared/src/PacketSender.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libraries/shared/src/PacketSender.cpp b/libraries/shared/src/PacketSender.cpp index 99a79d9881..ec3a6cff7f 100644 --- a/libraries/shared/src/PacketSender.cpp +++ b/libraries/shared/src/PacketSender.cpp @@ -8,6 +8,8 @@ // Threaded or non-threaded packet sender. // +#include + const uint64_t SEND_INTERVAL_USECS = 1000 * 5; // no more than 200pps... should be settable #include "NodeList.h" From beec5f60d8fc254028ba636e5d8b53c872cfa3ea Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Thu, 15 Aug 2013 08:36:06 -0700 Subject: [PATCH 20/26] 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__ From ced61e94d88bf3216d13eddf1a90e1ff9bfdf525 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Thu, 15 Aug 2013 08:48:21 -0700 Subject: [PATCH 21/26] added more doxygen comments --- interface/src/VoxelEditPacketSender.h | 15 +++++++++++---- interface/src/VoxelPacketProcessor.h | 5 ++++- libraries/shared/src/GenericThread.h | 2 ++ libraries/shared/src/PacketSender.h | 12 +++++++++--- libraries/shared/src/ReceivedPacketProcessor.h | 4 ++-- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/interface/src/VoxelEditPacketSender.h b/interface/src/VoxelEditPacketSender.h index f0d18e85f4..41c15ac8d2 100644 --- a/interface/src/VoxelEditPacketSender.h +++ b/interface/src/VoxelEditPacketSender.h @@ -16,6 +16,7 @@ class Application; +/// Used for construction of edit voxel packets class EditPacketBuffer { public: EditPacketBuffer() { _currentSize = 0; _currentType = PACKET_TYPE_UNKNOWN; _nodeID = UNKNOWN_NODE_ID; } @@ -25,14 +26,20 @@ public: ssize_t _currentSize; }; +/// Threaded processor for queueing and sending of outbound edit voxel packets. class VoxelEditPacketSender : public PacketSender { public: VoxelEditPacketSender(Application* app); - // Some ways you can send voxel edit messages... - void sendVoxelEditMessage(PACKET_TYPE type, VoxelDetail& detail); // sends it right away - void queueVoxelEditMessage(PACKET_TYPE type, unsigned char* codeColorBuffer, ssize_t length); // queues it into a multi-command packet - void flushQueue(); // flushes all queued packets + /// Send voxel edit message immediately + void sendVoxelEditMessage(PACKET_TYPE type, VoxelDetail& detail); + + /// Queues a voxel edit message. Will potentially sends a pending multi-command packet. Determines which voxel-server + /// node or nodes the packet should be sent to. + void queueVoxelEditMessage(PACKET_TYPE type, unsigned char* codeColorBuffer, ssize_t length); + + /// flushes all queued packets for all nodes + void flushQueue(); private: void actuallySendMessage(uint16_t nodeID, unsigned char* bufferOut, ssize_t sizeOut); diff --git a/interface/src/VoxelPacketProcessor.h b/interface/src/VoxelPacketProcessor.h index 725d69e181..f55daf5aba 100644 --- a/interface/src/VoxelPacketProcessor.h +++ b/interface/src/VoxelPacketProcessor.h @@ -15,11 +15,14 @@ class Application; +/// Handles processing of incoming voxel packets for the interface application. class VoxelPacketProcessor : public ReceivedPacketProcessor { public: VoxelPacketProcessor(Application* app); - virtual void processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); +protected: + virtual void processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); + private: Application* _app; }; diff --git a/libraries/shared/src/GenericThread.h b/libraries/shared/src/GenericThread.h index aa55adf4bf..2d4c90a469 100644 --- a/libraries/shared/src/GenericThread.h +++ b/libraries/shared/src/GenericThread.h @@ -36,6 +36,8 @@ protected: /// Locks all the resources of the thread. void lock() { pthread_mutex_lock(&_mutex); } + + /// Unlocks all the resources of the thread. void unlock() { pthread_mutex_unlock(&_mutex); } private: diff --git a/libraries/shared/src/PacketSender.h b/libraries/shared/src/PacketSender.h index 72b61011cc..5a1a63695f 100644 --- a/libraries/shared/src/PacketSender.h +++ b/libraries/shared/src/PacketSender.h @@ -14,16 +14,22 @@ #include "GenericThread.h" #include "NetworkPacket.h" +/// Generalized threaded processor for queueing and sending of outbound packets. class PacketSender : public GenericThread { public: PacketSender(); - // Call this when you have a packet you'd like sent... + + /// Add packet to outbound queue. + /// \param sockaddr& address the destination address + /// \param packetData pointer to data + /// \param ssize_t packetLength size of data + /// \thread any thread, typically the application thread void queuePacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength); - virtual bool process(); - private: + virtual bool process(); + std::vector _packets; uint64_t _lastSendTime; diff --git a/libraries/shared/src/ReceivedPacketProcessor.h b/libraries/shared/src/ReceivedPacketProcessor.h index a132267482..0ea1aba2c1 100644 --- a/libraries/shared/src/ReceivedPacketProcessor.h +++ b/libraries/shared/src/ReceivedPacketProcessor.h @@ -14,7 +14,7 @@ #include "GenericThread.h" #include "NetworkPacket.h" -/// Generalized threaded processor for handler received inbound packets. +/// Generalized threaded processor for handling received inbound packets. class ReceivedPacketProcessor : public GenericThread { public: @@ -25,6 +25,7 @@ public: /// \thread network receive thread void queuePacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); +protected: /// 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 @@ -32,7 +33,6 @@ public: /// \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: From 90fcfca561bfa2b00a3a1658a1a5e84da86694ba Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Thu, 15 Aug 2013 10:45:53 -0700 Subject: [PATCH 22/26] style fix --- libraries/voxels/src/VoxelSceneStats.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/voxels/src/VoxelSceneStats.h b/libraries/voxels/src/VoxelSceneStats.h index ba9fa559b1..feb8b81edc 100644 --- a/libraries/voxels/src/VoxelSceneStats.h +++ b/libraries/voxels/src/VoxelSceneStats.h @@ -171,7 +171,7 @@ private: static int const MAX_ITEM_VALUE_LENGTH = 128; char _itemValueBuffer[MAX_ITEM_VALUE_LENGTH]; - unsigned char* _jurisdictionRoot; + unsigned char* _jurisdictionRoot; std::vector _jurisdictionEndNodes; }; From 8b867df762f89434a812c16203d371f4659db75f Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Thu, 15 Aug 2013 10:47:59 -0700 Subject: [PATCH 23/26] style fix --- interface/src/VoxelEditPacketSender.h | 8 ++++---- libraries/shared/src/NetworkPacket.h | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/interface/src/VoxelEditPacketSender.h b/interface/src/VoxelEditPacketSender.h index 41c15ac8d2..d3058828e3 100644 --- a/interface/src/VoxelEditPacketSender.h +++ b/interface/src/VoxelEditPacketSender.h @@ -20,10 +20,10 @@ class Application; class EditPacketBuffer { public: EditPacketBuffer() { _currentSize = 0; _currentType = PACKET_TYPE_UNKNOWN; _nodeID = UNKNOWN_NODE_ID; } - uint16_t _nodeID; - PACKET_TYPE _currentType; - unsigned char _currentBuffer[MAX_PACKET_SIZE]; - ssize_t _currentSize; + uint16_t _nodeID; + PACKET_TYPE _currentType; + unsigned char _currentBuffer[MAX_PACKET_SIZE]; + ssize_t _currentSize; }; /// Threaded processor for queueing and sending of outbound edit voxel packets. diff --git a/libraries/shared/src/NetworkPacket.h b/libraries/shared/src/NetworkPacket.h index 16b6f7b261..01bced6a71 100644 --- a/libraries/shared/src/NetworkPacket.h +++ b/libraries/shared/src/NetworkPacket.h @@ -32,19 +32,19 @@ public: NetworkPacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength); - sockaddr& getAddress() { return _address; }; - ssize_t getLength() const { return _packetLength; }; - unsigned char* getData() { return &_packetData[0]; }; + sockaddr& getAddress() { return _address; }; + ssize_t getLength() const { return _packetLength; }; + unsigned char* getData() { return &_packetData[0]; }; - const sockaddr& getAddress() const { return _address; }; - const unsigned char* getData() const { return &_packetData[0]; }; + const sockaddr& getAddress() const { return _address; }; + const unsigned char* getData() const { return &_packetData[0]; }; private: void copyContents(const sockaddr& address, const unsigned char* packetData, ssize_t packetLength); - sockaddr _address; - ssize_t _packetLength; - unsigned char _packetData[MAX_PACKET_SIZE]; + sockaddr _address; + ssize_t _packetLength; + unsigned char _packetData[MAX_PACKET_SIZE]; }; #endif /* defined(__shared_NetworkPacket__) */ From 8e07bd42eaf2fb6c340356c8007c4f41faa776b4 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Thu, 15 Aug 2013 10:51:56 -0700 Subject: [PATCH 24/26] removed app from SendVoxelsOperationArgs since its available as singleton --- interface/src/Application.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 710ed6446b..9f1880e952 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1553,7 +1553,6 @@ void Application::chooseVoxelPaintColor() { const int MAXIMUM_EDIT_VOXEL_MESSAGE_SIZE = 1500; struct SendVoxelsOperationArgs { unsigned char* newBaseOctCode; - Application* app; }; bool Application::sendVoxelsOperation(VoxelNode* node, void* extraData) { @@ -1585,7 +1584,8 @@ bool Application::sendVoxelsOperation(VoxelNode* node, void* extraData) { codeColorBuffer[bytesInCode + GREEN_INDEX] = node->getColor()[GREEN_INDEX]; codeColorBuffer[bytesInCode + BLUE_INDEX ] = node->getColor()[BLUE_INDEX ]; - args->app->_voxelEditSender.queueVoxelEditMessage(PACKET_TYPE_SET_VOXEL_DESTRUCTIVE, codeColorBuffer, codeAndColorLength); + getInstance()->_voxelEditSender.queueVoxelEditMessage(PACKET_TYPE_SET_VOXEL_DESTRUCTIVE, + codeColorBuffer, codeAndColorLength); delete[] codeColorBuffer; } @@ -1769,7 +1769,6 @@ void Application::importVoxels() { // the server as an set voxel message, this will also rebase the voxels to the new location unsigned char* calculatedOctCode = NULL; SendVoxelsOperationArgs args; - args.app = this; // we only need the selected voxel to get the newBaseOctCode, which we can actually calculate from the // voxel size/position details. @@ -1818,7 +1817,6 @@ void Application::pasteVoxels() { // Recurse the clipboard tree, where everything is root relative, and send all the colored voxels to // the server as an set voxel message, this will also rebase the voxels to the new location SendVoxelsOperationArgs args; - args.app = this; // we only need the selected voxel to get the newBaseOctCode, which we can actually calculate from the // voxel size/position details. If we don't have an actual selectedNode then use the mouseVoxel to create a From 4f16157e5132ecd38d99e3b7862892b9eabfa192 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Thu, 15 Aug 2013 11:39:00 -0700 Subject: [PATCH 25/26] CR feedback --- interface/src/Application.h | 2 +- libraries/voxels/src/JurisdictionMap.cpp | 11 +---------- libraries/voxels/src/JurisdictionMap.h | 4 ++-- 3 files changed, 4 insertions(+), 13 deletions(-) diff --git a/interface/src/Application.h b/interface/src/Application.h index bb7eff8295..88cb86312f 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -474,7 +474,7 @@ private: VoxelSceneStats _voxelSceneStats; int parseVoxelStats(unsigned char* messageData, ssize_t messageLength, sockaddr senderAddress); - std::map _voxelServerJurisdictions; + std::map _voxelServerJurisdictions; std::vector _voxelFades; }; diff --git a/libraries/voxels/src/JurisdictionMap.cpp b/libraries/voxels/src/JurisdictionMap.cpp index 377329caf2..360f74093d 100644 --- a/libraries/voxels/src/JurisdictionMap.cpp +++ b/libraries/voxels/src/JurisdictionMap.cpp @@ -18,7 +18,6 @@ // copy assignment JurisdictionMap& JurisdictionMap::operator=(const JurisdictionMap& other) { copyContents(other); - //printf("JurisdictionMap COPY ASSIGNMENT %p from %p\n", this, &other); return *this; } @@ -28,7 +27,6 @@ JurisdictionMap::JurisdictionMap(JurisdictionMap&& other) : _rootOctalCode(NULL) init(other._rootOctalCode, other._endNodes); other._rootOctalCode = NULL; other._endNodes.clear(); - //printf("JurisdictionMap MOVE CONSTRUCTOR %p from %p\n", this, &other); } // move assignment @@ -36,7 +34,6 @@ JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap&& other) { init(other._rootOctalCode, other._endNodes); other._rootOctalCode = NULL; other._endNodes.clear(); - //printf("JurisdictionMap MOVE ASSIGNMENT %p from %p\n", this, &other); return *this; } #endif @@ -44,10 +41,9 @@ JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap&& other) { // Copy constructor JurisdictionMap::JurisdictionMap(const JurisdictionMap& other) : _rootOctalCode(NULL) { copyContents(other); - //printf("JurisdictionMap COPY CONSTRUCTOR %p from %p\n", this, &other); } -void JurisdictionMap::copyContents(unsigned char* rootCodeIn, const std::vector endNodesIn) { +void JurisdictionMap::copyContents(unsigned char* rootCodeIn, const std::vector& endNodesIn) { unsigned char* rootCode; std::vector endNodes; if (rootCodeIn) { @@ -76,7 +72,6 @@ void JurisdictionMap::copyContents(const JurisdictionMap& other) { JurisdictionMap::~JurisdictionMap() { clear(); - //printf("JurisdictionMap DESTRUCTOR %p\n",this); } void JurisdictionMap::clear() { @@ -99,19 +94,16 @@ JurisdictionMap::JurisdictionMap() : _rootOctalCode(NULL) { std::vector emptyEndNodes; init(rootCode, emptyEndNodes); - //printf("JurisdictionMap DEFAULT CONSTRUCTOR %p\n",this); } JurisdictionMap::JurisdictionMap(const char* filename) : _rootOctalCode(NULL) { clear(); // clean up our own memory readFromFile(filename); - //printf("JurisdictionMap INI FILE CONSTRUCTOR %p\n",this); } JurisdictionMap::JurisdictionMap(unsigned char* rootOctalCode, const std::vector& endNodes) : _rootOctalCode(NULL) { init(rootOctalCode, endNodes); - //printf("JurisdictionMap OCTCODE CONSTRUCTOR %p\n",this); } JurisdictionMap::JurisdictionMap(const char* rootHexCode, const char* endNodesHexCodes) { @@ -128,7 +120,6 @@ JurisdictionMap::JurisdictionMap(const char* rootHexCode, const char* endNodesHe //printOctalCode(endNodeOctcode); _endNodes.push_back(endNodeOctcode); } - //printf("JurisdictionMap HEX STRING CONSTRUCTOR %p\n",this); } diff --git a/libraries/voxels/src/JurisdictionMap.h b/libraries/voxels/src/JurisdictionMap.h index 50aabb65f2..2b72596f76 100644 --- a/libraries/voxels/src/JurisdictionMap.h +++ b/libraries/voxels/src/JurisdictionMap.h @@ -25,7 +25,7 @@ public: JurisdictionMap(const JurisdictionMap& other); // copy constructor // standard assignment - JurisdictionMap& operator= (JurisdictionMap const &other); // copy assignment + JurisdictionMap& operator=(const JurisdictionMap& other); // copy assignment #ifdef HAS_MOVE_SEMANTICS // move constructor and assignment @@ -48,7 +48,7 @@ public: unsigned char* getEndNodeOctalCode(int index) const { return _endNodes[index]; } int getEndNodeCount() const { return _endNodes.size(); } - void copyContents(unsigned char* rootCodeIn, const std::vector endNodesIn); + void copyContents(unsigned char* rootCodeIn, const std::vector& endNodesIn); private: void copyContents(const JurisdictionMap& other); // use assignment instead From 74100ad043723956126ecd14941a2ae0b3bfa124 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Thu, 15 Aug 2013 12:01:50 -0700 Subject: [PATCH 26/26] make threads sleep --- libraries/shared/src/PacketSender.cpp | 4 ++++ libraries/shared/src/ReceivedPacketProcessor.cpp | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/libraries/shared/src/PacketSender.cpp b/libraries/shared/src/PacketSender.cpp index ec3a6cff7f..4c150454a3 100644 --- a/libraries/shared/src/PacketSender.cpp +++ b/libraries/shared/src/PacketSender.cpp @@ -29,6 +29,10 @@ void PacketSender::queuePacket(sockaddr& address, unsigned char* packetData, ssi } bool PacketSender::process() { + if (_packets.size() == 0) { + const uint64_t SEND_THREAD_SLEEP_INTERVAL = (1000 * 1000)/60; // check at 60fps + usleep(SEND_THREAD_SLEEP_INTERVAL); + } while (_packets.size() > 0) { NetworkPacket& packet = _packets.front(); diff --git a/libraries/shared/src/ReceivedPacketProcessor.cpp b/libraries/shared/src/ReceivedPacketProcessor.cpp index 8ab76397a2..3b6ccf5a98 100644 --- a/libraries/shared/src/ReceivedPacketProcessor.cpp +++ b/libraries/shared/src/ReceivedPacketProcessor.cpp @@ -18,6 +18,10 @@ void ReceivedPacketProcessor::queuePacket(sockaddr& address, unsigned char* pack } bool ReceivedPacketProcessor::process() { + if (_packets.size() == 0) { + const uint64_t RECEIVED_THREAD_SLEEP_INTERVAL = (1000 * 1000)/60; // check at 60fps + usleep(RECEIVED_THREAD_SLEEP_INTERVAL); + } while (_packets.size() > 0) { NetworkPacket& packet = _packets.front(); processPacket(packet.getAddress(), packet.getData(), packet.getLength());