From 95305e7ba524adbba132d993d8c8386520e5085b Mon Sep 17 00:00:00 2001 From: lotodore Date: Thu, 10 Mar 2011 21:53:25 +0000 Subject: [PATCH] Each network session now has a weak ptr to its game, to prevent looking up the game id all the time. --- src/net/common/servergame.cpp | 2 +- src/net/common/serverlobbythread.cpp | 41 ++++++++++++++-------------- src/net/common/sessiondata.cpp | 12 ++++---- src/net/serverlobbythread.h | 4 +-- src/net/sessiondata.h | 7 +++-- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/net/common/servergame.cpp b/src/net/common/servergame.cpp index 1870537f..44dbe606 100644 --- a/src/net/common/servergame.cpp +++ b/src/net/common/servergame.cpp @@ -783,7 +783,7 @@ ServerGame::MoveSessionToLobby(boost::shared_ptr session, int reaso GracefulRemoveSession(session, reason); // Reset ready flag - just in case it is set, player may leave at any time. session->ResetReadyFlag(); - GetLobbyThread().ReAddSession(session, reason); + GetLobbyThread().ReAddSession(session, reason, GetId()); } void diff --git a/src/net/common/serverlobbythread.cpp b/src/net/common/serverlobbythread.cpp index 55bdeff7..a2de270c 100644 --- a/src/net/common/serverlobbythread.cpp +++ b/src/net/common/serverlobbythread.cpp @@ -295,13 +295,13 @@ ServerLobbyThread::AddConnection(boost::shared_ptr sock) } void -ServerLobbyThread::ReAddSession(boost::shared_ptr session, int reason) +ServerLobbyThread::ReAddSession(boost::shared_ptr session, int reason, unsigned gameId) { if (session && session->GetPlayerData()) { boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); packet->GetMsg()->present = PokerTHMessage_PR_gamePlayerMessage; GamePlayerMessage_t *netPlayerMsg = &packet->GetMsg()->choice.gamePlayerMessage; - netPlayerMsg->gameId = session->GetGameId(); + netPlayerMsg->gameId = gameId; netPlayerMsg->gamePlayerNotification.present = gamePlayerNotification_PR_removedFromGame; RemovedFromGame_t *removed = &netPlayerMsg->gamePlayerNotification.choice.removedFromGame; @@ -332,7 +332,7 @@ ServerLobbyThread::ReAddSession(boost::shared_ptr session, int reas } void -ServerLobbyThread::MoveSessionToGame(ServerGame &game, boost::shared_ptr session, bool autoLeave) +ServerLobbyThread::MoveSessionToGame(boost::shared_ptr game, boost::shared_ptr session, bool autoLeave) { // Remove session from the lobby. m_sessionManager.RemoveSession(session->GetId()); @@ -341,12 +341,12 @@ ServerLobbyThread::MoveSessionToGame(ServerGame &game, boost::shared_ptrSetGameId(game.GetId()); + session->SetGame(game); // Add session to the game. - game.AddSession(session); + game->AddSession(session); // Optionally enable auto leave after game finish. if (autoLeave) - game.SetPlayerAutoLeaveOnFinish(session->GetPlayerData()->GetUniqueId()); + game->SetPlayerAutoLeaveOnFinish(session->GetPlayerData()->GetUniqueId()); } void @@ -363,9 +363,9 @@ ServerLobbyThread::CloseSession(SessionId sessionId) if (!session) session = m_gameSessionManager.GetSessionById(sessionId); if (session) { - GameMap::iterator pos = m_gameMap.find(session->GetGameId()); - if (pos != m_gameMap.end()) { - pos->second->ErrorRemoveSession(session); + boost::shared_ptr tmpGame = session->GetGame(); + if (tmpGame) { + tmpGame->ErrorRemoveSession(session); } else { CloseSession(session); } @@ -386,7 +386,7 @@ ServerLobbyThread::CloseSession(boost::shared_ptr session) NotifyPlayerLeftLobby(session->GetPlayerData()->GetUniqueId()); // Update stats (if needed). UpdateStatisticsNumberOfPlayers(); - session->SetGameId(0); + session->SetGame(boost::shared_ptr()); } } @@ -857,7 +857,7 @@ ServerLobbyThread::DispatchPacket(boost::shared_ptr session, boost: { if (session) { // Retrieve current game, if applicable. - boost::shared_ptr game = InternalGetGameFromId(session->GetGameId()); + boost::shared_ptr game = session->GetGame(); if (game) { // We need to catch game-specific exceptions, so that they do not affect the server. try { @@ -1281,7 +1281,7 @@ ServerLobbyThread::HandleNetPacketCreateGame(boost::shared_ptr sess // Add game to list of games. InternalAddGame(game); - MoveSessionToGame(*game, session, autoLeave); + MoveSessionToGame(game, session, autoLeave); } } @@ -1292,21 +1292,21 @@ ServerLobbyThread::HandleNetPacketJoinGame(boost::shared_ptr sessio GameMap::iterator pos = m_gameMap.find(joinGame.gameId); if (pos != m_gameMap.end()) { - ServerGame &game = *pos->second; - const GameData &tmpData = game.GetGameData(); + boost::shared_ptr game = pos->second; + const GameData &tmpData = game->GetGameData(); if (session->GetPlayerData()->GetRights() == PLAYER_RIGHTS_GUEST && tmpData.gameType != GAME_TYPE_NORMAL) { SendJoinGameFailed(session, joinGame.gameId, NTF_NET_JOIN_GUEST_FORBIDDEN); } else if (tmpData.gameType == GAME_TYPE_INVITE_ONLY - && !game.IsPlayerInvited(session->GetPlayerData()->GetUniqueId())) { + && !game->IsPlayerInvited(session->GetPlayerData()->GetUniqueId())) { SendJoinGameFailed(session, joinGame.gameId, NTF_NET_JOIN_NOT_INVITED); - } else if (!game.CheckPassword(password)) { + } else if (!game->CheckPassword(password)) { SendJoinGameFailed(session, joinGame.gameId, NTF_NET_JOIN_INVALID_PASSWORD); } else if (tmpData.gameType == GAME_TYPE_RANKING && session->GetClientAddr() != SERVER_ADDRESS_LOCALHOST_STR && session->GetClientAddr() != SERVER_ADDRESS_LOCALHOST_STR_V4V6 && session->GetClientAddr() != SERVER_ADDRESS_LOCALHOST_STR_V4 - && game.IsClientAddressConnected(session->GetClientAddr())) { + && game->IsClientAddressConnected(session->GetClientAddr())) { SendJoinGameFailed(session, joinGame.gameId, NTF_NET_JOIN_IP_BLOCKED); } else { MoveSessionToGame(game, session, autoLeave); @@ -1357,9 +1357,8 @@ ServerLobbyThread::HandleNetPacketChatRequest(boost::shared_ptr ses if (targetSession && targetSession->GetPlayerData()) { // Only allow private messages to players which are not in running games. - unsigned gameId = targetSession->GetGameId(); - GameMap::const_iterator pos = m_gameMap.find(gameId); - if (pos == m_gameMap.end() || !pos->second->IsRunning()) { + boost::shared_ptr tmpGame = targetSession->GetGame(); + if (!tmpGame || !tmpGame->IsRunning()) { boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); packet->GetMsg()->present = PokerTHMessage_PR_chatMessage; ChatMessage_t *netChat = &packet->GetMsg()->choice.chatMessage; @@ -1823,7 +1822,7 @@ ServerLobbyThread::HandleReAddedSession(boost::shared_ptr session) if (m_sessionManager.GetRawSessionCount() <= SERVER_MAX_NUM_LOBBY_SESSIONS) { // Set state (back) to established. session->SetState(SessionData::Established); - session->SetGameId(0); + session->SetGame(boost::shared_ptr()); // Add session to lobby list. m_sessionManager.AddSession(session); } else { diff --git a/src/net/common/sessiondata.cpp b/src/net/common/sessiondata.cpp index f9bd1dd7..24910479 100644 --- a/src/net/common/sessiondata.cpp +++ b/src/net/common/sessiondata.cpp @@ -25,7 +25,7 @@ using namespace std; SessionData::SessionData(boost::shared_ptr sock, SessionId id, SessionDataCallback &cb) - : m_socket(sock), m_id(id), m_gameId(0), m_state(SessionData::Init), m_readyFlag(false), m_wantsLobbyMsg(true), + : m_socket(sock), m_id(id), m_state(SessionData::Init), m_readyFlag(false), m_wantsLobbyMsg(true), m_activityTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::microsec_timer::auto_start), m_activityTimeoutNoticeSent(false), m_autoDisconnectTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::microsec_timer::auto_start), @@ -47,18 +47,18 @@ SessionData::GetId() const return m_id; } -unsigned -SessionData::GetGameId() const +boost::shared_ptr +SessionData::GetGame() const { boost::mutex::scoped_lock lock(m_dataMutex); - return m_gameId; + return m_game.lock(); } void -SessionData::SetGameId(unsigned gameId) +SessionData::SetGame(boost::shared_ptr game) { boost::mutex::scoped_lock lock(m_dataMutex); - m_gameId = gameId; + m_game = game; } SessionData::State diff --git a/src/net/serverlobbythread.h b/src/net/serverlobbythread.h index 1ac066c7..cbc0a537 100644 --- a/src/net/serverlobbythread.h +++ b/src/net/serverlobbythread.h @@ -61,8 +61,8 @@ public: virtual void SignalTermination(); void AddConnection(boost::shared_ptr sock); - void ReAddSession(boost::shared_ptr session, int reason); - void MoveSessionToGame(ServerGame &game, boost::shared_ptr session, bool autoLeave); + void ReAddSession(boost::shared_ptr session, int reason, unsigned gameId); + void MoveSessionToGame(boost::shared_ptr game, boost::shared_ptr session, bool autoLeave); void RemoveSessionFromGame(boost::shared_ptr session); void SessionError(boost::shared_ptr session, int errorCode); void ResubscribeLobbyMsg(boost::shared_ptr session); diff --git a/src/net/sessiondata.h b/src/net/sessiondata.h index 8d4e51e7..47b297fe 100644 --- a/src/net/sessiondata.h +++ b/src/net/sessiondata.h @@ -42,6 +42,7 @@ class ReceiveBuffer; class SendBuffer; class NetPacket; class PlayerData; +class ServerGame; class SessionData : public boost::enable_shared_from_this { @@ -53,8 +54,8 @@ public: SessionId GetId() const; - unsigned GetGameId() const; - void SetGameId(unsigned gameId); + boost::shared_ptr GetGame() const; + void SetGame(boost::shared_ptr game); State GetState() const; void SetState(State state); @@ -111,7 +112,7 @@ protected: private: boost::shared_ptr m_socket; const SessionId m_id; - unsigned m_gameId; + boost::weak_ptr m_game; State m_state; std::string m_clientAddr; boost::shared_ptr m_receiveBuffer;