Skip to content

Commit ca5037a

Browse files
authored
Server: MatchManager: More usage of mutexes, release before throw
1 parent 3c91131 commit ca5037a

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

InternetGamesServer/MatchManager.cpp

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,24 @@ MatchManager::DestroyMatch(unsigned int index)
242242
Win7::Match*
243243
MatchManager::FindLobby(Win7::PlayerSocket& player)
244244
{
245+
switch (WaitForSingleObject(m_mutex, MATCH_MUTEX_TIMEOUT_MS))
246+
{
247+
case WAIT_OBJECT_0: // Acquired ownership of the mutex
248+
break;
249+
case WAIT_TIMEOUT:
250+
throw MutexError("MatchManager::FindLobby(): Timed out waiting for mutex: " + std::to_string(GetLastError()));
251+
case WAIT_ABANDONED: // Acquired ownership of an abandoned mutex
252+
throw MutexError("MatchManager::FindLobby(): Got ownership of an abandoned mutex: " + std::to_string(GetLastError()));
253+
default:
254+
throw MutexError("MatchManager::FindLobby(): An error occured waiting for mutex: " + std::to_string(GetLastError()));
255+
}
256+
245257
if (player.GetLevel() == Win7::Match::Level::INVALID)
258+
{
259+
if (!ReleaseMutex(m_mutex))
260+
throw MutexError("MatchManager::FindLobby(): Couldn't release mutex: " + std::to_string(GetLastError()));
246261
throw std::runtime_error("Cannot find lobby for Windows 7 player: Invalid level!");
262+
}
247263

248264
Win7::Match* targetMatch = nullptr;
249265
for (const auto& match : m_matches_win7)
@@ -262,6 +278,9 @@ MatchManager::FindLobby(Win7::PlayerSocket& player)
262278
SessionLog() << "[MATCH MANAGER] Added " << player.GetAddress()
263279
<< " to existing Windows 7 " << Win7::Match::GameToNameString(targetMatch->GetGame())
264280
<< " match " << targetMatch->GetGUID() << '.' << std::endl;
281+
282+
if (!ReleaseMutex(m_mutex))
283+
throw MutexError("MatchManager::FindLobby(): Couldn't release mutex: " + std::to_string(GetLastError()));
265284
return targetMatch;
266285
}
267286

@@ -270,14 +289,33 @@ MatchManager::FindLobby(Win7::PlayerSocket& player)
270289
SessionLog() << "[MATCH MANAGER] Added " << player.GetAddress()
271290
<< " to new Windows 7 " << Win7::Match::GameToNameString(match->GetGame())
272291
<< " match " << match->GetGUID() << '.' << std::endl;
292+
293+
if (!ReleaseMutex(m_mutex))
294+
throw MutexError("MatchManager::FindLobby(): Couldn't release mutex: " + std::to_string(GetLastError()));
273295
return match;
274296
}
275297

276298
WinXP::Match*
277299
MatchManager::FindLobby(WinXP::PlayerSocket& player)
278300
{
301+
switch (WaitForSingleObject(m_mutex, MATCH_MUTEX_TIMEOUT_MS))
302+
{
303+
case WAIT_OBJECT_0: // Acquired ownership of the mutex
304+
break;
305+
case WAIT_TIMEOUT:
306+
throw MutexError("MatchManager::FindLobby(): Timed out waiting for mutex: " + std::to_string(GetLastError()));
307+
case WAIT_ABANDONED: // Acquired ownership of an abandoned mutex
308+
throw MutexError("MatchManager::FindLobby(): Got ownership of an abandoned mutex: " + std::to_string(GetLastError()));
309+
default:
310+
throw MutexError("MatchManager::FindLobby(): An error occured waiting for mutex: " + std::to_string(GetLastError()));
311+
}
312+
279313
if (player.GetSkillLevel() == WinXP::Match::SkillLevel::INVALID)
314+
{
315+
if (!ReleaseMutex(m_mutex))
316+
throw MutexError("MatchManager::FindLobby(): Couldn't release mutex: " + std::to_string(GetLastError()));
280317
throw std::runtime_error("Cannot find lobby for Windows XP player: Invalid skill level!");
318+
}
281319

282320
WinXP::Match* targetMatch = nullptr;
283321
for (const auto& match : m_matches_winxp)
@@ -296,6 +334,9 @@ MatchManager::FindLobby(WinXP::PlayerSocket& player)
296334
SessionLog() << "[MATCH MANAGER] Added " << player.GetAddress()
297335
<< " to existing Windows XP " << WinXP::Match::GameToNameString(targetMatch->GetGame())
298336
<< " match " << targetMatch->GetGUID() << '.' << std::endl;
337+
338+
if (!ReleaseMutex(m_mutex))
339+
throw MutexError("MatchManager::FindLobby(): Couldn't release mutex: " + std::to_string(GetLastError()));
299340
return targetMatch;
300341
}
301342

@@ -304,6 +345,9 @@ MatchManager::FindLobby(WinXP::PlayerSocket& player)
304345
SessionLog() << "[MATCH MANAGER] Added " << player.GetAddress()
305346
<< " to new Windows XP " << WinXP::Match::GameToNameString(match->GetGame())
306347
<< " match " << match->GetGUID() << '.' << std::endl;
348+
349+
if (!ReleaseMutex(m_mutex))
350+
throw MutexError("MatchManager::FindLobby(): Couldn't release mutex: " + std::to_string(GetLastError()));
307351
return match;
308352
}
309353

@@ -338,12 +382,17 @@ MatchManager::CreateLobby(Win7::PlayerSocket& player)
338382
break;
339383

340384
default:
385+
{
386+
if (!ReleaseMutex(m_mutex))
387+
throw MutexError("MatchManager::CreateLobby(): Couldn't release mutex: " + std::to_string(GetLastError()));
341388
throw std::runtime_error("Cannot create lobby for Windows 7 player: Invalid game type!");
389+
}
342390
}
343391

392+
Win7::Match* targetMatch = m_matches_win7.back().get();
344393
if (!ReleaseMutex(m_mutex))
345394
throw MutexError("MatchManager::CreateLobby(): Couldn't release mutex: " + std::to_string(GetLastError()));
346-
return m_matches_win7.back().get();
395+
return targetMatch;
347396
}
348397

349398
WinXP::Match*
@@ -384,10 +433,15 @@ MatchManager::CreateLobby(WinXP::PlayerSocket& player)
384433
break;
385434

386435
default:
436+
{
437+
if (!ReleaseMutex(m_mutex))
438+
throw MutexError("MatchManager::CreateLobby(): Couldn't release mutex: " + std::to_string(GetLastError()));
387439
throw std::runtime_error("Cannot create lobby for Windows XP player: Invalid game type!");
440+
}
388441
}
389442

443+
WinXP::Match* targetMatch = m_matches_winxp.back().get();
390444
if (!ReleaseMutex(m_mutex))
391445
throw MutexError("MatchManager::CreateLobby(): Couldn't release mutex: " + std::to_string(GetLastError()));
392-
return m_matches_winxp.back().get();
446+
return targetMatch;
393447
}

0 commit comments

Comments
 (0)