Files
pokerth/src/net/serverlobbythread.h
T

251 lines
10 KiB
C++
Raw Normal View History

/***************************************************************************
2009-09-19 20:28:30 +00:00
* Copyright (C) 2007-2009 by Lothar May *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
2009-09-19 20:28:30 +00:00
/* Network server lobby thread. */
2009-09-19 20:28:30 +00:00
#ifndef _SERVERLOBBYTHREAD_H_
#define _SERVERLOBBYTHREAD_H_
#include <boost/asio.hpp>
#include <deque>
#include <boost/enable_shared_from_this.hpp>
#include <net/sessionmanager.h>
#include <net/netpacket.h>
#include <db/serverdbcallback.h>
#include <gui/guiinterface.h>
#include <gamedata.h>
#define NET_LOBBY_THREAD_TERMINATE_TIMEOUT_MSEC 20000
#define NET_ADMIN_IRC_TERMINATE_TIMEOUT_MSEC 4000
class SenderHelper;
class ReceiverHelper;
2009-09-19 20:28:30 +00:00
class InternalServerCallback;
class ServerIrcBotCallback;
class ServerGame;
class ServerBanManager;
class ConfigFile;
class AvatarManager;
2009-08-14 22:05:01 +00:00
class ChatCleanerManager;
2009-09-19 20:28:30 +00:00
class ServerDBInterface;
struct GameData;
class Game;
struct Gsasl;
class ServerLobbyThread : public Thread, public boost::enable_shared_from_this<ServerLobbyThread>
{
public:
ServerLobbyThread(GuiInterface &gui, ServerMode mode, ServerIrcBotCallback &ircBotCb, ConfigFile *playerConfig, AvatarManager &avatarManager,
boost::shared_ptr<boost::asio::io_service> ioService);
virtual ~ServerLobbyThread();
void Init(const std::string &logDir);
virtual void SignalTermination();
void AddConnection(boost::shared_ptr<boost::asio::ip::tcp::socket> sock);
void ReAddSession(SessionWrapper session, int reason);
void MoveSessionToGame(ServerGame &game, SessionWrapper session);
void RemoveSessionFromGame(SessionWrapper session);
void SessionError(SessionWrapper session, int errorCode);
void ResubscribeLobbyMsg(SessionWrapper session);
void NotifyPlayerJoinedLobby(unsigned playerId);
void NotifyPlayerLeftLobby(unsigned playerId);
void NotifyPlayerJoinedGame(unsigned gameId, unsigned playerId);
void NotifyPlayerLeftGame(unsigned gameId, unsigned playerId);
void NotifyGameAdminChanged(unsigned gameId, unsigned newAdminPlayerId);
void NotifyStartingGame(unsigned gameId);
void NotifyReopeningGame(unsigned gameId);
void HandleGameRetrievePlayerInfo(SessionWrapper session, const PlayerInfoRequestMessage_t &playerInfoRequest);
void HandleGameRetrieveAvatar(SessionWrapper session, const AvatarRequestMessage_t &retrieveAvatar);
void HandleChatRequest(SessionWrapper session, const ChatRequestMessage_t &chatRequest);
bool KickPlayerByName(const std::string &playerName);
std::string GetPlayerIPAddress(const std::string &playerName) const;
void RemovePlayer(unsigned playerId, unsigned errorCode);
void SendGlobalChat(const std::string &message);
void SendGlobalMsgBox(const std::string &message);
2009-08-17 09:45:06 +00:00
void SendChatBotMsg(const std::string &message);
void ReconnectChatBot();
void AddComputerPlayer(boost::shared_ptr<PlayerData> player);
void RemoveComputerPlayer(boost::shared_ptr<PlayerData> player);
u_int32_t GetNextUniquePlayerId();
u_int32_t GetNextGameId();
ServerCallback &GetCallback();
void SetGameDBId(u_int32_t gameId, DB_id gameDBId);
AvatarManager &GetAvatarManager();
ServerStats GetStats() const;
2007-11-28 23:31:34 +00:00
boost::posix_time::ptime GetStartTime() const;
SenderHelper &GetSender();
boost::asio::io_service &GetIOService();
ServerDBInterface &GetDatabase();
ServerBanManager &GetBanManager();
protected:
typedef std::deque<boost::shared_ptr<boost::asio::ip::tcp::socket> > ConnectQueue;
typedef std::deque<SessionWrapper> SessionQueue;
typedef std::list<SessionWrapper> SessionList;
typedef std::list<SessionId> SessionIdList;
typedef std::map<SessionId, boost::timers::portable::microsec_timer> TimerSessionMap;
typedef std::map<unsigned, boost::shared_ptr<ServerGame> > GameMap;
typedef std::map<std::string, boost::timers::portable::microsec_timer> TimerClientAddressMap;
typedef std::list<unsigned> RemoveGameList;
// Main function of the thread.
virtual void Main();
2009-05-10 14:57:51 +00:00
void RegisterTimers();
void CancelTimers();
void InitAuthContext();
void ClearAuthContext();
2009-08-14 22:05:01 +00:00
void InitChatCleaner();
void HandleRead(const boost::system::error_code &ec, SessionId sessionId, size_t bytesRead);
void HandlePacket(SessionWrapper session, boost::shared_ptr<NetPacket> packet);
void HandleNetPacketInit(SessionWrapper session, const InitMessage_t &initMessage);
void HandleNetPacketAuthClientResponse(SessionWrapper session, const AuthClientResponse_t &clientResponse);
void HandleNetPacketAvatarHeader(SessionWrapper session, unsigned requestId, const AvatarHeader_t &avatarHeader);
void HandleNetPacketUnknownAvatar(SessionWrapper session, unsigned requestId, const UnknownAvatar_t &unknownAvatar);
void HandleNetPacketAvatarFile(SessionWrapper session, unsigned requestId, const AvatarData_t &avatarData);
void HandleNetPacketAvatarEnd(SessionWrapper session, unsigned requestId, const AvatarEnd_t &avatarEnd);
void HandleNetPacketRetrievePlayerInfo(SessionWrapper session, const PlayerInfoRequestMessage_t &playerInfoRequest);
void HandleNetPacketRetrieveAvatar(SessionWrapper session, const AvatarRequestMessage_t &retrieveAvatar);
void HandleNetPacketCreateGame(SessionWrapper session, const std::string &password, const JoinNewGame_t &newGame);
void HandleNetPacketJoinGame(SessionWrapper session, const std::string &password, const JoinExistingGame_t &joinGame);
void HandleNetPacketChatRequest(SessionWrapper session, const ChatRequestMessage_t &chatRequest);
void AuthChallenge(SessionWrapper session, const std::string &secret);
2009-09-19 21:01:51 +00:00
void InitAfterLogin(SessionWrapper session);
void EstablishSession(SessionWrapper session);
void AuthenticatePlayer(SessionWrapper session);
2009-10-11 12:07:56 +00:00
void UserValid(unsigned playerId, DB_id dbPlayerId, const std::string &dbSecret);
void UserInvalid(unsigned playerId);
void RequestPlayerAvatar(SessionWrapper session);
void TimerRemoveGame(const boost::system::error_code &ec);
void TimerRemovePlayer(const boost::system::error_code &ec);
void TimerUpdateClientAvatarLock(const boost::system::error_code &ec);
void TimerCheckSessionTimeouts(const boost::system::error_code &ec);
void TimerCleanupAvatarCache(const boost::system::error_code &ec);
boost::shared_ptr<ServerGame> InternalGetGameFromId(unsigned gameId);
void InternalAddGame(boost::shared_ptr<ServerGame> game);
void InternalRemoveGame(boost::shared_ptr<ServerGame> game);
void InternalRemovePlayer(unsigned playerId, unsigned errorCode);
void InternalResubscribeMsg(SessionWrapper session);
void HandleReAddedSession(SessionWrapper session);
void InternalCheckSessionTimeouts(SessionWrapper session);
void CleanupSessionMap();
void CloseSession(SessionWrapper session);
void SendError(boost::shared_ptr<SessionData> s, int errorCode);
void SendJoinGameFailed(boost::shared_ptr<SessionData> s, int reason);
void SendPlayerList(boost::shared_ptr<SessionData> s);
void SendGameList(boost::shared_ptr<SessionData> s);
void UpdateStatisticsNumberOfPlayers();
void BroadcastStatisticsUpdate(const ServerStats &stats);
void ReadStatisticsFile();
void TimerSaveStatisticsFile(const boost::system::error_code &ec);
ReceiverHelper &GetReceiver();
2009-09-19 20:28:30 +00:00
InternalServerCallback &GetSenderCallback();
GuiInterface &GetGui();
ServerIrcBotCallback &GetIrcBotCallback();
bool IsPlayerConnected(const std::string &name) const;
static boost::shared_ptr<NetPacket> CreateNetPacketPlayerListNew(unsigned playerId);
static boost::shared_ptr<NetPacket> CreateNetPacketPlayerListLeft(unsigned playerId);
static boost::shared_ptr<NetPacket> CreateNetPacketGameListNew(const ServerGame &game);
static boost::shared_ptr<NetPacket> CreateNetPacketGameListUpdate(unsigned gameId, GameMode mode);
private:
boost::shared_ptr<boost::asio::io_service> m_ioService;
2009-09-19 20:28:30 +00:00
boost::shared_ptr<InternalServerCallback> m_internalServerCallback;
boost::shared_ptr<SenderHelper> m_sender;
boost::shared_ptr<ReceiverHelper> m_receiver;
SessionManager m_sessionManager;
SessionManager m_gameSessionManager;
Gsasl *m_authContext;
TimerClientAddressMap m_timerAvatarClientAddressMap;
mutable boost::mutex m_timerAvatarClientAddressMapMutex;
RemoveGameList m_removeGameList;
mutable boost::mutex m_removeGameListMutex;
RemovePlayerList m_removePlayerList;
mutable boost::mutex m_removePlayerListMutex;
PlayerDataMap m_computerPlayers;
mutable boost::mutex m_computerPlayersMutex;
GameMap m_gameMap;
GuiInterface &m_gui;
ServerIrcBotCallback &m_ircBotCb;
AvatarManager &m_avatarManager;
ServerMode m_mode;
std::string m_statisticsFileName;
ConfigFile *m_playerConfig;
u_int32_t m_curGameId;
u_int32_t m_curUniquePlayerId;
u_int32_t m_curSessionId;
mutable boost::mutex m_curUniquePlayerIdMutex;
ServerStats m_statData;
bool m_statDataChanged;
mutable boost::mutex m_statMutex;
boost::shared_ptr<ServerBanManager> m_banManager;
2009-08-14 22:05:01 +00:00
boost::shared_ptr<ChatCleanerManager> m_chatCleanerManager;
2009-09-19 20:28:30 +00:00
boost::shared_ptr<ServerDBInterface> m_database;
boost::asio::deadline_timer m_removeGameTimer;
boost::asio::deadline_timer m_removePlayerTimer;
boost::asio::deadline_timer m_sessionTimeoutTimer;
boost::asio::deadline_timer m_avatarCleanupTimer;
boost::asio::deadline_timer m_saveStatisticsTimer;
boost::asio::deadline_timer m_avatarLockTimer;
2007-11-28 23:31:34 +00:00
const boost::posix_time::ptime m_startTime;
friend class InternalServerCallback;
};
#endif