History

move datagram processing back to separate thread

Stephen Birarda committed 12 years ago

Commit 7efc9e21 parent 0e6faec6 files · +176 −105Browse files

Showing the changes to interface/src/Application.cpp (1 of 6 files). Show all

Unified · Split

interface/src/Application.cpp+28−98
@@ -108,6 +108,8 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : · 107 unmodified lines
108 QApplication(argc, argv),108 QApplication(argc, argv),
109 _window(new QMainWindow(desktop())),109 _window(new QMainWindow(desktop())),
110 _glWidget(new GLCanvas()),110 _glWidget(new GLCanvas()),
111 _nodeThread(new QThread(this)),
112 _datagramProcessor(new DatagramProcessor()),
111 _frameCount(0),113 _frameCount(0),
112 _fps(120.0f),114 _fps(120.0f),
113 _justStarted(true),115 _justStarted(true),
@@ -145,10 +147,8 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : · 31 unmodified lines
145 _voxelHideShowThread(&_voxels),147 _voxelHideShowThread(&_voxels),
146 _voxelEditSender(this),148 _voxelEditSender(this),
147 _particleEditSender(this),149 _particleEditSender(this),
148 _packetCount(0),
149 _packetsPerSecond(0),150 _packetsPerSecond(0),
150 _bytesPerSecond(0),151 _bytesPerSecond(0),
151 _bytesCount(0),
152 _recentMaxPackets(0),152 _recentMaxPackets(0),
153 _resetRecentMaxPacketsSoon(true),153 _resetRecentMaxPacketsSoon(true),
154 _swatch(NULL),154 _swatch(NULL),
@@ -173,11 +173,21 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : · 18 unmodified lines
173 if (portStr) {173 if (portStr) {
174 listenPort = atoi(portStr);174 listenPort = atoi(portStr);
175 }175 }
176 176
177 // put the NodeList and datagram processing on the node thread
177 NodeList* nodeList = NodeList::createInstance(NODE_TYPE_AGENT, listenPort);178 NodeList* nodeList = NodeList::createInstance(NODE_TYPE_AGENT, listenPort);
178 179
179 // connect our processDatagrams slot to the QUDPSocket readyRead() signal180 nodeList->moveToThread(_nodeThread);
180 connect(&nodeList->getNodeSocket(), SIGNAL(readyRead()), SLOT(processDatagrams()));181 _datagramProcessor->moveToThread(_nodeThread);
182
183 // connect the DataProcessor processDatagrams slot to the QUDPSocket readyRead() signal
184 connect(&nodeList->getNodeSocket(), SIGNAL(readyRead()), _datagramProcessor, SLOT(processDatagrams()));
185
186 // make sure the node thread is given highest priority
187 _nodeThread->setPriority(QThread::TimeCriticalPriority);
188
189 // start the nodeThread so its event loop is running
190 _nodeThread->start();
181 191
182 // put the audio processing on a separate thread192 // put the audio processing on a separate thread
183 QThread* audioThread = new QThread(this);193 QThread* audioThread = new QThread(this);
@@ -192,7 +202,7 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : · 8 unmodified lines
192 connect(nodeList, SIGNAL(nodeKilled(SharedNodePointer)), SLOT(nodeKilled(SharedNodePointer)));202 connect(nodeList, SIGNAL(nodeKilled(SharedNodePointer)), SLOT(nodeKilled(SharedNodePointer)));
193 connect(nodeList, SIGNAL(nodeAdded(SharedNodePointer)), &_voxels, SLOT(nodeAdded(SharedNodePointer)));203 connect(nodeList, SIGNAL(nodeAdded(SharedNodePointer)), &_voxels, SLOT(nodeAdded(SharedNodePointer)));
194 connect(nodeList, SIGNAL(nodeKilled(SharedNodePointer)), &_voxels, SLOT(nodeKilled(SharedNodePointer)));204 connect(nodeList, SIGNAL(nodeKilled(SharedNodePointer)), &_voxels, SLOT(nodeKilled(SharedNodePointer)));
195 205
196 // read the ApplicationInfo.ini file for Name/Version/Domain information206 // read the ApplicationInfo.ini file for Name/Version/Domain information
197 QSettings applicationInfo("resources/info/ApplicationInfo.ini", QSettings::IniFormat);207 QSettings applicationInfo("resources/info/ApplicationInfo.ini", QSettings::IniFormat);
198 208
@@ -225,8 +235,10 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : · 26 unmodified lines
225 NODE_TYPE_PARTICLE_SERVER, NODE_TYPE_METAVOXEL_SERVER};235 NODE_TYPE_PARTICLE_SERVER, NODE_TYPE_METAVOXEL_SERVER};
226 nodeList->setNodeTypesOfInterest(nodeTypesOfInterest, sizeof(nodeTypesOfInterest));236 nodeList->setNodeTypesOfInterest(nodeTypesOfInterest, sizeof(nodeTypesOfInterest));
227 237
238 // move the silentNodeTimer to the _nodeThread
228 QTimer* silentNodeTimer = new QTimer(this);239 QTimer* silentNodeTimer = new QTimer(this);
229 connect(silentNodeTimer, SIGNAL(timeout()), nodeList, SLOT(removeSilentNodes()));240 connect(silentNodeTimer, SIGNAL(timeout()), nodeList, SLOT(removeSilentNodes()));
241 silentNodeTimer->moveToThread(_nodeThread);
230 silentNodeTimer->start(NODE_SILENCE_THRESHOLD_USECS / 1000);242 silentNodeTimer->start(NODE_SILENCE_THRESHOLD_USECS / 1000);
231 243
232 QString cachePath = QStandardPaths::writableLocation(QStandardPaths::DataLocation);244 QString cachePath = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
@@ -273,6 +285,10 @@ Application::~Application() { · 40 unmodified lines
273 285
274 // make sure we don't call the idle timer any more286 // make sure we don't call the idle timer any more
275 delete idleTimer;287 delete idleTimer;
288
289 // ask the datagram processing thread to quit and wait until it is done
290 _nodeThread->thread()->quit();
291 _nodeThread->thread()->wait();
276 292
277 // ask the audio thread to quit and wait until it is done293 // ask the audio thread to quit and wait until it is done
278 _audio.thread()->quit();294 _audio.thread()->quit();
@@ -1312,11 +1328,12 @@ void Application::timer() { · 1033 unmodified lines
1312 }1328 }
1313 1329
1314 _fps = (float)_frameCount / ((float)diffclock(&_timerStart, &_timerEnd) / 1000.f);1330 _fps = (float)_frameCount / ((float)diffclock(&_timerStart, &_timerEnd) / 1000.f);
1315 _packetsPerSecond = (float)_packetCount / ((float)diffclock(&_timerStart, &_timerEnd) / 1000.f);1331
1316 _bytesPerSecond = (float)_bytesCount / ((float)diffclock(&_timerStart, &_timerEnd) / 1000.f);1332 _packetsPerSecond = (float) _datagramProcessor->getPacketCount() / ((float)diffclock(&_timerStart, &_timerEnd) / 1000.f);
1333 _bytesPerSecond = (float) _datagramProcessor->getByteCount() / ((float)diffclock(&_timerStart, &_timerEnd) / 1000.f);
1317 _frameCount = 0;1334 _frameCount = 0;
1318 _packetCount = 0;1335
1319 _bytesCount = 0;1336 _datagramProcessor->resetCounters();
1320 1337
1321 gettimeofday(&_timerStart, NULL);1338 gettimeofday(&_timerStart, NULL);
1322 1339
@@ -4052,93 +4069,6 @@ int Application::parseOctreeStats(unsigned char* messageData, ssize_t messageLen · 2729 unmodified lines
4052 return statsMessageLength;4069 return statsMessageLength;
4053}4070}
4054 4071
4055// Receive packets from other nodes/servers and decide what to do with them!
4056void Application::processDatagrams() {
4057 PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings),
4058 "Application::networkReceive()");
4059
4060 HifiSockAddr senderSockAddr;
4061 ssize_t bytesReceived;
4062
4063 while (NodeList::getInstance()->getNodeSocket().hasPendingDatagrams() &&
4064 (bytesReceived = NodeList::getInstance()->getNodeSocket().readDatagram((char*) _incomingPacket,
4065 MAX_PACKET_SIZE,
4066 senderSockAddr.getAddressPointer(),
4067 senderSockAddr.getPortPointer()))) {
4068
4069 _packetCount++;
4070 _bytesCount += bytesReceived;
4071
4072 if (packetVersionMatch(_incomingPacket)) {
4073 // only process this packet if we have a match on the packet version
4074 switch (_incomingPacket[0]) {
4075 case PACKET_TYPE_TRANSMITTER_DATA_V2:
4076 // V2 = IOS transmitter app
4077 _myTransmitter.processIncomingData(_incomingPacket, bytesReceived);
4078
4079 break;
4080 case PACKET_TYPE_MIXED_AUDIO:
4081 QMetaObject::invokeMethod(&_audio, "addReceivedAudioToBuffer", Qt::QueuedConnection,
4082 Q_ARG(QByteArray, QByteArray((char*) _incomingPacket, bytesReceived)));
4083 break;
4084
4085 case PACKET_TYPE_PARTICLE_ADD_RESPONSE:
4086 // this will keep creatorTokenIDs to IDs mapped correctly
4087 Particle::handleAddParticleResponse(_incomingPacket, bytesReceived);
4088 break;
4089
4090 case PACKET_TYPE_PARTICLE_DATA:
4091 case PACKET_TYPE_PARTICLE_ERASE:
4092 case PACKET_TYPE_VOXEL_DATA:
4093 case PACKET_TYPE_VOXEL_ERASE:
4094 case PACKET_TYPE_OCTREE_STATS:
4095 case PACKET_TYPE_ENVIRONMENT_DATA: {
4096 PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings),
4097 "Application::networkReceive()... _voxelProcessor.queueReceivedPacket()");
4098
4099 bool wantExtraDebugging = getLogger()->extraDebugging();
4100 if (wantExtraDebugging && _incomingPacket[0] == PACKET_TYPE_VOXEL_DATA) {
4101 int numBytesPacketHeader = numBytesForPacketHeader(_incomingPacket);
4102 unsigned char* dataAt = _incomingPacket + numBytesPacketHeader;
4103 dataAt += sizeof(VOXEL_PACKET_FLAGS);
4104 VOXEL_PACKET_SEQUENCE sequence = (*(VOXEL_PACKET_SEQUENCE*)dataAt);
4105 dataAt += sizeof(VOXEL_PACKET_SEQUENCE);
4106 VOXEL_PACKET_SENT_TIME sentAt = (*(VOXEL_PACKET_SENT_TIME*)dataAt);
4107 dataAt += sizeof(VOXEL_PACKET_SENT_TIME);
4108 VOXEL_PACKET_SENT_TIME arrivedAt = usecTimestampNow();
4109 int flightTime = arrivedAt - sentAt;
4110
4111 printf("got PACKET_TYPE_VOXEL_DATA, sequence:%d flightTime:%d\n", sequence, flightTime);
4112 }
4113
4114 // add this packet to our list of voxel packets and process them on the voxel processing
4115 _voxelProcessor.queueReceivedPacket(senderSockAddr, _incomingPacket, bytesReceived);
4116 break;
4117 }
4118 case PACKET_TYPE_METAVOXEL_DATA:
4119 _metavoxels.processData(QByteArray((const char*) _incomingPacket, bytesReceived),
4120 senderSockAddr);
4121 break;
4122 case PACKET_TYPE_BULK_AVATAR_DATA:
4123 NodeList::getInstance()->processBulkNodeData(senderSockAddr,
4124 _incomingPacket,
4125 bytesReceived);
4126 getInstance()->_bandwidthMeter.inputStream(BandwidthMeter::AVATARS).updateValue(bytesReceived);
4127 break;
4128 case PACKET_TYPE_DATA_SERVER_GET:
4129 case PACKET_TYPE_DATA_SERVER_PUT:
4130 case PACKET_TYPE_DATA_SERVER_SEND:
4131 case PACKET_TYPE_DATA_SERVER_CONFIRM:
4132 DataServerClient::processMessageFromDataServer(_incomingPacket, bytesReceived);
4133 break;
4134 default:
4135 NodeList::getInstance()->processNodeData(senderSockAddr, _incomingPacket, bytesReceived);
4136 break;
4137 }
4138 }
4139 }
4140}
4141
4142void Application::packetSentNotification(ssize_t length) {4072void Application::packetSentNotification(ssize_t length) {
4143 _bandwidthMeter.outputStream(BandwidthMeter::VOXELS).updateValue(length);4073 _bandwidthMeter.outputStream(BandwidthMeter::VOXELS).updateValue(length);
4144}4074}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t