handle ice response packet in domain handler

This commit is contained in:
Stephen Birarda 2014-10-02 16:05:15 -07:00
parent 956c212db2
commit c36774e85d
7 changed files with 127 additions and 50 deletions

View file

@ -26,6 +26,7 @@ DomainHandler::DomainHandler(QObject* parent) :
_sockAddr(HifiSockAddr(QHostAddress::Null, DEFAULT_DOMAIN_SERVER_PORT)), _sockAddr(HifiSockAddr(QHostAddress::Null, DEFAULT_DOMAIN_SERVER_PORT)),
_assignmentUUID(), _assignmentUUID(),
_iceServerSockAddr(), _iceServerSockAddr(),
_icePeer(),
_isConnected(false), _isConnected(false),
_handshakeTimer(NULL), _handshakeTimer(NULL),
_settingsObject(), _settingsObject(),
@ -36,8 +37,12 @@ DomainHandler::DomainHandler(QObject* parent) :
void DomainHandler::clearConnectionInfo() { void DomainHandler::clearConnectionInfo() {
_uuid = QUuid(); _uuid = QUuid();
_iceServerSockAddr = HifiSockAddr(); _iceServerSockAddr = HifiSockAddr();
_icePeer = NetworkPeer();
_isConnected = false; _isConnected = false;
emit disconnectedFromDomain(); emit disconnectedFromDomain();
if (_handshakeTimer) { if (_handshakeTimer) {
@ -231,3 +236,20 @@ void DomainHandler::parseDTLSRequirementPacket(const QByteArray& dtlsRequirement
// initializeDTLSSession(); // initializeDTLSSession();
} }
void DomainHandler::processICEResponsePacket(const QByteArray& icePacket) {
QDataStream iceResponseStream(icePacket);
iceResponseStream.skipRawData(numBytesForPacketHeader(icePacket));
NetworkPeer packetPeer;
iceResponseStream >> packetPeer;
if (packetPeer.getUUID() != _uuid) {
qDebug() << "Received a network peer with ID that does not match current domain. Will not attempt connection.";
} else {
qDebug() << "Received network peer object for domain -" << packetPeer;
_icePeer = packetPeer;
emit requestICEConnectionAttempt();
}
}

View file

@ -20,6 +20,7 @@
#include <QtNetwork/QHostInfo> #include <QtNetwork/QHostInfo>
#include "HifiSockAddr.h" #include "HifiSockAddr.h"
#include "NetworkPeer.h"
const QString DEFAULT_DOMAIN_HOSTNAME = "sandbox.highfidelity.io"; const QString DEFAULT_DOMAIN_HOSTNAME = "sandbox.highfidelity.io";
@ -55,6 +56,7 @@ public:
void setAssignmentUUID(const QUuid& assignmentUUID) { _assignmentUUID = assignmentUUID; } void setAssignmentUUID(const QUuid& assignmentUUID) { _assignmentUUID = assignmentUUID; }
bool requiresICE() const { return !_iceServerSockAddr.isNull(); } bool requiresICE() const { return !_iceServerSockAddr.isNull(); }
NetworkPeer& getICEPeer() { return _icePeer; }
bool isConnected() const { return _isConnected; } bool isConnected() const { return _isConnected; }
void setIsConnected(bool isConnected); void setIsConnected(bool isConnected);
@ -64,6 +66,7 @@ public:
const QJsonObject& getSettingsObject() const { return _settingsObject; } const QJsonObject& getSettingsObject() const { return _settingsObject; }
void parseDTLSRequirementPacket(const QByteArray& dtlsRequirementPacket); void parseDTLSRequirementPacket(const QByteArray& dtlsRequirementPacket);
void processICEResponsePacket(const QByteArray& icePacket);
void softReset(); void softReset();
public slots: public slots:
@ -77,6 +80,7 @@ signals:
void hostnameChanged(const QString& hostname); void hostnameChanged(const QString& hostname);
void connectedToDomain(const QString& hostname); void connectedToDomain(const QString& hostname);
void disconnectedFromDomain(); void disconnectedFromDomain();
void requestICEConnectionAttempt();
void settingsReceived(const QJsonObject& domainSettingsObject); void settingsReceived(const QJsonObject& domainSettingsObject);
void settingsReceiveFail(); void settingsReceiveFail();
@ -89,6 +93,7 @@ private:
HifiSockAddr _sockAddr; HifiSockAddr _sockAddr;
QUuid _assignmentUUID; QUuid _assignmentUUID;
HifiSockAddr _iceServerSockAddr; HifiSockAddr _iceServerSockAddr;
NetworkPeer _icePeer;
bool _isConnected; bool _isConnected;
QTimer* _handshakeTimer; QTimer* _handshakeTimer;
QJsonObject _settingsObject; QJsonObject _settingsObject;

View file

@ -16,58 +16,50 @@
#include "NetworkPeer.h" #include "NetworkPeer.h"
NetworkPeer::NetworkPeer(const QUuid& uuid, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket) : NetworkPeer::NetworkPeer() :
_uuid(uuid), _uuid(),
_publicSocket(publicSocket), _publicSocket(),
_localSocket(localSocket), _localSocket(),
_symmetricSocket(),
_activeSocket(NULL),
_wakeTimestamp(QDateTime::currentMSecsSinceEpoch()), _wakeTimestamp(QDateTime::currentMSecsSinceEpoch()),
_lastHeardMicrostamp(usecTimestampNow()) _lastHeardMicrostamp(usecTimestampNow())
{ {
} }
void NetworkPeer::setPublicSocket(const HifiSockAddr& publicSocket) { NetworkPeer::NetworkPeer(const QUuid& uuid, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket) :
if (_activeSocket == &_publicSocket) { _uuid(uuid),
// if the active socket was the public socket then reset it to NULL _publicSocket(publicSocket),
_activeSocket = NULL; _localSocket(localSocket),
} _wakeTimestamp(QDateTime::currentMSecsSinceEpoch()),
_lastHeardMicrostamp(usecTimestampNow())
{
_publicSocket = publicSocket;
} }
void NetworkPeer::setLocalSocket(const HifiSockAddr& localSocket) { NetworkPeer::NetworkPeer(const NetworkPeer& otherPeer) {
if (_activeSocket == &_localSocket) {
// if the active socket was the local socket then reset it to NULL
_activeSocket = NULL;
}
_localSocket = localSocket; _uuid = otherPeer._uuid;
} _publicSocket = otherPeer._publicSocket;
_localSocket = otherPeer._localSocket;
void NetworkPeer::setSymmetricSocket(const HifiSockAddr& symmetricSocket) {
if (_activeSocket == &_symmetricSocket) {
// if the active socket was the symmetric socket then reset it to NULL
_activeSocket = NULL;
}
_symmetricSocket = symmetricSocket; _wakeTimestamp = otherPeer._wakeTimestamp;
_lastHeardMicrostamp = otherPeer._lastHeardMicrostamp;
} }
void NetworkPeer::activateLocalSocket() { NetworkPeer& NetworkPeer::operator=(const NetworkPeer& otherPeer) {
qDebug() << "Activating local socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid); NetworkPeer temp(otherPeer);
_activeSocket = &_localSocket; swap(temp);
return *this;
} }
void NetworkPeer::activatePublicSocket() { void NetworkPeer::swap(NetworkPeer& otherPeer) {
qDebug() << "Activating public socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid); using std::swap;
_activeSocket = &_publicSocket;
} swap(_uuid, otherPeer._uuid);
swap(_publicSocket, otherPeer._publicSocket);
void NetworkPeer::activateSymmetricSocket() { swap(_localSocket, otherPeer._localSocket);
qDebug() << "Activating symmetric socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid); swap(_wakeTimestamp, otherPeer._wakeTimestamp);
_activeSocket = &_symmetricSocket; swap(_lastHeardMicrostamp, otherPeer._lastHeardMicrostamp);
} }
QByteArray NetworkPeer::toByteArray() const { QByteArray NetworkPeer::toByteArray() const {

View file

@ -23,23 +23,22 @@ const int ICE_HEARBEAT_INTERVAL_MSECS = 2 * 1000;
class NetworkPeer : public QObject { class NetworkPeer : public QObject {
public: public:
NetworkPeer();
NetworkPeer(const QUuid& uuid, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket); NetworkPeer(const QUuid& uuid, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket);
// privatize copy and assignment operator to disallow peer copying
NetworkPeer(const NetworkPeer &otherPeer);
NetworkPeer& operator=(const NetworkPeer& otherPeer);
const QUuid& getUUID() const { return _uuid; } const QUuid& getUUID() const { return _uuid; }
void setUUID(const QUuid& uuid) { _uuid = uuid; } void setUUID(const QUuid& uuid) { _uuid = uuid; }
void reset();
const HifiSockAddr& getPublicSocket() const { return _publicSocket; } const HifiSockAddr& getPublicSocket() const { return _publicSocket; }
void setPublicSocket(const HifiSockAddr& publicSocket); virtual void setPublicSocket(const HifiSockAddr& publicSocket) { _publicSocket = publicSocket; }
const HifiSockAddr& getLocalSocket() const { return _localSocket; } const HifiSockAddr& getLocalSocket() const { return _localSocket; }
void setLocalSocket(const HifiSockAddr& localSocket); virtual void setLocalSocket(const HifiSockAddr& localSocket) { _localSocket = localSocket; }
const HifiSockAddr& getSymmetricSocket() const { return _symmetricSocket; }
void setSymmetricSocket(const HifiSockAddr& symmetricSocket);
const HifiSockAddr* getActiveSocket() const { return _activeSocket; }
void activatePublicSocket();
void activateLocalSocket();
void activateSymmetricSocket();
quint64 getWakeTimestamp() const { return _wakeTimestamp; } quint64 getWakeTimestamp() const { return _wakeTimestamp; }
void setWakeTimestamp(quint64 wakeTimestamp) { _wakeTimestamp = wakeTimestamp; } void setWakeTimestamp(quint64 wakeTimestamp) { _wakeTimestamp = wakeTimestamp; }
@ -56,11 +55,11 @@ protected:
HifiSockAddr _publicSocket; HifiSockAddr _publicSocket;
HifiSockAddr _localSocket; HifiSockAddr _localSocket;
HifiSockAddr _symmetricSocket;
HifiSockAddr* _activeSocket;
quint64 _wakeTimestamp; quint64 _wakeTimestamp;
quint64 _lastHeardMicrostamp; quint64 _lastHeardMicrostamp;
private:
void swap(NetworkPeer& otherPeer);
}; };
QDebug operator<<(QDebug debug, const NetworkPeer &peer); QDebug operator<<(QDebug debug, const NetworkPeer &peer);

View file

@ -12,6 +12,8 @@
#include <cstring> #include <cstring>
#include <stdio.h> #include <stdio.h>
#include <UUID.h>
#include "Node.h" #include "Node.h"
#include "SharedUtil.h" #include "SharedUtil.h"
@ -45,6 +47,8 @@ const QString& NodeType::getNodeTypeName(NodeType_t nodeType) {
Node::Node(const QUuid& uuid, NodeType_t type, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket) : Node::Node(const QUuid& uuid, NodeType_t type, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket) :
NetworkPeer(uuid, publicSocket, localSocket), NetworkPeer(uuid, publicSocket, localSocket),
_type(type), _type(type),
_activeSocket(NULL),
_symmetricSocket(),
_connectionSecret(), _connectionSecret(),
_bytesReceivedMovingAverage(NULL), _bytesReceivedMovingAverage(NULL),
_linkedData(NULL), _linkedData(NULL),
@ -90,6 +94,48 @@ void Node::updateClockSkewUsec(int clockSkewSample) {
_clockSkewUsec = (int)_clockSkewMovingPercentile.getValueAtPercentile(); _clockSkewUsec = (int)_clockSkewMovingPercentile.getValueAtPercentile();
} }
void Node::setPublicSocket(const HifiSockAddr& publicSocket) {
if (_activeSocket == &_publicSocket) {
// if the active socket was the public socket then reset it to NULL
_activeSocket = NULL;
}
_publicSocket = publicSocket;
}
void Node::setLocalSocket(const HifiSockAddr& localSocket) {
if (_activeSocket == &_localSocket) {
// if the active socket was the local socket then reset it to NULL
_activeSocket = NULL;
}
_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() {
qDebug() << "Activating local socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid);
_activeSocket = &_localSocket;
}
void Node::activatePublicSocket() {
qDebug() << "Activating public socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid);
_activeSocket = &_publicSocket;
}
void Node::activateSymmetricSocket() {
qDebug() << "Activating symmetric socket for network peer with ID" << uuidStringWithoutCurlyBraces(_uuid);
_activeSocket = &_symmetricSocket;
}
QDataStream& operator<<(QDataStream& out, const Node& node) { QDataStream& operator<<(QDataStream& out, const Node& node) {
out << node._type; out << node._type;
out << node._uuid; out << node._uuid;

View file

@ -77,6 +77,17 @@ public:
void updateClockSkewUsec(int clockSkewSample); void updateClockSkewUsec(int clockSkewSample);
QMutex& getMutex() { return _mutex; } 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 activatePublicSocket();
void activateLocalSocket();
void activateSymmetricSocket();
friend QDataStream& operator<<(QDataStream& out, const Node& node); friend QDataStream& operator<<(QDataStream& out, const Node& node);
friend QDataStream& operator>>(QDataStream& in, Node& node); friend QDataStream& operator>>(QDataStream& in, Node& node);
@ -87,6 +98,8 @@ private:
NodeType_t _type; NodeType_t _type;
HifiSockAddr* _activeSocket;
HifiSockAddr _symmetricSocket;
QUuid _connectionSecret; QUuid _connectionSecret;
SimpleMovingAverage* _bytesReceivedMovingAverage; SimpleMovingAverage* _bytesReceivedMovingAverage;

View file

@ -124,7 +124,7 @@ void NodeList::processNodeData(const HifiSockAddr& senderSockAddr, const QByteAr
break; break;
} }
case PacketTypeIceServerHeartbeatResponse: { case PacketTypeIceServerHeartbeatResponse: {
_domainHandler.processICEResponsePacket(packet);
break; break;
} }
case PacketTypePing: { case PacketTypePing: {