Merge pull request #15006 from Atlante45/fix/shared-connection-master

Case 21407: Delete old nodes with identical socket
This commit is contained in:
John Conklin II 2019-02-26 16:24:03 -08:00 committed by GitHub
commit 2449693d8b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -645,6 +645,7 @@ SharedNodePointer LimitedNodeList::addOrUpdateNode(const QUuid& uuid, NodeType_t
const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket,
Node::LocalID localID, bool isReplicated, bool isUpstream,
const QUuid& connectionSecret, const NodePermissions& permissions) {
{
QReadLocker readLocker(&_nodeMutex);
NodeHash::const_iterator it = _nodeHash.find(uuid);
@ -660,7 +661,26 @@ SharedNodePointer LimitedNodeList::addOrUpdateNode(const QUuid& uuid, NodeType_t
matchingNode->setLocalID(localID);
return matchingNode;
} else {
}
}
auto removeOldNode = [&](auto node) {
if (node) {
QWriteLocker writeLocker(&_nodeMutex);
_localIDMap.unsafe_erase(node->getLocalID());
_nodeHash.unsafe_erase(node->getUUID());
handleNodeKill(node);
}
};
// if this is a solo node type, we assume that the DS has replaced its assignment and we should kill the previous node
if (SOLO_NODE_TYPES.count(nodeType)) {
removeOldNode(soloNodeOfType(nodeType));
}
// If there is a new node with the same socket, this is a reconnection, kill the old node
removeOldNode(findNodeWithAddr(publicSocket));
removeOldNode(findNodeWithAddr(localSocket));
auto it = _connectionIDs.find(uuid);
if (it == _connectionIDs.end()) {
_connectionIDs[uuid] = INITIAL_CONNECTION_ID;
@ -683,40 +703,13 @@ SharedNodePointer LimitedNodeList::addOrUpdateNode(const QUuid& uuid, NodeType_t
SharedNodePointer newNodePointer(newNode, &QObject::deleteLater);
// if this is a solo node type, we assume that the DS has replaced its assignment and we should kill the previous node
if (SOLO_NODE_TYPES.count(newNode->getType())) {
// while we still have the read lock, see if there is a previous solo node we'll need to remove
auto previousSoloIt = std::find_if(_nodeHash.cbegin(), _nodeHash.cend(), [newNode](const UUIDNodePair& nodePair){
return nodePair.second->getType() == newNode->getType();
});
if (previousSoloIt != _nodeHash.cend()) {
// we have a previous solo node, switch to a write lock so we can remove it
readLocker.unlock();
QWriteLocker writeLocker(&_nodeMutex);
auto oldSoloNode = previousSoloIt->second;
_localIDMap.unsafe_erase(oldSoloNode->getLocalID());
_nodeHash.unsafe_erase(previousSoloIt);
handleNodeKill(oldSoloNode);
// convert the current lock back to a read lock for insertion of new node
writeLocker.unlock();
readLocker.relock();
}
}
{
QReadLocker readLocker(&_nodeMutex);
// insert the new node and release our read lock
#if defined(Q_OS_ANDROID) || (defined(__clang__) && defined(Q_OS_LINUX))
_nodeHash.insert(UUIDNodePair(newNode->getUUID(), newNodePointer));
_localIDMap.insert(std::pair<Node::LocalID, SharedNodePointer>(localID, newNodePointer));
#else
_nodeHash.emplace(newNode->getUUID(), newNodePointer);
_localIDMap.emplace(localID, newNodePointer);
#endif
readLocker.unlock();
_nodeHash.insert({ newNode->getUUID(), newNodePointer });
_localIDMap.insert({ localID, newNodePointer });
}
qCDebug(networking) << "Added" << *newNode;
@ -741,7 +734,6 @@ SharedNodePointer LimitedNodeList::addOrUpdateNode(const QUuid& uuid, NodeType_t
});
return newNodePointer;
}
}
std::unique_ptr<NLPacket> LimitedNodeList::constructPingPacket(const QUuid& nodeId, PingType_t pingType) {