From d9e0c91e02a278af2a3d409755a22296fe35851b Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Fri, 3 Oct 2014 09:45:40 -0700 Subject: [PATCH] remove a peer in domain-server after max connection attempts --- domain-server/src/DomainServer.cpp | 34 +++++++++++++++++------- libraries/networking/src/NetworkPeer.cpp | 6 +++-- libraries/networking/src/NetworkPeer.h | 7 +++++ 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index d3a08f7eb7..365e335d32 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -1012,17 +1012,31 @@ void DomainServer::sendHearbeatToIceServer() { void DomainServer::sendICEPingPackets() { LimitedNodeList* nodeList = LimitedNodeList::getInstance(); - foreach(const NetworkPeer& peer, _connectingICEPeers) { - // send ping packets to this peer's interfaces - qDebug() << "Sending ping packets to establish connectivity with ICE peer with ID" - << peer.getUUID(); + QHash::iterator peer = _connectingICEPeers.begin(); + + while (peer != _connectingICEPeers.end()) { - // 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()); - - QByteArray publicPingPacket = nodeList->constructPingPacket(PingType::Public, false); - nodeList->writeUnverifiedDatagram(publicPingPacket, peer.getPublicSocket()); + if (peer->getConnectionAttempts() >= MAX_ICE_CONNECTION_ATTEMPTS) { + // we've already tried to connect to this peer enough times + // remove it from our list - if it wants to re-connect it'll come back through ice-server + peer = _connectingICEPeers.erase(peer); + } else { + // send ping packets to this peer's interfaces + qDebug() << "Sending ping packets to establish connectivity with ICE peer with ID" + << peer->getUUID(); + + // 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()); + + QByteArray publicPingPacket = nodeList->constructPingPacket(PingType::Public, false); + nodeList->writeUnverifiedDatagram(publicPingPacket, peer->getPublicSocket()); + + peer->incrementConnectionAttempts(); + + // go to next peer in hash + ++peer; + } } } diff --git a/libraries/networking/src/NetworkPeer.cpp b/libraries/networking/src/NetworkPeer.cpp index 1dda9eb7b1..d458f579c1 100644 --- a/libraries/networking/src/NetworkPeer.cpp +++ b/libraries/networking/src/NetworkPeer.cpp @@ -21,7 +21,8 @@ NetworkPeer::NetworkPeer() : _publicSocket(), _localSocket(), _wakeTimestamp(QDateTime::currentMSecsSinceEpoch()), - _lastHeardMicrostamp(usecTimestampNow()) + _lastHeardMicrostamp(usecTimestampNow()), + _connectionAttempts(0) { } @@ -31,7 +32,8 @@ NetworkPeer::NetworkPeer(const QUuid& uuid, const HifiSockAddr& publicSocket, co _publicSocket(publicSocket), _localSocket(localSocket), _wakeTimestamp(QDateTime::currentMSecsSinceEpoch()), - _lastHeardMicrostamp(usecTimestampNow()) + _lastHeardMicrostamp(usecTimestampNow()), + _connectionAttempts(0) { } diff --git a/libraries/networking/src/NetworkPeer.h b/libraries/networking/src/NetworkPeer.h index 1e9b61d9f2..bb92c54eb8 100644 --- a/libraries/networking/src/NetworkPeer.h +++ b/libraries/networking/src/NetworkPeer.h @@ -20,6 +20,7 @@ const QString ICE_SERVER_HOSTNAME = "localhost"; const int ICE_SERVER_DEFAULT_PORT = 7337; const int ICE_HEARBEAT_INTERVAL_MSECS = 2 * 1000; +const int MAX_ICE_CONNECTION_ATTEMPTS = 5; class NetworkPeer : public QObject { public: @@ -50,6 +51,10 @@ public: QByteArray toByteArray() const; + int getConnectionAttempts() const { return _connectionAttempts; } + void incrementConnectionAttempts() { ++_connectionAttempts; } + void resetConnectionAttemps() { _connectionAttempts = 0; } + friend QDataStream& operator<<(QDataStream& out, const NetworkPeer& peer); friend QDataStream& operator>>(QDataStream& in, NetworkPeer& peer); protected: @@ -60,6 +65,8 @@ protected: quint64 _wakeTimestamp; quint64 _lastHeardMicrostamp; + + int _connectionAttempts; private: void swap(NetworkPeer& otherPeer); };