cl flag to cache stun-server resposne

This commit is contained in:
Seth Alves 2016-09-19 13:34:56 -07:00
parent fe085d7b90
commit c87652ab6d
2 changed files with 52 additions and 26 deletions

View file

@ -39,6 +39,9 @@ ICEClientApp::ICEClientApp(int argc, char* argv[]) :
const QCommandLineOption domainIDOption("d", "domain-server uuid", "00000000-0000-0000-0000-000000000000"); const QCommandLineOption domainIDOption("d", "domain-server uuid", "00000000-0000-0000-0000-000000000000");
parser.addOption(domainIDOption); parser.addOption(domainIDOption);
const QCommandLineOption cacheSTUNOption("s", "cache stun-server response");
parser.addOption(cacheSTUNOption);
if (!parser.parse(QCoreApplication::arguments())) { if (!parser.parse(QCoreApplication::arguments())) {
qCritical() << parser.errorText() << endl; qCritical() << parser.errorText() << endl;
@ -52,6 +55,7 @@ ICEClientApp::ICEClientApp(int argc, char* argv[]) :
} }
_verbose = parser.isSet(verboseOutput); _verbose = parser.isSet(verboseOutput);
_cacheSTUNResult = parser.isSet(cacheSTUNOption);
if (parser.isSet(howManyTimesOption)) { if (parser.isSet(howManyTimesOption)) {
_actionMax = parser.value(howManyTimesOption).toInt(); _actionMax = parser.value(howManyTimesOption).toInt();
@ -99,6 +103,31 @@ void ICEClientApp::setState(int newState) {
_state = newState; _state = newState;
} }
void ICEClientApp::closeSocket() {
_domainServerPeerSet = false;
delete _socket;
_socket = nullptr;
}
void ICEClientApp::openSocket() {
if (_socket) {
return;
}
_socket = new udt::Socket();
unsigned int localPort = 0;
_socket->bind(QHostAddress::AnyIPv4, localPort);
_socket->setPacketHandler([this](std::unique_ptr<udt::Packet> packet) { processPacket(std::move(packet)); });
_socket->addUnfilteredHandler(_stunSockAddr,
[this](std::unique_ptr<udt::BasePacket> packet) {
processSTUNResponse(std::move(packet));
});
qDebug() << "local port is" << _socket->localPort();
_localSockAddr = HifiSockAddr("127.0.0.1", _socket->localPort());
_publicSockAddr = HifiSockAddr("127.0.0.1", _socket->localPort());
}
void ICEClientApp::doSomething() { void ICEClientApp::doSomething() {
if (_actionMax > 0 && _actionCount >= _actionMax) { if (_actionMax > 0 && _actionCount >= _actionMax) {
// time to stop. // time to stop.
@ -113,29 +142,22 @@ void ICEClientApp::doSomething() {
} else if (_state == sendStunRequestPacket) { } else if (_state == sendStunRequestPacket) {
// send STUN request packet // send STUN request packet
closeSocket();
openSocket();
_domainServerPeerSet = false; if (!_cacheSTUNResult || !_stunResultSet) {
unsigned int localPort = 0; const int NUM_BYTES_STUN_HEADER = 20;
delete _socket; char stunRequestPacket[NUM_BYTES_STUN_HEADER];
_socket = new udt::Socket(); LimitedNodeList::makeSTUNRequestPacket(stunRequestPacket);
_socket->bind(QHostAddress::AnyIPv4, localPort); qDebug() << "sending STUN request";
_socket->setPacketHandler([this](std::unique_ptr<udt::Packet> packet) { processPacket(std::move(packet)); }); _socket->writeDatagram(stunRequestPacket, sizeof(stunRequestPacket), _stunSockAddr);
_socket->addUnfilteredHandler(_stunSockAddr,
[this](std::unique_ptr<udt::BasePacket> packet) {
processSTUNResponse(std::move(packet));
});
qDebug() << "local port is" << _socket->localPort(); setState(waitForStunResponse);
_localSockAddr = HifiSockAddr("127.0.0.1", _socket->localPort()); } else {
_publicSockAddr = HifiSockAddr("127.0.0.1", _socket->localPort()); qDebug() << "using cached STUN resposne";
_publicSockAddr.setPort(_socket->localPort());
const int NUM_BYTES_STUN_HEADER = 20; setState(talkToIceServer);
char stunRequestPacket[NUM_BYTES_STUN_HEADER]; }
LimitedNodeList::makeSTUNRequestPacket(stunRequestPacket);
qDebug() << "sending STUN request";
_socket->writeDatagram(stunRequestPacket, sizeof(stunRequestPacket), _stunSockAddr);
setState(waitForStunResponse);
} else if (_state == talkToIceServer) { } else if (_state == talkToIceServer) {
QUuid peerID; QUuid peerID;
@ -210,6 +232,7 @@ void ICEClientApp::processSTUNResponse(std::unique_ptr<udt::BasePacket> packet)
if (LimitedNodeList::parseSTUNResponse(packet.get(), newPublicAddress, newPublicPort)) { if (LimitedNodeList::parseSTUNResponse(packet.get(), newPublicAddress, newPublicPort)) {
_publicSockAddr = HifiSockAddr(newPublicAddress, newPublicPort); _publicSockAddr = HifiSockAddr(newPublicAddress, newPublicPort);
qDebug() << "My public address is" << _publicSockAddr; qDebug() << "My public address is" << _publicSockAddr;
_stunResultSet = true;
setState(talkToIceServer); setState(talkToIceServer);
} }
} }
@ -223,8 +246,6 @@ void ICEClientApp::processPacket(std::unique_ptr<udt::Packet> packet) {
return; return;
} }
qDebug() << "here" << nlPacket->getType();
QSharedPointer<ReceivedMessage> message = QSharedPointer<ReceivedMessage>::create(*nlPacket); QSharedPointer<ReceivedMessage> message = QSharedPointer<ReceivedMessage>::create(*nlPacket);
const HifiSockAddr& senderAddr = message->getSenderSockAddr(); const HifiSockAddr& senderAddr = message->getSenderSockAddr();
@ -240,9 +261,9 @@ void ICEClientApp::processPacket(std::unique_ptr<udt::Packet> packet) {
connect(_pingDomainTimer, &QTimer::timeout, this, &ICEClientApp::icePingDomainServer); connect(_pingDomainTimer, &QTimer::timeout, this, &ICEClientApp::icePingDomainServer);
_pingDomainTimer->start(500); _pingDomainTimer->start(500);
} else { } else {
// NetworkPeer domainServerPeer; NetworkPeer domainServerPeer;
// iceResponseStream >> domainServerPeer; iceResponseStream >> domainServerPeer;
// qDebug() << "got repeat ICEServerPeerInformation from" << domainServerPeer; qDebug() << "got repeat ICEServerPeerInformation from" << domainServerPeer;
} }
} else if (nlPacket->getType() == PacketType::ICEPing) { } else if (nlPacket->getType() == PacketType::ICEPing) {

View file

@ -37,6 +37,9 @@ private:
pause1 // 6 pause1 // 6
}; };
void closeSocket();
void openSocket();
void setState(int newState); void setState(int newState);
void doSomething(); void doSomething();
@ -47,6 +50,8 @@ private:
void processPacket(std::unique_ptr<udt::Packet> packet); void processPacket(std::unique_ptr<udt::Packet> packet);
bool _verbose; bool _verbose;
bool _cacheSTUNResult; // should we only talk to stun server once?
bool _stunResultSet { false }; // have we already talked to stun server?
HifiSockAddr _stunSockAddr; HifiSockAddr _stunSockAddr;