From 1cd16da83edbd92105ed89dfb20aafebd3c4d0e0 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 15 Mar 2016 15:28:55 -0700 Subject: [PATCH 1/2] do sleep timing off when next packet would have been --- libraries/networking/src/udt/SendQueue.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/libraries/networking/src/udt/SendQueue.cpp b/libraries/networking/src/udt/SendQueue.cpp index 5c6db5adf3..3149c5c7bf 100644 --- a/libraries/networking/src/udt/SendQueue.cpp +++ b/libraries/networking/src/udt/SendQueue.cpp @@ -280,11 +280,11 @@ void SendQueue::run() { // Once we're here we've either received the handshake ACK or it's going to be time to re-send a handshake. // Either way let's continue processing - no packets will be sent if no handshake ACK has been received. } - + + // Keep an HRC to know when the next packet should have been + auto nextPacketTimestamp = p_high_resolution_clock::now(); + while (_state == State::Running) { - // Record how long the loop takes to execute - const auto loopStartTimestamp = p_high_resolution_clock::now(); - bool sentAPacket = maybeResendPacket(); // if we didn't find a packet to re-send AND we think we can fit a new packet on the wire @@ -304,9 +304,15 @@ void SendQueue::run() { return; } + // grab the current HRC timestamp + const auto now = p_high_resolution_clock::now(); + + // push the next packet timestamp forwards by the current packet send period + nextPacketTimestamp += std::chrono::microseconds(_packetSendPeriod); + // sleep as long as we need until next packet send, if we can - const auto loopEndTimestamp = p_high_resolution_clock::now(); - const auto timeToSleep = (loopStartTimestamp + std::chrono::microseconds(_packetSendPeriod)) - loopEndTimestamp; + const auto timeToSleep = std::chrono::duration_cast(nextPacketTimestamp - now); + std::this_thread::sleep_for(timeToSleep); } } From c94e2bdc8b71dcf1925edf76aaef1fa8095c5456 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 16 Mar 2016 08:53:31 -0700 Subject: [PATCH 2/2] grab the now timestamp only when needed --- libraries/networking/src/udt/SendQueue.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libraries/networking/src/udt/SendQueue.cpp b/libraries/networking/src/udt/SendQueue.cpp index 3149c5c7bf..8e5141b58a 100644 --- a/libraries/networking/src/udt/SendQueue.cpp +++ b/libraries/networking/src/udt/SendQueue.cpp @@ -29,6 +29,7 @@ #include "Socket.h" using namespace udt; +using namespace std::chrono; template class DoubleLock { @@ -303,15 +304,12 @@ void SendQueue::run() { if (_state != State::Running || isInactive(sentAPacket)) { return; } - - // grab the current HRC timestamp - const auto now = p_high_resolution_clock::now(); // push the next packet timestamp forwards by the current packet send period nextPacketTimestamp += std::chrono::microseconds(_packetSendPeriod); // sleep as long as we need until next packet send, if we can - const auto timeToSleep = std::chrono::duration_cast(nextPacketTimestamp - now); + const auto timeToSleep = duration_cast(nextPacketTimestamp - p_high_resolution_clock::now()); std::this_thread::sleep_for(timeToSleep); }