Skip to content

Commit f22a5f1

Browse files
committed
Fixed multithreading issue in http server
1 parent 4899692 commit f22a5f1

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

Server/mods/deathmatch/StdInc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <vector>
2121
#include <ctime>
2222
#include <sstream>
23+
#include <mutex>
2324

2425
// Forward declarations
2526
class CAclRightName;

Server/mods/deathmatch/logic/CHTTPD.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ bool CHTTPD::StartHTTPD ( const char* szIP, unsigned int port )
9191
return bResult;
9292
}
9393

94-
// Called from worker thread.
94+
// Called from worker thread. Careful now.
9595
// Do some stuff before allowing EHS to do the proper routing
9696
HttpResponse * CHTTPD::RouteRequest ( HttpRequest * ipoHttpRequest )
9797
{
@@ -227,6 +227,7 @@ CAccount * CHTTPD::CheckAuthentication ( HttpRequest * ipoHttpRequest )
227227
if ( strAccountName.compare ( CONSOLE_ACCOUNT_NAME ) != 0 )
228228
{
229229
// Handle initial login logging
230+
std::lock_guard< std::mutex > guard( m_mutexLoggedInMap );
230231
if ( m_LoggedInMap.find ( authName ) == m_LoggedInMap.end () )
231232
CLogger::AuthPrintf ( "HTTP: '%s' entered correct password from %s\n", authName.c_str () , ipoHttpRequest->GetAddress ().c_str () );
232233
m_LoggedInMap[authName] = GetTickCount64_ ();
@@ -245,15 +246,10 @@ CAccount * CHTTPD::CheckAuthentication ( HttpRequest * ipoHttpRequest )
245246
return m_pGuestAccount;
246247
}
247248

249+
// Called from worker thread. Careful now.
248250
void CHTTPD::HttpPulse ( void )
249251
{
250-
// Prevent more than one thread running this at once
251-
static int iBusy = 0;
252-
if ( ++iBusy > 1 )
253-
{
254-
iBusy--;
255-
return;
256-
}
252+
std::lock_guard< std::mutex > guard( m_mutexLoggedInMap );
257253

258254
long long llExpireTime = GetTickCount64_ () - 1000 * 60 * 5; // 5 minute timeout
259255

@@ -263,23 +259,21 @@ void CHTTPD::HttpPulse ( void )
263259
// Remove if too long since last request
264260
if ( iter->second < llExpireTime )
265261
{
266-
g_pGame->Lock(); // get the mutex (blocking)
267262
CLogger::AuthPrintf ( "HTTP: '%s' no longer connected\n", iter->first.c_str () );
268263
m_LoggedInMap.erase ( iter++ );
269-
g_pGame->Unlock();
270264
}
271265
else
272266
iter++;
273267
}
274-
275-
iBusy--;
276268
}
277269

278270
//
279271
// Do DoS check here
280-
//
272+
// Called from worker thread. Careful now.
281273
bool CHTTPD::ShouldAllowConnection ( const char * szAddress )
282274
{
275+
std::lock_guard< std::mutex > guard( m_mutexHttpDosProtect );
276+
283277
if ( MapContains( m_HttpDosExcludeMap, szAddress ) )
284278
return true;
285279

Server/mods/deathmatch/logic/CHTTPD.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class CHTTPD : public EHS
5757
CConnectHistory m_BruteForceProtect;
5858
CConnectHistory m_HttpDosProtect;
5959
std::set < SString > m_HttpDosExcludeMap;
60+
std::mutex m_mutexHttpDosProtect;
61+
std::mutex m_mutexLoggedInMap;
6062
};
6163

6264
#endif

0 commit comments

Comments
 (0)