bool ReceivedPacketProcessor::process() { quint64 now = usecTimestampNow(); quint64 sinceLastWindow = now - _lastWindowAt; if (sinceLastWindow > USECS_PER_SECOND) { lock(); float secondsSinceLastWindow = sinceLastWindow / USECS_PER_SECOND; float incomingPacketsPerSecondInWindow = (float)_lastWindowIncomingPackets / secondsSinceLastWindow; _incomingPPS.updateAverage(incomingPacketsPerSecondInWindow); float processedPacketsPerSecondInWindow = (float)_lastWindowProcessedPackets / secondsSinceLastWindow; _processedPPS.updateAverage(processedPacketsPerSecondInWindow); _lastWindowAt = now; _lastWindowIncomingPackets = 0; _lastWindowProcessedPackets = 0; unlock(); } if (_packets.size() == 0) { _waitingOnPacketsMutex.lock(); _hasPackets.wait(&_waitingOnPacketsMutex, getMaxWait()); _waitingOnPacketsMutex.unlock(); } preProcess(); if (!_packets.size()) { return isStillRunning(); } lock(); std::list<NodeSharedReceivedMessagePair> currentPackets; currentPackets.swap(_packets); unlock(); for(auto& packetPair : currentPackets) { processPacket(packetPair.second, packetPair.first); _lastWindowProcessedPackets++; midProcess(); } lock(); for(auto& packetPair : currentPackets) { _nodePacketCounts[packetPair.first->getUUID()]--; } unlock(); postProcess(); return isStillRunning(); // keep running till they terminate us }
bool ReceivedPacketProcessor::process() { if (_packets.size() == 0) { _waitingOnPacketsMutex.lock(); _hasPackets.wait(&_waitingOnPacketsMutex, getMaxWait()); _waitingOnPacketsMutex.unlock(); } preProcess(); while (_packets.size() > 0) { lock(); // lock to make sure nothing changes on us NetworkPacket& packet = _packets.front(); // get the oldest packet NetworkPacket temporary = packet; // make a copy of the packet in case the vector is resized on us _packets.erase(_packets.begin()); // remove the oldest packet if (!temporary.getNode().isNull()) { _nodePacketCounts[temporary.getNode()->getUUID()]--; } unlock(); // let others add to the packets processPacket(temporary.getNode(), temporary.getByteArray()); // process our temporary copy midProcess(); } postProcess(); return isStillRunning(); // keep running till they terminate us }