repairs to ping-reply, removal of deconstructPacketHeader to simply return UUID

This commit is contained in:
Stephen Birarda 2014-02-07 10:20:50 -08:00
parent b988308a8d
commit fa1825fa32
10 changed files with 65 additions and 69 deletions

View file

@ -835,10 +835,8 @@ void AnimationServer::readPendingDatagrams() {
int headerBytes = numBytesForPacketHeader(receivedPacket);
// PacketType_JURISDICTION, first byte is the node type...
if (receivedPacket.data()[headerBytes] == NodeType::VoxelServer && ::jurisdictionListener) {
QUuid nodeUUID;
deconstructPacketHeader(receivedPacket, nodeUUID);
SharedNodePointer matchedNode = NodeList::getInstance()->nodeWithUUID(nodeUUID);
SharedNodePointer matchedNode = NodeList::getInstance()->sendingNodeForPacket(receivedPacket);
if (matchedNode) {
::jurisdictionListener->queueReceivedPacket(matchedNode, receivedPacket);
}

View file

@ -213,12 +213,10 @@ void AudioMixer::processDatagram(const QByteArray& dataByteArray, const HifiSock
if (mixerPacketType == PacketTypeMicrophoneAudioNoEcho
|| mixerPacketType == PacketTypeMicrophoneAudioWithEcho
|| mixerPacketType == PacketTypeInjectAudio) {
QUuid nodeUUID;
deconstructPacketHeader(dataByteArray, nodeUUID);
NodeList* nodeList = NodeList::getInstance();
SharedNodePointer matchingNode = nodeList->nodeWithUUID(nodeUUID);
SharedNodePointer matchingNode = nodeList->sendingNodeForPacket(dataByteArray);
if (matchingNode) {
nodeList->updateNodeWithData(matchingNode.data(), senderSockAddr, dataByteArray);

View file

@ -142,11 +142,9 @@ void AvatarMixer::processDatagram(const QByteArray& dataByteArray, const HifiSoc
switch (packetTypeForPacket(dataByteArray)) {
case PacketTypeAvatarData: {
QUuid nodeUUID;
deconstructPacketHeader(dataByteArray, nodeUUID);
// add or update the node in our list
SharedNodePointer avatarNode = nodeList->nodeWithUUID(nodeUUID);
SharedNodePointer avatarNode = nodeList->sendingNodeForPacket(dataByteArray);
if (avatarNode) {
// parse positional data from an node
@ -156,11 +154,9 @@ void AvatarMixer::processDatagram(const QByteArray& dataByteArray, const HifiSoc
break;
}
case PacketTypeAvatarIdentity: {
QUuid nodeUUID;
deconstructPacketHeader(dataByteArray, nodeUUID);
// check if we have a matching node in our list
SharedNodePointer avatarNode = nodeList->nodeWithUUID(nodeUUID);
SharedNodePointer avatarNode = nodeList->sendingNodeForPacket(dataByteArray);
if (avatarNode && avatarNode->getLinkedData()) {
AvatarMixerClientData* nodeData = reinterpret_cast<AvatarMixerClientData*>(avatarNode->getLinkedData());
@ -170,7 +166,7 @@ void AvatarMixer::processDatagram(const QByteArray& dataByteArray, const HifiSoc
QByteArray identityPacket = byteArrayWithPopluatedHeader(PacketTypeAvatarIdentity);
QByteArray individualByteArray = nodeData->identityByteArray();
individualByteArray.replace(0, NUM_BYTES_RFC4122_UUID, nodeUUID.toRfc4122());
individualByteArray.replace(0, NUM_BYTES_RFC4122_UUID, avatarNode->getUUID().toRfc4122());
identityPacket.append(individualByteArray);

View file

@ -463,10 +463,7 @@ void OctreeServer::processDatagram(const QByteArray& dataByteArray, const HifiSo
PacketType packetType = packetTypeForPacket(dataByteArray);
QUuid nodeUUID;
deconstructPacketHeader(dataByteArray, nodeUUID);
SharedNodePointer matchingNode = nodeList->nodeWithUUID(nodeUUID);
SharedNodePointer matchingNode = nodeList->sendingNodeForPacket(dataByteArray);
if (packetType == getMyQueryMessageType()) {
bool debug = false;
@ -487,7 +484,7 @@ void OctreeServer::processDatagram(const QByteArray& dataByteArray, const HifiSo
}
OctreeQueryNode* nodeData = (OctreeQueryNode*) matchingNode->getLinkedData();
if (nodeData && !nodeData->isOctreeSendThreadInitalized()) {
nodeData->initializeOctreeSendThread(this, nodeUUID);
nodeData->initializeOctreeSendThread(this, matchingNode->getUUID());
}
}
} else if (packetType == PacketTypeJurisdictionRequest) {

View file

@ -229,7 +229,7 @@ void DomainServer::readAvailableDatagrams() {
QByteArray receivedPacket;
NodeType_t nodeType;
QUuid nodeUUID;
while (nodeList->getNodeSocket().hasPendingDatagrams()) {
receivedPacket.resize(nodeList->getNodeSocket().pendingDatagramSize());
@ -244,7 +244,7 @@ void DomainServer::readAvailableDatagrams() {
QDataStream packetStream(receivedPacket);
packetStream.skipRawData(numBytesForPacketHeader(receivedPacket));
deconstructPacketHeader(receivedPacket, nodeUUID);
QUuid nodeUUID = uuidFromPacketHeader(receivedPacket);
packetStream >> nodeType;
packetStream >> nodePublicAddress >> nodeLocalAddress;

View file

@ -40,9 +40,6 @@ void DatagramProcessor::processDatagrams() {
_packetCount++;
_byteCount += incomingPacket.size();
QUuid nodeUUID;
deconstructPacketHeader(incomingPacket, nodeUUID);
if (packetVersionMatch(incomingPacket)) {
// only process this packet if we have a match on the packet version
switch (packetTypeForPacket(incomingPacket)) {
@ -87,7 +84,7 @@ void DatagramProcessor::processDatagrams() {
printf("got PacketType_VOXEL_DATA, sequence:%d flightTime:%d\n", sequence, flightTime);
}
SharedNodePointer matchedNode = NodeList::getInstance()->nodeWithUUID(nodeUUID);
SharedNodePointer matchedNode = NodeList::getInstance()->sendingNodeForPacket(incomingPacket);
if (matchedNode) {
// add this packet to our list of voxel packets and process them on the voxel processing
@ -103,7 +100,7 @@ void DatagramProcessor::processDatagrams() {
case PacketTypeKillAvatar:
case PacketTypeAvatarIdentity: {
// update having heard from the avatar-mixer and record the bytes received
SharedNodePointer avatarMixer = nodeList->nodeWithUUID(nodeUUID);
SharedNodePointer avatarMixer = nodeList->sendingNodeForPacket(incomingPacket);
if (avatarMixer) {
avatarMixer->setLastHeardMicrostamp(usecTimestampNow());

View file

@ -142,13 +142,9 @@ void NodeList::setDomainHostname(const QString& domainHostname) {
}
}
void NodeList::timePingReply(const QByteArray& packet) {
QUuid nodeUUID;
deconstructPacketHeader(packet, nodeUUID);
void NodeList::timePingReply(const QByteArray& packet, const SharedNodePointer& sendingNode) {
SharedNodePointer matchingNode = nodeWithUUID(nodeUUID);
if (matchingNode) {
if (sendingNode) {
QDataStream packetStream(packet);
packetStream.skipRawData(numBytesForPacketHeader(packet));
@ -165,13 +161,13 @@ void NodeList::timePingReply(const QByteArray& packet) {
quint64 othersExprectedReply = ourOriginalTime + oneWayFlightTime;
int clockSkew = othersReplyTime - othersExprectedReply;
matchingNode->setPingMs(pingTime / 1000);
matchingNode->setClockSkewUsec(clockSkew);
sendingNode->setPingMs(pingTime / 1000);
sendingNode->setClockSkewUsec(clockSkew);
const bool wantDebug = false;
if (wantDebug) {
qDebug() << "PING_REPLY from node " << *matchingNode << "\n" <<
qDebug() << "PING_REPLY from node " << *sendingNode << "\n" <<
" now: " << now << "\n" <<
" ourTime: " << ourOriginalTime << "\n" <<
" pingTime: " << pingTime << "\n" <<
@ -200,11 +196,16 @@ void NodeList::processNodeData(const HifiSockAddr& senderSockAddr, const QByteAr
break;
}
case PacketTypePingReply: {
// activate the appropriate socket for this node, if not yet updated
activateSocketFromNodeCommunication(senderSockAddr);
// set the ping time for this node for stat collection
timePingReply(packet);
SharedNodePointer sendingNode = sendingNodeForPacket(packet);
if (sendingNode) {
// activate the appropriate socket for this node, if not yet updated
activateSocketFromNodeCommunication(packet, sendingNode);
// set the ping time for this node for stat collection
timePingReply(packet, sendingNode);
}
break;
}
case PacketTypeStunResponse: {
@ -252,8 +253,7 @@ SharedNodePointer NodeList::nodeWithUUID(const QUuid& nodeUUID) {
}
SharedNodePointer NodeList::sendingNodeForPacket(const QByteArray& packet) {
QUuid nodeUUID;
deconstructPacketHeader(packet, nodeUUID);
QUuid nodeUUID = uuidFromPacketHeader(packet);
// return the matching node, or NULL if there is no match
return nodeWithUUID(nodeUUID);
@ -590,10 +590,12 @@ void NodeList::sendAssignment(Assignment& assignment) {
_nodeSocket.writeDatagram(packet, assignmentServerSocket->getAddress(), assignmentServerSocket->getPort());
}
QByteArray NodeList::constructPingPacket() {
QByteArray NodeList::constructPingPacket(PingType_t pingType) {
QByteArray pingPacket = byteArrayWithPopluatedHeader(PacketTypePing);
QDataStream packetStream(&pingPacket, QIODevice::Append);
packetStream << pingType;
packetStream << usecTimestampNow();
return pingPacket;
@ -615,11 +617,13 @@ QByteArray NodeList::constructPingReplyPacket(const QByteArray& pingPacket) {
}
void NodeList::pingPublicAndLocalSocketsForInactiveNode(const SharedNodePointer& node) {
QByteArray pingPacket = constructPingPacket();
// send the ping packet to the local and public sockets for this node
writeDatagram(pingPacket, node);
writeDatagram(pingPacket, node);
QByteArray localPingPacket = constructPingPacket(PingType::Local);
writeDatagram(localPingPacket, node, node->getLocalSocket());
QByteArray publicPingPacket = constructPingPacket(PingType::Public);
writeDatagram(publicPingPacket, node, node->getPublicSocket());
}
SharedNodePointer NodeList::addOrUpdateNode(const QUuid& uuid, char nodeType,
@ -703,20 +707,20 @@ const HifiSockAddr* NodeList::getNodeActiveSocketOrPing(const SharedNodePointer&
return NULL;
}
void NodeList::activateSocketFromNodeCommunication(const HifiSockAddr& nodeAddress) {
foreach (const SharedNodePointer& node, _nodeHash) {
if (!node->getActiveSocket()) {
// check both the public and local addresses for each node to see if we find a match
// prioritize the private address so that we prune erroneous local matches
if (node->getPublicSocket() == nodeAddress) {
node->activatePublicSocket();
break;
} else if (node->getLocalSocket() == nodeAddress) {
node->activateLocalSocket();
break;
}
}
void NodeList::activateSocketFromNodeCommunication(const QByteArray& packet, const SharedNodePointer& sendingNode) {
// deconstruct this ping packet to see if it is a public or local reply
QDataStream packetStream(packet);
packetStream.skipRawData(numBytesForPacketHeader(packet));
quint8 pingType;
packetStream >> pingType;
// if this is a local or public ping then we can activate a socket
// we do nothing with agnostic pings, those are simply for timing
if (pingType == PingType::Local) {
sendingNode->activateLocalSocket();
} else if (pingType == PingType::Public) {
sendingNode->activatePublicSocket();
}
}

View file

@ -54,6 +54,13 @@ typedef QSharedPointer<Node> SharedNodePointer;
typedef QHash<QUuid, SharedNodePointer> NodeHash;
Q_DECLARE_METATYPE(SharedNodePointer)
typedef quint8 PingType_t;
namespace PingType {
const PingType_t Agnostic = 0;
const PingType_t Local = 1;
const PingType_t Public = 2;
}
class NodeList : public QObject {
Q_OBJECT
public:
@ -100,7 +107,7 @@ public:
void setAssignmentServerSocket(const HifiSockAddr& serverSocket) { _assignmentServerSocket = serverSocket; }
void sendAssignment(Assignment& assignment);
QByteArray constructPingPacket();
QByteArray constructPingPacket(PingType_t pingType = PingType::Agnostic);
QByteArray constructPingReplyPacket(const QByteArray& pingPacket);
void pingPublicAndLocalSocketsForInactiveNode(const SharedNodePointer& node);
@ -158,8 +165,8 @@ private:
bool _hasCompletedInitialSTUNFailure;
unsigned int _stunRequestsSinceSuccess;
void activateSocketFromNodeCommunication(const HifiSockAddr& nodeSockAddr);
void timePingReply(const QByteArray& packet);
void activateSocketFromNodeCommunication(const QByteArray& packet, const SharedNodePointer& sendingNode);
void timePingReply(const QByteArray& packet, const SharedNodePointer& sendingNode);
void resetDomainData(char domainField[], const char* domainData);
void domainLookup();
void clear();

View file

@ -95,8 +95,7 @@ bool packetVersionMatch(const QByteArray& packet) {
PacketType mismatchType = packetTypeForPacket(packet);
int numPacketTypeBytes = arithmeticCodingValueFromBuffer(packet.data());
QUuid nodeUUID;
deconstructPacketHeader(packet, nodeUUID);
QUuid nodeUUID = uuidFromPacketHeader(packet);
qDebug() << "Packet mismatch on" << packetTypeForPacket(packet) << "- Sender"
<< nodeUUID << "sent" << qPrintable(QString::number(packet[numPacketTypeBytes])) << "but"
@ -120,9 +119,9 @@ int numBytesForPacketHeaderGivenPacketType(PacketType type) {
return (int) ceilf((float)type / 255) + NUM_STATIC_HEADER_BYTES;
}
void deconstructPacketHeader(const QByteArray& packet, QUuid& senderUUID) {
senderUUID = QUuid::fromRfc4122(packet.mid(numBytesArithmeticCodingFromBuffer(packet.data()) + sizeof(PacketVersion),
NUM_BYTES_RFC4122_UUID));
QUuid uuidFromPacketHeader(const QByteArray& packet) {
return QUuid::fromRfc4122(packet.mid(numBytesArithmeticCodingFromBuffer(packet.data()) + sizeof(PacketVersion),
NUM_BYTES_RFC4122_UUID));
}
PacketType packetTypeForPacket(const QByteArray& packet) {

View file

@ -76,7 +76,7 @@ int numBytesForPacketHeader(const QByteArray& packet);
int numBytesForPacketHeader(const char* packet);
int numBytesForPacketHeaderGivenPacketType(PacketType type);
void deconstructPacketHeader(const QByteArray& packet, QUuid& senderUUID);
QUuid uuidFromPacketHeader(const QByteArray& packet);
PacketType packetTypeForPacket(const QByteArray& packet);
PacketType packetTypeForPacket(const char* packet);