From 8baa863242ad0450089fcca4793a592d8bd49769 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 7 Oct 2013 17:34:41 -0700 Subject: [PATCH] fixed PPS bugs in threaded version of PacketSender class --- libraries/shared/src/PacketSender.cpp | 11 ++++++++--- libraries/shared/src/PacketSender.h | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/libraries/shared/src/PacketSender.cpp b/libraries/shared/src/PacketSender.cpp index 41d5db87d1..5f6de128ba 100644 --- a/libraries/shared/src/PacketSender.cpp +++ b/libraries/shared/src/PacketSender.cpp @@ -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); } } diff --git a/libraries/shared/src/PacketSender.h b/libraries/shared/src/PacketSender.h index 84ae6ae9ca..9197c3c7d7 100644 --- a/libraries/shared/src/PacketSender.h +++ b/libraries/shared/src/PacketSender.h @@ -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; }