mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-19 10:18:43 +02:00
Merge branch 'protocol' of https://github.com/huffman/hifi into atp
This commit is contained in:
commit
71f824bc6a
17 changed files with 248 additions and 22 deletions
|
@ -1797,7 +1797,7 @@ void Application::sendPingPackets() {
|
|||
return false;
|
||||
}
|
||||
}, [nodeList](const SharedNodePointer& node) {
|
||||
nodeList->sendPacket(std::move(nodeList->constructPingPacket()), *node);
|
||||
nodeList->sendPacket(nodeList->constructPingPacket(), *node);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -371,6 +371,22 @@ int AvatarData::parseDataFromBuffer(const QByteArray& buffer) {
|
|||
_headData->setBaseRoll(headRoll);
|
||||
} // 6 bytes
|
||||
|
||||
{ // Head lean (relative to pelvis)
|
||||
float leanForward, leanSideways, torsoTwist;
|
||||
sourceBuffer += unpackFloatAngleFromTwoByte((uint16_t*)sourceBuffer, &leanForward);
|
||||
sourceBuffer += unpackFloatAngleFromTwoByte((uint16_t*)sourceBuffer, &leanSideways);
|
||||
sourceBuffer += unpackFloatAngleFromTwoByte((uint16_t*)sourceBuffer, &torsoTwist);
|
||||
if (glm::isnan(leanForward) || glm::isnan(leanSideways)) {
|
||||
if (shouldLogError(now)) {
|
||||
qCDebug(avatars) << "Discard nan AvatarData::leanForward,leanSideways,torsoTwise; displayName = '" << _displayName << "'";
|
||||
}
|
||||
return maxAvailableSize;
|
||||
}
|
||||
_headData->_leanForward = leanForward;
|
||||
_headData->_leanSideways = leanSideways;
|
||||
_headData->_torsoTwist = torsoTwist;
|
||||
} // 6 bytes
|
||||
|
||||
{ // Lookat Position
|
||||
glm::vec3 lookAt;
|
||||
memcpy(&lookAt, sourceBuffer, sizeof(lookAt));
|
||||
|
|
|
@ -22,7 +22,7 @@ AvatarHashMap::AvatarHashMap() {
|
|||
connect(DependencyManager::get<NodeList>().data(), &NodeList::uuidChanged, this, &AvatarHashMap::sessionUUIDChanged);
|
||||
|
||||
auto& packetReceiver = DependencyManager::get<NodeList>()->getPacketReceiver();
|
||||
packetReceiver.registerListener(PacketType::BulkAvatarData, this, "processAvatarDataPacket");
|
||||
packetReceiver.registerListener(PacketType::AvatarData, this, "processAvatarDataPacket");
|
||||
packetReceiver.registerListener(PacketType::KillAvatar, this, "processKillAvatar");
|
||||
packetReceiver.registerListener(PacketType::AvatarIdentity, this, "processAvatarIdentityPacket");
|
||||
packetReceiver.registerListener(PacketType::AvatarBillboard, this, "processAvatarBillboardPacket");
|
||||
|
|
|
@ -826,7 +826,7 @@ std::unique_ptr<NLPacket> EntityTree::encodeEntitiesDeletedSince(OCTREE_PACKET_S
|
|||
deletesPacket->seek(numberOfIDsPos);
|
||||
deletesPacket->writePrimitive(numberOfIDs);
|
||||
|
||||
return std::move(deletesPacket);
|
||||
return deletesPacket;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -50,7 +50,7 @@ LimitedNodeList::LimitedNodeList(unsigned short socketListenPort, unsigned short
|
|||
_localSockAddr(),
|
||||
_publicSockAddr(),
|
||||
_stunSockAddr(STUN_SERVER_HOSTNAME, STUN_SERVER_PORT),
|
||||
_packetReceiver(this),
|
||||
_packetReceiver(new PacketReceiver(this)),
|
||||
_numCollectedPackets(0),
|
||||
_numCollectedBytes(0),
|
||||
_packetStatTimer(),
|
||||
|
@ -99,12 +99,12 @@ LimitedNodeList::LimitedNodeList(unsigned short socketListenPort, unsigned short
|
|||
|
||||
// TODO: Create a new thread, and move PacketReceiver to it
|
||||
|
||||
connect(&_nodeSocket, &QUdpSocket::readyRead, &_packetReceiver, &PacketReceiver::processDatagrams);
|
||||
connect(&_nodeSocket, &QUdpSocket::readyRead, _packetReceiver, &PacketReceiver::processDatagrams);
|
||||
|
||||
_packetStatTimer.start();
|
||||
|
||||
// make sure we handle STUN response packets
|
||||
_packetReceiver.registerListener(PacketType::StunResponse, this, "processSTUNResponse");
|
||||
_packetReceiver->registerListener(PacketType::StunResponse, this, "processSTUNResponse");
|
||||
}
|
||||
|
||||
void LimitedNodeList::setSessionUUID(const QUuid& sessionUUID) {
|
||||
|
@ -290,7 +290,7 @@ qint64 LimitedNodeList::sendPacketList(NLPacketList& packetList, const Node& des
|
|||
packetList.closeCurrentPacket();
|
||||
|
||||
while (!packetList._packets.empty()) {
|
||||
bytesSent += sendPacket(std::move(packetList.takeFront<NLPacket>()), destinationNode);
|
||||
bytesSent += sendPacket(packetList.takeFront<NLPacket>(), destinationNode);
|
||||
}
|
||||
|
||||
return bytesSent;
|
||||
|
@ -304,7 +304,7 @@ qint64 LimitedNodeList::sendPacketList(NLPacketList& packetList, const HifiSockA
|
|||
packetList.closeCurrentPacket();
|
||||
|
||||
while (!packetList._packets.empty()) {
|
||||
bytesSent += sendPacket(std::move(packetList.takeFront<NLPacket>()), sockAddr, connectionSecret);
|
||||
bytesSent += sendPacket(packetList.takeFront<NLPacket>(), sockAddr, connectionSecret);
|
||||
}
|
||||
|
||||
return bytesSent;
|
||||
|
|
|
@ -117,7 +117,7 @@ public:
|
|||
|
||||
bool packetSourceAndHashMatch(const NLPacket& packet, SharedNodePointer& matchingNode);
|
||||
|
||||
PacketReceiver& getPacketReceiver() { return _packetReceiver; }
|
||||
PacketReceiver& getPacketReceiver() { return *_packetReceiver; }
|
||||
|
||||
qint64 sendUnreliablePacket(const NLPacket& packet, const Node& destinationNode);
|
||||
qint64 sendUnreliablePacket(const NLPacket& packet, const HifiSockAddr& sockAddr,
|
||||
|
@ -277,7 +277,7 @@ protected:
|
|||
HifiSockAddr _publicSockAddr;
|
||||
HifiSockAddr _stunSockAddr;
|
||||
|
||||
PacketReceiver _packetReceiver;
|
||||
PacketReceiver* _packetReceiver;
|
||||
|
||||
// XXX can BandwidthRecorder be used for this?
|
||||
int _numCollectedPackets;
|
||||
|
|
|
@ -17,6 +17,6 @@ NLPacketList::NLPacketList(PacketType::Value packetType) : PacketList(packetType
|
|||
}
|
||||
|
||||
std::unique_ptr<Packet> NLPacketList::createPacket() {
|
||||
return std::move(NLPacket::create(getType()));
|
||||
return NLPacket::create(getType());
|
||||
}
|
||||
|
||||
|
|
|
@ -11,6 +11,8 @@
|
|||
|
||||
#include "Packet.h"
|
||||
|
||||
const qint64 Packet::PACKET_WRITE_ERROR = -1;
|
||||
|
||||
qint64 Packet::localHeaderSize(PacketType::Value type) {
|
||||
qint64 size = numBytesForArithmeticCodedPacketType(type) + sizeof(PacketVersion) +
|
||||
((SEQUENCE_NUMBERED_PACKETS.contains(type)) ? sizeof(SequenceNumber) : 0);
|
||||
|
@ -208,7 +210,6 @@ void Packet::writeSequenceNumber(SequenceNumber seqNum) {
|
|||
&seqNum, sizeof(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
|
||||
|
|
|
@ -24,6 +24,8 @@ class Packet : public QIODevice {
|
|||
public:
|
||||
using SequenceNumber = uint16_t;
|
||||
|
||||
static const qint64 PACKET_WRITE_ERROR;
|
||||
|
||||
static std::unique_ptr<Packet> create(PacketType::Value type, qint64 size = -1);
|
||||
static std::unique_ptr<Packet> fromReceivedPacket(std::unique_ptr<char> data, qint64 size, const HifiSockAddr& senderSockAddr);
|
||||
|
||||
|
@ -61,7 +63,7 @@ public:
|
|||
// Returns the number of bytes allocated for the payload
|
||||
qint64 getPayloadCapacity() const { return _payloadCapacity; }
|
||||
|
||||
qint64 bytesLeftToRead() const { return _payloadCapacity - pos(); }
|
||||
qint64 bytesLeftToRead() const { return _payloadSize - pos(); }
|
||||
qint64 bytesAvailableForWrite() const { return _payloadCapacity - pos(); }
|
||||
|
||||
HifiSockAddr& getSenderSockAddr() { return _senderSockAddr; }
|
||||
|
@ -114,7 +116,6 @@ protected:
|
|||
HifiSockAddr _senderSockAddr; // sender address for packet (only used on receiving end)
|
||||
};
|
||||
|
||||
|
||||
template<typename T> qint64 Packet::peekPrimitive(T* data) {
|
||||
return QIODevice::peek(reinterpret_cast<char*>(data), sizeof(T));
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ void PacketList::endSegment() {
|
|||
|
||||
std::unique_ptr<Packet> PacketList::createPacket() {
|
||||
// use the static create method to create a new packet
|
||||
return std::move(Packet::create(getType()));
|
||||
return Packet::create(getType());
|
||||
}
|
||||
|
||||
std::unique_ptr<Packet> PacketList::createPacketWithExtendedHeader() {
|
||||
|
@ -46,7 +46,7 @@ std::unique_ptr<Packet> PacketList::createPacketWithExtendedHeader() {
|
|||
}
|
||||
}
|
||||
|
||||
return std::move(packet);
|
||||
return packet;
|
||||
}
|
||||
|
||||
qint64 PacketList::writeData(const char* data, qint64 maxSize) {
|
||||
|
|
|
@ -80,7 +80,7 @@ template<typename T> std::unique_ptr<T> PacketList::takeFront() {
|
|||
|
||||
auto packet = std::move(_packets.front());
|
||||
_packets.pop_front();
|
||||
return std::move(std::unique_ptr<T>(dynamic_cast<T*>(packet.release())));
|
||||
return std::unique_ptr<T>(dynamic_cast<T*>(packet.release()));
|
||||
}
|
||||
|
||||
#endif // hifi_PacketList_h
|
|
@ -260,7 +260,7 @@ std::unique_ptr<NLPacket> JurisdictionMap::packEmptyJurisdictionIntoMessage(Node
|
|||
// No root or end node details to pack!
|
||||
packet->writePrimitive(bytes);
|
||||
|
||||
return std::move(packet); // includes header!
|
||||
return packet; // includes header!
|
||||
}
|
||||
|
||||
std::unique_ptr<NLPacket> JurisdictionMap::packIntoPacket() {
|
||||
|
@ -295,7 +295,7 @@ std::unique_ptr<NLPacket> JurisdictionMap::packIntoPacket() {
|
|||
packet->writePrimitive(bytes);
|
||||
}
|
||||
|
||||
return std::move(packet);
|
||||
return packet;
|
||||
}
|
||||
|
||||
int JurisdictionMap::unpackFromPacket(NLPacket& packet) {
|
||||
|
|
|
@ -121,8 +121,7 @@ void OctreeEditPacketSender::processPreServerExistsPackets() {
|
|||
// First send out all the single message packets...
|
||||
_pendingPacketsLock.lock();
|
||||
while (!_preServerSingleMessagePackets.empty()) {
|
||||
std::unique_ptr<NLPacket> packet = std::move(_preServerSingleMessagePackets.front());
|
||||
queuePacketToNodes(std::move(packet));
|
||||
queuePacketToNodes(std::move(_preServerSingleMessagePackets.front()));
|
||||
_preServerSingleMessagePackets.pop_front();
|
||||
}
|
||||
|
||||
|
|
|
@ -327,7 +327,7 @@ glm::vec2 toGlm(const QPointF & pt) {
|
|||
|
||||
glm::vec3 toGlm(const xColor & color) {
|
||||
static const float MAX_COLOR = 255.0f;
|
||||
return std::move(glm::vec3(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR));
|
||||
return glm::vec3(color.red, color.green, color.blue) / MAX_COLOR;
|
||||
}
|
||||
|
||||
glm::vec4 toGlm(const QColor & color) {
|
||||
|
|
|
@ -251,6 +251,31 @@ do { \
|
|||
|
||||
#endif
|
||||
|
||||
#define QCOMPARE_WITH_EXPR(actual, expected, testExpr) \
|
||||
do { \
|
||||
if (!(testExpr)) { \
|
||||
QTest_failWithMessage("Compared values are not the same", (actual), (expected), #actual, #expected, __LINE__, __FILE__); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
|
||||
struct ByteData {
|
||||
ByteData (const char* data, size_t length)
|
||||
: data(data), length(length) {}
|
||||
const char* data;
|
||||
size_t length;
|
||||
};
|
||||
|
||||
QTextStream & operator << (QTextStream& stream, const ByteData & wrapper) {
|
||||
// Print bytes as hex
|
||||
stream << QByteArray::fromRawData(wrapper.data, wrapper.length).toHex();
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
bool compareData (const char* data, const char* expectedData, size_t length) {
|
||||
return memcmp(data, expectedData, length) == 0;
|
||||
}
|
||||
|
||||
#define COMPARE_DATA(actual, expected, length) \
|
||||
QCOMPARE_WITH_EXPR((ByteData ( actual, length )), (ByteData ( expected, length )), compareData(actual, expected, length))
|
||||
|
|
|
@ -10,5 +10,173 @@
|
|||
//
|
||||
|
||||
#include "PacketTests.h"
|
||||
#include "../QTestExtensions.h"
|
||||
|
||||
#include <udt/Packet.h>
|
||||
|
||||
QTEST_MAIN(PacketTests)
|
||||
|
||||
std::unique_ptr<Packet> copyToReadPacket(std::unique_ptr<Packet>& packet) {
|
||||
auto size = packet->getDataSize();
|
||||
auto data = std::unique_ptr<char>(new char[size]);
|
||||
memcpy(data.get(), packet->getData(), size);
|
||||
return Packet::fromReceivedPacket(std::move(data), size, HifiSockAddr());
|
||||
}
|
||||
|
||||
void PacketTests::emptyPacketTest() {
|
||||
auto packet = Packet::create(PacketType::Unknown);
|
||||
|
||||
QCOMPARE(packet->getType(), PacketType::Unknown);
|
||||
QCOMPARE(packet->getPayloadSize(), 0);
|
||||
QCOMPARE(packet->getDataSize(), packet->totalHeadersSize());
|
||||
QCOMPARE(packet->bytesLeftToRead(), 0);
|
||||
QCOMPARE(packet->bytesAvailableForWrite(), packet->getPayloadCapacity());
|
||||
}
|
||||
|
||||
void PacketTests::packetTypeTest() {
|
||||
auto packet = Packet::create(PacketType::EntityAdd);
|
||||
|
||||
QCOMPARE(packet->getType(), PacketType::EntityAdd);
|
||||
|
||||
packet->setType(PacketType::MixedAudio);
|
||||
QCOMPARE(packet->getType(), PacketType::MixedAudio);
|
||||
|
||||
packet->setType(PacketType::MuteEnvironment);
|
||||
QCOMPARE(packet->getType(), PacketType::MuteEnvironment);
|
||||
}
|
||||
|
||||
void PacketTests::writeTest() {
|
||||
auto packet = Packet::create(PacketType::Unknown);
|
||||
|
||||
QCOMPARE(packet->getPayloadSize(), 0);
|
||||
|
||||
QCOMPARE(packet->write("somedata"), 8);
|
||||
QCOMPARE(packet->getPayloadSize(), 8);
|
||||
|
||||
QCOMPARE(packet->write("moredata"), 8);
|
||||
QCOMPARE(packet->getPayloadSize(), 16);
|
||||
|
||||
COMPARE_DATA(packet->getPayload(), "somedatamoredata", 16);
|
||||
}
|
||||
|
||||
void PacketTests::readTest() {
|
||||
// Test reads for several different size packets
|
||||
for (int i = 1; i < 4; i++) {
|
||||
auto packet = Packet::create(PacketType::Unknown);
|
||||
|
||||
auto size = packet->getPayloadCapacity();
|
||||
size /= i;
|
||||
|
||||
char* data = new char[size];
|
||||
|
||||
// Fill with "random" data
|
||||
for (qint64 i = 0; i < size; i++) {
|
||||
data[i] = i;
|
||||
}
|
||||
|
||||
QCOMPARE(packet->write(data, size), size);
|
||||
|
||||
auto readPacket = copyToReadPacket(packet);
|
||||
|
||||
QCOMPARE(readPacket->getDataSize(), packet->getDataSize());
|
||||
QCOMPARE(readPacket->getPayloadSize(), packet->getPayloadSize());
|
||||
COMPARE_DATA(readPacket->getData(), packet->getData(), packet->getDataSize());
|
||||
COMPARE_DATA(readPacket->getPayload(), packet->getPayload(), packet->getPayloadSize());
|
||||
|
||||
auto payloadSize = readPacket->getPayloadSize();
|
||||
char* readData = new char[payloadSize];
|
||||
QCOMPARE(readPacket->read(readData, payloadSize), payloadSize);
|
||||
COMPARE_DATA(readData, data, payloadSize);
|
||||
}
|
||||
}
|
||||
|
||||
void PacketTests::writePastCapacityTest() {
|
||||
auto packet = Packet::create(PacketType::Unknown);
|
||||
|
||||
auto size = packet->getPayloadCapacity();
|
||||
char* data = new char[size];
|
||||
|
||||
// Fill with "random" data
|
||||
for (qint64 i = 0; i < size; i++) {
|
||||
data[i] = i;
|
||||
}
|
||||
|
||||
QCOMPARE(packet->getPayloadSize(), 0);
|
||||
|
||||
QCOMPARE(packet->write(data, size), size);
|
||||
|
||||
QCOMPARE(packet->getPayloadSize(), size);
|
||||
COMPARE_DATA(packet->getPayload(), data, size);
|
||||
|
||||
QCOMPARE(packet->bytesAvailableForWrite(), 0);
|
||||
QCOMPARE(packet->getPayloadSize(), size);
|
||||
|
||||
QCOMPARE(Packet::PACKET_WRITE_ERROR, packet->write("data"));
|
||||
|
||||
// Packet::write() shouldn't allow the caller to write if no space is left
|
||||
QCOMPARE(packet->getPayloadSize(), size);
|
||||
}
|
||||
|
||||
void PacketTests::primitiveTest() {
|
||||
auto packet = Packet::create(PacketType::Unknown);
|
||||
|
||||
int value1 = 5;
|
||||
char value2 = 10;
|
||||
bool value3 = true;
|
||||
qint64 value4 = -93404;
|
||||
|
||||
packet->writePrimitive(value1);
|
||||
packet->writePrimitive(value2);
|
||||
packet->writePrimitive(value3);
|
||||
packet->writePrimitive(value4);
|
||||
|
||||
auto recvPacket = copyToReadPacket(packet);
|
||||
|
||||
// Peek & read first value
|
||||
{
|
||||
int peekValue = 0;
|
||||
QCOMPARE(recvPacket->peekPrimitive(&peekValue), (int)sizeof(peekValue));
|
||||
QCOMPARE(peekValue, value1);
|
||||
int readValue = 0;
|
||||
QCOMPARE(recvPacket->readPrimitive(&readValue), (int)sizeof(readValue));
|
||||
QCOMPARE(readValue, value1);
|
||||
}
|
||||
|
||||
// Peek & read second value
|
||||
{
|
||||
char peekValue = 0;
|
||||
QCOMPARE(recvPacket->peekPrimitive(&peekValue), (int)sizeof(peekValue));
|
||||
QCOMPARE(peekValue, value2);
|
||||
|
||||
char readValue = 0;
|
||||
QCOMPARE(recvPacket->readPrimitive(&readValue), (int)sizeof(readValue));
|
||||
QCOMPARE(readValue, value2);
|
||||
}
|
||||
|
||||
// Peek & read third value
|
||||
{
|
||||
bool peekValue = 0;
|
||||
QCOMPARE(recvPacket->peekPrimitive(&peekValue), (int)sizeof(peekValue));
|
||||
QCOMPARE(peekValue, value3);
|
||||
|
||||
bool readValue = 0;
|
||||
QCOMPARE(recvPacket->readPrimitive(&readValue), (int)sizeof(readValue));
|
||||
QCOMPARE(readValue, value3);
|
||||
}
|
||||
|
||||
// Peek & read fourth value
|
||||
{
|
||||
qint64 peekValue = 0;
|
||||
QCOMPARE(recvPacket->peekPrimitive(&peekValue), (int)sizeof(peekValue));
|
||||
QCOMPARE(peekValue, value4);
|
||||
|
||||
qint64 readValue = 0;
|
||||
QCOMPARE(recvPacket->readPrimitive(&readValue), (int)sizeof(readValue));
|
||||
QCOMPARE(readValue, value4);
|
||||
}
|
||||
|
||||
// We should be at the end of the packet, with no more to peek or read
|
||||
int noValue;
|
||||
QCOMPARE(recvPacket->peekPrimitive(&noValue), 0);
|
||||
QCOMPARE(recvPacket->readPrimitive(&noValue), 0);
|
||||
}
|
||||
|
|
|
@ -19,7 +19,23 @@
|
|||
class PacketTests : public QObject {
|
||||
Q_OBJECT
|
||||
private slots:
|
||||
// Test the base state of Packet
|
||||
void emptyPacketTest();
|
||||
|
||||
// Test Packet writes
|
||||
void writeTest();
|
||||
|
||||
// Test Packet reads
|
||||
void readTest();
|
||||
|
||||
// Test Packet writing past capacity
|
||||
void writePastCapacityTest();
|
||||
|
||||
// Test primitive reads and writes
|
||||
void primitiveTest();
|
||||
|
||||
// Test set/get packet type
|
||||
void packetTypeTest();
|
||||
};
|
||||
|
||||
#endif // hifi_PacketTests_h
|
||||
|
|
Loading…
Reference in a new issue