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

This commit is contained in:
Stephen Birarda 2015-07-30 15:23:40 -07:00
commit 4529eea5f8
9 changed files with 103 additions and 104 deletions

View file

@ -128,7 +128,7 @@ void OctreeInboundPacketProcessor::processPacket(QSharedPointer<NLPacket> packet
}
if (debugProcessPacket) {
qDebug() << " numBytesPacketHeader=" << packet->totalHeadersSize();
qDebug() << " numBytesPacketHeader=" << NLPacket::totalHeaderSize(packetType);
qDebug() << " sizeof(sequence)=" << sizeof(sequence);
qDebug() << " sizeof(sentAt)=" << sizeof(sentAt);
}

View file

@ -11,26 +11,17 @@
#include "NLPacket.h"
qint64 NLPacket::maxPayloadSize(PacketType type) {
return Packet::maxPayloadSize(false) - localHeaderSize(type);
}
qint64 NLPacket::localHeaderSize(PacketType type) {
qint64 optionalSize = ((NON_SOURCED_PACKETS.contains(type)) ? 0 : NUM_BYTES_RFC4122_UUID) +
((NON_SOURCED_PACKETS.contains(type) || NON_VERIFIED_PACKETS.contains(type)) ? 0 : NUM_BYTES_MD5_HASH);
int NLPacket::localHeaderSize(PacketType type) {
bool sourced = NON_SOURCED_PACKETS.contains(type);
bool verified = NON_VERIFIED_PACKETS.contains(type);
qint64 optionalSize = (sourced ? 0 : NUM_BYTES_RFC4122_UUID) + ((sourced || verified) ? 0 : NUM_BYTES_MD5_HASH);
return sizeof(PacketType) + sizeof(PacketVersion) + optionalSize;
}
qint64 NLPacket::maxPayloadSize() const {
return Packet::maxPayloadSize() - localHeaderSize();
int NLPacket::totalHeaderSize(PacketType type, bool isPartOfMessage) {
return Packet::totalHeaderSize(isPartOfMessage) + NLPacket::localHeaderSize(type);
}
qint64 NLPacket::totalHeadersSize() const {
return Packet::totalHeadersSize() + localHeaderSize();
}
qint64 NLPacket::localHeaderSize() const {
return localHeaderSize(_type);
int NLPacket::maxPayloadSize(PacketType type, bool isPartOfMessage) {
return Packet::maxPayloadSize(isPartOfMessage) - NLPacket::localHeaderSize(type);
}
std::unique_ptr<NLPacket> NLPacket::create(PacketType type, qint64 size, bool isReliable, bool isPartOfMessage) {
@ -84,19 +75,19 @@ NLPacket::NLPacket(PacketType type, bool isReliable, bool isPartOfMessage) :
_type(type),
_version(versionForPacketType(type))
{
adjustPayloadStartAndCapacity(localHeaderSize());
adjustPayloadStartAndCapacity(NLPacket::localHeaderSize(_type));
writeTypeAndVersion();
}
NLPacket::NLPacket(PacketType type, qint64 size, bool isReliable, bool isPartOfMessage) :
Packet(localHeaderSize(type) + size, isReliable, isPartOfMessage),
Packet(NLPacket::localHeaderSize(type) + size, isReliable, isPartOfMessage),
_type(type),
_version(versionForPacketType(type))
{
Q_ASSERT(size >= 0);
adjustPayloadStartAndCapacity(localHeaderSize());
adjustPayloadStartAndCapacity(NLPacket::localHeaderSize(_type));
writeTypeAndVersion();
}
@ -108,7 +99,7 @@ NLPacket::NLPacket(std::unique_ptr<Packet> packet) :
readVersion();
readSourceID();
adjustPayloadStartAndCapacity(localHeaderSize(), _payloadSize > 0);
adjustPayloadStartAndCapacity(NLPacket::localHeaderSize(_type), _payloadSize > 0);
}
NLPacket::NLPacket(const NLPacket& other) : Packet(other) {
@ -123,11 +114,11 @@ NLPacket::NLPacket(std::unique_ptr<char> data, qint64 size, const HifiSockAddr&
// sanity check before we decrease the payloadSize with the payloadCapacity
Q_ASSERT(_payloadSize == _payloadCapacity);
adjustPayloadStartAndCapacity(localHeaderSize(), _payloadSize > 0);
readType();
readVersion();
readSourceID();
adjustPayloadStartAndCapacity(NLPacket::localHeaderSize(_type), _payloadSize > 0);
}
NLPacket::NLPacket(NLPacket&& other) :
@ -160,29 +151,29 @@ NLPacket& NLPacket::operator=(NLPacket&& other) {
}
PacketType NLPacket::typeInHeader(const udt::Packet& packet) {
auto headerOffset = packet.Packet::totalHeadersSize();
auto headerOffset = Packet::totalHeaderSize(packet.isPartOfMessage());
return *reinterpret_cast<const PacketType*>(packet.getData() + headerOffset);
}
PacketVersion NLPacket::versionInHeader(const udt::Packet& packet) {
auto headerOffset = packet.Packet::totalHeadersSize();
auto headerOffset = Packet::totalHeaderSize(packet.isPartOfMessage());
return *reinterpret_cast<const PacketVersion*>(packet.getData() + headerOffset + sizeof(PacketType));
}
QUuid NLPacket::sourceIDInHeader(const udt::Packet& packet) {
int offset = packet.Packet::totalHeadersSize() + sizeof(PacketType) + sizeof(PacketVersion);
int offset = Packet::totalHeaderSize(packet.isPartOfMessage()) + 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::totalHeadersSize() + sizeof(PacketType) + sizeof(PacketVersion) + NUM_BYTES_RFC4122_UUID;
int offset = Packet::totalHeaderSize(packet.isPartOfMessage()) + 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::totalHeadersSize() + sizeof(PacketType) + sizeof(PacketVersion)
int offset = Packet::totalHeaderSize(packet.isPartOfMessage()) + sizeof(PacketType) + sizeof(PacketVersion)
+ NUM_BYTES_RFC4122_UUID + NUM_BYTES_MD5_HASH;
// add the packet payload and the connection UUID
@ -194,7 +185,7 @@ QByteArray NLPacket::hashForPacketAndSecret(const udt::Packet& packet, const QUu
}
void NLPacket::writeTypeAndVersion() {
auto headerOffset = Packet::localHeaderSize();
auto headerOffset = Packet::localHeaderSize(isPartOfMessage());
// Pack the packet type
memcpy(_packet.get() + headerOffset, &_type, sizeof(PacketType));
@ -204,16 +195,14 @@ void NLPacket::writeTypeAndVersion() {
}
void NLPacket::setType(PacketType type) {
auto currentHeaderSize = totalHeadersSize();
// Setting new packet type with a different header size not currently supported
Q_ASSERT(NLPacket::totalHeaderSize(_type, isPartOfMessage()) ==
NLPacket::totalHeaderSize(type, isPartOfMessage()));
_type = type;
_version = versionForPacketType(_type);
writeTypeAndVersion();
// Setting new packet type with a different header size not currently supported
Q_ASSERT(currentHeaderSize == totalHeadersSize());
Q_UNUSED(currentHeaderSize);
}
void NLPacket::readType() {
@ -233,7 +222,7 @@ void NLPacket::readSourceID() {
void NLPacket::writeSourceID(const QUuid& sourceID) {
Q_ASSERT(!NON_SOURCED_PACKETS.contains(_type));
auto offset = Packet::totalHeadersSize() + sizeof(PacketType) + sizeof(PacketVersion);
auto offset = Packet::totalHeaderSize(isPartOfMessage()) + sizeof(PacketType) + sizeof(PacketVersion);
memcpy(_packet.get() + offset, sourceID.toRfc4122().constData(), NUM_BYTES_RFC4122_UUID);
_sourceID = sourceID;
@ -242,7 +231,8 @@ 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::totalHeadersSize() + sizeof(PacketType) + sizeof(PacketVersion) + NUM_BYTES_RFC4122_UUID;
auto offset = Packet::totalHeaderSize(isPartOfMessage()) + sizeof(PacketType) + sizeof(PacketVersion)
+ NUM_BYTES_RFC4122_UUID;
QByteArray verificationHash = hashForPacketAndSecret(*this, connectionSecret);
memcpy(_packet.get() + offset, verificationHash.data(), verificationHash.size());

View file

@ -35,6 +35,13 @@ public:
// Provided for convenience, try to limit use
static std::unique_ptr<NLPacket> createCopy(const NLPacket& other);
// Current level's header size
static int localHeaderSize(PacketType type);
// Cumulated size of all the headers
static int totalHeaderSize(PacketType type, bool isPartOfMessage = false);
// The maximum payload size this packet can use to fit in MTU
static int maxPayloadSize(PacketType type, bool isPartOfMessage = false);
static PacketType typeInHeader(const udt::Packet& packet);
static PacketVersion versionInHeader(const udt::Packet& packet);
@ -42,13 +49,6 @@ public:
static QByteArray verificationHashInHeader(const udt::Packet& packet);
static QByteArray hashForPacketAndSecret(const udt::Packet& packet, const QUuid& connectionSecret);
static qint64 maxPayloadSize(PacketType type);
static qint64 localHeaderSize(PacketType type);
virtual qint64 maxPayloadSize() const; // The maximum payload size this packet can use to fit in MTU
virtual qint64 totalHeadersSize() const; // Cumulated size of all the headers
virtual qint64 localHeaderSize() const; // Current level's header size
PacketType getType() const { return _type; }
void setType(PacketType type);

View file

@ -15,6 +15,16 @@ using namespace udt;
const qint64 BasePacket::PACKET_WRITE_ERROR = -1;
int BasePacket::localHeaderSize() {
return 0;
}
int BasePacket::totalHeaderSize() {
return 0;
}
int BasePacket::maxPayloadSize() {
return MAX_PACKET_SIZE;
}
std::unique_ptr<BasePacket> BasePacket::create(qint64 size) {
auto packet = std::unique_ptr<BasePacket>(new BasePacket(size));
@ -37,7 +47,7 @@ std::unique_ptr<BasePacket> BasePacket::fromReceivedPacket(std::unique_ptr<char>
}
BasePacket::BasePacket(qint64 size) {
auto maxPayload = maxPayloadSize();
auto maxPayload = BasePacket::maxPayloadSize();
if (size == -1) {
// default size of -1, means biggest packet possible
@ -116,6 +126,10 @@ BasePacket& BasePacket::operator=(BasePacket&& other) {
return *this;
}
qint64 BasePacket::getDataSize() const {
return (_payloadStart - _packet.get()) + _payloadSize;
}
void BasePacket::setPayloadSize(qint64 payloadSize) {
if (isWritable()) {
Q_ASSERT(payloadSize <= _payloadCapacity);

View file

@ -30,10 +30,12 @@ public:
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 localHeaderSize() { return 0; } // Current level's header size
virtual qint64 totalHeadersSize() const { return 0; } // Cumulated size of all the headers
// Current level's header size
static int localHeaderSize();
// Cumulated size of all the headers
static int totalHeaderSize();
// The maximum payload size this packet can use to fit in MTU
static int maxPayloadSize();
// Payload direct access to the payload, use responsibly!
char* getPayload() { return _payloadStart; }
@ -44,7 +46,7 @@ public:
const char* getData() const { return _packet.get(); }
// Returns the size of the packet, including the header
qint64 getDataSize() const { return totalHeadersSize() + _payloadSize; }
qint64 getDataSize() const;
// Returns the size of the payload only
qint64 getPayloadSize() const { return _payloadSize; }
@ -86,7 +88,7 @@ protected:
virtual qint64 writeData(const char* data, qint64 maxSize);
virtual qint64 readData(char* data, qint64 maxSize);
virtual void adjustPayloadStartAndCapacity(qint64 headerSize, bool shouldDecreasePayloadSize = false);
void adjustPayloadStartAndCapacity(qint64 headerSize, bool shouldDecreasePayloadSize = false);
qint64 _packetSize = 0; // Total size of the allocated memory
std::unique_ptr<char> _packet; // Allocated memory

View file

@ -15,6 +15,16 @@
using namespace udt;
int ControlPacket::localHeaderSize() {
return sizeof(ControlPacket::ControlBitAndType);
}
int ControlPacket::totalHeaderSize() {
return BasePacket::totalHeaderSize() + ControlPacket::localHeaderSize();
}
int ControlPacket::maxPayloadSize() {
return BasePacket::maxPayloadSize() - ControlPacket::localHeaderSize();
}
std::unique_ptr<ControlPacket> ControlPacket::fromReceivedPacket(std::unique_ptr<char> data, qint64 size,
const HifiSockAddr &senderSockAddr) {
// Fail with null data
@ -45,19 +55,11 @@ std::unique_ptr<ControlPacket> ControlPacket::create(Type type, qint64 size) {
}
}
qint64 ControlPacket::localHeaderSize() {
return sizeof(ControlBitAndType);
}
qint64 ControlPacket::totalHeadersSize() const {
return BasePacket::totalHeadersSize() + localHeaderSize();
}
ControlPacket::ControlPacket(Type type) :
BasePacket(-1),
_type(type)
{
adjustPayloadStartAndCapacity(localHeaderSize());
adjustPayloadStartAndCapacity(ControlPacket::localHeaderSize());
open(QIODevice::ReadWrite);
@ -65,10 +67,10 @@ ControlPacket::ControlPacket(Type type) :
}
ControlPacket::ControlPacket(Type type, qint64 size) :
BasePacket(localHeaderSize() + size),
BasePacket(ControlPacket::localHeaderSize() + size),
_type(type)
{
adjustPayloadStartAndCapacity(localHeaderSize());
adjustPayloadStartAndCapacity(ControlPacket::localHeaderSize());
open(QIODevice::ReadWrite);
@ -81,7 +83,7 @@ ControlPacket::ControlPacket(std::unique_ptr<char> data, qint64 size, const Hifi
// sanity check before we decrease the payloadSize with the payloadCapacity
Q_ASSERT(_payloadSize == _payloadCapacity);
adjustPayloadStartAndCapacity(localHeaderSize(), _payloadSize > 0);
adjustPayloadStartAndCapacity(ControlPacket::localHeaderSize(), _payloadSize > 0);
readType();
}

View file

@ -33,12 +33,15 @@ public:
TimeoutNAK
};
static std::unique_ptr<ControlPacket> create(Type type, qint64 size = -1);
static std::unique_ptr<ControlPacket> fromReceivedPacket(std::unique_ptr<char> data, qint64 size,
const HifiSockAddr& senderSockAddr);
static std::unique_ptr<ControlPacket> create(Type type, qint64 size = -1);
static qint64 localHeaderSize(); // Current level's header size
virtual qint64 totalHeadersSize() const; // Cumulated size of all the headers
// Current level's header size
static int localHeaderSize();
// Cumulated size of all the headers
static int totalHeaderSize();
// The maximum payload size this packet can use to fit in MTU
static int maxPayloadSize();
Type getType() const { return _type; }
void setType(Type type);
@ -53,16 +56,13 @@ private:
ControlPacket& operator=(ControlPacket&& other);
ControlPacket& operator=(const ControlPacket& other) = delete;
// Header writers
void writeControlBitAndType();
void writeType();
// Header readers
// Header read/write
void readType();
void writeType();
Type _type;
};
} // namespace udt

View file

@ -13,6 +13,20 @@
using namespace udt;
int Packet::localHeaderSize(bool isPartOfMessage) {
return sizeof(Packet::SequenceNumberAndBitField) +
(isPartOfMessage ? sizeof(Packet::MessageNumberAndBitField) : 0);
}
int Packet::totalHeaderSize(bool isPartOfMessage) {
return BasePacket::totalHeaderSize() + Packet::localHeaderSize(isPartOfMessage);
}
int Packet::maxPayloadSize(bool isPartOfMessage) {
return BasePacket::maxPayloadSize() - Packet::localHeaderSize(isPartOfMessage);
}
std::unique_ptr<Packet> Packet::create(qint64 size, bool isReliable, bool isPartOfMessage) {
auto packet = std::unique_ptr<Packet>(new Packet(size, isReliable, isPartOfMessage));
@ -37,32 +51,12 @@ std::unique_ptr<Packet> Packet::createCopy(const Packet& other) {
return std::unique_ptr<Packet>(new Packet(other));
}
qint64 Packet::maxPayloadSize(bool isPartOfMessage) {
return MAX_PACKET_SIZE - localHeaderSize(isPartOfMessage);
}
qint64 Packet::localHeaderSize(bool isPartOfMessage) {
return sizeof(SequenceNumberAndBitField) + (isPartOfMessage ? sizeof(MessageNumberAndBitField) : 0);
}
qint64 Packet::maxPayloadSize() const {
return MAX_PACKET_SIZE - localHeaderSize();
}
qint64 Packet::totalHeadersSize() const {
return BasePacket::totalHeadersSize() + Packet::localHeaderSize();
}
qint64 Packet::localHeaderSize() const {
return localHeaderSize(_isPartOfMessage);
}
Packet::Packet(qint64 size, bool isReliable, bool isPartOfMessage) :
BasePacket(size),
BasePacket(Packet::localHeaderSize() + size),
_isReliable(isReliable),
_isPartOfMessage(isPartOfMessage)
{
adjustPayloadStartAndCapacity(localHeaderSize());
adjustPayloadStartAndCapacity(Packet::localHeaderSize(_isPartOfMessage));
// set the UDT header to default values
writeHeader();
@ -73,7 +67,7 @@ Packet::Packet(std::unique_ptr<char> data, qint64 size, const HifiSockAddr& send
{
readHeader();
adjustPayloadStartAndCapacity(localHeaderSize(), _payloadSize > 0);
adjustPayloadStartAndCapacity(Packet::localHeaderSize(_isPartOfMessage), _payloadSize > 0);
}
Packet::Packet(const Packet& other) :

View file

@ -38,15 +38,12 @@ public:
// Provided for convenience, try to limit use
static std::unique_ptr<Packet> createCopy(const Packet& other);
// The maximum payload size this packet can use to fit in MTU
static qint64 maxPayloadSize(bool isPartOfMessage);
virtual qint64 maxPayloadSize() const;
// Current level's header size
static qint64 localHeaderSize(bool isPartOfMessage);
virtual qint64 localHeaderSize() const;
virtual qint64 totalHeadersSize() const; // Cumulated size of all the headers
static int localHeaderSize(bool isPartOfMessage = false);
// Cumulated size of all the headers
static int totalHeaderSize(bool isPartOfMessage = false);
// The maximum payload size this packet can use to fit in MTU
static int maxPayloadSize(bool isPartOfMessage = false);
bool isPartOfMessage() const { return _isPartOfMessage; }
bool isReliable() const { return _isReliable; }