From 98a53cbd723b484a2d223f2cbf184fd486ddf2dd Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Fri, 31 Jul 2015 09:56:50 -0700 Subject: [PATCH] don't require a sendQueue for control packet sending --- libraries/networking/src/udt/Connection.cpp | 18 ++++++++++-------- libraries/networking/src/udt/Connection.h | 6 +++--- libraries/networking/src/udt/SendQueue.cpp | 8 ++++---- libraries/networking/src/udt/SendQueue.h | 5 ++--- libraries/networking/src/udt/Socket.cpp | 10 ++++++++++ libraries/networking/src/udt/Socket.h | 1 + 6 files changed, 30 insertions(+), 18 deletions(-) diff --git a/libraries/networking/src/udt/Connection.cpp b/libraries/networking/src/udt/Connection.cpp index b315611cf3..b6b3f9f12a 100644 --- a/libraries/networking/src/udt/Connection.cpp +++ b/libraries/networking/src/udt/Connection.cpp @@ -30,6 +30,8 @@ Connection::Connection(Socket* parentSocket, HifiSockAddr destination, unique_pt _destination(destination), _congestionControl(move(congestionControl)) { + Q_ASSERT_X(socket, "Connection::Connection", "Must be called with a valid Socket*"); + // setup default SYN, RTT and RTT Variance based on the SYN interval in CongestionControl object _synInterval = _congestionControl->synInterval(); _rtt = _synInterval * 10; @@ -132,8 +134,8 @@ void Connection::sendACK(bool wasCausedBySyncTimeout) { lastACKSendTime = high_resolution_clock::now(); } - // have the send queue send off our packet - _sendQueue->sendPacket(*ackPacket); + // have the socket send off our packet + _parentSocket->writeBasePacket(*ackPacket, _destination); // write this ACK to the map of sent ACKs _sentACKs[_currentACKSubSequenceNumber] = { nextACKNumber, currentTime }; @@ -162,8 +164,8 @@ void Connection::sendLightACK() { // pack in the ACK lightACKPacket->writePrimitive(nextACKNumber); - // have the send queue send off our packet immediately - _sendQueue->sendPacket(*lightACKPacket); + // have the socket send off our packet immediately + _parentSocket->writeBasePacket(*lightACKPacket, _destination); _stats.recordSentLightACK(); } @@ -197,8 +199,8 @@ void Connection::sendNAK(SequenceNumber sequenceNumberRecieved) { lossReport->writePrimitive(sequenceNumberRecieved - 1); } - // have the send queue send off our packet immediately - _sendQueue->sendPacket(*lossReport); + // have the parent socket send off our packet immediately + _parentSocket->writeBasePacket(*lossReport, _destination); // record our last NAK time _lastNAKTime = high_resolution_clock::now(); @@ -215,8 +217,8 @@ void Connection::sendTimeoutNAK() { // Pack in the lost sequence numbers _lossList.write(*lossListPacket); - // have our SendQueue send off this control packet - _sendQueue->sendPacket(*lossListPacket); + // have our parent socket send off this control packet + _parentSocket->writeBasePacket(*lossListPacket, _destination); // record this as the last NAK time _lastNAKTime = high_resolution_clock::now(); diff --git a/libraries/networking/src/udt/Connection.h b/libraries/networking/src/udt/Connection.h index 606359e6f5..71cae880d0 100644 --- a/libraries/networking/src/udt/Connection.h +++ b/libraries/networking/src/udt/Connection.h @@ -98,11 +98,11 @@ private: HifiSockAddr _destination; PacketTimeWindow _receiveWindow { 16, 64 }; // Window of interval between packets (16) and probes (64) for bandwidth and receive speed - - std::unique_ptr _sendQueue; - + std::unique_ptr _congestionControl; + std::unique_ptr _sendQueue; + // Data packet stat collection int _totalReceivedDataPackets { 0 }; int _packetsSinceACK { 0 }; // The number of packets that have been received during the current ACK interval diff --git a/libraries/networking/src/udt/SendQueue.cpp b/libraries/networking/src/udt/SendQueue.cpp index 249cb6e069..c092d089c4 100644 --- a/libraries/networking/src/udt/SendQueue.cpp +++ b/libraries/networking/src/udt/SendQueue.cpp @@ -28,6 +28,8 @@ using namespace std::chrono; std::unique_ptr SendQueue::create(Socket* socket, HifiSockAddr dest) { auto queue = std::unique_ptr(new SendQueue(socket, dest)); + Q_ASSERT_X(socket, "SendQueue::create", "Must be called with a valid Socket*"); + // Setup queue private thread QThread* thread = new QThread(); thread->setObjectName("Networking: SendQueue " + dest.objectName()); // Name thread for easier debug @@ -65,10 +67,8 @@ void SendQueue::stop() { _isRunning = false; } -void SendQueue::sendPacket(const BasePacket& packet) { - if (_socket) { - _socket->writeDatagram(packet.getData(), packet.getDataSize(), _destination); - } +void SendQueue::sendPacket(const Packet& packet) { + _socket->writeDatagram(packet.getData(), packet.getDataSize(), _destination); } void SendQueue::ack(SequenceNumber ack) { diff --git a/libraries/networking/src/udt/SendQueue.h b/libraries/networking/src/udt/SendQueue.h index 9c8d6878c0..4a4cf1ffea 100644 --- a/libraries/networking/src/udt/SendQueue.h +++ b/libraries/networking/src/udt/SendQueue.h @@ -49,9 +49,6 @@ public: int getPacketSendPeriod() const { return _packetSendPeriod; } void setPacketSendPeriod(int newPeriod) { _packetSendPeriod = newPeriod; } - // Send a packet through the socket - void sendPacket(const BasePacket& packet); - public slots: void stop(); @@ -70,6 +67,8 @@ private: SendQueue(SendQueue& other) = delete; SendQueue(SendQueue&& other) = delete; + void sendPacket(const Packet& packet); + // Increments current sequence number and return it SequenceNumber getNextSequenceNumber(); diff --git a/libraries/networking/src/udt/Socket.cpp b/libraries/networking/src/udt/Socket.cpp index ba0e196669..99cdb06b29 100644 --- a/libraries/networking/src/udt/Socket.cpp +++ b/libraries/networking/src/udt/Socket.cpp @@ -74,6 +74,16 @@ void Socket::setSystemBufferSizes() { } } +qint64 Socket::writeBasePacket(const udt::BasePacket& packet, const HifiSockAddr &sockAddr) { + // Since this is a base packet we have no way to know if this is reliable or not - we just fire it off + + // this should not be called with an instance of Packet + Q_ASSERT_X(!dynamic_cast(&packet), + "Socket::writeBasePacket", "Cannot send a Packet/NLPacket via writeBasePacket"); + + return writeDatagram(packet.getData(), packet.getDataSize(), sockAddr); +} + qint64 Socket::writePacket(const Packet& packet, const HifiSockAddr& sockAddr) { Q_ASSERT_X(!packet.isReliable(), "Socket::writePacket", "Cannot send a reliable packet unreliably"); diff --git a/libraries/networking/src/udt/Socket.h b/libraries/networking/src/udt/Socket.h index 8c8ecb32b2..4f947e4043 100644 --- a/libraries/networking/src/udt/Socket.h +++ b/libraries/networking/src/udt/Socket.h @@ -45,6 +45,7 @@ public: quint16 localPort() const { return _udpSocket.localPort(); } // Simple functions writing to the socket with no processing + qint64 writeBasePacket(const BasePacket& packet, const HifiSockAddr& sockAddr); qint64 writePacket(const Packet& packet, const HifiSockAddr& sockAddr); qint64 writePacket(std::unique_ptr packet, const HifiSockAddr& sockAddr); qint64 writeDatagram(const char* data, qint64 size, const HifiSockAddr& sockAddr);