From 1c03aeac206bfce9e8ab12d657e42846f1df28dc Mon Sep 17 00:00:00 2001 From: Simon Walton Date: Tue, 11 Jun 2019 15:14:01 -0700 Subject: [PATCH] Add lock for Connections map --- libraries/networking/src/udt/Socket.cpp | 9 +++++++++ libraries/networking/src/udt/Socket.h | 1 + 2 files changed, 10 insertions(+) diff --git a/libraries/networking/src/udt/Socket.cpp b/libraries/networking/src/udt/Socket.cpp index 2867e83a1c..18f455a52f 100644 --- a/libraries/networking/src/udt/Socket.cpp +++ b/libraries/networking/src/udt/Socket.cpp @@ -243,6 +243,7 @@ qint64 Socket::writeDatagram(const QByteArray& datagram, const HifiSockAddr& soc } Connection* Socket::findOrCreateConnection(const HifiSockAddr& sockAddr, bool filterCreate) { + Lock connectionsLock(_connectionsHashMutex); auto it = _connectionsHash.find(sockAddr); if (it == _connectionsHash.end()) { @@ -282,6 +283,7 @@ void Socket::clearConnections() { return; } + Lock connectionsLock(_connectionsHashMutex); if (_connectionsHash.size() > 0) { // clear all of the current connections in the socket qCDebug(networking) << "Clearing all remaining connections in Socket."; @@ -290,6 +292,7 @@ void Socket::clearConnections() { } void Socket::cleanupConnection(HifiSockAddr sockAddr) { + Lock connectionsLock(_connectionsHashMutex); auto numErased = _connectionsHash.erase(sockAddr); if (numErased > 0) { @@ -447,6 +450,7 @@ void Socket::readPendingDatagrams() { } void Socket::connectToSendSignal(const HifiSockAddr& destinationAddr, QObject* receiver, const char* slot) { + Lock connectionsLock(_connectionsHashMutex); auto it = _connectionsHash.find(destinationAddr); if (it != _connectionsHash.end()) { connect(it->second.get(), SIGNAL(packetSent()), receiver, slot); @@ -463,6 +467,7 @@ void Socket::setConnectionMaxBandwidth(int maxBandwidth) { qInfo() << "Setting socket's maximum bandwith to" << maxBandwidth << "bps. (" << _connectionsHash.size() << "live connections)"; _maxBandwidth = maxBandwidth; + Lock connectionsLock(_connectionsHashMutex); for (auto& pair : _connectionsHash) { auto& connection = pair.second; connection->setMaxBandwidth(_maxBandwidth); @@ -480,6 +485,8 @@ ConnectionStats::Stats Socket::sampleStatsForConnection(const HifiSockAddr& dest Socket::StatsVector Socket::sampleStatsForAllConnections() { StatsVector result; + Lock connectionsLock(_connectionsHashMutex); + result.reserve(_connectionsHash.size()); for (const auto& connectionPair : _connectionsHash) { result.emplace_back(connectionPair.first, connectionPair.second->sampleStats()); @@ -490,6 +497,8 @@ Socket::StatsVector Socket::sampleStatsForAllConnections() { std::vector Socket::getConnectionSockAddrs() { std::vector addr; + Lock connectionsLock(_connectionsHashMutex); + addr.reserve(_connectionsHash.size()); for (const auto& connectionPair : _connectionsHash) { diff --git a/libraries/networking/src/udt/Socket.h b/libraries/networking/src/udt/Socket.h index 99266e105e..ad9d6de8b8 100644 --- a/libraries/networking/src/udt/Socket.h +++ b/libraries/networking/src/udt/Socket.h @@ -129,6 +129,7 @@ private: ConnectionCreationFilterOperator _connectionCreationFilterOperator; Mutex _unreliableSequenceNumbersMutex; + Mutex _connectionsHashMutex; std::unordered_map _unfilteredHandlers; std::unordered_map _unreliableSequenceNumbers;