From 8551e681849b84752609588fd00a283f60601c46 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Sun, 13 Nov 2022 14:00:31 +0100 Subject: [PATCH] fix bug which could cause multiple players to have the same ID (closes #154) --- include/TNetwork.h | 1 + src/TNetwork.cpp | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/include/TNetwork.h b/include/TNetwork.h index 3b4980e..5ccb8b2 100644 --- a/include/TNetwork.h +++ b/include/TNetwork.h @@ -37,6 +37,7 @@ private: TResourceManager& mResourceManager; std::thread mUDPThread; std::thread mTCPThread; + std::mutex mOpenIDMutex; std::vector UDPRcvFromClient(ip::udp::endpoint& ClientEndpoint); void HandleDownload(TConnection&& TCPSock); diff --git a/src/TNetwork.cpp b/src/TNetwork.cpp index 5a48c46..5e7444c 100644 --- a/src/TNetwork.cpp +++ b/src/TNetwork.cpp @@ -577,6 +577,10 @@ void TNetwork::OnDisconnect(const std::weak_ptr& ClientPtr) { } int TNetwork::OpenID() { + // This lock ensures that each call to OpenID is exclusive. + // If we didn't have this, two concurrent calls to this function may result + // in the same ID. + std::unique_lock Lock(mOpenIDMutex); int ID = 0; bool found; do {