From c36774e85da24f3a280d21c7ff582a7f2e20f288 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 2 Oct 2014 16:05:15 -0700 Subject: [PATCH] handle ice response packet in domain handler --- libraries/networking/src/DomainHandler.cpp | 22 ++++++++ libraries/networking/src/DomainHandler.h | 5 ++ libraries/networking/src/NetworkPeer.cpp | 66 ++++++++++------------ libraries/networking/src/NetworkPeer.h | 23 ++++---- libraries/networking/src/Node.cpp | 46 +++++++++++++++ libraries/networking/src/Node.h | 13 +++++ libraries/networking/src/NodeList.cpp | 2 +- 7 files changed, 127 insertions(+), 50 deletions(-) diff --git a/libraries/networking/src/DomainHandler.cpp b/libraries/networking/src/DomainHandler.cpp index e5c8abb171..2eb11f4922 100644 --- a/libraries/networking/src/DomainHandler.cpp +++ b/libraries/networking/src/DomainHandler.cpp @@ -26,6 +26,7 @@ DomainHandler::DomainHandler(QObject* parent) : _sockAddr(HifiSockAddr(QHostAddress::Null, DEFAULT_DOMAIN_SERVER_PORT)), _assignmentUUID(), _iceServerSockAddr(), + _icePeer(), _isConnected(false), _handshakeTimer(NULL), _settingsObject(), @@ -36,8 +37,12 @@ DomainHandler::DomainHandler(QObject* parent) : void DomainHandler::clearConnectionInfo() { _uuid = QUuid(); + _iceServerSockAddr = HifiSockAddr(); + _icePeer = NetworkPeer(); + _isConnected = false; + emit disconnectedFromDomain(); if (_handshakeTimer) { @@ -231,3 +236,20 @@ void DomainHandler::parseDTLSRequirementPacket(const QByteArray& dtlsRequirement // initializeDTLSSession(); } + +void DomainHandler::processICEResponsePacket(const QByteArray& icePacket) { + QDataStream iceResponseStream(icePacket); + iceResponseStream.skipRawData(numBytesForPacketHeader(icePacket)); + + NetworkPeer packetPeer; + iceResponseStream >> packetPeer; + + if (packetPeer.getUUID() != _uuid) { + qDebug() << "Received a network peer with ID that does not match current domain. Will not attempt connection."; + } else { + qDebug() << "Received network peer object for domain -" << packetPeer; + _icePeer = packetPeer; + + emit requestICEConnectionAttempt(); + } +} diff --git a/libraries/networking/src/DomainHandler.h b/libraries/networking/src/DomainHandler.h index ffd288d93d..4c58a2b615 100644 --- a/libraries/networking/src/DomainHandler.h +++ b/libraries/networking/src/DomainHandler.h @@ -20,6 +20,7 @@ #include #include "HifiSockAddr.h" +#include "NetworkPeer.h" const QString DEFAULT_DOMAIN_HOSTNAME = "sandbox.highfidelity.io"; @@ -55,6 +56,7 @@ public: void setAssignmentUUID(const QUuid& assignmentUUID) { _assignmentUUID = assignmentUUID; } bool requiresICE() const { return !_iceServerSockAddr.isNull(); } + NetworkPeer& getICEPeer() { return _icePeer; } bool isConnected() const { return _isConnected; } void setIsConnected(bool isConnected); @@ -64,6 +66,7 @@ public: const QJsonObject& getSettingsObject() const { return _settingsObject; } void parseDTLSRequirementPacket(const QByteArray& dtlsRequirementPacket); + void processICEResponsePacket(const QByteArray& icePacket); void softReset(); public slots: @@ -77,6 +80,7 @@ signals: void hostnameChanged(const QString& hostname); void connectedToDomain(const QString& hostname); void disconnectedFromDomain(); + void requestICEConnectionAttempt(); void settingsReceived(const QJsonObject& domainSettingsObject); void settingsReceiveFail(); @@ -89,6 +93,7 @@ private: HifiSockAddr _sockAddr; QUuid _assignmentUUID; HifiSockAddr _iceServerSockAddr; + NetworkPeer _icePeer; bool _isConnected; QTimer* _handshakeTimer; QJsonObject _settingsObject; diff --git a/libraries/networking/src/NetworkPeer.cpp b/libraries/networking/src/NetworkPeer.cpp index 79bed4f226..1dda9eb7b1 100644 --- a/libraries/networking/src/NetworkPeer.cpp +++ b/libraries/networking/src/NetworkPeer.cpp @@ -16,58 +16,50 @@ #include "NetworkPeer.h" -NetworkPeer::NetworkPeer(const QUuid& uuid, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket) : - _uuid(uuid), - _publicSocket(publicSocket), - _localSocket(localSocket), - _symmetricSocket(), - _activeSocket(NULL), +NetworkPeer::NetworkPeer() : + _uuid(), + _publicSocket(), + _localSocket(), _wakeTimestamp(QDateTime::currentMSecsSinceEpoch()), _lastHeardMicrostamp(usecTimestampNow()) { } -void NetworkPeer::setPublicSocket(const HifiSockAddr& publicSocket) { - if (_activeSocket == &_publicSocket) { - // if the active socket was the public socket then reset it to NULL - _activeSocket = NULL; - } +NetworkPeer::NetworkPeer(const QUuid& uuid, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket) : + _uuid(uuid), + _publicSocket(publicSocket), + _localSocket(localSocket), + _wakeTimestamp(QDateTime::currentMSecsSinceEpoch()), + _lastHeardMicrostamp(usecTimestampNow()) +{ - _publicSocket = publicSocket; } -void NetworkPeer::setLocalSocket(const HifiSockAddr& localSocket) { - if (_activeSocket == &_localSocket) { - // if the active socket was the local socket then reset it to NULL - _activeSocket = NULL; - } +NetworkPeer::NetworkPeer(const NetworkPeer& otherPeer) { - _localSocket = localSocket; -} - -void NetworkPeer::setSymmetricSocket(const HifiSockAddr& symmetricSocket) { - if (_activeSocket == &_symmetricSocket) { - // if the active socket was the symmetric socket then reset it to NULL - _activeSocket = NULL; - } + _uuid = otherPeer._uuid; + _publicSocket = otherPeer._publicSocket; + _localSocket = otherPeer._localSocket; - _symmetricSocket = symmetricSocket; + _wakeTimestamp = otherPeer._wakeTimestamp; + _lastHeardMicrostamp = otherPeer._lastHeardMicrostamp; } -void NetworkPeer::activateLocalSocket() { - qDebug() << "Activating local socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid); - _activeSocket = &_localSocket; +NetworkPeer& NetworkPeer::operator=(const NetworkPeer& otherPeer) { + NetworkPeer temp(otherPeer); + swap(temp); + return *this; } -void NetworkPeer::activatePublicSocket() { - qDebug() << "Activating public socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid); - _activeSocket = &_publicSocket; -} - -void NetworkPeer::activateSymmetricSocket() { - qDebug() << "Activating symmetric socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid); - _activeSocket = &_symmetricSocket; +void NetworkPeer::swap(NetworkPeer& otherPeer) { + using std::swap; + + swap(_uuid, otherPeer._uuid); + swap(_publicSocket, otherPeer._publicSocket); + swap(_localSocket, otherPeer._localSocket); + swap(_wakeTimestamp, otherPeer._wakeTimestamp); + swap(_lastHeardMicrostamp, otherPeer._lastHeardMicrostamp); } QByteArray NetworkPeer::toByteArray() const { diff --git a/libraries/networking/src/NetworkPeer.h b/libraries/networking/src/NetworkPeer.h index 18015bc605..c590c49cf3 100644 --- a/libraries/networking/src/NetworkPeer.h +++ b/libraries/networking/src/NetworkPeer.h @@ -23,23 +23,22 @@ const int ICE_HEARBEAT_INTERVAL_MSECS = 2 * 1000; class NetworkPeer : public QObject { public: + NetworkPeer(); NetworkPeer(const QUuid& uuid, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket); + // privatize copy and assignment operator to disallow peer copying + NetworkPeer(const NetworkPeer &otherPeer); + NetworkPeer& operator=(const NetworkPeer& otherPeer); + const QUuid& getUUID() const { return _uuid; } void setUUID(const QUuid& uuid) { _uuid = uuid; } + void reset(); + const HifiSockAddr& getPublicSocket() const { return _publicSocket; } - void setPublicSocket(const HifiSockAddr& publicSocket); + virtual void setPublicSocket(const HifiSockAddr& publicSocket) { _publicSocket = publicSocket; } const HifiSockAddr& getLocalSocket() const { return _localSocket; } - void setLocalSocket(const HifiSockAddr& localSocket); - const HifiSockAddr& getSymmetricSocket() const { return _symmetricSocket; } - void setSymmetricSocket(const HifiSockAddr& symmetricSocket); - - const HifiSockAddr* getActiveSocket() const { return _activeSocket; } - - void activatePublicSocket(); - void activateLocalSocket(); - void activateSymmetricSocket(); + virtual void setLocalSocket(const HifiSockAddr& localSocket) { _localSocket = localSocket; } quint64 getWakeTimestamp() const { return _wakeTimestamp; } void setWakeTimestamp(quint64 wakeTimestamp) { _wakeTimestamp = wakeTimestamp; } @@ -56,11 +55,11 @@ protected: HifiSockAddr _publicSocket; HifiSockAddr _localSocket; - HifiSockAddr _symmetricSocket; - HifiSockAddr* _activeSocket; quint64 _wakeTimestamp; quint64 _lastHeardMicrostamp; +private: + void swap(NetworkPeer& otherPeer); }; QDebug operator<<(QDebug debug, const NetworkPeer &peer); diff --git a/libraries/networking/src/Node.cpp b/libraries/networking/src/Node.cpp index a25350c3f3..20461cb754 100644 --- a/libraries/networking/src/Node.cpp +++ b/libraries/networking/src/Node.cpp @@ -12,6 +12,8 @@ #include #include +#include + #include "Node.h" #include "SharedUtil.h" @@ -45,6 +47,8 @@ const QString& NodeType::getNodeTypeName(NodeType_t nodeType) { Node::Node(const QUuid& uuid, NodeType_t type, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket) : NetworkPeer(uuid, publicSocket, localSocket), _type(type), + _activeSocket(NULL), + _symmetricSocket(), _connectionSecret(), _bytesReceivedMovingAverage(NULL), _linkedData(NULL), @@ -90,6 +94,48 @@ void Node::updateClockSkewUsec(int clockSkewSample) { _clockSkewUsec = (int)_clockSkewMovingPercentile.getValueAtPercentile(); } +void Node::setPublicSocket(const HifiSockAddr& publicSocket) { + if (_activeSocket == &_publicSocket) { + // if the active socket was the public socket then reset it to NULL + _activeSocket = NULL; + } + + _publicSocket = publicSocket; +} + +void Node::setLocalSocket(const HifiSockAddr& localSocket) { + if (_activeSocket == &_localSocket) { + // if the active socket was the local socket then reset it to NULL + _activeSocket = NULL; + } + + _localSocket = localSocket; +} + +void Node::setSymmetricSocket(const HifiSockAddr& symmetricSocket) { + if (_activeSocket == &_symmetricSocket) { + // if the active socket was the symmetric socket then reset it to NULL + _activeSocket = NULL; + } + + _symmetricSocket = symmetricSocket; +} + +void Node::activateLocalSocket() { + qDebug() << "Activating local socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid); + _activeSocket = &_localSocket; +} + +void Node::activatePublicSocket() { + qDebug() << "Activating public socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid); + _activeSocket = &_publicSocket; +} + +void Node::activateSymmetricSocket() { + qDebug() << "Activating symmetric socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid); + _activeSocket = &_symmetricSocket; +} + QDataStream& operator<<(QDataStream& out, const Node& node) { out << node._type; out << node._uuid; diff --git a/libraries/networking/src/Node.h b/libraries/networking/src/Node.h index 238331947c..994ddd3bec 100644 --- a/libraries/networking/src/Node.h +++ b/libraries/networking/src/Node.h @@ -77,6 +77,17 @@ public: void updateClockSkewUsec(int clockSkewSample); QMutex& getMutex() { return _mutex; } + virtual void setPublicSocket(const HifiSockAddr& publicSocket); + virtual void setLocalSocket(const HifiSockAddr& localSocket); + const HifiSockAddr& getSymmetricSocket() const { return _symmetricSocket; } + virtual void setSymmetricSocket(const HifiSockAddr& symmetricSocket); + + const HifiSockAddr* getActiveSocket() const { return _activeSocket; } + + void activatePublicSocket(); + void activateLocalSocket(); + void activateSymmetricSocket(); + friend QDataStream& operator<<(QDataStream& out, const Node& node); friend QDataStream& operator>>(QDataStream& in, Node& node); @@ -87,6 +98,8 @@ private: NodeType_t _type; + HifiSockAddr* _activeSocket; + HifiSockAddr _symmetricSocket; QUuid _connectionSecret; SimpleMovingAverage* _bytesReceivedMovingAverage; diff --git a/libraries/networking/src/NodeList.cpp b/libraries/networking/src/NodeList.cpp index a1b6011032..0333b4fb34 100644 --- a/libraries/networking/src/NodeList.cpp +++ b/libraries/networking/src/NodeList.cpp @@ -124,7 +124,7 @@ void NodeList::processNodeData(const HifiSockAddr& senderSockAddr, const QByteAr break; } case PacketTypeIceServerHeartbeatResponse: { - + _domainHandler.processICEResponsePacket(packet); break; } case PacketTypePing: {