Add lock for Connections map

This commit is contained in:
Simon Walton 2019-06-11 15:14:01 -07:00
parent d8791c4379
commit 1c03aeac20
2 changed files with 10 additions and 0 deletions

View file

@ -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<HifiSockAddr> Socket::getConnectionSockAddrs() {
std::vector<HifiSockAddr> addr;
Lock connectionsLock(_connectionsHashMutex);
addr.reserve(_connectionsHash.size());
for (const auto& connectionPair : _connectionsHash) {

View file

@ -129,6 +129,7 @@ private:
ConnectionCreationFilterOperator _connectionCreationFilterOperator;
Mutex _unreliableSequenceNumbersMutex;
Mutex _connectionsHashMutex;
std::unordered_map<HifiSockAddr, BasePacketHandler> _unfilteredHandlers;
std::unordered_map<HifiSockAddr, SequenceNumber> _unreliableSequenceNumbers;