From e096cbe04033d14f043768cbf6404611499adde2 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 28 May 2015 14:25:18 -0700 Subject: [PATCH] have ice-server immediately send peer to DS --- domain-server/src/DomainServer.cpp | 11 +-- ice-server/src/IceServer.cpp | 20 +++-- ice-server/src/IceServer.h | 2 +- libraries/networking/src/NetworkPeer.cpp | 110 ++++++++++++++++++++++- libraries/networking/src/NetworkPeer.h | 22 ++++- libraries/networking/src/Node.cpp | 73 --------------- libraries/networking/src/Node.h | 16 ---- 7 files changed, 142 insertions(+), 112 deletions(-) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 651685a3fd..0ffdabcb8a 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -656,16 +656,7 @@ void DomainServer::handleConnectRequest(const QByteArray& packet, const HifiSock canAdjustLocks, canRez); // So that we can send messages to this node at will - we need to activate the correct socket on this node now - if (senderSockAddr == publicSockAddr) { - newNode->activatePublicSocket(); - } else if (senderSockAddr == localSockAddr) { - newNode->activateLocalSocket(); - } else { - // set the Node's symmetric socket to the sender socket - newNode->setSymmetricSocket(senderSockAddr); - // activate that symmetric socket - newNode->activateSymmetricSocket(); - } + newNode->activateMatchingOrNewSymmetricSocket(senderSockAddr); // when the newNode is created the linked data is also created // if this was a static assignment set the UUID, set the sendingSockAddr diff --git a/ice-server/src/IceServer.cpp b/ice-server/src/IceServer.cpp index ec9943d2a1..c39a2259af 100644 --- a/ice-server/src/IceServer.cpp +++ b/ice-server/src/IceServer.cpp @@ -57,8 +57,12 @@ void IceServer::processDatagrams() { PacketType packetType = packetTypeForPacket(incomingPacket); if (packetType == PacketTypeIceServerHeartbeat) { - addOrUpdateHeartbeatingPeer(incomingPacket); + SharedNetworkPeer peer = addOrUpdateHeartbeatingPeer(incomingPacket); + + // so that we can send packets to the heartbeating peer when we need, we need to activate a socket now + peer->activateMatchingOrNewSymmetricSocket(sendingSockAddr); } else if (packetType == PacketTypeIceServerQuery) { + // this is a node hoping to connect to a heartbeating peer - do we have the heartbeating peer? QUuid senderUUID = uuidFromPacketHeader(incomingPacket); @@ -78,7 +82,13 @@ void IceServer::processDatagrams() { if (matchingPeer) { // we have the peer they want to connect to - send them pack the information for that peer - sendPeerInformationPacket(matchingPeer, sendingSockAddr); + sendPeerInformationPacket(matchingPeer.data(), &sendingSockAddr); + + // we also need to send them to the active peer they are hoping to connect to + // create a dummy peer object we can pass to sendPeerInformationPacket + + NetworkPeer dummyPeer(senderUUID, publicSocket, localSocket); + sendPeerInformationPacket(dummyPeer, matchingPeer->getActiveSocket()); } } } @@ -118,20 +128,20 @@ SharedNetworkPeer IceServer::addOrUpdateHeartbeatingPeer(const QByteArray& incom return matchingPeer; } -void IceServer::sendPeerInformationPacket(const SharedNetworkPeer& peer, const HifiSockAddr& destinationSockAddr) { +void IceServer::sendPeerInformationPacket(const NetworkPeer& peer, const HifiSockAddr* destinationSockAddr) { QByteArray outgoingPacket(MAX_PACKET_SIZE, 0); int currentPacketSize = populatePacketHeaderWithUUID(outgoingPacket, PacketTypeIceServerPeerInformation, _id); int numHeaderBytes = currentPacketSize; // get the byte array for this peer - QByteArray peerBytes = peer->toByteArray(); + QByteArray peerBytes = peer.toByteArray(); outgoingPacket.replace(numHeaderBytes, peerBytes.size(), peerBytes); currentPacketSize += peerBytes.size(); // write the current packet _serverSocket.writeDatagram(outgoingPacket.data(), outgoingPacket.size(), - destinationSockAddr.getAddress(), destinationSockAddr.getPort()); + destinationSockAddr->getAddress(), destinationSockAddr->getPort()); } void IceServer::clearInactivePeers() { diff --git a/ice-server/src/IceServer.h b/ice-server/src/IceServer.h index 454a867ec2..1f213fa606 100644 --- a/ice-server/src/IceServer.h +++ b/ice-server/src/IceServer.h @@ -33,7 +33,7 @@ private slots: private: SharedNetworkPeer addOrUpdateHeartbeatingPeer(const QByteArray& incomingPacket); - void sendPeerInformationPacket(const SharedNetworkPeer& peer, const HifiSockAddr& destinationSockAddr); + void sendPeerInformationPacket(const NetworkPeer& peer, const HifiSockAddr* destinationSockAddr); QUuid _id; QUdpSocket _serverSocket; diff --git a/libraries/networking/src/NetworkPeer.cpp b/libraries/networking/src/NetworkPeer.cpp index a6ed7e44fb..77e3de13e0 100644 --- a/libraries/networking/src/NetworkPeer.cpp +++ b/libraries/networking/src/NetworkPeer.cpp @@ -9,12 +9,16 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include +#include "NetworkPeer.h" + +#include +#include #include #include -#include "NetworkPeer.h" +#include "NetworkLogging.h" + #include "BandwidthRecorder.h" NetworkPeer::NetworkPeer(QObject* parent) : @@ -22,6 +26,8 @@ NetworkPeer::NetworkPeer(QObject* parent) : _uuid(), _publicSocket(), _localSocket(), + _symmetricSocket(), + _activeSocket(NULL), _wakeTimestamp(QDateTime::currentMSecsSinceEpoch()), _lastHeardMicrostamp(usecTimestampNow()), _connectionAttempts(0) @@ -29,10 +35,13 @@ NetworkPeer::NetworkPeer(QObject* parent) : } -NetworkPeer::NetworkPeer(const QUuid& uuid, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket) : +NetworkPeer::NetworkPeer(const QUuid& uuid, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket, QObject* parent) : + QObject(parent), _uuid(uuid), _publicSocket(publicSocket), _localSocket(localSocket), + _symmetricSocket(), + _activeSocket(NULL), _wakeTimestamp(QDateTime::currentMSecsSinceEpoch()), _lastHeardMicrostamp(usecTimestampNow()), _connectionAttempts(0) @@ -44,6 +53,17 @@ NetworkPeer::NetworkPeer(const NetworkPeer& otherPeer) : QObject() { _uuid = otherPeer._uuid; _publicSocket = otherPeer._publicSocket; _localSocket = otherPeer._localSocket; + _symmetricSocket = otherPeer._symmetricSocket; + + if (otherPeer._activeSocket) { + if (otherPeer._activeSocket == &otherPeer._localSocket) { + _activeSocket = &_localSocket; + } else if (otherPeer._activeSocket == &otherPeer._publicSocket) { + _activeSocket = &_publicSocket; + } else if (otherPeer._activeSocket == &otherPeer._symmetricSocket) { + _activeSocket = &_symmetricSocket; + } + } _wakeTimestamp = otherPeer._wakeTimestamp; _lastHeardMicrostamp = otherPeer._lastHeardMicrostamp; @@ -62,11 +82,95 @@ void NetworkPeer::swap(NetworkPeer& otherPeer) { swap(_uuid, otherPeer._uuid); swap(_publicSocket, otherPeer._publicSocket); swap(_localSocket, otherPeer._localSocket); + swap(_symmetricSocket, otherPeer._symmetricSocket); + swap(_activeSocket, otherPeer._activeSocket); swap(_wakeTimestamp, otherPeer._wakeTimestamp); swap(_lastHeardMicrostamp, otherPeer._lastHeardMicrostamp); swap(_connectionAttempts, otherPeer._connectionAttempts); } +void NetworkPeer::setPublicSocket(const HifiSockAddr& publicSocket) { + if (publicSocket != _publicSocket) { + if (_activeSocket == &_publicSocket) { + // if the active socket was the public socket then reset it to NULL + _activeSocket = NULL; + } + + if (!_publicSocket.isNull()) { + qCDebug(networking) << "Public socket change for node" << *this; + } + + _publicSocket = publicSocket; + } +} + +void NetworkPeer::setLocalSocket(const HifiSockAddr& localSocket) { + if (localSocket != _localSocket) { + if (_activeSocket == &_localSocket) { + // if the active socket was the local socket then reset it to NULL + _activeSocket = NULL; + } + + if (!_localSocket.isNull()) { + qCDebug(networking) << "Local socket change for node" << *this; + } + + _localSocket = localSocket; + } +} + +void NetworkPeer::setSymmetricSocket(const HifiSockAddr& symmetricSocket) { + if (symmetricSocket != _symmetricSocket) { + if (_activeSocket == &_symmetricSocket) { + // if the active socket was the symmetric socket then reset it to NULL + _activeSocket = NULL; + } + + if (!_symmetricSocket.isNull()) { + qCDebug(networking) << "Symmetric socket change for node" << *this; + } + + _symmetricSocket = symmetricSocket; + } +} + +void NetworkPeer::setActiveSocket(HifiSockAddr* discoveredSocket) { + _activeSocket = discoveredSocket; + + // we have an active socket, stop our ping timer + stopPingTimer(); + + // we're now considered connected to this peer - reset the number of connection attemps + resetConnectionAttempts(); +} + +void NetworkPeer::activateLocalSocket() { + qCDebug(networking) << "Activating local socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid); + setActiveSocket(&_localSocket); +} + +void NetworkPeer::activatePublicSocket() { + qCDebug(networking) << "Activating public socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid); + setActiveSocket(&_publicSocket); +} + +void NetworkPeer::activateSymmetricSocket() { + qCDebug(networking) << "Activating symmetric socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid); + setActiveSocket(&_symmetricSocket); +} + +void NetworkPeer::activateMatchingOrNewSymmetricSocket(const HifiSockAddr& matchableSockAddr) { + if (matchableSockAddr == _publicSocket) { + activatePublicSocket(); + } else if (matchableSockAddr == _localSocket) { + activateLocalSocket(); + } else { + // set the Node's symmetric socket to the passed socket + setSymmetricSocket(matchableSockAddr); + activateSymmetricSocket(); + } +} + void NetworkPeer::softReset() { // a soft reset should clear the sockets and reset the number of connection attempts _localSocket.clear(); diff --git a/libraries/networking/src/NetworkPeer.h b/libraries/networking/src/NetworkPeer.h index 53b79c0126..a4db99ba9e 100644 --- a/libraries/networking/src/NetworkPeer.h +++ b/libraries/networking/src/NetworkPeer.h @@ -29,9 +29,8 @@ class NetworkPeer : public QObject { Q_OBJECT public: NetworkPeer(QObject* parent = 0); - NetworkPeer(const QUuid& uuid, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket); + NetworkPeer(const QUuid& uuid, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket, QObject* parent = 0); - // privatize copy and assignment operator to disallow peer copying NetworkPeer(const NetworkPeer &otherPeer); NetworkPeer& operator=(const NetworkPeer& otherPeer); @@ -44,9 +43,20 @@ public: void softReset(); const HifiSockAddr& getPublicSocket() const { return _publicSocket; } - virtual void setPublicSocket(const HifiSockAddr& publicSocket) { _publicSocket = publicSocket; } const HifiSockAddr& getLocalSocket() const { return _localSocket; } - virtual void setLocalSocket(const HifiSockAddr& localSocket) { _localSocket = localSocket; } + const HifiSockAddr& getSymmetricSocket() const { return _symmetricSocket; } + + void setPublicSocket(const HifiSockAddr& publicSocket); + void setLocalSocket(const HifiSockAddr& localSocket); + void setSymmetricSocket(const HifiSockAddr& symmetricSocket); + + const HifiSockAddr* getActiveSocket() const { return _activeSocket; } + + void activatePublicSocket(); + void activateLocalSocket(); + void activateSymmetricSocket(); + + void activateMatchingOrNewSymmetricSocket(const HifiSockAddr& matchableSockAddr); quint64 getWakeTimestamp() const { return _wakeTimestamp; } void setWakeTimestamp(quint64 wakeTimestamp) { _wakeTimestamp = wakeTimestamp; } @@ -74,10 +84,14 @@ public slots: signals: void pingTimerTimeout(); protected: + void setActiveSocket(HifiSockAddr* discoveredSocket); + QUuid _uuid; HifiSockAddr _publicSocket; HifiSockAddr _localSocket; + HifiSockAddr _symmetricSocket; + HifiSockAddr* _activeSocket; quint64 _wakeTimestamp; quint64 _lastHeardMicrostamp; diff --git a/libraries/networking/src/Node.cpp b/libraries/networking/src/Node.cpp index e4144d080c..94b57b3f59 100644 --- a/libraries/networking/src/Node.cpp +++ b/libraries/networking/src/Node.cpp @@ -16,7 +16,6 @@ #include "Node.h" #include "SharedUtil.h" -#include "NetworkLogging.h" #include #include @@ -45,8 +44,6 @@ Node::Node(const QUuid& uuid, NodeType_t type, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket, bool canAdjustLocks, bool canRez, const QUuid& connectionSecret) : NetworkPeer(uuid, publicSocket, localSocket), _type(type), - _activeSocket(NULL), - _symmetricSocket(), _connectionSecret(connectionSecret), _linkedData(NULL), _isAlive(true), @@ -69,76 +66,6 @@ void Node::updateClockSkewUsec(int clockSkewSample) { _clockSkewUsec = (int)_clockSkewMovingPercentile.getValueAtPercentile(); } -void Node::setPublicSocket(const HifiSockAddr& publicSocket) { - if (publicSocket != _publicSocket) { - if (_activeSocket == &_publicSocket) { - // if the active socket was the public socket then reset it to NULL - _activeSocket = NULL; - } - - if (!_publicSocket.isNull()) { - qCDebug(networking) << "Public socket change for node" << *this; - } - - _publicSocket = publicSocket; - } -} - -void Node::setLocalSocket(const HifiSockAddr& localSocket) { - if (localSocket != _localSocket) { - if (_activeSocket == &_localSocket) { - // if the active socket was the local socket then reset it to NULL - _activeSocket = NULL; - } - - if (!_localSocket.isNull()) { - qCDebug(networking) << "Local socket change for node" << *this; - } - - _localSocket = localSocket; - } -} - -void Node::setSymmetricSocket(const HifiSockAddr& symmetricSocket) { - if (symmetricSocket != _symmetricSocket) { - if (_activeSocket == &_symmetricSocket) { - // if the active socket was the symmetric socket then reset it to NULL - _activeSocket = NULL; - } - - if (!_symmetricSocket.isNull()) { - qCDebug(networking) << "Symmetric socket change for node" << *this; - } - - _symmetricSocket = symmetricSocket; - } -} - -void Node::setActiveSocket(HifiSockAddr* discoveredSocket) { - _activeSocket = discoveredSocket; - - // we have an active socket, stop our ping timer - stopPingTimer(); - - // we're now considered connected to this peer - reset the number of connection attemps - resetConnectionAttempts(); -} - -void Node::activateLocalSocket() { - qCDebug(networking) << "Activating local socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid); - setActiveSocket(&_localSocket); -} - -void Node::activatePublicSocket() { - qCDebug(networking) << "Activating public socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid); - setActiveSocket(&_publicSocket); -} - -void Node::activateSymmetricSocket() { - qCDebug(networking) << "Activating symmetric socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid); - setActiveSocket(&_symmetricSocket); -} - PacketSequenceNumber Node::getLastSequenceNumberForPacketType(PacketType packetType) const { auto typeMatch = _lastSequenceNumbers.find(packetType); if (typeMatch != _lastSequenceNumbers.end()) { diff --git a/libraries/networking/src/Node.h b/libraries/networking/src/Node.h index 884e65a626..1eaf1a02c7 100644 --- a/libraries/networking/src/Node.h +++ b/libraries/networking/src/Node.h @@ -72,23 +72,12 @@ 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 setCanAdjustLocks(bool canAdjustLocks) { _canAdjustLocks = canAdjustLocks; } bool getCanAdjustLocks() { return _canAdjustLocks; } void setCanRez(bool canRez) { _canRez = canRez; } bool getCanRez() { return _canRez; } - void activatePublicSocket(); - void activateLocalSocket(); - void activateSymmetricSocket(); - void setLastSequenceNumberForPacketType(PacketSequenceNumber sequenceNumber, PacketType packetType) { _lastSequenceNumbers[packetType] = sequenceNumber; } PacketSequenceNumber getLastSequenceNumberForPacketType(PacketType packetType) const; @@ -101,13 +90,8 @@ private: Node(const Node &otherNode); Node& operator=(Node otherNode); - void setActiveSocket(HifiSockAddr* discoveredSocket); - NodeType_t _type; - HifiSockAddr* _activeSocket; - HifiSockAddr _symmetricSocket; - QUuid _connectionSecret; NodeData* _linkedData; bool _isAlive;