diff --git a/assignment-client/src/audio/AvatarAudioStream.cpp b/assignment-client/src/audio/AvatarAudioStream.cpp index 87581a637c..9fd44d9e85 100644 --- a/assignment-client/src/audio/AvatarAudioStream.cpp +++ b/assignment-client/src/audio/AvatarAudioStream.cpp @@ -18,7 +18,7 @@ AvatarAudioStream::AvatarAudioStream(bool isStereo, const InboundAudioStream::Se { } -int AvatarAudioStream::parseStreamProperties(PacketType type, const QByteArray& packetAfterSeqNum, int& numAudioSamples) { +int AvatarAudioStream::parseStreamProperties (PacketType::Value type, const QByteArray& packetAfterSeqNum, int& numAudioSamples) { int readBytes = 0; if (type == PacketTypeSilentAudioFrame) { diff --git a/assignment-client/src/audio/AvatarAudioStream.h b/assignment-client/src/audio/AvatarAudioStream.h index 482c6fd538..78f57bb18f 100644 --- a/assignment-client/src/audio/AvatarAudioStream.h +++ b/assignment-client/src/audio/AvatarAudioStream.h @@ -25,7 +25,7 @@ private: AvatarAudioStream(const AvatarAudioStream&); AvatarAudioStream& operator= (const AvatarAudioStream&); - int parseStreamProperties(PacketType type, const QByteArray& packetAfterSeqNum, int& numAudioSamples); + int parseStreamProperties (PacketType::Value type, const QByteArray& packetAfterSeqNum, int& numAudioSamples); }; #endif // hifi_AvatarAudioStream_h diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 6b6a25f7c9..203b6842be 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -1322,14 +1322,14 @@ void DomainServer::pingPunchForConnectingPeer(const SharedNetworkPeer& peer) { _icePeers.remove(peer->getUUID()); } else { - auto nodeList = DependencyManager::get(); + auto limitedNodeList = DependencyManager::get(); // send the ping packet to the local and public sockets for this node - QByteArray localPingPacket = nodeList->constructPingPacket(PingType::Local, false); - nodeList->writeUnverifiedDatagram(localPingPacket, peer->getLocalSocket()); + auto localPingPacket = nodeList->constructICEPingPacket(PingType::Local, limitedNodeList->getSessionUUID()); + limitedNodeList->sendPacket(localPingPacket, peer->getLocalSocket()); - QByteArray publicPingPacket = nodeList->constructPingPacket(PingType::Public, false); - nodeList->writeUnverifiedDatagram(publicPingPacket, peer->getPublicSocket()); + auto publicPingPacket = nodeList->constructICEPingPacket(PingType::Public, limitedNodeList->getSessionUUID()); + limitedNodeList->sendPacket(publicPingPacket, peer->getPublicSocket()); peer->incrementConnectionAttempts(); } diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index a4c9a61aad..2a5a5a3817 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1097,13 +1097,6 @@ void Application::updateProjectionMatrix(Camera& camera, bool updateViewFrustum) glMatrixMode(GL_MODELVIEW); } -void Application::controlledBroadcastToNodes(const QByteArray& packet, const NodeSet& destinationNodeTypes) { - foreach(NodeType_t type, destinationNodeTypes) { - // Perform the broadcast for one type - DependencyManager::get()->broadcastToNodes(packet, NodeSet() << type); - } -} - bool Application::importSVOFromURL(const QString& urlString) { QUrl url(urlString); emit svoImportRequested(url.url()); @@ -1763,10 +1756,21 @@ bool Application::acceptSnapshot(const QString& urlString) { } void Application::sendPingPackets() { - QByteArray pingPacket = DependencyManager::get()->constructPingPacket(); - controlledBroadcastToNodes(pingPacket, NodeSet() - << NodeType::EntityServer - << NodeType::AudioMixer << NodeType::AvatarMixer); + + auto nodeList = DependencyManager::get(); + + nodeList->eachMatchingNode([](const SharedNodePointer& node)->bool { + switch (node->getType()) { + case NodeType::AvatarMixer: + case NodeType::AudioMixer: + case NodeType::EntityServer: + return true; + default: + return false; + } + }, [nodeList](const SharedNodePointer& node) { + nodeList->sendPacket(std::move(nodeList->constructPingPacket()), node); + }); } // Every second, check the frame rates and other stuff diff --git a/interface/src/Application.h b/interface/src/Application.h index 03487a5306..9a9b4cdea0 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -271,8 +271,6 @@ public: void resetProfile(const QString& username); - void controlledBroadcastToNodes(const QByteArray& packet, const NodeSet& destinationNodeTypes); - virtual void setupWorldLight(); virtual bool shouldRenderMesh(float largestDimension, float distanceToCamera); diff --git a/libraries/audio/src/InboundAudioStream.cpp b/libraries/audio/src/InboundAudioStream.cpp index 315db2029d..3725c22988 100644 --- a/libraries/audio/src/InboundAudioStream.cpp +++ b/libraries/audio/src/InboundAudioStream.cpp @@ -168,7 +168,7 @@ int InboundAudioStream::parseData(const QByteArray& packet) { return readBytes; } -int InboundAudioStream::parseStreamProperties(PacketType type, const QByteArray& packetAfterSeqNum, int& numAudioSamples) { +int InboundAudioStream::parseStreamProperties (PacketType::Value type, const QByteArray& packetAfterSeqNum, int& numAudioSamples) { if (type == PacketTypeSilentAudioFrame) { quint16 numSilentSamples = 0; memcpy(&numSilentSamples, packetAfterSeqNum.constData(), sizeof(quint16)); @@ -181,7 +181,7 @@ int InboundAudioStream::parseStreamProperties(PacketType type, const QByteArray& } } -int InboundAudioStream::parseAudioData(PacketType type, const QByteArray& packetAfterStreamProperties, int numAudioSamples) { +int InboundAudioStream::parseAudioData (PacketType::Value type, const QByteArray& packetAfterStreamProperties, int numAudioSamples) { return _ringBuffer.writeData(packetAfterStreamProperties.data(), numAudioSamples * sizeof(int16_t)); } diff --git a/libraries/audio/src/InboundAudioStream.h b/libraries/audio/src/InboundAudioStream.h index f80961675d..370d853bd2 100644 --- a/libraries/audio/src/InboundAudioStream.h +++ b/libraries/audio/src/InboundAudioStream.h @@ -194,11 +194,11 @@ protected: /// parses the info between the seq num and the audio data in the network packet and calculates /// how many audio samples this packet contains (used when filling in samples for dropped packets). /// default implementation assumes no stream properties and raw audio samples after stream propertiess - virtual int parseStreamProperties(PacketType type, const QByteArray& packetAfterSeqNum, int& networkSamples); + virtual int parseStreamProperties (PacketType::Value type, const QByteArray& packetAfterSeqNum, int& networkSamples); /// parses the audio data in the network packet. /// default implementation assumes packet contains raw audio samples after stream properties - virtual int parseAudioData(PacketType type, const QByteArray& packetAfterStreamProperties, int networkSamples); + virtual int parseAudioData (PacketType::Value type, const QByteArray& packetAfterStreamProperties, int networkSamples); /// writes silent samples to the buffer that may be dropped to reduce latency caused by the buffer virtual int writeDroppableSilentSamples(int silentSamples); diff --git a/libraries/audio/src/InjectedAudioStream.cpp b/libraries/audio/src/InjectedAudioStream.cpp index b405e30df7..b1005fb9db 100644 --- a/libraries/audio/src/InjectedAudioStream.cpp +++ b/libraries/audio/src/InjectedAudioStream.cpp @@ -30,7 +30,7 @@ InjectedAudioStream::InjectedAudioStream(const QUuid& streamIdentifier, const bo const uchar MAX_INJECTOR_VOLUME = 255; -int InjectedAudioStream::parseStreamProperties(PacketType type, +int InjectedAudioStream::parseStreamProperties (PacketType::Value type, const QByteArray& packetAfterSeqNum, int& numAudioSamples) { // setup a data stream to read from this packet diff --git a/libraries/audio/src/InjectedAudioStream.h b/libraries/audio/src/InjectedAudioStream.h index 60c36dfb12..29a9947a24 100644 --- a/libraries/audio/src/InjectedAudioStream.h +++ b/libraries/audio/src/InjectedAudioStream.h @@ -31,7 +31,7 @@ private: InjectedAudioStream& operator= (const InjectedAudioStream&); AudioStreamStats getAudioStreamStats() const; - int parseStreamProperties(PacketType type, const QByteArray& packetAfterSeqNum, int& numAudioSamples); + int parseStreamProperties (PacketType::Value type, const QByteArray& packetAfterSeqNum, int& numAudioSamples); const QUuid _streamIdentifier; float _radius; diff --git a/libraries/audio/src/MixedProcessedAudioStream.cpp b/libraries/audio/src/MixedProcessedAudioStream.cpp index d236ac7aad..2a5b94cac1 100644 --- a/libraries/audio/src/MixedProcessedAudioStream.cpp +++ b/libraries/audio/src/MixedProcessedAudioStream.cpp @@ -42,7 +42,7 @@ int MixedProcessedAudioStream::writeLastFrameRepeatedWithFade(int samples) { return deviceSamplesWritten; } -int MixedProcessedAudioStream::parseAudioData(PacketType type, const QByteArray& packetAfterStreamProperties, int networkSamples) { +int MixedProcessedAudioStream::parseAudioData (PacketType::Value type, const QByteArray& packetAfterStreamProperties, int networkSamples) { emit addedStereoSamples(packetAfterStreamProperties); diff --git a/libraries/audio/src/MixedProcessedAudioStream.h b/libraries/audio/src/MixedProcessedAudioStream.h index 5ea0157421..efd06b996e 100644 --- a/libraries/audio/src/MixedProcessedAudioStream.h +++ b/libraries/audio/src/MixedProcessedAudioStream.h @@ -35,7 +35,7 @@ public: protected: int writeDroppableSilentSamples(int silentSamples); int writeLastFrameRepeatedWithFade(int samples); - int parseAudioData(PacketType type, const QByteArray& packetAfterStreamProperties, int networkSamples); + int parseAudioData (PacketType::Value type, const QByteArray& packetAfterStreamProperties, int networkSamples); private: int networkToDeviceSamples(int networkSamples); diff --git a/libraries/entities/src/EntityEditPacketSender.cpp b/libraries/entities/src/EntityEditPacketSender.cpp index 8d03ca0fcb..c6c61f70a0 100644 --- a/libraries/entities/src/EntityEditPacketSender.cpp +++ b/libraries/entities/src/EntityEditPacketSender.cpp @@ -18,7 +18,7 @@ #include "EntityItem.h" -void EntityEditPacketSender::adjustEditPacketForClockSkew(PacketType type, +void EntityEditPacketSender::adjustEditPacketForClockSkew (PacketType::Value type, unsigned char* editBuffer, size_t length, int clockSkew) { if (type == PacketTypeEntityAdd || type == PacketTypeEntityEdit) { @@ -26,7 +26,7 @@ void EntityEditPacketSender::adjustEditPacketForClockSkew(PacketType type, } } -void EntityEditPacketSender::queueEditEntityMessage(PacketType type, EntityItemID modelID, +void EntityEditPacketSender::queueEditEntityMessage (PacketType::Value type, EntityItemID modelID, const EntityItemProperties& properties) { if (!_shouldSend) { return; // bail early diff --git a/libraries/entities/src/EntityEditPacketSender.h b/libraries/entities/src/EntityEditPacketSender.h index 69171ae16b..ff0c02789e 100644 --- a/libraries/entities/src/EntityEditPacketSender.h +++ b/libraries/entities/src/EntityEditPacketSender.h @@ -24,12 +24,12 @@ public: /// which voxel-server node or nodes the packet should be sent to. Can be called even before voxel servers are known, in /// which case up to MaxPendingMessages will be buffered and processed when voxel servers are known. /// NOTE: EntityItemProperties assumes that all distances are in meter units - void queueEditEntityMessage(PacketType type, EntityItemID modelID, const EntityItemProperties& properties); + void queueEditEntityMessage (PacketType::Value type, EntityItemID modelID, const EntityItemProperties& properties); void queueEraseEntityMessage(const EntityItemID& entityItemID); // My server type is the model server virtual char getMyNodeType() const { return NodeType::EntityServer; } - virtual void adjustEditPacketForClockSkew(PacketType type, unsigned char* editBuffer, size_t length, int clockSkew); + virtual void adjustEditPacketForClockSkew (PacketType::Value type, unsigned char* editBuffer, size_t length, int clockSkew); }; #endif // hifi_EntityEditPacketSender_h diff --git a/libraries/entities/src/EntityItemProperties.cpp b/libraries/entities/src/EntityItemProperties.cpp index 9a1a5494b7..ae021a7b67 100644 --- a/libraries/entities/src/EntityItemProperties.cpp +++ b/libraries/entities/src/EntityItemProperties.cpp @@ -614,7 +614,7 @@ void EntityItemPropertiesFromScriptValueHonorReadOnly(const QScriptValue &object // // TODO: Implement support for script and visible properties. // -bool EntityItemProperties::encodeEntityEditPacket(PacketType command, EntityItemID id, const EntityItemProperties& properties, +bool EntityItemProperties::encodeEntityEditPacket (PacketType::Value command, EntityItemID id, const EntityItemProperties& properties, unsigned char* bufferOut, int sizeIn, int& sizeOut) { OctreePacketData ourDataPacket(false, sizeIn); // create a packetData object to add out packet details too. OctreePacketData* packetData = &ourDataPacket; // we want a pointer to this so we can use our APPEND_ENTITY_PROPERTY macro diff --git a/libraries/entities/src/EntityItemProperties.h b/libraries/entities/src/EntityItemProperties.h index b8200d025c..a1f64e03bd 100644 --- a/libraries/entities/src/EntityItemProperties.h +++ b/libraries/entities/src/EntityItemProperties.h @@ -174,7 +174,7 @@ public: void setGlowLevel(float value) { _glowLevel = value; _glowLevelChanged = true; } void setLocalRenderAlpha(float value) { _localRenderAlpha = value; _localRenderAlphaChanged = true; } - static bool encodeEntityEditPacket(PacketType command, EntityItemID id, const EntityItemProperties& properties, + static bool encodeEntityEditPacket (PacketType::Value command, EntityItemID id, const EntityItemProperties& properties, unsigned char* bufferOut, int sizeIn, int& sizeOut); static bool encodeEraseEntityMessage(const EntityItemID& entityItemID, diff --git a/libraries/entities/src/EntityScriptingInterface.cpp b/libraries/entities/src/EntityScriptingInterface.cpp index 7cc2c03dfc..e4b34fc51f 100644 --- a/libraries/entities/src/EntityScriptingInterface.cpp +++ b/libraries/entities/src/EntityScriptingInterface.cpp @@ -32,7 +32,7 @@ EntityScriptingInterface::EntityScriptingInterface() : connect(nodeList.data(), &NodeList::canRezChanged, this, &EntityScriptingInterface::canRezChanged); } -void EntityScriptingInterface::queueEntityMessage(PacketType packetType, +void EntityScriptingInterface::queueEntityMessage (PacketType::Value packetType, EntityItemID entityID, const EntityItemProperties& properties) { getEntityPacketSender()->queueEditEntityMessage(packetType, entityID, properties); } diff --git a/libraries/entities/src/EntityScriptingInterface.h b/libraries/entities/src/EntityScriptingInterface.h index 5c1e4141a6..378a3cdf4a 100644 --- a/libraries/entities/src/EntityScriptingInterface.h +++ b/libraries/entities/src/EntityScriptingInterface.h @@ -164,7 +164,7 @@ private: bool actionWorker(const QUuid& entityID, std::function actor); bool setVoxels(QUuid entityID, std::function actor); bool setPoints(QUuid entityID, std::function actor); - void queueEntityMessage(PacketType packetType, EntityItemID entityID, const EntityItemProperties& properties); + void queueEntityMessage (PacketType::Value packetType, EntityItemID entityID, const EntityItemProperties& properties); /// actually does the work of finding the ray intersection, can be called in locking mode or tryLock mode RayToEntityIntersectionResult findRayIntersectionWorker(const PickRay& ray, Octree::lockType lockType, diff --git a/libraries/entities/src/EntityTree.cpp b/libraries/entities/src/EntityTree.cpp index d2b94b3267..5a16f5a003 100644 --- a/libraries/entities/src/EntityTree.cpp +++ b/libraries/entities/src/EntityTree.cpp @@ -63,7 +63,7 @@ void EntityTree::eraseAllOctreeElements(bool createNewRoot) { Octree::eraseAllOctreeElements(createNewRoot); } -bool EntityTree::handlesEditPacketType(PacketType packetType) const { +bool EntityTree::handlesEditPacketType (PacketType::Value packetType) const { // we handle these types of "edit" packets switch (packetType) { case PacketTypeEntityAdd: @@ -572,7 +572,7 @@ EntityItemPointer EntityTree::findEntityByEntityItemID(const EntityItemID& entit return foundEntity; } -int EntityTree::processEditPacketData(PacketType packetType, const unsigned char* packetData, int packetLength, +int EntityTree::processEditPacketData (PacketType::Value packetType, const unsigned char* packetData, int packetLength, const unsigned char* editData, int maxLength, const SharedNodePointer& senderNode) { if (!getIsServer()) { diff --git a/libraries/entities/src/EntityTree.h b/libraries/entities/src/EntityTree.h index 43c05763b5..99d08fc937 100644 --- a/libraries/entities/src/EntityTree.h +++ b/libraries/entities/src/EntityTree.h @@ -65,8 +65,8 @@ public: virtual PacketType::Value expectedDataPacketType() const { return PacketTypeEntityData; } virtual bool canProcessVersion(PacketVersion thisVersion) const { return thisVersion >= VERSION_ENTITIES_USE_METERS_AND_RADIANS; } - virtual bool handlesEditPacketType(PacketType packetType) const; - virtual int processEditPacketData(PacketType packetType, const unsigned char* packetData, int packetLength, + virtual bool handlesEditPacketType (PacketType::Value packetType) const; + virtual int processEditPacketData (PacketType::Value packetType, const unsigned char* packetData, int packetLength, const unsigned char* editData, int maxLength, const SharedNodePointer& senderNode); virtual bool rootElementHasData() const { return true; } diff --git a/libraries/networking/src/Assignment.cpp b/libraries/networking/src/Assignment.cpp index 0722394807..3da1fb9a41 100644 --- a/libraries/networking/src/Assignment.cpp +++ b/libraries/networking/src/Assignment.cpp @@ -68,9 +68,9 @@ Assignment::Assignment(const QByteArray& packet) : { PacketType::Value packetType = packetTypeForPacket(packet); - if (packetType == PacketTypeRequestAssignment) { + if (packetType == PacketType::RequestAssignment) { _command = Assignment::RequestCommand; - } else if (packetType == PacketTypeCreateAssignment) { + } else if (packetType == PacketType::CreateAssignment) { _command = Assignment::CreateCommand; } diff --git a/libraries/networking/src/LimitedNodeList.cpp b/libraries/networking/src/LimitedNodeList.cpp index faae5594e9..4eaa61b5e3 100644 --- a/libraries/networking/src/LimitedNodeList.cpp +++ b/libraries/networking/src/LimitedNodeList.cpp @@ -168,10 +168,10 @@ bool LimitedNodeList::packetVersionAndHashMatch(const QByteArray& packet) { int numPacketTypeBytes = numBytesArithmeticCodingFromBuffer(packet.data()); if (packet[numPacketTypeBytes] != versionForPacketType(checkType) - && checkType != PacketTypeStunResponse) { + && checkType != PacketType::StunResponse) { PacketType::Value mismatchType = packetTypeForPacket(packet); - static QMultiMap versionDebugSuppressMap; + static QMultiMap versionDebugSuppressMap; QUuid senderUUID = uuidFromPacketHeader(packet); if (!versionDebugSuppressMap.contains(senderUUID, checkType)) { @@ -195,7 +195,7 @@ bool LimitedNodeList::packetVersionAndHashMatch(const QByteArray& packet) { if (hashFromPacketHeader(packet) == hashForPacketAndConnectionUUID(packet, sendingNode->getConnectionSecret())) { return true; } else { - static QMultiMap hashDebugSuppressMap; + static QMultiMap hashDebugSuppressMap; QUuid senderUUID = uuidFromPacketHeader(packet); if (!hashDebugSuppressMap.contains(senderUUID, checkType)) { @@ -521,14 +521,11 @@ unsigned LimitedNodeList::broadcastToNodes(const QByteArray& packet, const NodeS return n; } -QByteArray LimitedNodeList::constructPingPacket(PingType_t pingType, bool isVerified, const QUuid& packetHeaderID) { +NodeListPacket&& LimitedNodeList::constructPingPacket(PingType_t pingType) { + int packetSize = sizeof(PingType_t) + sizeof(quint64); + NodeListPacket pingPacket = NodeListPacket::create(PacketType::Ping, packetSize); - QUuid packetUUID = packetHeaderID.isNull() ? _sessionUUID : packetHeaderID; - - QByteArray pingPacket = byteArrayWithUUIDPopulatedHeader(isVerified ? PacketTypePing : PacketTypeUnverifiedPing, - packetUUID); - - QDataStream packetStream(&pingPacket, QIODevice::Append); + QDataStream packetStream(&pingPacket.payload(), QIODevice::Append); packetStream << pingType; packetStream << usecTimestampNow(); @@ -536,7 +533,7 @@ QByteArray LimitedNodeList::constructPingPacket(PingType_t pingType, bool isVeri return pingPacket; } -QByteArray LimitedNodeList::constructPingReplyPacket(const QByteArray& pingPacket, const QUuid& packetHeaderID) { +NodeListPacket&& LimitedNodeList::constructPingReplyPacket(const QByteArray& pingPacket) { QDataStream pingPacketStream(pingPacket); pingPacketStream.skipRawData(numBytesForPacketHeader(pingPacket)); @@ -545,20 +542,44 @@ QByteArray LimitedNodeList::constructPingReplyPacket(const QByteArray& pingPacke quint64 timeFromOriginalPing; pingPacketStream >> timeFromOriginalPing; - - PacketType::Value replyType = (packetTypeForPacket(pingPacket) == PacketTypePing) - ? PacketTypePingReply : PacketTypeUnverifiedPingReply; - - QUuid packetUUID = packetHeaderID.isNull() ? _sessionUUID : packetHeaderID; - - QByteArray replyPacket = byteArrayWithUUIDPopulatedHeader(replyType, packetUUID); + + int packetSize = sizeof(PingType_t) + sizeof(quint64) + sizeof(quint64); + + NodeListPacket replyPacket = NodeListPacket::create(PacketType::Ping, packetSize); + QDataStream packetStream(&replyPacket, QIODevice::Append); - packetStream << typeFromOriginalPing << timeFromOriginalPing << usecTimestampNow(); return replyPacket; } +NodeListPacket&& constructICEPingPacket(PingType_t pingType, const QUuid& iceID) { + int packetSize = NUM_BYTES_RFC4122_UUID + sizeof(PingType_t); + + NodeListPacket icePingPacket = NodeListPacket::create(PacketType::ICEPing, packetSize); + + icePingPacket.payload().replace(0, NUM_BYTES_RFC4122_UUID, iceID.toRfc4122().data()); + memcpy(icePingPacket.payload() + NUM_BYTES_RFC4122_UUID, &pingType, sizeof(PingType_t)); + + return icePingPacket; +} + +NodeListPacket&& constructICEPingReplyPacket(const QByteArray& pingPacket, const QUuid& iceID) { + // pull out the ping type so we can reply back with that + PingType_t pingType; + + memcpy(&pingType, pingPacket.data() + NUM_BYTES_RFC4122_UUID, sizeof(PingType_t)); + + int packetSize = NUM_BYTES_RFC4122_UUID + sizeof(PingType_t); + NodeListPacket icePingReplyPacket = NodeListPacket::create(PacketType::ICEPingReply, packetSize); + + // pack the ICE ID and then the ping type + memcpy(icePingReplyPacket.payload(), iceID.toRfc4122().data(), NUM_BYTES_RFC4122_UUID); + memcpy(icePingReplyPacket.payload() + NUM_BYTES_RFC4122_UUID, &pingType, sizeof(PingType_t)); + + return icePingReplyPacket; +} + SharedNodePointer LimitedNodeList::soloNodeOfType(char nodeType) { return nodeMatchingPredicate([&](const SharedNodePointer& node){ return node->getType() == nodeType; @@ -835,7 +856,7 @@ void LimitedNodeList::sendPeerQueryToIceServer(const HifiSockAddr& iceServerSock sendPacketToIceServer(PacketTypeIceServerQuery, iceServerSockAddr, clientID, peerID); } -void LimitedNodeList::sendPacketToIceServer(PacketType packetType, const HifiSockAddr& iceServerSockAddr, +void LimitedNodeList::sendPacketToIceServer(PacketType::Value packetType, const HifiSockAddr& iceServerSockAddr, const QUuid& headerID, const QUuid& peerID) { QByteArray iceRequestByteArray = byteArrayWithUUIDPopulatedHeader(packetType, headerID); diff --git a/libraries/networking/src/LimitedNodeList.h b/libraries/networking/src/LimitedNodeList.h index d4e92a55a0..b705b0e2ea 100644 --- a/libraries/networking/src/LimitedNodeList.h +++ b/libraries/networking/src/LimitedNodeList.h @@ -118,28 +118,28 @@ public: bool packetVersionAndHashMatch(const QByteArray& packet); - // QByteArray byteArrayWithPopulatedHeader(PacketType packetType) + // QByteArray byteArrayWithPopulatedHeader (PacketType::Value packetType) // { return byteArrayWithUUIDPopulatedHeader(packetType, _sessionUUID); } // int populatePacketHeader(QByteArray& packet, PacketType::Value packetType) // { return populatePacketHeaderWithUUID(packet, packetType, _sessionUUID); } // int populatePacketHeader(char* packet, PacketType::Value packetType) // { return populatePacketHeaderWithUUID(packet, packetType, _sessionUUID); } - qint64 readDatagram(QByteArray& incomingPacket, QHostAddress* address, quint16 * port); - - qint64 writeDatagram(const QByteArray& datagram, const SharedNodePointer& destinationNode, - const HifiSockAddr& overridenSockAddr = HifiSockAddr()); - - qint64 writeUnverifiedDatagram(const QByteArray& datagram, const SharedNodePointer& destinationNode, - const HifiSockAddr& overridenSockAddr = HifiSockAddr()); - - qint64 writeUnverifiedDatagram(const QByteArray& datagram, const HifiSockAddr& destinationSockAddr); - - qint64 writeDatagram(const char* data, qint64 size, const SharedNodePointer& destinationNode, - const HifiSockAddr& overridenSockAddr = HifiSockAddr()); - - qint64 writeUnverifiedDatagram(const char* data, qint64 size, const SharedNodePointer& destinationNode, - const HifiSockAddr& overridenSockAddr = HifiSockAddr()); +// qint64 readDatagram(QByteArray& incomingPacket, QHostAddress* address, quint16 * port); +// +// qint64 writeDatagram(const QByteArray& datagram, const SharedNodePointer& destinationNode, +// const HifiSockAddr& overridenSockAddr = HifiSockAddr()); +// +// qint64 writeUnverifiedDatagram(const QByteArray& datagram, const SharedNodePointer& destinationNode, +// const HifiSockAddr& overridenSockAddr = HifiSockAddr()); +// +// qint64 writeUnverifiedDatagram(const QByteArray& datagram, const HifiSockAddr& destinationSockAddr); +// +// qint64 writeDatagram(const char* data, qint64 size, const SharedNodePointer& destinationNode, +// const HifiSockAddr& overridenSockAddr = HifiSockAddr()); +// +// qint64 writeUnverifiedDatagram(const char* data, qint64 size, const SharedNodePointer& destinationNode, +// const HifiSockAddr& overridenSockAddr = HifiSockAddr()); void (*linkedDataCreateCallback)(Node *); @@ -170,9 +170,11 @@ public: void getPacketStats(float &packetsPerSecond, float &bytesPerSecond); void resetPacketStats(); - QByteArray constructPingPacket(PingType_t pingType = PingType::Agnostic, bool isVerified = true, - const QUuid& packetHeaderID = QUuid()); - QByteArray constructPingReplyPacket(const QByteArray& pingPacket, const QUuid& packetHeaderID = QUuid()); + NodeListPacket&& constructPingPacket(PingType_t pingType = PingType::Agnostic); + NodeListPacket&& constructPingReplyPacket(const QByteArray& pingPacket); + + NodeListPacket&& constructICEPingPacket(PingType_t pingType, const QUuid& iceID); + NodeListPacket&& constructICEPingReplyPacket(const QByteArray& pingPacket, const QUuid& iceID); virtual bool processSTUNResponse(const QByteArray& packet); @@ -274,7 +276,7 @@ protected: void stopInitialSTUNUpdate(bool success); - void sendPacketToIceServer(PacketType packetType, const HifiSockAddr& iceServerSockAddr, const QUuid& headerID, + void sendPacketToIceServer (PacketType::Value packetType, const HifiSockAddr& iceServerSockAddr, const QUuid& headerID, const QUuid& peerRequestID = QUuid()); QUuid _sessionUUID; diff --git a/libraries/networking/src/Node.cpp b/libraries/networking/src/Node.cpp index 05da87d69a..fcb22a08b0 100644 --- a/libraries/networking/src/Node.cpp +++ b/libraries/networking/src/Node.cpp @@ -67,7 +67,7 @@ void Node::updateClockSkewUsec(int clockSkewSample) { _clockSkewUsec = (int)_clockSkewMovingPercentile.getValueAtPercentile(); } -PacketSequenceNumber Node::getLastSequenceNumberForPacketType(PacketType packetType) const { +PacketSequenceNumber Node::getLastSequenceNumberForPacketType (PacketType::Value packetType) const { auto typeMatch = _lastSequenceNumbers.find(packetType); if (typeMatch != _lastSequenceNumbers.end()) { return typeMatch->second; diff --git a/libraries/networking/src/Node.h b/libraries/networking/src/Node.h index be06906527..83bdf0ebd3 100644 --- a/libraries/networking/src/Node.h +++ b/libraries/networking/src/Node.h @@ -67,7 +67,7 @@ public: void setLastSequenceNumberForPacketType(PacketSequenceNumber sequenceNumber, PacketType::Value packetType) { _lastSequenceNumbers[packetType] = sequenceNumber; } - PacketSequenceNumber getLastSequenceNumberForPacketType(PacketType packetType) const; + PacketSequenceNumber getLastSequenceNumberForPacketType(PacketType::Value packetType) const; friend QDataStream& operator<<(QDataStream& out, const Node& node); friend QDataStream& operator>>(QDataStream& in, Node& node); diff --git a/libraries/networking/src/NodeList.cpp b/libraries/networking/src/NodeList.cpp index eadc0adbfb..11f04e188f 100644 --- a/libraries/networking/src/NodeList.cpp +++ b/libraries/networking/src/NodeList.cpp @@ -175,36 +175,36 @@ void NodeList::timePingReply(const QByteArray& packet, const SharedNodePointer& void NodeList::processNodeData(const HifiSockAddr& senderSockAddr, const QByteArray& packet) { PacketType::Value packetType = packetTypeForPacket(packet); switch (packetType) { - case PacketTypeDomainList: - case PacketTypeDomainServerAddedNode: { + case PacketType::DomainList: + case PacketType::DomainServerAddedNode: { if (!_domainHandler.getSockAddr().isNull()) { // only process a packet from domain-server if we're talking to a domain // TODO: how do we make sure this is actually the domain we want the list from (DTLS probably) - if (packetType == PacketTypeDomainList) { + if (packetType == PacketType::DomainList) { processDomainServerList(packet); - } else if (packetType == PacketTypeDomainServerAddedNode) { + } else if (packetType == PacketType::DomainServerAddedNode) { processDomainServerAddedNode(packet); } } break; } - case PacketTypeDomainServerRequireDTLS: { + case PacketType::DomainServerRequireDTLS: { _domainHandler.parseDTLSRequirementPacket(packet); break; } - case PacketTypeIceServerPeerInformation: { + case PacketType::IceServerPeerInformation: { if (!_domainHandler.getICEPeer().hasSockets()) { _domainHandler.processICEResponsePacket(packet); } break; } - case PacketTypePing: { + case PacketType::Ping: { // send back a reply SharedNodePointer matchingNode = sendingNodeForPacket(packet); if (matchingNode) { matchingNode->setLastHeardMicrostamp(usecTimestampNow()); - QByteArray replyPacket = constructPingReplyPacket(packet); - writeDatagram(replyPacket, matchingNode, senderSockAddr); + auto replyPacket = constructPingReplyPacket(packet); + sendPacket(replyPacket, matchingNode, senderSockAddr); // If we don't have a symmetric socket for this node and this socket doesn't match // what we have for public and local then set it as the symmetric. @@ -218,7 +218,7 @@ void NodeList::processNodeData(const HifiSockAddr& senderSockAddr, const QByteAr break; } - case PacketTypePingReply: { + case PacketType::PingReply: { SharedNodePointer sendingNode = sendingNodeForPacket(packet); if (sendingNode) { @@ -233,13 +233,13 @@ void NodeList::processNodeData(const HifiSockAddr& senderSockAddr, const QByteAr break; } - case PacketTypeUnverifiedPing: { + case PacketType::ICEPing: { // send back a reply - QByteArray replyPacket = constructPingReplyPacket(packet, _domainHandler.getICEClientID()); - writeUnverifiedDatagram(replyPacket, senderSockAddr); + auto replyPacket = constructICEPingReplyPacket(packet, _domainHandler.getICEClientID()); + sendPacket(replyPacket, senderSockAddr); break; } - case PacketTypeUnverifiedPingReply: { + case PacketType::ICEPingReply: { qCDebug(networking) << "Received reply from domain-server on" << senderSockAddr; if (_domainHandler.getIP().isNull()) { @@ -256,13 +256,13 @@ void NodeList::processNodeData(const HifiSockAddr& senderSockAddr, const QByteAr } } - case PacketTypeStunResponse: { + case PacketType::StunResponse: { // a STUN packet begins with 00, we've checked the second zero with packetVersionMatch // pass it along so it can be processed into our public address and port processSTUNResponse(packet); break; } - case PacketTypeDomainServerPathResponse: { + case PacketType::DomainServerPathResponse: { handleDSPathQueryResponse(packet); break; } @@ -518,11 +518,11 @@ void NodeList::pingPunchForDomainServer() { flagTimeForConnectionStep(LimitedNodeList::ConnectionStep::SendPingsToDS); // send the ping packet to the local and public sockets for this node - QByteArray localPingPacket = constructPingPacket(PingType::Local, false, _domainHandler.getICEClientID()); - writeUnverifiedDatagram(localPingPacket, _domainHandler.getICEPeer().getLocalSocket()); + auto localPingPacket = constructICEPingPacket(PingType::Local); + sendPacket(localPingPacket, _domainHandler.getICEPeer().getLocalSocket()); - QByteArray publicPingPacket = constructPingPacket(PingType::Public, false, _domainHandler.getICEClientID()); - writeUnverifiedDatagram(publicPingPacket, _domainHandler.getICEPeer().getPublicSocket()); + auto publicPingPacket = constructICEPingPacket(PingType::Public); + sendPacket(publicPingPacket, _domainHandler.getICEPeer().getPublicSocket()); _domainHandler.getICEPeer().incrementConnectionAttempts(); } @@ -625,15 +625,15 @@ void NodeList::pingPunchForInactiveNode(const SharedNodePointer& node) { } // send the ping packet to the local and public sockets for this node - QByteArray localPingPacket = constructPingPacket(PingType::Local); - writeDatagram(localPingPacket, node, node->getLocalSocket()); + auto localPingPacket = constructPingPacket(PingType::Local); + sendPacket(localPingPacket, node, node->getLocalSocket()); - QByteArray publicPingPacket = constructPingPacket(PingType::Public); - writeDatagram(publicPingPacket, node, node->getPublicSocket()); + auto publicPingPacket = constructPingPacket(PingType::Public); + sendPacket(publicPingPacket, node, node->getPublicSocket()); if (!node->getSymmetricSocket().isNull()) { - QByteArray symmetricPingPacket = constructPingPacket(PingType::Symmetric); - writeDatagram(symmetricPingPacket, node, node->getSymmetricSocket()); + auto symmetricPingPacket = constructPingPacket(PingType::Symmetric); + sendPacket(symmetricPingPacket, node, node->getSymmetricSocket()); } node->incrementConnectionAttempts(); diff --git a/libraries/networking/src/PacketHeaders.cpp b/libraries/networking/src/PacketHeaders.cpp index 3e254c29f6..83b424d44d 100644 --- a/libraries/networking/src/PacketHeaders.cpp +++ b/libraries/networking/src/PacketHeaders.cpp @@ -31,6 +31,8 @@ const QSet NON_VERIFIED_PACKETS = QSet() const QSet SEQUENCE_NUMBERED_PACKETS = QSet() << AvatarData; +const QSet NON_SOURCED_PACKETS = QSet() << ICEPing << ICEPingReply; + int arithmeticCodingValueFromBuffer(const char* checkValue) { if (((uchar) *checkValue) < 255) { return *checkValue; diff --git a/libraries/networking/src/PacketHeaders.h b/libraries/networking/src/PacketHeaders.h index 7815849863..298a0f7246 100644 --- a/libraries/networking/src/PacketHeaders.h +++ b/libraries/networking/src/PacketHeaders.h @@ -78,9 +78,9 @@ namespace PacketType { AudioEnvironment, EntityEditNack, SignedTransactionPayment, - IceServerHeartbeat, // 50 - UnverifiedPing, - UnverifiedPingReply + ICEServerHeartbeat, // 50 + ICEPing, + ICEPingReply }; }; @@ -93,6 +93,7 @@ typedef std::map PacketTypeSequenceMap; extern const QSet NON_VERIFIED_PACKETS; extern const QSet SEQUENCE_NUMBERED_PACKETS; +extern const QSet NON_SOURCED_PACKETS; const int NUM_BYTES_MD5_HASH = 16; const int NUM_STATIC_HEADER_BYTES = sizeof(PacketVersion) + NUM_BYTES_RFC4122_UUID; diff --git a/libraries/octree/src/EditPacketBuffer.cpp b/libraries/octree/src/EditPacketBuffer.cpp index c7f66c9b0a..9968990692 100644 --- a/libraries/octree/src/EditPacketBuffer.cpp +++ b/libraries/octree/src/EditPacketBuffer.cpp @@ -20,7 +20,7 @@ EditPacketBuffer::EditPacketBuffer() : } -EditPacketBuffer::EditPacketBuffer(PacketType type, unsigned char* buffer, size_t length, qint64 satoshiCost, QUuid nodeUUID) : +EditPacketBuffer::EditPacketBuffer (PacketType::Value type, unsigned char* buffer, size_t length, qint64 satoshiCost, QUuid nodeUUID) : _nodeUUID(nodeUUID), _currentType(type), _currentSize(length), diff --git a/libraries/octree/src/EditPacketBuffer.h b/libraries/octree/src/EditPacketBuffer.h index cc26a2dfe5..e817a07d12 100644 --- a/libraries/octree/src/EditPacketBuffer.h +++ b/libraries/octree/src/EditPacketBuffer.h @@ -21,7 +21,7 @@ class EditPacketBuffer { public: EditPacketBuffer(); - EditPacketBuffer(PacketType type, unsigned char* codeColorBuffer, size_t length, + EditPacketBuffer (PacketType::Value type, unsigned char* codeColorBuffer, size_t length, qint64 satoshiCost = 0, const QUuid nodeUUID = QUuid()); QUuid _nodeUUID; diff --git a/libraries/octree/src/Octree.h b/libraries/octree/src/Octree.h index 3717308008..2397c6bcae 100644 --- a/libraries/octree/src/Octree.h +++ b/libraries/octree/src/Octree.h @@ -230,8 +230,8 @@ public: virtual bool canProcessVersion(PacketVersion thisVersion) const { return thisVersion == versionForPacketType(expectedDataPacketType()); } virtual PacketVersion expectedVersion() const { return versionForPacketType(expectedDataPacketType()); } - virtual bool handlesEditPacketType(PacketType packetType) const { return false; } - virtual int processEditPacketData(PacketType packetType, const unsigned char* packetData, int packetLength, + virtual bool handlesEditPacketType (PacketType::Value packetType) const { return false; } + virtual int processEditPacketData (PacketType::Value packetType, const unsigned char* packetData, int packetLength, const unsigned char* editData, int maxLength, const SharedNodePointer& sourceNode) { return 0; } virtual bool recurseChildrenWithData() const { return true; } diff --git a/libraries/octree/src/OctreeEditPacketSender.cpp b/libraries/octree/src/OctreeEditPacketSender.cpp index be05fbb153..d94d3b613c 100644 --- a/libraries/octree/src/OctreeEditPacketSender.cpp +++ b/libraries/octree/src/OctreeEditPacketSender.cpp @@ -159,7 +159,7 @@ void OctreeEditPacketSender::processPreServerExistsPackets() { } } -void OctreeEditPacketSender::queuePendingPacketToNodes(PacketType type, unsigned char* buffer, +void OctreeEditPacketSender::queuePendingPacketToNodes (PacketType::Value type, unsigned char* buffer, size_t length, qint64 satoshiCost) { // If we're asked to save messages while waiting for voxel servers to arrive, then do so... @@ -213,7 +213,7 @@ void OctreeEditPacketSender::queuePacketToNodes(unsigned char* buffer, size_t le // NOTE: editPacketBuffer - is JUST the octcode/color and does not contain the packet header! -void OctreeEditPacketSender::queueOctreeEditMessage(PacketType type, unsigned char* editPacketBuffer, +void OctreeEditPacketSender::queueOctreeEditMessage (PacketType::Value type, unsigned char* editPacketBuffer, size_t length, qint64 satoshiCost) { if (!_shouldSend) { diff --git a/libraries/octree/src/OctreeEditPacketSender.h b/libraries/octree/src/OctreeEditPacketSender.h index 077078adeb..a9b9efb73c 100644 --- a/libraries/octree/src/OctreeEditPacketSender.h +++ b/libraries/octree/src/OctreeEditPacketSender.h @@ -30,7 +30,7 @@ public: /// Queues a single edit message. Will potentially send a pending multi-command packet. Determines which server /// node or nodes the packet should be sent to. Can be called even before servers are known, in which case up to /// MaxPendingMessages will be buffered and processed when servers are known. - void queueOctreeEditMessage(PacketType type, unsigned char* buffer, size_t length, qint64 satoshiCost = 0); + void queueOctreeEditMessage (PacketType::Value type, unsigned char* buffer, size_t length, qint64 satoshiCost = 0); /// Releases all queued messages even if those messages haven't filled an MTU packet. This will move the packed message /// packets onto the send queue. If running in threaded mode, the caller does not need to do any further processing to @@ -81,7 +81,7 @@ public: // you must override these... virtual char getMyNodeType() const = 0; - virtual void adjustEditPacketForClockSkew(PacketType type, + virtual void adjustEditPacketForClockSkew (PacketType::Value type, unsigned char* editPacketBuffer, size_t length, int clockSkew) { } bool hasDestinationWalletUUID() const { return !_destinationWalletUUID.isNull(); } @@ -99,7 +99,7 @@ signals: protected: bool _shouldSend; void queuePacketToNode(const QUuid& nodeID, unsigned char* buffer, size_t length, qint64 satoshiCost = 0); - void queuePendingPacketToNodes(PacketType type, unsigned char* buffer, size_t length, qint64 satoshiCost = 0); + void queuePendingPacketToNodes (PacketType::Value type, unsigned char* buffer, size_t length, qint64 satoshiCost = 0); void queuePacketToNodes(unsigned char* buffer, size_t length, qint64 satoshiCost = 0); void initializePacket(EditPacketBuffer& packetBuffer, PacketType::Value type, int nodeClockSkew); void releaseQueuedPacket(EditPacketBuffer& packetBuffer); // releases specific queued packet