diff --git a/src/net/common/serverlobbythread.cpp b/src/net/common/serverlobbythread.cpp index 55c04213..00ac9c58 100644 --- a/src/net/common/serverlobbythread.cpp +++ b/src/net/common/serverlobbythread.cpp @@ -521,8 +521,6 @@ ServerLobbyThread::Main() LOG_ERROR(e.what()); } - // Stop all running games. - TerminateGames(); // Remove all sessions. m_gameSessionManager.Clear(); m_sessionManager.Clear(); @@ -572,16 +570,6 @@ ServerLobbyThread::HandleRead(SessionId sessionId, const boost::system::error_co session = m_gameSessionManager.GetSessionById(sessionId); if (session.sessionData) { - // Retrieve current game, if applicable. - unsigned gameId = session.sessionData->GetGameId(); - boost::shared_ptr game; - if (gameId) - { - GameMap::iterator pos = m_gameMap.find(gameId); - - if (pos != m_gameMap.end()) - game = pos->second; - } if (!error) { @@ -593,11 +581,22 @@ ServerLobbyThread::HandleRead(SessionId sessionId, const boost::system::error_co { boost::shared_ptr packet = buf.receivedPackets.front(); buf.receivedPackets.pop_front(); + // Retrieve current game, if applicable. + boost::shared_ptr game = InternalGetGameFromId(session.sessionData->GetGameId()); if (game) - game->HandlePacket(session, packet); + { + // We need to catch game-specific exceptions, so that they do not affect the server. + try + { + game->HandlePacket(session, packet); + } catch (const PokerTHException &e) + { + LOG_ERROR(e.what()); + InternalRemoveGame(game); + } + } else HandlePacket(session, packet); - // TODO state may have changed between. } session.sessionData->GetAsioSocket()->async_read_some( boost::asio::buffer(buf.recvBuf + buf.recvBufUsed, RECV_BUF_SIZE - buf.recvBufUsed), @@ -611,6 +610,7 @@ ServerLobbyThread::HandleRead(SessionId sessionId, const boost::system::error_co else { // On error: Close this session. + boost::shared_ptr game = InternalGetGameFromId(session.sessionData->GetGameId()); if (game) game->ErrorRemoveSession(session); else @@ -1144,6 +1144,20 @@ ServerLobbyThread::TimerCleanupAvatarCache() } } +boost::shared_ptr +ServerLobbyThread::InternalGetGameFromId(unsigned gameId) +{ + boost::shared_ptr game; + if (gameId) + { + GameMap::iterator pos = m_gameMap.find(gameId); + + if (pos != m_gameMap.end()) + game = pos->second; + } + return game; +} + void ServerLobbyThread::InternalAddGame(boost::shared_ptr game) { @@ -1233,22 +1247,6 @@ ServerLobbyThread::InternalResubscribeMsg(SessionWrapper session) } } -void -ServerLobbyThread::TerminateGames() -{ - // TODO deprecated - GameMap::iterator i = m_gameMap.begin(); - GameMap::iterator end = m_gameMap.end(); - - while (i != end) - { - //i->second->SignalTermination(); - //i->second->Join(GAME_THREAD_TERMINATE_TIMEOUT); - ++i; - } - m_gameMap.clear(); -} - void ServerLobbyThread::HandleReAddedSession(SessionWrapper session) { diff --git a/src/net/serverlobbythread.h b/src/net/serverlobbythread.h index 81cd905f..3d44c668 100644 --- a/src/net/serverlobbythread.h +++ b/src/net/serverlobbythread.h @@ -134,13 +134,12 @@ protected: void TimerCheckSessionTimeouts(); void TimerCleanupAvatarCache(); + boost::shared_ptr InternalGetGameFromId(unsigned gameId); void InternalAddGame(boost::shared_ptr game); void InternalRemoveGame(boost::shared_ptr game); void InternalRemovePlayer(unsigned playerId, unsigned errorCode); void InternalResubscribeMsg(SessionWrapper session); - void TerminateGames(); - void HandleReAddedSession(SessionWrapper session); void InternalCheckSessionTimeouts(SessionWrapper session);