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();
// register the SharedNodePointer meta-type for signals/slots
qRegisterMetaType<QSharedPointer<Node>>();
qRegisterMetaType<SharedNodePointer>();
firstCall = false;
}
@ -175,24 +177,30 @@ void LimitedNodeList::changeSocketBufferSizes(int numBytes) {
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
matchingNode = nodeWithUUID(packet.getSourceID());
if (matchingNode) {
// check if the md5 hash in the header matches the hash we would expect
if (packet.getVerificationHash() == packet.payloadHashWithConnectionUUID(matchingNode->getConnectionSecret())) {
return true;
} else {
static QMultiMap<QUuid, PacketType::Value> hashDebugSuppressMap;
const QUuid& senderID = packet.getSourceID();
if (!NON_VERIFIED_PACKETS.contains(packet.getType())) {
// check if the md5 hash in the header matches the hash we would expect
if (packet.getVerificationHash() != packet.payloadHashWithConnectionUUID(matchingNode->getConnectionSecret())) {
static QMultiMap<QUuid, PacketType::Value> hashDebugSuppressMap;
const QUuid& senderID = packet.getSourceID();
if (!hashDebugSuppressMap.contains(senderID, packet.getType())) {
qCDebug(networking) << "Packet hash mismatch on" << packet.getType() << "- Sender" << senderID;
if (!hashDebugSuppressMap.contains(senderID, packet.getType())) {
qCDebug(networking) << "Packet hash mismatch on" << packet.getType() << "- Sender" << senderID;
hashDebugSuppressMap.insert(senderID, packet.getType());
hashDebugSuppressMap.insert(senderID, packet.getType());
}
return false;
}
return true;
}
} else {
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"
<< qPrintable(uuidStringWithoutCurlyBraces(packet.getSourceID()));
}
} else {
return true;
}
return false;

View file

@ -30,8 +30,17 @@ qint64 NLPacket::localHeaderSize() const {
}
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);
return packet;
@ -44,7 +53,7 @@ std::unique_ptr<NLPacket> NLPacket::fromReceivedPacket(std::unique_ptr<char> dat
// Fail with invalid size
Q_ASSERT(size >= 0);
// allocate memory
auto packet = std::unique_ptr<NLPacket>(new NLPacket(std::move(data), size, senderSockAddr));
@ -64,7 +73,22 @@ std::unique_ptr<NLPacket> NLPacket::createCopy(const NLPacket& other) {
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) {

View file

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

View file

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

View file

@ -21,7 +21,7 @@ PacketReceiver::PacketReceiver(QObject* parent) :
QObject(parent),
_packetListenerMap()
{
qRegisterMetaType<QSharedPointer<NLPacket>>();
}
void PacketReceiver::registerListenerForTypes(const QSet<PacketType::Value>& types, PacketListener* listener, const char* slot) {
@ -206,7 +206,7 @@ void PacketReceiver::processDatagrams() {
_inByteCount += packetSizeWithHeader;
if (packetVersionMatch(*packet)) {
SharedNodePointer matchingNode;
if (nodeList->packetSourceAndHashMatch(*packet, matchingNode)) {
@ -227,6 +227,7 @@ void PacketReceiver::processDatagrams() {
if (listener.first) {
bool success = false;
PacketType::Value packetType = packet->getType();
if (matchingNode) {
// if this was a sequence numbered packet we should store the last seq number for
@ -238,12 +239,20 @@ void PacketReceiver::processDatagrams() {
emit dataReceived(matchingNode->getType(), packet->getSizeWithHeader());
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)) {
qDebug() << "invoking with matchingNode" << matchingNode;
success = metaMethod.invoke(listener.first,
Q_ARG(QSharedPointer<NLPacket>, QSharedPointer<NLPacket>(packet.release())),
Q_ARG(SharedNodePointer, matchingNode));
Q_ARG(QSharedPointer<NLPacket>, QSharedPointer<NLPacket>(packet.release())),
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 {
success = metaMethod.invoke(listener.first,
@ -257,13 +266,15 @@ void PacketReceiver::processDatagrams() {
}
if (!success) {
qDebug() << "Error delivering packet " << nameForPacketType(packet->getType()) << " to listener: "
<< listener.first->objectName() << "::" << listener.second.name();
qDebug().nospace() << "Error delivering packet " << packetType
<< " (" << qPrintable(nameForPacketType(packetType)) << ") to listener "
<< listener.first << "::" << qPrintable(listener.second.methodSignature());
}
} else {
// 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.";
_packetListenerMap.erase(it);
}

View file

@ -192,7 +192,7 @@ void Packet::writeSequenceNumber(SequenceNumber seqNum) {
static const qint64 PACKET_WRITE_ERROR = -1;
qint64 Packet::writeData(const char* data, qint64 maxSize) {
// make sure we have the space required to write this block
if (maxSize <= bytesAvailable()) {
if (maxSize <= bytesAvailableForWrite()) {
qint64 currentPos = pos();
// good to go - write the data
@ -202,7 +202,7 @@ qint64 Packet::writeData(const char* data, qint64 maxSize) {
seek(currentPos + maxSize);
// 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 maxSize;

View file

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

View file

@ -18,7 +18,6 @@
using namespace PacketType;
const QSet<PacketType::Value> NON_VERIFIED_PACKETS = QSet<PacketType::Value>()
<< CreateAssignment << RequestAssignment << StunResponse
<< NodeJsonStats << EntityQuery
<< OctreeDataNack << EntityEditNack
<< 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> NON_SOURCED_PACKETS = QSet<PacketType::Value>()
<< StunResponse << CreateAssignment << RequestAssignment
<< DomainServerRequireDTLS << DomainConnectRequest
<< DomainList << DomainConnectionDenied
<< DomainServerPathQuery << DomainServerPathResponse

View file

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