udt namespace fixes and NLPacket operator fixes

This commit is contained in:
Stephen Birarda 2015-07-20 17:28:04 -07:00
parent 9556fecbe2
commit 796dfee687
14 changed files with 59 additions and 16 deletions

View file

@ -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());

View file

@ -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;

View file

@ -99,12 +99,13 @@ NLPacket::NLPacket(std::unique_ptr<Packet> 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<char> 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);

View file

@ -16,7 +16,7 @@
#include "udt/Packet.h"
class NLPacket : public Packet {
class NLPacket : public udt::Packet {
Q_OBJECT
public:
static std::unique_ptr<NLPacket> create(PacketType::Value type, qint64 size = -1);

View file

@ -19,7 +19,7 @@ NLPacketList::NLPacketList(PacketType::Value packetType, QByteArray extendedHead
}
std::unique_ptr<Packet> NLPacketList::createPacket() {
std::unique_ptr<udt::Packet> NLPacketList::createPacket() {
return NLPacket::create(getType());
}

View file

@ -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<Packet> createPacket();
virtual std::unique_ptr<udt::Packet> createPacket();
};
#endif // hifi_PacketList_h

View file

@ -234,7 +234,7 @@ bool PacketReceiver::packetVersionMatch(const NLPacket& packet) {
}
}
void PacketReceiver::handleVerifiedPacket(std::unique_ptr<Packet> packet) {
void PacketReceiver::handleVerifiedPacket(std::unique_ptr<udt::Packet> packet) {
// if we're supposed to drop this packet then break out here
if (_shouldDropPackets) {

View file

@ -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> packet);
void handleVerifiedPacket(std::unique_ptr<udt::Packet> packet);
signals:
void dataReceived(quint8 channelType, int bytes);

View file

@ -11,6 +11,8 @@
#include "Packet.h"
using namespace udt;
const qint64 Packet::PACKET_WRITE_ERROR = -1;
qint64 Packet::localHeaderSize(PacketType::Value type) {

View file

@ -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<typename T> qint64 Packet::writePrimitive(const T& data) {
static_assert(!std::is_pointer<T>::value, "T must not be a pointer");
return QIODevice::write(reinterpret_cast<const char*>(&data), sizeof(T));
}
} // namespace udt
#endif // hifi_Packet_h

View file

@ -15,6 +15,8 @@
#include "Packet.h"
using namespace udt;
PacketList::PacketList(PacketType::Value packetType, QByteArray extendedHeader) :
_packetType(packetType),
_extendedHeader(extendedHeader)

View file

@ -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<typename T> std::unique_ptr<T> PacketList::takeFront() {
_packets.pop_front();
return std::unique_ptr<T>(dynamic_cast<T*>(packet.release()));
}
}
#endif // hifi_PacketList_h

View file

@ -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<char> buffer = std::unique_ptr<char>(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);
}
}

View file

@ -44,6 +44,10 @@ public:
{ _verifiedPacketFunction = verifiedPacketFunction; }
void setBufferSizes(int numBytes);
private slots:
void readPendingDatagrams();
private:
QUdpSocket _udpSocket { this };
VerifiedPacketFunction _verifiedPacketFunction;