Each network session now has a weak ptr to its game, to prevent looking up the game id all the time.

This commit is contained in:
lotodore
2011-03-10 21:53:25 +00:00
parent 9b9a921867
commit 95305e7ba5
5 changed files with 33 additions and 33 deletions
+1 -1
View File
@@ -783,7 +783,7 @@ ServerGame::MoveSessionToLobby(boost::shared_ptr<SessionData> 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
+20 -21
View File
@@ -295,13 +295,13 @@ ServerLobbyThread::AddConnection(boost::shared_ptr<tcp::socket> sock)
}
void
ServerLobbyThread::ReAddSession(boost::shared_ptr<SessionData> session, int reason)
ServerLobbyThread::ReAddSession(boost::shared_ptr<SessionData> session, int reason, unsigned gameId)
{
if (session && session->GetPlayerData()) {
boost::shared_ptr<NetPacket> 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<SessionData> session, int reas
}
void
ServerLobbyThread::MoveSessionToGame(ServerGame &game, boost::shared_ptr<SessionData> session, bool autoLeave)
ServerLobbyThread::MoveSessionToGame(boost::shared_ptr<ServerGame> game, boost::shared_ptr<SessionData> session, bool autoLeave)
{
// Remove session from the lobby.
m_sessionManager.RemoveSession(session->GetId());
@@ -341,12 +341,12 @@ ServerLobbyThread::MoveSessionToGame(ServerGame &game, boost::shared_ptr<Session
// Store it in the list of game sessions.
m_gameSessionManager.AddSession(session);
// Set the game id of the session.
session->SetGameId(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<ServerGame> tmpGame = session->GetGame();
if (tmpGame) {
tmpGame->ErrorRemoveSession(session);
} else {
CloseSession(session);
}
@@ -386,7 +386,7 @@ ServerLobbyThread::CloseSession(boost::shared_ptr<SessionData> session)
NotifyPlayerLeftLobby(session->GetPlayerData()->GetUniqueId());
// Update stats (if needed).
UpdateStatisticsNumberOfPlayers();
session->SetGameId(0);
session->SetGame(boost::shared_ptr<ServerGame>());
}
}
@@ -857,7 +857,7 @@ ServerLobbyThread::DispatchPacket(boost::shared_ptr<SessionData> session, boost:
{
if (session) {
// Retrieve current game, if applicable.
boost::shared_ptr<ServerGame> game = InternalGetGameFromId(session->GetGameId());
boost::shared_ptr<ServerGame> 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<SessionData> 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<SessionData> 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<ServerGame> 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<SessionData> 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<ServerGame> tmpGame = targetSession->GetGame();
if (!tmpGame || !tmpGame->IsRunning()) {
boost::shared_ptr<NetPacket> 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<SessionData> 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<ServerGame>());
// Add session to lobby list.
m_sessionManager.AddSession(session);
} else {
+6 -6
View File
@@ -25,7 +25,7 @@
using namespace std;
SessionData::SessionData(boost::shared_ptr<boost::asio::ip::tcp::socket> 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<ServerGame>
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<ServerGame> game)
{
boost::mutex::scoped_lock lock(m_dataMutex);
m_gameId = gameId;
m_game = game;
}
SessionData::State
+2 -2
View File
@@ -61,8 +61,8 @@ public:
virtual void SignalTermination();
void AddConnection(boost::shared_ptr<boost::asio::ip::tcp::socket> sock);
void ReAddSession(boost::shared_ptr<SessionData> session, int reason);
void MoveSessionToGame(ServerGame &game, boost::shared_ptr<SessionData> session, bool autoLeave);
void ReAddSession(boost::shared_ptr<SessionData> session, int reason, unsigned gameId);
void MoveSessionToGame(boost::shared_ptr<ServerGame> game, boost::shared_ptr<SessionData> session, bool autoLeave);
void RemoveSessionFromGame(boost::shared_ptr<SessionData> session);
void SessionError(boost::shared_ptr<SessionData> session, int errorCode);
void ResubscribeLobbyMsg(boost::shared_ptr<SessionData> session);
+4 -3
View File
@@ -42,6 +42,7 @@ class ReceiveBuffer;
class SendBuffer;
class NetPacket;
class PlayerData;
class ServerGame;
class SessionData : public boost::enable_shared_from_this<SessionData>
{
@@ -53,8 +54,8 @@ public:
SessionId GetId() const;
unsigned GetGameId() const;
void SetGameId(unsigned gameId);
boost::shared_ptr<ServerGame> GetGame() const;
void SetGame(boost::shared_ptr<ServerGame> game);
State GetState() const;
void SetState(State state);
@@ -111,7 +112,7 @@ protected:
private:
boost::shared_ptr<boost::asio::ip::tcp::socket> m_socket;
const SessionId m_id;
unsigned m_gameId;
boost::weak_ptr<ServerGame> m_game;
State m_state;
std::string m_clientAddr;
boost::shared_ptr<ReceiveBuffer> m_receiveBuffer;