From 1abc9dc452e44b29f6c5c927bab30355f8dd8fc6 Mon Sep 17 00:00:00 2001 From: Albert Medela Date: Mon, 18 Jul 2016 19:35:59 +0200 Subject: [PATCH] Get guest users from session manager --- src/net/common/netpacket.cpp | 3 --- src/net/common/serverlobbythread.cpp | 35 +++++----------------------- src/net/common/sessionmanager.cpp | 18 ++++++++++++++ src/net/serverlobbythread.h | 8 ------- src/net/sessionmanager.h | 1 + src/net/socket_msg.h | 2 -- 6 files changed, 25 insertions(+), 42 deletions(-) diff --git a/src/net/common/netpacket.cpp b/src/net/common/netpacket.cpp index af6fb13f..c07cc7a7 100644 --- a/src/net/common/netpacket.cpp +++ b/src/net/common/netpacket.cpp @@ -248,9 +248,6 @@ NetPacket::GameErrorToNetError(int gameErrorReason) case ERR_NET_SESSION_TIMED_OUT : retVal = ErrorMessage::sessionTimeout; break; - case ERR_NET_FULL_GUESTS : - retVal = ErrorMessage::initServerFull; // LG: @TODO: maybe create a better error message, if possible - break; default : retVal = ErrorMessage::reserved; break; diff --git a/src/net/common/serverlobbythread.cpp b/src/net/common/serverlobbythread.cpp index 45892da2..084f1d71 100644 --- a/src/net/common/serverlobbythread.cpp +++ b/src/net/common/serverlobbythread.cpp @@ -223,7 +223,7 @@ ServerLobbyThread::ServerLobbyThread(GuiInterface &gui, ServerMode mode, ServerI m_mode(mode), m_serverConfig(serverConfig), m_curGameId(0), m_curUniquePlayerId(0), m_curSessionId(INVALID_SESSION + 1), m_statDataChanged(false), m_removeGameTimer(*ioService), m_saveStatisticsTimer(*ioService), m_loginLockTimer(*ioService), - m_startTime(boost::posix_time::second_clock::local_time()), guests_(0) + m_startTime(boost::posix_time::second_clock::local_time()) { m_internalServerCallback.reset(new InternalServerCallback(*this)); m_sender.reset(new SenderHelper(m_ioService)); @@ -397,9 +397,6 @@ ServerLobbyThread::CloseSession(boost::shared_ptr session) m_gameSessionManager.RemoveSession(session->GetId()); if (session->GetPlayerData()) { - if (session->GetPlayerData()->GetRights() == PLAYER_RIGHTS_GUEST) { - DecrementGuests(); - } NotifyPlayerLeftLobby(session->GetPlayerData()->GetUniqueId()); } // Update stats (if needed). @@ -1066,7 +1063,11 @@ ServerLobbyThread::HandleNetPacketInit(boost::shared_ptr session, c } // check if a guest session with same ip is already connected - decline if true if(m_sessionManager.IsGuestConnectedMultiple(session->GetClientAddr())) { - SessionError(session, ERR_NET_FULL_GUESTS); + SessionError(session, ERR_NET_SERVER_FULL); + return; + } + if (m_sessionManager.GetGuestsCount() + m_gameSessionManager.GetGuestsCount() > SERVER_MAX_GUEST_USERS) { + SessionError(session, ERR_NET_SERVER_FULL); return; } } @@ -1075,12 +1076,6 @@ ServerLobbyThread::HandleNetPacketInit(boost::shared_ptr session, c SessionError(session, ERR_NET_INVALID_PLAYER_NAME); return; } - // LG: If user is guest, and SERVER_MAX_GUEST_USERS reached, don't allow connection. - else if (getGuests() > SERVER_MAX_GUEST_USERS) { - SessionError(session, ERR_NET_FULL_GUESTS); - return; - } - } #ifdef POKERTH_OFFICIAL_SERVER else if (initMessage.login() == InitMessage::authenticatedLogin) { @@ -1755,11 +1750,6 @@ ServerLobbyThread::EstablishSession(boost::shared_ptr session) // Session is now established. session->SetState(SessionData::Established); - // LG: Increment guests - if (session->GetPlayerData()->GetRights() == PLAYER_RIGHTS_GUEST) { - IncrementGuests(); - } - { boost::mutex::scoped_lock lock(m_statMutex); ++m_statData.totalPlayersEverLoggedIn; @@ -2389,16 +2379,3 @@ ServerLobbyThread::GetRejoinGameIdForPlayer(const std::string &playerName, const } return retGameId; } - -// LG: Handle guests_ variable. Remove in production to access variable itself -void ServerLobbyThread::DecrementGuests() { - this->guests_.fetch_sub(1); -} - -void ServerLobbyThread::IncrementGuests() { - this->guests_.fetch_add(1); -} - -int ServerLobbyThread::getGuests() { - return this->guests_; -} diff --git a/src/net/common/sessionmanager.cpp b/src/net/common/sessionmanager.cpp index 0ca580fc..a41fa0cc 100644 --- a/src/net/common/sessionmanager.cpp +++ b/src/net/common/sessionmanager.cpp @@ -432,3 +432,21 @@ SessionManager::SendToAllButOneSessions(SenderHelper &sender, boost::shared_ptr< ++i; } } + +unsigned +SessionManager::GetGuestsCount() const +{ + unsigned counter = 0; + boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex); + + SessionMap::const_iterator i = m_sessionMap.begin(); + SessionMap::const_iterator end = m_sessionMap.end(); + + while (i != end) { + if (i->second && i->second->GetPlayerData() && + i->second->GetPlayerData()->GetRights() == PLAYER_RIGHTS_GUEST) + ++counter; + ++i; + } + return counter; +} diff --git a/src/net/serverlobbythread.h b/src/net/serverlobbythread.h index b9bae419..40038432 100644 --- a/src/net/serverlobbythread.h +++ b/src/net/serverlobbythread.h @@ -225,11 +225,6 @@ protected: u_int32_t GetRejoinGameIdForPlayer(const std::string &playerName, const std::string &guid, unsigned &outPlayerUniqueId); - // LG: Handle guests_ counter. NOTE: These functions in production could be removed and access directly to guest_ - void IncrementGuests(); - void DecrementGuests(); - int getGuests(); - private: boost::shared_ptr m_ioService; @@ -282,9 +277,6 @@ private: const boost::posix_time::ptime m_startTime; - // LG: guest_ is thread safe, even if ServerLobbyThread pointer is shared accross the code, as long as it is ONLY ONE running instance - boost::atomic guests_; - friend class InternalServerCallback; }; diff --git a/src/net/sessionmanager.h b/src/net/sessionmanager.h index 882c333f..da983a7a 100644 --- a/src/net/sessionmanager.h +++ b/src/net/sessionmanager.h @@ -75,6 +75,7 @@ public: unsigned GetRawSessionCount() const; unsigned GetSessionCountWithState(int state) const; bool HasSessionWithState(int state) const; + unsigned GetGuestsCount() const; void SendToAllSessions(SenderHelper &sender, boost::shared_ptr packet, int state); void SendLobbyMsgToAllSessions(SenderHelper &sender, boost::shared_ptr packet, int state); diff --git a/src/net/socket_msg.h b/src/net/socket_msg.h index 84987a4b..d65fe604 100644 --- a/src/net/socket_msg.h +++ b/src/net/socket_msg.h @@ -98,8 +98,6 @@ #define ERR_NET_GSASL_INIT_FAILED 134 #define ERR_NET_GSASL_NO_SCRAM 135 #define ERR_NET_DB_CONNECT_FAILED 136 -// LG: New code for max number of guest users -#define ERR_NET_FULL_GUESTS 137 #define ERR_IRC_INTERNAL 151 #define ERR_IRC_CONNECT_FAILED 152