fixed PPS bugs in threaded version of PacketSender class

This commit is contained in:
ZappoMan 2013-10-07 17:34:41 -07:00
parent c6981d1912
commit 8baa863242
2 changed files with 9 additions and 4 deletions

View file

@ -85,7 +85,8 @@ bool PacketSender::process() {
int packetsLeft = _packets.size();
bool keepGoing = packetsLeft > 0;
while (keepGoing) {
uint64_t SEND_INTERVAL_USECS = (_packetsPerSecond == 0) ? USECS_PER_SECOND : (USECS_PER_SECOND / _packetsPerSecond);
NetworkPacket& packet = _packets.front();
// send the packet through the NodeList...
@ -93,7 +94,7 @@ bool PacketSender::process() {
nodeSocket->send(&packet.getAddress(), packet.getData(), packet.getLength());
packetsThisCall++;
if (_notify) {
_notify->packetSentNotification(packet.getLength());
}
@ -110,11 +111,15 @@ bool PacketSender::process() {
// dynamically sleep until we need to fire off the next set of voxels we only sleep in threaded mode
if (keepGoing) {
now = usecTimestampNow();
uint64_t elapsed = now - _lastSendTime;
int usecToSleep = std::max(SEND_INTERVAL_USECS, SEND_INTERVAL_USECS - elapsed);
int usecToSleep = SEND_INTERVAL_USECS - elapsed;
// we only sleep in non-threaded mode
if (usecToSleep > 0) {
if (usecToSleep > SEND_INTERVAL_USECS) {
usecToSleep = SEND_INTERVAL_USECS;
}
usleep(usecToSleep);
}
}

View file

@ -36,7 +36,7 @@ public:
/// \thread any thread, typically the application thread
void queuePacketForSending(sockaddr& address, unsigned char* packetData, ssize_t packetLength);
void setPacketsPerSecond(int packetsPerSecond) { _packetsPerSecond = std::min(MINIMUM_PACKETS_PER_SECOND, packetsPerSecond); }
void setPacketsPerSecond(int packetsPerSecond) { _packetsPerSecond = std::max(MINIMUM_PACKETS_PER_SECOND, packetsPerSecond); }
int getPacketsPerSecond() const { return _packetsPerSecond; }
void setPacketSenderNotify(PacketSenderNotify* notify) { _notify = notify; }