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:
@@ -783,7 +783,7 @@ ServerGame::MoveSessionToLobby(boost::shared_ptr<SessionData> session, int reaso
|
|||||||
GracefulRemoveSession(session, reason);
|
GracefulRemoveSession(session, reason);
|
||||||
// Reset ready flag - just in case it is set, player may leave at any time.
|
// Reset ready flag - just in case it is set, player may leave at any time.
|
||||||
session->ResetReadyFlag();
|
session->ResetReadyFlag();
|
||||||
GetLobbyThread().ReAddSession(session, reason);
|
GetLobbyThread().ReAddSession(session, reason, GetId());
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|||||||
@@ -295,13 +295,13 @@ ServerLobbyThread::AddConnection(boost::shared_ptr<tcp::socket> sock)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
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()) {
|
if (session && session->GetPlayerData()) {
|
||||||
boost::shared_ptr<NetPacket> packet(new NetPacket(NetPacket::Alloc));
|
boost::shared_ptr<NetPacket> packet(new NetPacket(NetPacket::Alloc));
|
||||||
packet->GetMsg()->present = PokerTHMessage_PR_gamePlayerMessage;
|
packet->GetMsg()->present = PokerTHMessage_PR_gamePlayerMessage;
|
||||||
GamePlayerMessage_t *netPlayerMsg = &packet->GetMsg()->choice.gamePlayerMessage;
|
GamePlayerMessage_t *netPlayerMsg = &packet->GetMsg()->choice.gamePlayerMessage;
|
||||||
netPlayerMsg->gameId = session->GetGameId();
|
netPlayerMsg->gameId = gameId;
|
||||||
netPlayerMsg->gamePlayerNotification.present = gamePlayerNotification_PR_removedFromGame;
|
netPlayerMsg->gamePlayerNotification.present = gamePlayerNotification_PR_removedFromGame;
|
||||||
RemovedFromGame_t *removed = &netPlayerMsg->gamePlayerNotification.choice.removedFromGame;
|
RemovedFromGame_t *removed = &netPlayerMsg->gamePlayerNotification.choice.removedFromGame;
|
||||||
|
|
||||||
@@ -332,7 +332,7 @@ ServerLobbyThread::ReAddSession(boost::shared_ptr<SessionData> session, int reas
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
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.
|
// Remove session from the lobby.
|
||||||
m_sessionManager.RemoveSession(session->GetId());
|
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.
|
// Store it in the list of game sessions.
|
||||||
m_gameSessionManager.AddSession(session);
|
m_gameSessionManager.AddSession(session);
|
||||||
// Set the game id of the session.
|
// Set the game id of the session.
|
||||||
session->SetGameId(game.GetId());
|
session->SetGame(game);
|
||||||
// Add session to the game.
|
// Add session to the game.
|
||||||
game.AddSession(session);
|
game->AddSession(session);
|
||||||
// Optionally enable auto leave after game finish.
|
// Optionally enable auto leave after game finish.
|
||||||
if (autoLeave)
|
if (autoLeave)
|
||||||
game.SetPlayerAutoLeaveOnFinish(session->GetPlayerData()->GetUniqueId());
|
game->SetPlayerAutoLeaveOnFinish(session->GetPlayerData()->GetUniqueId());
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -363,9 +363,9 @@ ServerLobbyThread::CloseSession(SessionId sessionId)
|
|||||||
if (!session)
|
if (!session)
|
||||||
session = m_gameSessionManager.GetSessionById(sessionId);
|
session = m_gameSessionManager.GetSessionById(sessionId);
|
||||||
if (session) {
|
if (session) {
|
||||||
GameMap::iterator pos = m_gameMap.find(session->GetGameId());
|
boost::shared_ptr<ServerGame> tmpGame = session->GetGame();
|
||||||
if (pos != m_gameMap.end()) {
|
if (tmpGame) {
|
||||||
pos->second->ErrorRemoveSession(session);
|
tmpGame->ErrorRemoveSession(session);
|
||||||
} else {
|
} else {
|
||||||
CloseSession(session);
|
CloseSession(session);
|
||||||
}
|
}
|
||||||
@@ -386,7 +386,7 @@ ServerLobbyThread::CloseSession(boost::shared_ptr<SessionData> session)
|
|||||||
NotifyPlayerLeftLobby(session->GetPlayerData()->GetUniqueId());
|
NotifyPlayerLeftLobby(session->GetPlayerData()->GetUniqueId());
|
||||||
// Update stats (if needed).
|
// Update stats (if needed).
|
||||||
UpdateStatisticsNumberOfPlayers();
|
UpdateStatisticsNumberOfPlayers();
|
||||||
session->SetGameId(0);
|
session->SetGame(boost::shared_ptr<ServerGame>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -857,7 +857,7 @@ ServerLobbyThread::DispatchPacket(boost::shared_ptr<SessionData> session, boost:
|
|||||||
{
|
{
|
||||||
if (session) {
|
if (session) {
|
||||||
// Retrieve current game, if applicable.
|
// Retrieve current game, if applicable.
|
||||||
boost::shared_ptr<ServerGame> game = InternalGetGameFromId(session->GetGameId());
|
boost::shared_ptr<ServerGame> game = session->GetGame();
|
||||||
if (game) {
|
if (game) {
|
||||||
// We need to catch game-specific exceptions, so that they do not affect the server.
|
// We need to catch game-specific exceptions, so that they do not affect the server.
|
||||||
try {
|
try {
|
||||||
@@ -1281,7 +1281,7 @@ ServerLobbyThread::HandleNetPacketCreateGame(boost::shared_ptr<SessionData> sess
|
|||||||
// Add game to list of games.
|
// Add game to list of games.
|
||||||
InternalAddGame(game);
|
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);
|
GameMap::iterator pos = m_gameMap.find(joinGame.gameId);
|
||||||
|
|
||||||
if (pos != m_gameMap.end()) {
|
if (pos != m_gameMap.end()) {
|
||||||
ServerGame &game = *pos->second;
|
boost::shared_ptr<ServerGame> game = pos->second;
|
||||||
const GameData &tmpData = game.GetGameData();
|
const GameData &tmpData = game->GetGameData();
|
||||||
if (session->GetPlayerData()->GetRights() == PLAYER_RIGHTS_GUEST
|
if (session->GetPlayerData()->GetRights() == PLAYER_RIGHTS_GUEST
|
||||||
&& tmpData.gameType != GAME_TYPE_NORMAL) {
|
&& tmpData.gameType != GAME_TYPE_NORMAL) {
|
||||||
SendJoinGameFailed(session, joinGame.gameId, NTF_NET_JOIN_GUEST_FORBIDDEN);
|
SendJoinGameFailed(session, joinGame.gameId, NTF_NET_JOIN_GUEST_FORBIDDEN);
|
||||||
} else if (tmpData.gameType == GAME_TYPE_INVITE_ONLY
|
} 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);
|
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);
|
SendJoinGameFailed(session, joinGame.gameId, NTF_NET_JOIN_INVALID_PASSWORD);
|
||||||
} else if (tmpData.gameType == GAME_TYPE_RANKING
|
} else if (tmpData.gameType == GAME_TYPE_RANKING
|
||||||
&& session->GetClientAddr() != SERVER_ADDRESS_LOCALHOST_STR
|
&& session->GetClientAddr() != SERVER_ADDRESS_LOCALHOST_STR
|
||||||
&& session->GetClientAddr() != SERVER_ADDRESS_LOCALHOST_STR_V4V6
|
&& session->GetClientAddr() != SERVER_ADDRESS_LOCALHOST_STR_V4V6
|
||||||
&& session->GetClientAddr() != SERVER_ADDRESS_LOCALHOST_STR_V4
|
&& session->GetClientAddr() != SERVER_ADDRESS_LOCALHOST_STR_V4
|
||||||
&& game.IsClientAddressConnected(session->GetClientAddr())) {
|
&& game->IsClientAddressConnected(session->GetClientAddr())) {
|
||||||
SendJoinGameFailed(session, joinGame.gameId, NTF_NET_JOIN_IP_BLOCKED);
|
SendJoinGameFailed(session, joinGame.gameId, NTF_NET_JOIN_IP_BLOCKED);
|
||||||
} else {
|
} else {
|
||||||
MoveSessionToGame(game, session, autoLeave);
|
MoveSessionToGame(game, session, autoLeave);
|
||||||
@@ -1357,9 +1357,8 @@ ServerLobbyThread::HandleNetPacketChatRequest(boost::shared_ptr<SessionData> ses
|
|||||||
|
|
||||||
if (targetSession && targetSession->GetPlayerData()) {
|
if (targetSession && targetSession->GetPlayerData()) {
|
||||||
// Only allow private messages to players which are not in running games.
|
// Only allow private messages to players which are not in running games.
|
||||||
unsigned gameId = targetSession->GetGameId();
|
boost::shared_ptr<ServerGame> tmpGame = targetSession->GetGame();
|
||||||
GameMap::const_iterator pos = m_gameMap.find(gameId);
|
if (!tmpGame || !tmpGame->IsRunning()) {
|
||||||
if (pos == m_gameMap.end() || !pos->second->IsRunning()) {
|
|
||||||
boost::shared_ptr<NetPacket> packet(new NetPacket(NetPacket::Alloc));
|
boost::shared_ptr<NetPacket> packet(new NetPacket(NetPacket::Alloc));
|
||||||
packet->GetMsg()->present = PokerTHMessage_PR_chatMessage;
|
packet->GetMsg()->present = PokerTHMessage_PR_chatMessage;
|
||||||
ChatMessage_t *netChat = &packet->GetMsg()->choice.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) {
|
if (m_sessionManager.GetRawSessionCount() <= SERVER_MAX_NUM_LOBBY_SESSIONS) {
|
||||||
// Set state (back) to established.
|
// Set state (back) to established.
|
||||||
session->SetState(SessionData::Established);
|
session->SetState(SessionData::Established);
|
||||||
session->SetGameId(0);
|
session->SetGame(boost::shared_ptr<ServerGame>());
|
||||||
// Add session to lobby list.
|
// Add session to lobby list.
|
||||||
m_sessionManager.AddSession(session);
|
m_sessionManager.AddSession(session);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -25,7 +25,7 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
SessionData::SessionData(boost::shared_ptr<boost::asio::ip::tcp::socket> sock, SessionId id, SessionDataCallback &cb)
|
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_activityTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::microsec_timer::auto_start),
|
||||||
m_activityTimeoutNoticeSent(false),
|
m_activityTimeoutNoticeSent(false),
|
||||||
m_autoDisconnectTimer(boost::posix_time::time_duration(0, 0, 0), boost::timers::portable::microsec_timer::auto_start),
|
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;
|
return m_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned
|
boost::shared_ptr<ServerGame>
|
||||||
SessionData::GetGameId() const
|
SessionData::GetGame() const
|
||||||
{
|
{
|
||||||
boost::mutex::scoped_lock lock(m_dataMutex);
|
boost::mutex::scoped_lock lock(m_dataMutex);
|
||||||
return m_gameId;
|
return m_game.lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
SessionData::SetGameId(unsigned gameId)
|
SessionData::SetGame(boost::shared_ptr<ServerGame> game)
|
||||||
{
|
{
|
||||||
boost::mutex::scoped_lock lock(m_dataMutex);
|
boost::mutex::scoped_lock lock(m_dataMutex);
|
||||||
m_gameId = gameId;
|
m_game = game;
|
||||||
}
|
}
|
||||||
|
|
||||||
SessionData::State
|
SessionData::State
|
||||||
|
|||||||
@@ -61,8 +61,8 @@ public:
|
|||||||
virtual void SignalTermination();
|
virtual void SignalTermination();
|
||||||
|
|
||||||
void AddConnection(boost::shared_ptr<boost::asio::ip::tcp::socket> sock);
|
void AddConnection(boost::shared_ptr<boost::asio::ip::tcp::socket> sock);
|
||||||
void ReAddSession(boost::shared_ptr<SessionData> session, int reason);
|
void ReAddSession(boost::shared_ptr<SessionData> session, int reason, unsigned gameId);
|
||||||
void MoveSessionToGame(ServerGame &game, boost::shared_ptr<SessionData> session, bool autoLeave);
|
void MoveSessionToGame(boost::shared_ptr<ServerGame> game, boost::shared_ptr<SessionData> session, bool autoLeave);
|
||||||
void RemoveSessionFromGame(boost::shared_ptr<SessionData> session);
|
void RemoveSessionFromGame(boost::shared_ptr<SessionData> session);
|
||||||
void SessionError(boost::shared_ptr<SessionData> session, int errorCode);
|
void SessionError(boost::shared_ptr<SessionData> session, int errorCode);
|
||||||
void ResubscribeLobbyMsg(boost::shared_ptr<SessionData> session);
|
void ResubscribeLobbyMsg(boost::shared_ptr<SessionData> session);
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ class ReceiveBuffer;
|
|||||||
class SendBuffer;
|
class SendBuffer;
|
||||||
class NetPacket;
|
class NetPacket;
|
||||||
class PlayerData;
|
class PlayerData;
|
||||||
|
class ServerGame;
|
||||||
|
|
||||||
class SessionData : public boost::enable_shared_from_this<SessionData>
|
class SessionData : public boost::enable_shared_from_this<SessionData>
|
||||||
{
|
{
|
||||||
@@ -53,8 +54,8 @@ public:
|
|||||||
|
|
||||||
SessionId GetId() const;
|
SessionId GetId() const;
|
||||||
|
|
||||||
unsigned GetGameId() const;
|
boost::shared_ptr<ServerGame> GetGame() const;
|
||||||
void SetGameId(unsigned gameId);
|
void SetGame(boost::shared_ptr<ServerGame> game);
|
||||||
|
|
||||||
State GetState() const;
|
State GetState() const;
|
||||||
void SetState(State state);
|
void SetState(State state);
|
||||||
@@ -111,7 +112,7 @@ protected:
|
|||||||
private:
|
private:
|
||||||
boost::shared_ptr<boost::asio::ip::tcp::socket> m_socket;
|
boost::shared_ptr<boost::asio::ip::tcp::socket> m_socket;
|
||||||
const SessionId m_id;
|
const SessionId m_id;
|
||||||
unsigned m_gameId;
|
boost::weak_ptr<ServerGame> m_game;
|
||||||
State m_state;
|
State m_state;
|
||||||
std::string m_clientAddr;
|
std::string m_clientAddr;
|
||||||
boost::shared_ptr<ReceiveBuffer> m_receiveBuffer;
|
boost::shared_ptr<ReceiveBuffer> m_receiveBuffer;
|
||||||
|
|||||||
Reference in New Issue
Block a user