mirror of
https://github.com/overte-org/overte.git
synced 2025-04-14 07:27:04 +02:00
manage sets of potential connections in ice server
This commit is contained in:
parent
d26a94f84c
commit
956c212db2
4 changed files with 72 additions and 12 deletions
|
@ -11,6 +11,7 @@
|
||||||
|
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
||||||
|
#include <LimitedNodeList.h>
|
||||||
#include <PacketHeaders.h>
|
#include <PacketHeaders.h>
|
||||||
#include <SharedUtil.h>
|
#include <SharedUtil.h>
|
||||||
|
|
||||||
|
@ -22,7 +23,8 @@ const int PEER_SILENCE_THRESHOLD_MSECS = 5 * 1000;
|
||||||
IceServer::IceServer(int argc, char* argv[]) :
|
IceServer::IceServer(int argc, char* argv[]) :
|
||||||
QCoreApplication(argc, argv),
|
QCoreApplication(argc, argv),
|
||||||
_id(QUuid::createUuid()),
|
_id(QUuid::createUuid()),
|
||||||
_serverSocket()
|
_serverSocket(),
|
||||||
|
_activePeers()
|
||||||
{
|
{
|
||||||
// start the ice-server socket
|
// start the ice-server socket
|
||||||
qDebug() << "ice-server socket is listening on" << ICE_SERVER_DEFAULT_PORT;
|
qDebug() << "ice-server socket is listening on" << ICE_SERVER_DEFAULT_PORT;
|
||||||
|
@ -84,24 +86,67 @@ void IceServer::processDatagrams() {
|
||||||
QUuid connectRequestID;
|
QUuid connectRequestID;
|
||||||
hearbeatStream >> connectRequestID;
|
hearbeatStream >> connectRequestID;
|
||||||
|
|
||||||
|
// get the peers asking for connections with this peer
|
||||||
|
QSet<QUuid>& requestingConnections = _currentConnections[senderUUID];
|
||||||
|
|
||||||
if (!connectRequestID.isNull()) {
|
if (!connectRequestID.isNull()) {
|
||||||
qDebug() << "Peer wants to connect to peer with ID" << uuidStringWithoutCurlyBraces(connectRequestID);
|
qDebug() << "Peer wants to connect to peer with ID" << uuidStringWithoutCurlyBraces(connectRequestID);
|
||||||
|
|
||||||
// check if we have that ID - if we do we can respond with their info, otherwise nothing we can do
|
// ensure this peer is in the set of current connections for the peer with ID it wants to connect with
|
||||||
SharedNetworkPeer matchingConectee = _activePeers.value(connectRequestID);
|
_currentConnections[connectRequestID].insert(senderUUID);
|
||||||
if (matchingConectee) {
|
|
||||||
QByteArray heartbeatResponse = byteArrayWithPopulatedHeader(PacketTypeIceServerHeartbeatResponse, _id);
|
// add the ID of the node they have said they would like to connect to
|
||||||
QDataStream responseStream(&heartbeatResponse, QIODevice::Append);
|
requestingConnections.insert(connectRequestID);
|
||||||
|
}
|
||||||
responseStream << matchingConectee;
|
|
||||||
|
if (requestingConnections.size() > 0) {
|
||||||
_serverSocket.writeDatagram(heartbeatResponse, sendingSockAddr.getAddress(), sendingSockAddr.getPort());
|
// send a heartbeart response based on the set of connections
|
||||||
}
|
qDebug() << "Sending a heartbeat response to" << senderUUID << "who has" << requestingConnections.size()
|
||||||
|
<< "potential connections";
|
||||||
|
sendHeartbeatResponse(sendingSockAddr, requestingConnections);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void IceServer::sendHeartbeatResponse(const HifiSockAddr& destinationSockAddr, QSet<QUuid>& connections) {
|
||||||
|
QSet<QUuid>::iterator peerID = connections.begin();
|
||||||
|
|
||||||
|
QByteArray outgoingPacket(MAX_PACKET_SIZE, 0);
|
||||||
|
int currentPacketSize = populatePacketHeader(outgoingPacket, PacketTypeIceServerHeartbeatResponse, _id);
|
||||||
|
|
||||||
|
// go through the connections, sending packets containing connection information for those nodes
|
||||||
|
while (peerID != connections.end()) {
|
||||||
|
SharedNetworkPeer matchingPeer = _activePeers.value(*peerID);
|
||||||
|
// if this node is inactive we remove it from the set
|
||||||
|
if (!matchingPeer) {
|
||||||
|
peerID = connections.erase(peerID);
|
||||||
|
} else {
|
||||||
|
// get the byte array for this peer
|
||||||
|
QByteArray peerBytes = matchingPeer->toByteArray();
|
||||||
|
|
||||||
|
if (currentPacketSize + peerBytes.size() > MAX_PACKET_SIZE) {
|
||||||
|
// write the current packet
|
||||||
|
_serverSocket.writeDatagram(outgoingPacket.data(), currentPacketSize,
|
||||||
|
destinationSockAddr.getAddress(), destinationSockAddr.getPort());
|
||||||
|
|
||||||
|
// reset the packet size to our number of header bytes
|
||||||
|
currentPacketSize = populatePacketHeader(outgoingPacket, PacketTypeIceServerHeartbeatResponse, _id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// append the current peer bytes
|
||||||
|
outgoingPacket.insert(currentPacketSize, peerBytes);
|
||||||
|
currentPacketSize += peerBytes.size();
|
||||||
|
|
||||||
|
++peerID;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// write the last packet
|
||||||
|
_serverSocket.writeDatagram(outgoingPacket.data(), currentPacketSize,
|
||||||
|
destinationSockAddr.getAddress(), destinationSockAddr.getPort());
|
||||||
|
}
|
||||||
|
|
||||||
void IceServer::clearInactivePeers() {
|
void IceServer::clearInactivePeers() {
|
||||||
NetworkPeerHash::iterator peerItem = _activePeers.begin();
|
NetworkPeerHash::iterator peerItem = _activePeers.begin();
|
||||||
|
|
||||||
|
|
|
@ -27,9 +27,13 @@ private slots:
|
||||||
void processDatagrams();
|
void processDatagrams();
|
||||||
void clearInactivePeers();
|
void clearInactivePeers();
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
void sendHeartbeatResponse(const HifiSockAddr& destinationSockAddr, QSet<QUuid>& connections);
|
||||||
|
|
||||||
QUuid _id;
|
QUuid _id;
|
||||||
NetworkPeerHash _activePeers;
|
|
||||||
QUdpSocket _serverSocket;
|
QUdpSocket _serverSocket;
|
||||||
|
NetworkPeerHash _activePeers;
|
||||||
|
QHash<QUuid, QSet<QUuid> > _currentConnections;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // hifi_IceServer_h
|
#endif // hifi_IceServer_h
|
|
@ -70,6 +70,15 @@ void NetworkPeer::activateSymmetricSocket() {
|
||||||
_activeSocket = &_symmetricSocket;
|
_activeSocket = &_symmetricSocket;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QByteArray NetworkPeer::toByteArray() const {
|
||||||
|
QByteArray peerByteArray;
|
||||||
|
|
||||||
|
QDataStream peerStream(&peerByteArray, QIODevice::Append);
|
||||||
|
peerStream << *this;
|
||||||
|
|
||||||
|
return peerByteArray;
|
||||||
|
}
|
||||||
|
|
||||||
QDataStream& operator<<(QDataStream& out, const NetworkPeer& peer) {
|
QDataStream& operator<<(QDataStream& out, const NetworkPeer& peer) {
|
||||||
out << peer._uuid;
|
out << peer._uuid;
|
||||||
out << peer._publicSocket;
|
out << peer._publicSocket;
|
||||||
|
|
|
@ -47,6 +47,8 @@ public:
|
||||||
quint64 getLastHeardMicrostamp() const { return _lastHeardMicrostamp; }
|
quint64 getLastHeardMicrostamp() const { return _lastHeardMicrostamp; }
|
||||||
void setLastHeardMicrostamp(quint64 lastHeardMicrostamp) { _lastHeardMicrostamp = lastHeardMicrostamp; }
|
void setLastHeardMicrostamp(quint64 lastHeardMicrostamp) { _lastHeardMicrostamp = lastHeardMicrostamp; }
|
||||||
|
|
||||||
|
QByteArray toByteArray() const;
|
||||||
|
|
||||||
friend QDataStream& operator<<(QDataStream& out, const NetworkPeer& peer);
|
friend QDataStream& operator<<(QDataStream& out, const NetworkPeer& peer);
|
||||||
friend QDataStream& operator>>(QDataStream& in, NetworkPeer& peer);
|
friend QDataStream& operator>>(QDataStream& in, NetworkPeer& peer);
|
||||||
protected:
|
protected:
|
||||||
|
|
Loading…
Reference in a new issue