mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-09 08:30:35 +02:00
allow STUN to succeed started late or immediately
This commit is contained in:
parent
687836ff96
commit
60bf617ea1
3 changed files with 71 additions and 68 deletions
|
@ -95,8 +95,7 @@ void HifiSockAddr::handleLookupResult(const QHostInfo& hostInfo) {
|
|||
if (hostInfo.error() != QHostInfo::NoError) {
|
||||
qCDebug(networking) << "Lookup failed for" << hostInfo.lookupId() << ":" << hostInfo.errorString();
|
||||
emit lookupFailed();
|
||||
}
|
||||
|
||||
} else {
|
||||
foreach(const QHostAddress& address, hostInfo.addresses()) {
|
||||
// just take the first IPv4 address
|
||||
if (address.protocol() == QAbstractSocket::IPv4Protocol) {
|
||||
|
@ -108,6 +107,7 @@ void HifiSockAddr::handleLookupResult(const QHostInfo& hostInfo) {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QDebug operator<<(QDebug debug, const HifiSockAddr& sockAddr) {
|
||||
debug.nospace() << sockAddr._address.toString().toLocal8Bit().constData() << ":" << sockAddr._port;
|
||||
|
|
|
@ -672,6 +672,7 @@ const int NUM_BYTES_STUN_HEADER = 20;
|
|||
|
||||
void LimitedNodeList::sendSTUNRequest() {
|
||||
|
||||
if (!_stunSockAddr.isNull()) {
|
||||
const int NUM_INITIAL_STUN_REQUESTS_BEFORE_FAIL = 10;
|
||||
|
||||
if (!_hasCompletedInitialSTUN) {
|
||||
|
@ -713,6 +714,7 @@ void LimitedNodeList::sendSTUNRequest() {
|
|||
|
||||
_nodeSocket.writeDatagram(stunRequestPacket, sizeof(stunRequestPacket), _stunSockAddr);
|
||||
}
|
||||
}
|
||||
|
||||
void LimitedNodeList::processSTUNResponse(std::unique_ptr<udt::BasePacket> packet) {
|
||||
// check the cookie to make sure this is actually a STUN response
|
||||
|
@ -796,36 +798,38 @@ void LimitedNodeList::processSTUNResponse(std::unique_ptr<udt::BasePacket> packe
|
|||
}
|
||||
|
||||
void LimitedNodeList::startSTUNPublicSocketUpdate() {
|
||||
if (!_hasStartedSTUN) {
|
||||
_hasStartedSTUN = true;
|
||||
|
||||
if (!_initialSTUNTimer ) {
|
||||
// if we don't know the STUN IP yet we need to have ourselves be called once it is known
|
||||
if (_stunSockAddr.getAddress().isNull()) {
|
||||
connect(&_stunSockAddr, &HifiSockAddr::lookupCompleted, this, &LimitedNodeList::startSTUNPublicSocketUpdate);
|
||||
|
||||
// in case we just completely fail to lookup the stun socket - add a 10s timeout that will trigger the fail case
|
||||
const quint64 STUN_DNS_LOOKUP_TIMEOUT_MSECS = 10 * 1000;
|
||||
|
||||
QTimer* stunLookupFailTimer = new QTimer(this);
|
||||
connect(stunLookupFailTimer, &QTimer::timeout, this, &LimitedNodeList::possiblyTimeoutSTUNAddressLookup);
|
||||
stunLookupFailTimer->start(STUN_DNS_LOOKUP_TIMEOUT_MSECS);
|
||||
|
||||
} else {
|
||||
// setup our initial STUN timer here so we can quickly find out our public IP address
|
||||
_initialSTUNTimer = new QTimer(this);
|
||||
_initialSTUNTimer = new QTimer { this };
|
||||
|
||||
connect(_initialSTUNTimer.data(), &QTimer::timeout, this, &LimitedNodeList::sendSTUNRequest);
|
||||
|
||||
const int STUN_INITIAL_UPDATE_INTERVAL_MSECS = 250;
|
||||
_initialSTUNTimer->start(STUN_INITIAL_UPDATE_INTERVAL_MSECS);
|
||||
_initialSTUNTimer->setInterval(STUN_INITIAL_UPDATE_INTERVAL_MSECS);
|
||||
|
||||
// if we don't know the STUN IP yet we need to wait until it is known to start STUN requests
|
||||
if (_stunSockAddr.getAddress().isNull()) {
|
||||
|
||||
// if we fail to lookup the socket then timeout the STUN address lookup
|
||||
connect(&_stunSockAddr, &HifiSockAddr::lookupFailed, this, &LimitedNodeList::possiblyTimeoutSTUNAddressLookup);
|
||||
|
||||
// immediately send a STUN request once we know the socket
|
||||
connect(&_stunSockAddr, &HifiSockAddr::lookupCompleted, this, &LimitedNodeList::sendSTUNRequest);
|
||||
|
||||
// start the initial STUN timer once we know the socket
|
||||
connect(&_stunSockAddr, SIGNAL(lookupCompleted), _initialSTUNTimer, SLOT(start()));
|
||||
|
||||
// in case we just completely fail to lookup the stun socket - add a 10s single shot that will trigger the fail case
|
||||
const quint64 STUN_DNS_LOOKUP_TIMEOUT_MSECS = 10 * 1000;
|
||||
QTimer::singleShot(STUN_DNS_LOOKUP_TIMEOUT_MSECS, this, &LimitedNodeList::possiblyTimeoutSTUNAddressLookup);
|
||||
} else {
|
||||
_initialSTUNTimer->start();
|
||||
|
||||
// send an initial STUN request right away
|
||||
sendSTUNRequest();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LimitedNodeList::possiblyTimeoutSTUNAddressLookup() {
|
||||
if (_stunSockAddr.getAddress().isNull()) {
|
||||
|
@ -868,7 +872,7 @@ void LimitedNodeList::stopInitialSTUNUpdate(bool success) {
|
|||
// Or, if we failed - if will check if we can eventually get a public socket
|
||||
const int STUN_IP_ADDRESS_CHECK_INTERVAL_MSECS = 30 * 1000;
|
||||
|
||||
QTimer* stunOccasionalTimer = new QTimer(this);
|
||||
QTimer* stunOccasionalTimer = new QTimer { this };
|
||||
connect(stunOccasionalTimer, &QTimer::timeout, this, &LimitedNodeList::sendSTUNRequest);
|
||||
|
||||
stunOccasionalTimer->start(STUN_IP_ADDRESS_CHECK_INTERVAL_MSECS);
|
||||
|
|
|
@ -293,7 +293,6 @@ protected:
|
|||
bool _thisNodeCanRez;
|
||||
|
||||
QPointer<QTimer> _initialSTUNTimer;
|
||||
bool _hasStartedSTUN { false };
|
||||
|
||||
int _numInitialSTUNRequests = 0;
|
||||
bool _hasCompletedInitialSTUN = false;
|
||||
|
|
Loading…
Reference in a new issue