From 796dfee687b1799035a77ff480146d26f1162911 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 20 Jul 2015 17:28:04 -0700 Subject: [PATCH] udt namespace fixes and NLPacket operator fixes --- ice-server/src/IceServer.cpp | 6 +++--- ice-server/src/IceServer.h | 2 +- libraries/networking/src/NLPacket.cpp | 11 ++++++++--- libraries/networking/src/NLPacket.h | 2 +- libraries/networking/src/NLPacketList.cpp | 2 +- libraries/networking/src/NLPacketList.h | 4 ++-- libraries/networking/src/PacketReceiver.cpp | 2 +- libraries/networking/src/PacketReceiver.h | 2 +- libraries/networking/src/udt/Packet.cpp | 2 ++ libraries/networking/src/udt/Packet.h | 6 +++++- libraries/networking/src/udt/PacketList.cpp | 2 ++ libraries/networking/src/udt/PacketList.h | 8 +++++++- libraries/networking/src/udt/Socket.cpp | 22 ++++++++++++++++++++- libraries/networking/src/udt/Socket.h | 4 ++++ 14 files changed, 59 insertions(+), 16 deletions(-) diff --git a/ice-server/src/IceServer.cpp b/ice-server/src/IceServer.cpp index f56fe9202f..1a08426081 100644 --- a/ice-server/src/IceServer.cpp +++ b/ice-server/src/IceServer.cpp @@ -55,7 +55,7 @@ void IceServer::processDatagrams() { _serverSocket.readDatagram(buffer.get(), packetSizeWithHeader, sendingSockAddr.getAddressPointer(), sendingSockAddr.getPortPointer()); - auto packet = Packet::fromReceivedPacket(std::move(buffer), packetSizeWithHeader, sendingSockAddr); + auto packet = udt::Packet::fromReceivedPacket(std::move(buffer), packetSizeWithHeader, sendingSockAddr); PacketType::Value packetType = packet->getType(); @@ -100,7 +100,7 @@ void IceServer::processDatagrams() { } } -SharedNetworkPeer IceServer::addOrUpdateHeartbeatingPeer(Packet& packet) { +SharedNetworkPeer IceServer::addOrUpdateHeartbeatingPeer(udt::Packet& packet) { // pull the UUID, public and private sock addrs for this peer QUuid senderUUID; @@ -133,7 +133,7 @@ SharedNetworkPeer IceServer::addOrUpdateHeartbeatingPeer(Packet& packet) { } void IceServer::sendPeerInformationPacket(const NetworkPeer& peer, const HifiSockAddr* destinationSockAddr) { - auto peerPacket = Packet::create(PacketType::ICEServerPeerInformation); + auto peerPacket = udt::Packet::create(PacketType::ICEServerPeerInformation); // get the byte array for this peer peerPacket->write(peer.toByteArray()); diff --git a/ice-server/src/IceServer.h b/ice-server/src/IceServer.h index 7820ae2e22..e91dc4e064 100644 --- a/ice-server/src/IceServer.h +++ b/ice-server/src/IceServer.h @@ -33,7 +33,7 @@ private slots: void clearInactivePeers(); private: - SharedNetworkPeer addOrUpdateHeartbeatingPeer(Packet& incomingPacket); + SharedNetworkPeer addOrUpdateHeartbeatingPeer(udt::Packet& incomingPacket); void sendPeerInformationPacket(const NetworkPeer& peer, const HifiSockAddr* destinationSockAddr); QUuid _id; diff --git a/libraries/networking/src/NLPacket.cpp b/libraries/networking/src/NLPacket.cpp index 7ccd33a1ed..5f24810d0b 100644 --- a/libraries/networking/src/NLPacket.cpp +++ b/libraries/networking/src/NLPacket.cpp @@ -99,12 +99,13 @@ NLPacket::NLPacket(std::unique_ptr packet) : } NLPacket::NLPacket(const NLPacket& other) : Packet(other) { - *this = other; + _sourceID = other._sourceID; + _verificationHash = other._verificationHash; } NLPacket& NLPacket::operator=(const NLPacket& other) { Packet::operator=(other); - + _sourceID = other._sourceID; _verificationHash = other._verificationHash; @@ -123,10 +124,14 @@ NLPacket::NLPacket(std::unique_ptr data, qint64 size, const HifiSockAddr& NLPacket::NLPacket(NLPacket&& other) : Packet(other) { - *this = std::move(other); + _sourceID = std::move(other._sourceID); + _verificationHash = std::move(other._verificationHash); } NLPacket& NLPacket::operator=(NLPacket&& other) { + + Packet::operator=(other); + _sourceID = std::move(other._sourceID); _verificationHash = std::move(other._verificationHash); diff --git a/libraries/networking/src/NLPacket.h b/libraries/networking/src/NLPacket.h index eab0fa3a99..aaf6c5df75 100644 --- a/libraries/networking/src/NLPacket.h +++ b/libraries/networking/src/NLPacket.h @@ -16,7 +16,7 @@ #include "udt/Packet.h" -class NLPacket : public Packet { +class NLPacket : public udt::Packet { Q_OBJECT public: static std::unique_ptr create(PacketType::Value type, qint64 size = -1); diff --git a/libraries/networking/src/NLPacketList.cpp b/libraries/networking/src/NLPacketList.cpp index 8c794cf5f8..6b7b53e8e9 100644 --- a/libraries/networking/src/NLPacketList.cpp +++ b/libraries/networking/src/NLPacketList.cpp @@ -19,7 +19,7 @@ NLPacketList::NLPacketList(PacketType::Value packetType, QByteArray extendedHead } -std::unique_ptr NLPacketList::createPacket() { +std::unique_ptr NLPacketList::createPacket() { return NLPacket::create(getType()); } diff --git a/libraries/networking/src/NLPacketList.h b/libraries/networking/src/NLPacketList.h index 28fbde9112..33a8316f95 100644 --- a/libraries/networking/src/NLPacketList.h +++ b/libraries/networking/src/NLPacketList.h @@ -14,7 +14,7 @@ #include "udt/PacketList.h" -class NLPacketList : public PacketList { +class NLPacketList : public udt::PacketList { public: NLPacketList(PacketType::Value packetType, QByteArray extendedHeader = QByteArray()); @@ -22,7 +22,7 @@ private: NLPacketList(const NLPacketList& other) = delete; NLPacketList& operator=(const NLPacketList& other) = delete; - virtual std::unique_ptr createPacket(); + virtual std::unique_ptr createPacket(); }; #endif // hifi_PacketList_h diff --git a/libraries/networking/src/PacketReceiver.cpp b/libraries/networking/src/PacketReceiver.cpp index a97b212355..eacdff9704 100644 --- a/libraries/networking/src/PacketReceiver.cpp +++ b/libraries/networking/src/PacketReceiver.cpp @@ -234,7 +234,7 @@ bool PacketReceiver::packetVersionMatch(const NLPacket& packet) { } } -void PacketReceiver::handleVerifiedPacket(std::unique_ptr packet) { +void PacketReceiver::handleVerifiedPacket(std::unique_ptr packet) { // if we're supposed to drop this packet then break out here if (_shouldDropPackets) { diff --git a/libraries/networking/src/PacketReceiver.h b/libraries/networking/src/PacketReceiver.h index ac38749a88..9851521250 100644 --- a/libraries/networking/src/PacketReceiver.h +++ b/libraries/networking/src/PacketReceiver.h @@ -45,7 +45,7 @@ public: bool registerListener(PacketType::Value type, QObject* listener, const char* slot); void unregisterListener(QObject* listener); - void handleVerifiedPacket(std::unique_ptr packet); + void handleVerifiedPacket(std::unique_ptr packet); signals: void dataReceived(quint8 channelType, int bytes); diff --git a/libraries/networking/src/udt/Packet.cpp b/libraries/networking/src/udt/Packet.cpp index 05b5f3fe50..b8c38f3b09 100644 --- a/libraries/networking/src/udt/Packet.cpp +++ b/libraries/networking/src/udt/Packet.cpp @@ -11,6 +11,8 @@ #include "Packet.h" +using namespace udt; + const qint64 Packet::PACKET_WRITE_ERROR = -1; qint64 Packet::localHeaderSize(PacketType::Value type) { diff --git a/libraries/networking/src/udt/Packet.h b/libraries/networking/src/udt/Packet.h index b4c53b8165..96e8f8c3d4 100644 --- a/libraries/networking/src/udt/Packet.h +++ b/libraries/networking/src/udt/Packet.h @@ -1,6 +1,6 @@ // // Packet.h -// libraries/networking/src +// libraries/networking/src/udt // // Created by Clement on 7/2/15. // Copyright 2015 High Fidelity, Inc. @@ -19,6 +19,8 @@ #include "../HifiSockAddr.h" #include "PacketHeaders.h" +namespace udt { + class Packet : public QIODevice { Q_OBJECT public: @@ -131,5 +133,7 @@ template qint64 Packet::writePrimitive(const T& data) { static_assert(!std::is_pointer::value, "T must not be a pointer"); return QIODevice::write(reinterpret_cast(&data), sizeof(T)); } + +} // namespace udt #endif // hifi_Packet_h diff --git a/libraries/networking/src/udt/PacketList.cpp b/libraries/networking/src/udt/PacketList.cpp index 5dcacfa9b1..47545eb910 100644 --- a/libraries/networking/src/udt/PacketList.cpp +++ b/libraries/networking/src/udt/PacketList.cpp @@ -15,6 +15,8 @@ #include "Packet.h" +using namespace udt; + PacketList::PacketList(PacketType::Value packetType, QByteArray extendedHeader) : _packetType(packetType), _extendedHeader(extendedHeader) diff --git a/libraries/networking/src/udt/PacketList.h b/libraries/networking/src/udt/PacketList.h index 30288dcaab..3d1166149c 100644 --- a/libraries/networking/src/udt/PacketList.h +++ b/libraries/networking/src/udt/PacketList.h @@ -18,6 +18,10 @@ #include "PacketHeaders.h" +class LimitedNodeList; + +namespace udt { + class Packet; class PacketList : public QIODevice { @@ -42,7 +46,7 @@ protected: virtual qint64 readData(char* data, qint64 maxSize) { return 0; } private: - friend class LimitedNodeList; + friend class ::LimitedNodeList; PacketList(const PacketList& other) = delete; PacketList& operator=(const PacketList& other) = delete; @@ -82,5 +86,7 @@ template std::unique_ptr PacketList::takeFront() { _packets.pop_front(); return std::unique_ptr(dynamic_cast(packet.release())); } + +} #endif // hifi_PacketList_h diff --git a/libraries/networking/src/udt/Socket.cpp b/libraries/networking/src/udt/Socket.cpp index 2e1e3481f9..8f766594c6 100644 --- a/libraries/networking/src/udt/Socket.cpp +++ b/libraries/networking/src/udt/Socket.cpp @@ -18,7 +18,7 @@ using namespace udt; Socket::Socket(QObject* parent) : QObject(parent) { - + connect(&_udpSocket, &QUdpSocket::readyRead, this, &Socket::readPendingDatagrams); } void Socket::rebind() { @@ -66,3 +66,23 @@ qint64 Socket::writeDatagram(const QByteArray& datagram, const HifiSockAddr& soc return bytesWritten; } + +void Socket::readPendingDatagrams() { + while (_udpSocket.hasPendingDatagrams()) { + // setup a HifiSockAddr to read into + HifiSockAddr senderSockAddr; + + // setup a buffer to read the packet into + int packetSizeWithHeader = _udpSocket.pendingDatagramSize(); + std::unique_ptr buffer = std::unique_ptr(new char[packetSizeWithHeader]); + + // pull the datagram + _udpSocket.readDatagram(buffer.get(), packetSizeWithHeader, + senderSockAddr.getAddressPointer(), senderSockAddr.getPortPointer()); + + // setup a Packet from the data we just read + auto packet = Packet::fromReceivedPacket(std::move(buffer), packetSizeWithHeader, senderSockAddr); + + + } +} diff --git a/libraries/networking/src/udt/Socket.h b/libraries/networking/src/udt/Socket.h index 23ad75db12..3ab568c952 100644 --- a/libraries/networking/src/udt/Socket.h +++ b/libraries/networking/src/udt/Socket.h @@ -44,6 +44,10 @@ public: { _verifiedPacketFunction = verifiedPacketFunction; } void setBufferSizes(int numBytes); + +private slots: + void readPendingDatagrams(); + private: QUdpSocket _udpSocket { this }; VerifiedPacketFunction _verifiedPacketFunction;