Merge branch 'atp' of https://github.com/birarda/hifi into protocol

This commit is contained in:
Atlante45 2015-07-14 18:59:57 -07:00
commit c6faf6e84a
9 changed files with 78 additions and 37 deletions

View file

@ -62,7 +62,9 @@ LimitedNodeList::LimitedNodeList(unsigned short socketListenPort, unsigned short
NodeType::init(); NodeType::init();
// register the SharedNodePointer meta-type for signals/slots // register the SharedNodePointer meta-type for signals/slots
qRegisterMetaType<QSharedPointer<Node>>();
qRegisterMetaType<SharedNodePointer>(); qRegisterMetaType<SharedNodePointer>();
firstCall = false; firstCall = false;
} }
@ -175,15 +177,16 @@ void LimitedNodeList::changeSocketBufferSizes(int numBytes) {
bool LimitedNodeList::packetSourceAndHashMatch(const NLPacket& packet, SharedNodePointer& matchingNode) { bool LimitedNodeList::packetSourceAndHashMatch(const NLPacket& packet, SharedNodePointer& matchingNode) {
if (!NON_VERIFIED_PACKETS.contains(packet.getType()) && !NON_SOURCED_PACKETS.contains(packet.getType())) { if (NON_SOURCED_PACKETS.contains(packet.getType())) {
return true;
} else {
// figure out which node this is from // figure out which node this is from
matchingNode = nodeWithUUID(packet.getSourceID()); matchingNode = nodeWithUUID(packet.getSourceID());
if (matchingNode) { if (matchingNode) {
if (!NON_VERIFIED_PACKETS.contains(packet.getType())) {
// check if the md5 hash in the header matches the hash we would expect // check if the md5 hash in the header matches the hash we would expect
if (packet.getVerificationHash() == packet.payloadHashWithConnectionUUID(matchingNode->getConnectionSecret())) { if (packet.getVerificationHash() != packet.payloadHashWithConnectionUUID(matchingNode->getConnectionSecret())) {
return true;
} else {
static QMultiMap<QUuid, PacketType::Value> hashDebugSuppressMap; static QMultiMap<QUuid, PacketType::Value> hashDebugSuppressMap;
const QUuid& senderID = packet.getSourceID(); const QUuid& senderID = packet.getSourceID();
@ -193,6 +196,11 @@ bool LimitedNodeList::packetSourceAndHashMatch(const NLPacket& packet, SharedNod
hashDebugSuppressMap.insert(senderID, packet.getType()); hashDebugSuppressMap.insert(senderID, packet.getType());
} }
return false;
}
return true;
} }
} else { } else {
static QString repeatedMessage static QString repeatedMessage
@ -201,8 +209,6 @@ bool LimitedNodeList::packetSourceAndHashMatch(const NLPacket& packet, SharedNod
qCDebug(networking) << "Packet of type" << packet.getType() << "received from unknown node with UUID" qCDebug(networking) << "Packet of type" << packet.getType() << "received from unknown node with UUID"
<< qPrintable(uuidStringWithoutCurlyBraces(packet.getSourceID())); << qPrintable(uuidStringWithoutCurlyBraces(packet.getSourceID()));
} }
} else {
return true;
} }
return false; return false;

View file

@ -30,7 +30,16 @@ qint64 NLPacket::localHeaderSize() const {
} }
std::unique_ptr<NLPacket> NLPacket::create(PacketType::Value type, qint64 size) { std::unique_ptr<NLPacket> NLPacket::create(PacketType::Value type, qint64 size) {
auto packet = std::unique_ptr<NLPacket>(new NLPacket(type, size)); std::unique_ptr<NLPacket> packet;
if (size == -1) {
packet = std::unique_ptr<NLPacket>(new NLPacket(type));
} else {
// Fail with invalid size
Q_ASSERT(size >= 0);
packet = std::unique_ptr<NLPacket>(new NLPacket(type, size));
}
packet->open(QIODevice::WriteOnly); packet->open(QIODevice::WriteOnly);
@ -64,7 +73,22 @@ std::unique_ptr<NLPacket> NLPacket::createCopy(const NLPacket& other) {
return packet; return packet;
} }
NLPacket::NLPacket(PacketType::Value type, qint64 size) : Packet(type, localHeaderSize(type) + size) { NLPacket::NLPacket(PacketType::Value type, qint64 size) :
Packet(type, localHeaderSize(type) + size)
{
Q_ASSERT(size >= 0);
qint64 headerSize = localHeaderSize(type);
_payloadStart += headerSize;
_capacity -= headerSize;
}
NLPacket::NLPacket(PacketType::Value type) :
Packet(type, -1)
{
qint64 headerSize = localHeaderSize(type);
_payloadStart += headerSize;
_capacity -= headerSize;
} }
NLPacket::NLPacket(const NLPacket& other) : Packet(other) { NLPacket::NLPacket(const NLPacket& other) : Packet(other) {

View file

@ -12,6 +12,8 @@
#ifndef hifi_NLPacket_h #ifndef hifi_NLPacket_h
#define hifi_NLPacket_h #define hifi_NLPacket_h
#include <QtCore/QSharedPointer>
#include "udt/Packet.h" #include "udt/Packet.h"
class NLPacket : public Packet { class NLPacket : public Packet {
@ -35,6 +37,7 @@ public:
QByteArray payloadHashWithConnectionUUID(const QUuid& connectionUUID) const; QByteArray payloadHashWithConnectionUUID(const QUuid& connectionUUID) const;
protected: protected:
NLPacket(PacketType::Value type);
NLPacket(PacketType::Value type, qint64 size); NLPacket(PacketType::Value type, qint64 size);
NLPacket(std::unique_ptr<char> data, qint64 size, const HifiSockAddr& senderSockAddr); NLPacket(std::unique_ptr<char> data, qint64 size, const HifiSockAddr& senderSockAddr);
NLPacket(const NLPacket& other); NLPacket(const NLPacket& other);

View file

@ -38,14 +38,6 @@ NodeList::NodeList(char newOwnerType, unsigned short socketListenPort, unsigned
_numNoReplyDomainCheckIns(0), _numNoReplyDomainCheckIns(0),
_assignmentServerSocket() _assignmentServerSocket()
{ {
static bool firstCall = true;
if (firstCall) {
NodeType::init();
// register the SharedNodePointer meta-type for signals/slots
qRegisterMetaType<SharedNodePointer>();
firstCall = false;
}
setCustomDeleter([](Dependency* dependency){ setCustomDeleter([](Dependency* dependency){
static_cast<NodeList*>(dependency)->deleteLater(); static_cast<NodeList*>(dependency)->deleteLater();
}); });

View file

@ -21,7 +21,7 @@ PacketReceiver::PacketReceiver(QObject* parent) :
QObject(parent), QObject(parent),
_packetListenerMap() _packetListenerMap()
{ {
qRegisterMetaType<QSharedPointer<NLPacket>>();
} }
void PacketReceiver::registerListenerForTypes(const QSet<PacketType::Value>& types, PacketListener* listener, const char* slot) { void PacketReceiver::registerListenerForTypes(const QSet<PacketType::Value>& types, PacketListener* listener, const char* slot) {
@ -227,6 +227,7 @@ void PacketReceiver::processDatagrams() {
if (listener.first) { if (listener.first) {
bool success = false; bool success = false;
PacketType::Value packetType = packet->getType();
if (matchingNode) { if (matchingNode) {
// if this was a sequence numbered packet we should store the last seq number for // if this was a sequence numbered packet we should store the last seq number for
@ -238,13 +239,21 @@ void PacketReceiver::processDatagrams() {
emit dataReceived(matchingNode->getType(), packet->getSizeWithHeader()); emit dataReceived(matchingNode->getType(), packet->getSizeWithHeader());
QMetaMethod metaMethod = listener.second; QMetaMethod metaMethod = listener.second;
static const QByteArray SHARED_NODE_NORMALIZED = QMetaObject::normalizedType("QSharedPointer<Node>"); static const QByteArray QSHAREDPOINTER_NODE_NORMALIZED = QMetaObject::normalizedType("QSharedPointer<Node>");
static const QByteArray SHARED_NODE_NORMALIZED = QMetaObject::normalizedType("SharedNodePointer");
if (metaMethod.parameterTypes().contains(SHARED_NODE_NORMALIZED)) { if (metaMethod.parameterTypes().contains(SHARED_NODE_NORMALIZED)) {
qDebug() << "invoking with matchingNode" << matchingNode;
success = metaMethod.invoke(listener.first, success = metaMethod.invoke(listener.first,
Q_ARG(QSharedPointer<NLPacket>, QSharedPointer<NLPacket>(packet.release())), Q_ARG(QSharedPointer<NLPacket>, QSharedPointer<NLPacket>(packet.release())),
Q_ARG(SharedNodePointer, matchingNode)); Q_ARG(SharedNodePointer, matchingNode));
} else if (metaMethod.parameterTypes().contains(QSHAREDPOINTER_NODE_NORMALIZED)) {
qDebug() << "invoking with matchingNode" << matchingNode;
success = metaMethod.invoke(listener.first,
Q_ARG(QSharedPointer<NLPacket>, QSharedPointer<NLPacket>(packet.release())),
Q_ARG(QSharedPointer<Node>, matchingNode));
} else { } else {
success = metaMethod.invoke(listener.first, success = metaMethod.invoke(listener.first,
Q_ARG(QSharedPointer<NLPacket>, QSharedPointer<NLPacket>(packet.release()))); Q_ARG(QSharedPointer<NLPacket>, QSharedPointer<NLPacket>(packet.release())));
@ -257,13 +266,15 @@ void PacketReceiver::processDatagrams() {
} }
if (!success) { if (!success) {
qDebug() << "Error delivering packet " << nameForPacketType(packet->getType()) << " to listener: " qDebug().nospace() << "Error delivering packet " << packetType
<< listener.first->objectName() << "::" << listener.second.name(); << " (" << qPrintable(nameForPacketType(packetType)) << ") to listener "
<< listener.first << "::" << qPrintable(listener.second.methodSignature());
} }
} else { } else {
// we have a dead listener - remove this mapping from the _packetListenerMap // we have a dead listener - remove this mapping from the _packetListenerMap
qDebug() << "Listener for packet type" << nameForPacketType(packet->getType()) qDebug() << "Listener for packet type" << packet->getType() << "("
<< qPrintable(nameForPacketType(packet->getType())) << ")"
<< "has been destroyed - removing mapping."; << "has been destroyed - removing mapping.";
_packetListenerMap.erase(it); _packetListenerMap.erase(it);
} }

View file

@ -192,7 +192,7 @@ void Packet::writeSequenceNumber(SequenceNumber seqNum) {
static const qint64 PACKET_WRITE_ERROR = -1; static const qint64 PACKET_WRITE_ERROR = -1;
qint64 Packet::writeData(const char* data, qint64 maxSize) { qint64 Packet::writeData(const char* data, qint64 maxSize) {
// make sure we have the space required to write this block // make sure we have the space required to write this block
if (maxSize <= bytesAvailable()) { if (maxSize <= bytesAvailableForWrite()) {
qint64 currentPos = pos(); qint64 currentPos = pos();
// good to go - write the data // good to go - write the data
@ -202,7 +202,7 @@ qint64 Packet::writeData(const char* data, qint64 maxSize) {
seek(currentPos + maxSize); seek(currentPos + maxSize);
// keep track of _sizeUsed so we can just write the actual data when packet is about to be sent // keep track of _sizeUsed so we can just write the actual data when packet is about to be sent
_sizeUsed = std::max(pos() + 1, _sizeUsed); _sizeUsed = std::max(pos(), _sizeUsed);
// return the number of bytes written // return the number of bytes written
return maxSize; return maxSize;

View file

@ -49,10 +49,12 @@ public:
PacketVersion getVersion() const { return _version; } PacketVersion getVersion() const { return _version; }
qint64 getSizeWithHeader() const { return localHeaderSize() + getSizeUsed(); } qint64 getSizeWithHeader() const { return totalHeadersSize() + getSizeUsed(); }
qint64 getSizeUsed() const { return _sizeUsed; } qint64 getSizeUsed() const { return _sizeUsed; }
void setSizeUsed(qint64 sizeUsed) { _sizeUsed = sizeUsed; } void setSizeUsed(qint64 sizeUsed) { _sizeUsed = sizeUsed; }
qint64 bytesAvailableForWrite() const { return _capacity - _sizeUsed; }
HifiSockAddr& getSenderSockAddr() { return _senderSockAddr; } HifiSockAddr& getSenderSockAddr() { return _senderSockAddr; }
const HifiSockAddr& getSenderSockAddr() const { return _senderSockAddr; } const HifiSockAddr& getSenderSockAddr() const { return _senderSockAddr; }

View file

@ -18,7 +18,6 @@
using namespace PacketType; using namespace PacketType;
const QSet<PacketType::Value> NON_VERIFIED_PACKETS = QSet<PacketType::Value>() const QSet<PacketType::Value> NON_VERIFIED_PACKETS = QSet<PacketType::Value>()
<< CreateAssignment << RequestAssignment << StunResponse
<< NodeJsonStats << EntityQuery << NodeJsonStats << EntityQuery
<< OctreeDataNack << EntityEditNack << OctreeDataNack << EntityEditNack
<< DomainListRequest << DomainListRequest
@ -28,6 +27,7 @@ const QSet<PacketType::Value> NON_VERIFIED_PACKETS = QSet<PacketType::Value>()
const QSet<PacketType::Value> SEQUENCE_NUMBERED_PACKETS = QSet<PacketType::Value>() << AvatarData; const QSet<PacketType::Value> SEQUENCE_NUMBERED_PACKETS = QSet<PacketType::Value>() << AvatarData;
const QSet<PacketType::Value> NON_SOURCED_PACKETS = QSet<PacketType::Value>() const QSet<PacketType::Value> NON_SOURCED_PACKETS = QSet<PacketType::Value>()
<< StunResponse << CreateAssignment << RequestAssignment
<< DomainServerRequireDTLS << DomainConnectRequest << DomainServerRequireDTLS << DomainConnectRequest
<< DomainList << DomainConnectionDenied << DomainList << DomainConnectionDenied
<< DomainServerPathQuery << DomainServerPathResponse << DomainServerPathQuery << DomainServerPathResponse

View file

@ -15,7 +15,10 @@
#include "Packet.h" #include "Packet.h"
PacketList::PacketList(PacketType::Value packetType) : _packetType(packetType) { PacketList::PacketList(PacketType::Value packetType) :
_packetType(packetType)
{
QIODevice::open(WriteOnly);
} }
void PacketList::startSegment() { void PacketList::startSegment() {