From 1724e3c6320fa621dfe68b8bb1b0204d39b18168 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 30 Jul 2015 17:17:03 -0700 Subject: [PATCH] fix a couple of bugs in SendQueue --- libraries/networking/src/udt/SendQueue.cpp | 94 ++++++++++++---------- 1 file changed, 50 insertions(+), 44 deletions(-) diff --git a/libraries/networking/src/udt/SendQueue.cpp b/libraries/networking/src/udt/SendQueue.cpp index 7d0edf9d7c..d0ec6ee38e 100644 --- a/libraries/networking/src/udt/SendQueue.cpp +++ b/libraries/networking/src/udt/SendQueue.cpp @@ -62,7 +62,9 @@ void SendQueue::run() { // We need to make sure this is called on the right thread if (thread() != QThread::currentThread()) { QMetaObject::invokeMethod(this, "run", Qt::QueuedConnection); + return; } + _isRunning = true; // This will loop and sleep to send packets @@ -161,60 +163,64 @@ void SendQueue::loop() { } // If there is no packet to resend, grab the next one in the list - if (!nextPacket) { + if (!nextPacket && _packets.size() > 0) { QWriteLocker locker(&_packetsLock); nextPacket.swap(_packets.front()); _packets.pop_front(); } - bool shouldSendSecondOfPair = false; - - if (!hasResend) { - // if we're not re-sending a packet then need to check if this should be a packet pair - sequenceNumber = getNextSequenceNumber(); + if (nextPacket) { + bool shouldSendSecondOfPair = false; - // the first packet in the pair is every 16 (rightmost 16 bits = 0) packets - if (((uint32_t) sequenceNumber & 0xF) == 0) { - shouldSendSecondOfPair = true; + if (!hasResend) { + // if we're not re-sending a packet then need to check if this should be a packet pair + sequenceNumber = getNextSequenceNumber(); + + // the first packet in the pair is every 16 (rightmost 16 bits = 0) packets + if (((uint32_t) sequenceNumber & 0xF) == 0) { + shouldSendSecondOfPair = true; + } + } + + // Write packet's sequence number and send it off + nextPacket->writeSequenceNumber(sequenceNumber); + sendPacket(*nextPacket); + + // Insert the packet we have just sent in the sent list + QWriteLocker locker(&_sentLock); + _sentPackets[nextPacket->getSequenceNumber()].swap(nextPacket); + Q_ASSERT_X(!nextPacket, + "SendQueue::sendNextPacket()", "Overriden packet in sent list"); + + emit packetSent(); + + if (shouldSendSecondOfPair) { + std::unique_ptr pairedPacket; + + // we've detected we should send the second packet in a pair, do that now before sleeping + { + QWriteLocker locker(&_packetsLock); + pairedPacket.swap(_packets.front()); + _packets.pop_front(); + } + + if (pairedPacket) { + // write this packet's sequence number and send it off + pairedPacket->writeSequenceNumber(getNextSequenceNumber()); + sendPacket(*pairedPacket); + + // add the paired packet to the sent list + QWriteLocker locker(&_sentLock); + _sentPackets[pairedPacket->getSequenceNumber()].swap(pairedPacket); + Q_ASSERT_X(!pairedPacket, + "SendQueue::sendNextPacket()", "Overriden packet in sent list"); + + emit packetSent(); + } } } - // Write packet's sequence number and send it off - nextPacket->writeSequenceNumber(sequenceNumber); - sendPacket(*nextPacket); - // Insert the packet we have just sent in the sent list - QWriteLocker locker(&_sentLock); - _sentPackets[nextPacket->getSequenceNumber()].swap(nextPacket); - Q_ASSERT_X(!nextPacket, - "SendQueue::sendNextPacket()", "Overriden packet in sent list"); - - emit packetSent(); - - if (shouldSendSecondOfPair) { - std::unique_ptr pairedPacket; - - // we've detected we should send the second packet in a pair, do that now before sleeping - { - QWriteLocker locker(&_packetsLock); - pairedPacket.swap(_packets.front()); - _packets.pop_front(); - } - - if (pairedPacket) { - // write this packet's sequence number and send it off - pairedPacket->writeSequenceNumber(getNextSequenceNumber()); - sendPacket(*pairedPacket); - - // add the paired packet to the sent list - QWriteLocker locker(&_sentLock); - _sentPackets[pairedPacket->getSequenceNumber()].swap(pairedPacket); - Q_ASSERT_X(!pairedPacket, - "SendQueue::sendNextPacket()", "Overriden packet in sent list"); - - emit packetSent(); - } - } } // since we're a while loop, give the thread a chance to process events