handle activation and setting of symmetric socket

This commit is contained in:
Stephen Birarda 2014-03-28 09:07:30 -07:00
parent 18a293c020
commit f80b415497
4 changed files with 29 additions and 1 deletions

View file

@ -85,6 +85,15 @@ void Node::setLocalSocket(const HifiSockAddr& localSocket) {
_localSocket = localSocket; _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() { void Node::activateLocalSocket() {
qDebug() << "Activating local socket for node" << *this; qDebug() << "Activating local socket for node" << *this;
_activeSocket = &_localSocket; _activeSocket = &_localSocket;
@ -95,6 +104,11 @@ void Node::activatePublicSocket() {
_activeSocket = &_publicSocket; _activeSocket = &_publicSocket;
} }
void Node::activateSymmetricSocket() {
qDebug() << "Activating symmetric socket for node" << *this;
_activeSocket = &_symmetricSocket;
}
void Node::recordBytesReceived(int bytesReceived) { void Node::recordBytesReceived(int bytesReceived) {
if (!_bytesReceivedMovingAverage) { if (!_bytesReceivedMovingAverage) {
_bytesReceivedMovingAverage = new SimpleMovingAverage(100); _bytesReceivedMovingAverage = new SimpleMovingAverage(100);

View file

@ -71,12 +71,13 @@ public:
const HifiSockAddr& getLocalSocket() const { return _localSocket; } const HifiSockAddr& getLocalSocket() const { return _localSocket; }
void setLocalSocket(const HifiSockAddr& localSocket); void setLocalSocket(const HifiSockAddr& localSocket);
const HifiSockAddr& getSymmetricSocket() const { return _symmetricSocket; } const HifiSockAddr& getSymmetricSocket() const { return _symmetricSocket; }
void setSymmetricSocket(const HifiSockAddr& symmetricSocket) { _symmetricSocket = symmetricSocket; } void setSymmetricSocket(const HifiSockAddr& symmetricSocket);
const HifiSockAddr* getActiveSocket() const { return _activeSocket; } const HifiSockAddr* getActiveSocket() const { return _activeSocket; }
void activatePublicSocket(); void activatePublicSocket();
void activateLocalSocket(); void activateLocalSocket();
void activateSymmetricSocket();
const QUuid& getConnectionSecret() const { return _connectionSecret; } const QUuid& getConnectionSecret() const { return _connectionSecret; }
void setConnectionSecret(const QUuid& connectionSecret) { _connectionSecret = connectionSecret; } void setConnectionSecret(const QUuid& connectionSecret) { _connectionSecret = connectionSecret; }

View file

@ -294,6 +294,15 @@ void NodeList::processNodeData(const HifiSockAddr& senderSockAddr, const QByteAr
matchingNode->setLastHeardMicrostamp(usecTimestampNow()); matchingNode->setLastHeardMicrostamp(usecTimestampNow());
QByteArray replyPacket = constructPingReplyPacket(packet); QByteArray replyPacket = constructPingReplyPacket(packet);
writeDatagram(replyPacket, matchingNode, senderSockAddr); writeDatagram(replyPacket, matchingNode, senderSockAddr);
// If we don't have a symmetric socket for this node and this socket doesn't match
// what we have for public and local then set it as the symmetric.
// This allows a server on a reachable port to communicate with nodes on symmetric NATs
if (matchingNode->getSymmetricSocket().isNull()) {
if (senderSockAddr != matchingNode->getLocalSocket() && senderSockAddr != matchingNode->getPublicSocket()) {
matchingNode->setSymmetricSocket(senderSockAddr);
}
}
} }
break; break;
@ -879,6 +888,9 @@ void NodeList::activateSocketFromNodeCommunication(const QByteArray& packet, con
sendingNode->activateLocalSocket(); sendingNode->activateLocalSocket();
} else if (pingType == PingType::Public && !sendingNode->getActiveSocket()) { } else if (pingType == PingType::Public && !sendingNode->getActiveSocket()) {
sendingNode->activatePublicSocket(); sendingNode->activatePublicSocket();
} else if (pingType == PingType::Symmetric && !sendingNode->getActiveSocket()) {
} }
} }

View file

@ -58,6 +58,7 @@ namespace PingType {
const PingType_t Agnostic = 0; const PingType_t Agnostic = 0;
const PingType_t Local = 1; const PingType_t Local = 1;
const PingType_t Public = 2; const PingType_t Public = 2;
const PingType_t Symmetric = 3;
} }
class NodeList : public QObject { class NodeList : public QObject {