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

This commit is contained in:
Atlante45 2015-07-27 11:27:49 -07:00
commit 5261d7b4d1
13 changed files with 55 additions and 78 deletions

View file

@ -160,29 +160,29 @@ NLPacket& NLPacket::operator=(NLPacket&& other) {
}
PacketType NLPacket::typeInHeader(const udt::Packet& packet) {
auto headerOffset = packet.Packet::localHeaderSize();
auto headerOffset = packet.Packet::totalHeadersSize();
return *reinterpret_cast<const PacketType*>(packet.getData() + headerOffset);
}
PacketVersion NLPacket::versionInHeader(const udt::Packet& packet) {
auto headerOffset = packet.Packet::localHeaderSize();
auto headerOffset = packet.Packet::totalHeadersSize();
return *reinterpret_cast<const PacketVersion*>(packet.getData() + headerOffset + sizeof(PacketType));
}
QUuid NLPacket::sourceIDInHeader(const udt::Packet& packet) {
int offset = packet.Packet::localHeaderSize() + sizeof(PacketType) + sizeof(PacketVersion);
int offset = packet.Packet::totalHeadersSize() + sizeof(PacketType) + sizeof(PacketVersion);
return QUuid::fromRfc4122(QByteArray::fromRawData(packet.getData() + offset, NUM_BYTES_RFC4122_UUID));
}
QByteArray NLPacket::verificationHashInHeader(const udt::Packet& packet) {
int offset = packet.Packet::localHeaderSize() + sizeof(PacketType) + sizeof(PacketVersion) + NUM_BYTES_RFC4122_UUID;
int offset = packet.Packet::totalHeadersSize() + sizeof(PacketType) + sizeof(PacketVersion) + NUM_BYTES_RFC4122_UUID;
return QByteArray(packet.getData() + offset, NUM_BYTES_MD5_HASH);
}
QByteArray NLPacket::hashForPacketAndSecret(const udt::Packet& packet, const QUuid& connectionSecret) {
QCryptographicHash hash(QCryptographicHash::Md5);
int offset = packet.Packet::localHeaderSize() + sizeof(PacketType) + sizeof(PacketVersion)
int offset = packet.Packet::totalHeadersSize() + sizeof(PacketType) + sizeof(PacketVersion)
+ NUM_BYTES_RFC4122_UUID + NUM_BYTES_MD5_HASH;
// add the packet payload and the connection UUID
@ -194,7 +194,7 @@ QByteArray NLPacket::hashForPacketAndSecret(const udt::Packet& packet, const QUu
}
void NLPacket::writeTypeAndVersion() {
auto headerOffset = Packet::localHeaderSize();
auto headerOffset = Packet::totalHeadersSize();
// Pack the packet type
memcpy(_packet.get() + headerOffset, &_type, sizeof(PacketType));
@ -233,7 +233,7 @@ void NLPacket::readSourceID() {
void NLPacket::writeSourceID(const QUuid& sourceID) {
Q_ASSERT(!NON_SOURCED_PACKETS.contains(_type));
auto offset = Packet::localHeaderSize() + sizeof(PacketType) + sizeof(PacketVersion);
auto offset = Packet::totalHeadersSize() + sizeof(PacketType) + sizeof(PacketVersion);
memcpy(_packet.get() + offset, sourceID.toRfc4122().constData(), NUM_BYTES_RFC4122_UUID);
_sourceID = sourceID;
@ -242,7 +242,7 @@ void NLPacket::writeSourceID(const QUuid& sourceID) {
void NLPacket::writeVerificationHashGivenSecret(const QUuid& connectionSecret) {
Q_ASSERT(!NON_SOURCED_PACKETS.contains(_type) && !NON_VERIFIED_PACKETS.contains(_type));
auto offset = Packet::localHeaderSize() + sizeof(PacketType) + sizeof(PacketVersion) + NUM_BYTES_RFC4122_UUID;
auto offset = Packet::totalHeadersSize() + sizeof(PacketType) + sizeof(PacketVersion) + NUM_BYTES_RFC4122_UUID;
QByteArray verificationHash = hashForPacketAndSecret(*this, connectionSecret);
memcpy(_packet.get() + offset, verificationHash.data(), verificationHash.size());

View file

@ -19,6 +19,12 @@
class NLPacket : public udt::Packet {
Q_OBJECT
public:
// this is used by the Octree classes - must be known at compile time
static const int MAX_PACKET_HEADER_SIZE =
sizeof(udt::Packet::SequenceNumberAndBitField) + sizeof(udt::Packet::MessageNumberAndBitField) +
sizeof(PacketType) + sizeof(PacketVersion) + NUM_BYTES_RFC4122_UUID + NUM_BYTES_MD5_HASH;
static std::unique_ptr<NLPacket> create(PacketType type, qint64 size = -1,
bool isReliable = false, bool isPartOfMessage = false);

View file

@ -24,7 +24,6 @@
#include "NetworkPeer.h"
#include "NodeData.h"
#include "NodeType.h"
#include "udt/Packet.h"
#include "SimpleMovingAverage.h"
#include "MovingPercentile.h"
@ -84,8 +83,6 @@ private:
MovingPercentile _clockSkewMovingPercentile;
bool _canAdjustLocks;
bool _canRez;
std::map<PacketType, udt::SeqNum> _lastSequenceNumbers;
};
typedef QSharedPointer<Node> SharedNodePointer;

View file

@ -226,11 +226,11 @@ void PacketReceiver::handleVerifiedPacket(std::unique_ptr<udt::Packet> packet) {
auto it = _packetListenerMap.find(nlPacket->getType());
if (it != _packetListenerMap.end()) {
if (it != _packetListenerMap.end() && it->second.isValid()) {
auto listener = it.value();
if (listener.first && it->second.isValid()) {
if (listener.first) {
bool success = false;
@ -273,12 +273,21 @@ void PacketReceiver::handleVerifiedPacket(std::unique_ptr<udt::Packet> packet) {
Q_ARG(QSharedPointer<NLPacket>,
QSharedPointer<NLPacket>(nlPacket.release())));
}
} else {
listenerIsDead = true;
}
} else {
emit dataReceived(NodeType::Unassigned, nlPacket->getDataSize());
success = listener.second.invoke(listener.first,
Q_ARG(QSharedPointer<NLPacket>, QSharedPointer<NLPacket>(nlPacket.release())));
// one final check on the QPointer before we invoke
if (listener.first) {
success = listener.second.invoke(listener.first,
Q_ARG(QSharedPointer<NLPacket>,
QSharedPointer<NLPacket>(nlPacket.release())));
} else {
listenerIsDead = true;
}
}
if (!success) {

View file

@ -24,8 +24,7 @@ std::unique_ptr<BasePacket> BasePacket::create(qint64 size) {
}
std::unique_ptr<BasePacket> BasePacket::fromReceivedPacket(std::unique_ptr<char> data,
qint64 size,
const HifiSockAddr& senderSockAddr) {
qint64 size, const HifiSockAddr& senderSockAddr) {
// Fail with invalid size
Q_ASSERT(size >= 0);
@ -66,7 +65,9 @@ BasePacket::BasePacket(std::unique_ptr<char> data, qint64 size, const HifiSockAd
}
BasePacket::BasePacket(const BasePacket& other) {
BasePacket::BasePacket(const BasePacket& other) :
QIODevice()
{
*this = other;
}

View file

@ -30,7 +30,7 @@ public:
static std::unique_ptr<BasePacket> create(qint64 size = -1);
static std::unique_ptr<BasePacket> fromReceivedPacket(std::unique_ptr<char> data, qint64 size, const HifiSockAddr& senderSockAddr);
static qint64 maxPayloadSize() { return MAX_PACKET_SIZE; } // The maximum payload size this packet can use to fit in MTU
static qint64 maxPayloadSize() { return MAX_PACKET_SIZE; } // The maximum payload size this packet can use to fit in MTU
static qint64 localHeaderSize() { return 0; } // Current level's header size
virtual qint64 totalHeadersSize() const { return 0; } // Cumulated size of all the headers

View file

@ -29,7 +29,7 @@ qint64 ControlPacket::localHeaderSize() {
}
qint64 ControlPacket::totalHeadersSize() const {
return BasePacket::localHeaderSize() + localHeaderSize();
return BasePacket::totalHeadersSize() + localHeaderSize();
}
ControlPacket::ControlPacket(Type type, const SequenceNumberList& sequenceNumbers) :
@ -61,11 +61,13 @@ ControlPacket::ControlPacket(quint64 timestamp) :
ControlPacket::ControlPacket(ControlPacket&& other) :
BasePacket(std::move(other))
{
_type = other._type;
}
ControlPacket& ControlPacket::operator=(Packet&& other) {
ControlPacket& ControlPacket::operator=(ControlPacket&& other) {
BasePacket::operator=(std::move(other));
_type = other._type;
return *this;
}

View file

@ -46,10 +46,10 @@ private:
ControlPacket(Type type, const SequenceNumberList& sequenceNumbers);
ControlPacket(quint64 timestamp);
ControlPacket(ControlPacket&& other);
ControlPacket(const Packet& other) = delete;
ControlPacket(const ControlPacket& other) = delete;
ControlPacket& operator=(Packet&& other);
ControlPacket& operator=(const Packet& other) = delete;
ControlPacket& operator=(ControlPacket&& other);
ControlPacket& operator=(const ControlPacket& other) = delete;
Type _type;
};

View file

@ -32,34 +32,6 @@ const QSet<PacketType> NON_SOURCED_PACKETS = QSet<PacketType>()
const QSet<PacketType> RELIABLE_PACKETS = QSet<PacketType>();
int arithmeticCodingValueFromBuffer(const char* checkValue) {
if (((uchar) *checkValue) < 255) {
return *checkValue;
} else {
return 255 + arithmeticCodingValueFromBuffer(checkValue + 1);
}
}
int numBytesArithmeticCodingFromBuffer(const char* checkValue) {
if (((uchar) *checkValue) < 255) {
return 1;
} else {
return 1 + numBytesArithmeticCodingFromBuffer(checkValue + 1);
}
}
int packArithmeticallyCodedValue(int value, char* destination) {
if (value < 255) {
// less than 255, just pack our value
destination[0] = (uchar) value;
return 1;
} else {
// pack 255 and then recursively pack on
((unsigned char*)destination)[0] = 255;
return 1 + packArithmeticallyCodedValue(value - 255, destination + 1);
}
}
PacketVersion versionForPacketType(PacketType packetType) {
switch (packetType) {
case PacketType::EntityAdd:
@ -123,10 +95,6 @@ QString nameForPacketType(PacketType packetType) {
return QString("unexpected");
}
int numBytesForArithmeticCodedPacketType(PacketType packetType) {
return (int) ceilf((float) packetType / 255);
}
uint qHash(const PacketType& key, uint seed) {
// seems odd that Qt couldn't figure out this cast itself, but this fixes a compile error after switch to
// strongly typed enum for PacketType

View file

@ -23,9 +23,9 @@
#include "UUID.h"
// NOTE: if adding a new packet packetType, you can replace one marked usable or add at the end
// NOTE: if you want the name of the packet packetType to be available for debugging or logging, update nameForPacketType() as well
// If adding a new packet packetType, you can replace one marked usable or add at the end.
// If you want the name of the packet packetType to be available for debugging or logging, update nameForPacketType() as well
// This enum must hold 256 or fewer packet types (so the value is <= 255) since it is statically typed as a uint8_t
enum class PacketType : uint8_t {
Unknown,
StunResponse,
@ -76,7 +76,6 @@ enum class PacketType : uint8_t {
};
const int NUM_BYTES_MD5_HASH = 16;
const int MAX_PACKET_HEADER_BYTES = 4 + NUM_BYTES_RFC4122_UUID + NUM_BYTES_MD5_HASH;
typedef char PacketVersion;
@ -87,13 +86,6 @@ extern const QSet<PacketType> RELIABLE_PACKETS;
QString nameForPacketType(PacketType packetType);
PacketVersion versionForPacketType(PacketType packetType);
int numBytesForArithmeticCodedPacketType(PacketType packetType);
int numBytesForPacketHeaderGivenPacketType(PacketType packetType);
int packArithmeticallyCodedValue(int value, char* destination);
int arithmeticCodingValueFromBuffer(const char* checkValue);
int numBytesArithmeticCodingFromBuffer(const char* checkValue);
uint qHash(const PacketType& key, uint seed);
QDebug operator<<(QDebug debug, const PacketType& type);

View file

@ -65,7 +65,7 @@ qint64 Socket::writeDatagram(const QByteArray& datagram, const HifiSockAddr& soc
qint64 bytesWritten = _udpSocket.writeDatagram(datagram, sockAddr.getAddress(), sockAddr.getPort());
// TODO::
// TODO: write the correct sequence number to the Packet here
// const_cast<NLPacket&>(packet).writeSequenceNumber(sequenceNumber);
if (bytesWritten < 0) {

View file

@ -1902,7 +1902,7 @@ bool Octree::readSVOFromStream(unsigned long streamLength, QDataStream& inputStr
if (getWantSVOfileVersions()) {
// read just enough of the file to parse the header...
const unsigned long HEADER_LENGTH = sizeof(PacketType) + sizeof(PacketVersion);
const unsigned long HEADER_LENGTH = sizeof(int) + sizeof(PacketVersion);
unsigned char fileHeader[HEADER_LENGTH];
inputStream.readRawData((char*)&fileHeader, HEADER_LENGTH);
@ -1912,8 +1912,9 @@ bool Octree::readSVOFromStream(unsigned long streamLength, QDataStream& inputStr
unsigned long dataLength = HEADER_LENGTH;
// if so, read the first byte of the file and see if it matches the expected version code
PacketType gotType;
memcpy(&gotType, dataAt, sizeof(gotType));
int intPacketType;
memcpy(&intPacketType, dataAt, sizeof(intPacketType));
PacketType gotType = (PacketType) intPacketType;
dataAt += sizeof(expectedType);
dataLength -= sizeof(expectedType);
@ -2076,16 +2077,17 @@ void Octree::writeToSVOFile(const char* fileName, OctreeElement* element) {
if(file.is_open()) {
qCDebug(octree, "Saving binary SVO to file %s...", fileName);
PacketType expectedType = expectedDataPacketType();
PacketVersion expectedVersion = versionForPacketType(expectedType);
PacketType expectedPacketType = expectedDataPacketType();
int expectedIntType = (int) expectedPacketType;
PacketVersion expectedVersion = versionForPacketType(expectedPacketType);
bool hasBufferBreaks = versionHasSVOfileBreaks(expectedVersion);
// before reading the file, check to see if this version of the Octree supports file versions
if (getWantSVOfileVersions()) {
// if so, read the first byte of the file and see if it matches the expected version code
file.write(reinterpret_cast<char*>(&expectedType), sizeof(expectedType));
file.write(reinterpret_cast<char*>(&expectedIntType), sizeof(expectedIntType));
file.write(&expectedVersion, sizeof(expectedVersion));
qCDebug(octree) << "SVO file type: " << nameForPacketType(expectedType) << " version: " << (int)expectedVersion;
qCDebug(octree) << "SVO file type: " << expectedPacketType << " version: " << (int)expectedVersion;
hasBufferBreaks = versionHasSVOfileBreaks(expectedVersion);
}

View file

@ -29,7 +29,7 @@
#include <BackgroundMode.h>
#include <SharedUtil.h>
#include <ShapeInfo.h>
#include <udt/BasePacket.h>
#include <NLPacket.h>
#include <udt/PacketHeaders.h>
#include "OctreeConstants.h"
@ -46,7 +46,7 @@ const unsigned int OCTREE_PACKET_EXTRA_HEADERS_SIZE = sizeof(OCTREE_PACKET_FLAGS
+ sizeof(OCTREE_PACKET_SEQUENCE) + sizeof(OCTREE_PACKET_SENT_TIME);
const unsigned int MAX_OCTREE_PACKET_DATA_SIZE =
udt::MAX_PACKET_SIZE - (MAX_PACKET_HEADER_BYTES + OCTREE_PACKET_EXTRA_HEADERS_SIZE);
udt::MAX_PACKET_SIZE - (NLPacket::MAX_PACKET_HEADER_SIZE + OCTREE_PACKET_EXTRA_HEADERS_SIZE);
const unsigned int MAX_OCTREE_UNCOMRESSED_PACKET_SIZE = MAX_OCTREE_PACKET_DATA_SIZE;
const unsigned int MINIMUM_ATTEMPT_MORE_PACKING = sizeof(OCTREE_PACKET_INTERNAL_SECTION_SIZE) + 40;