From a38e7b0431f55958818d3855ff0a9b61838002a9 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 30 Jul 2015 17:59:27 -0700 Subject: [PATCH] cleanup SendQueue start and UDTTest bind --- libraries/networking/src/udt/SendQueue.cpp | 43 ++++++++++------------ libraries/networking/src/udt/SendQueue.h | 4 +- tools/udt-test/src/UDTTest.cpp | 2 +- 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/libraries/networking/src/udt/SendQueue.cpp b/libraries/networking/src/udt/SendQueue.cpp index 66051ad1c4..5e3d1cc6e4 100644 --- a/libraries/networking/src/udt/SendQueue.cpp +++ b/libraries/networking/src/udt/SendQueue.cpp @@ -31,6 +31,9 @@ std::unique_ptr SendQueue::create(Socket* socket, HifiSockAddr dest) // Setup queue private thread QThread* thread = new QThread(); thread->setObjectName("Networking: SendQueue " + dest.objectName()); // Name thread for easier debug + + connect(thread, &QThread::started, queue.get(), &SendQueue::run); + connect(queue.get(), &QObject::destroyed, thread, &QThread::quit); // Thread auto cleanup connect(thread, &QThread::finished, thread, &QThread::deleteLater); // Thread auto cleanup @@ -45,7 +48,7 @@ SendQueue::SendQueue(Socket* socket, HifiSockAddr dest) : _socket(socket), _destination(dest) { - _packetSendPeriod = DEFAULT_SEND_PERIOD; + } void SendQueue::queuePacket(std::unique_ptr packet) { @@ -53,8 +56,8 @@ void SendQueue::queuePacket(std::unique_ptr packet) { QWriteLocker locker(&_packetsLock); _packets.push_back(std::move(packet)); } - if (!_isRunning) { - run(); + if (!this->thread()->isRunning()) { + this->thread()->start(); } } @@ -116,13 +119,6 @@ SequenceNumber SendQueue::getNextSequenceNumber() { } 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; while (_isRunning) { @@ -166,7 +162,6 @@ void SendQueue::run() { } if (nextPacket) { - qDebug() << "the next packet is" << nextPacket->getDataSize() << "bytes"; bool shouldSendSecondOfPair = false; if (!hasResend) { @@ -183,11 +178,13 @@ void SendQueue::run() { 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"); + { + // 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(); @@ -206,18 +203,18 @@ void SendQueue::run() { 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"); + { + // 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 diff --git a/libraries/networking/src/udt/SendQueue.h b/libraries/networking/src/udt/SendQueue.h index 3915414e1f..f8ffa91c96 100644 --- a/libraries/networking/src/udt/SendQueue.h +++ b/libraries/networking/src/udt/SendQueue.h @@ -37,7 +37,7 @@ class SendQueue : public QObject { Q_OBJECT public: - static const int DEFAULT_SEND_PERIOD = 16 * 1000; // 16ms, in microseconds + static const int DEFAULT_SEND_PERIOD = 1; // in microseconds static std::unique_ptr create(Socket* socket, HifiSockAddr dest); @@ -86,7 +86,7 @@ private: SequenceNumber _currentSequenceNumber; // Last sequence number sent out std::atomic _atomicCurrentSequenceNumber; // Atomic for last sequence number sent out - std::atomic _packetSendPeriod { 0 }; // Interval between two packet send event in microseconds + std::atomic _packetSendPeriod { DEFAULT_SEND_PERIOD }; // Interval between two packet send event in microseconds std::chrono::high_resolution_clock::time_point _lastSendTimestamp; // Record last time of packet departure std::atomic _isRunning { false }; diff --git a/tools/udt-test/src/UDTTest.cpp b/tools/udt-test/src/UDTTest.cpp index 2bfcd48a76..9f149dce52 100644 --- a/tools/udt-test/src/UDTTest.cpp +++ b/tools/udt-test/src/UDTTest.cpp @@ -49,7 +49,7 @@ UDTTest::UDTTest(int& argc, char** argv) : // randomize the seed for packet size randomization srand(time(NULL)); - _socket.bind(QHostAddress::LocalHost, _argumentParser.value(PORT_OPTION).toUInt()); + _socket.bind(QHostAddress::AnyIPv4, _argumentParser.value(PORT_OPTION).toUInt()); qDebug() << "Test socket is listening on" << _socket.localPort(); if (_argumentParser.isSet(TARGET_OPTION)) {