2007-11-16 15:24:25 +00:00
|
|
|
/***************************************************************************
|
2009-09-19 20:28:30 +00:00
|
|
|
* Copyright (C) 2007-2009 by Lothar May *
|
2007-11-16 15:24:25 +00:00
|
|
|
* *
|
|
|
|
|
* 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. */
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-09-19 20:28:30 +00:00
|
|
|
#ifndef _SERVERLOBBYTHREAD_H_
|
|
|
|
|
#define _SERVERLOBBYTHREAD_H_
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-05-04 21:11:29 +00:00
|
|
|
#include <boost/asio.hpp>
|
2009-05-07 22:36:51 +00:00
|
|
|
#include <deque>
|
2009-06-12 15:48:34 +00:00
|
|
|
#include <boost/enable_shared_from_this.hpp>
|
2009-05-07 22:36:51 +00:00
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
#include <net/sessionmanager.h>
|
|
|
|
|
#include <net/netpacket.h>
|
2009-09-24 20:10:40 +00:00
|
|
|
#include <db/serverdbcallback.h>
|
2007-11-16 15:24:25 +00:00
|
|
|
#include <gui/guiinterface.h>
|
|
|
|
|
#include <gamedata.h>
|
|
|
|
|
|
|
|
|
|
|
2007-11-16 22:34:38 +00:00
|
|
|
#define NET_LOBBY_THREAD_TERMINATE_TIMEOUT_MSEC 20000
|
|
|
|
|
#define NET_ADMIN_IRC_TERMINATE_TIMEOUT_MSEC 4000
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
|
2009-06-07 08:51:43 +00:00
|
|
|
class SenderHelper;
|
2007-11-16 15:24:25 +00:00
|
|
|
class ReceiverHelper;
|
2009-09-19 20:28:30 +00:00
|
|
|
class InternalServerCallback;
|
2009-08-20 11:42:13 +00:00
|
|
|
class ServerIrcBotCallback;
|
2009-05-31 20:14:46 +00:00
|
|
|
class ServerGame;
|
2009-06-18 20:16:22 +00:00
|
|
|
class ServerBanManager;
|
2007-11-16 15:24:25 +00:00
|
|
|
class ConfigFile;
|
|
|
|
|
class AvatarManager;
|
2009-08-14 22:05:01 +00:00
|
|
|
class ChatCleanerManager;
|
2009-09-19 20:28:30 +00:00
|
|
|
class ServerDBInterface;
|
2007-11-16 15:24:25 +00:00
|
|
|
struct GameData;
|
|
|
|
|
class Game;
|
2009-10-11 09:20:17 +00:00
|
|
|
struct Gsasl;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-06-12 15:48:34 +00:00
|
|
|
class ServerLobbyThread : public Thread, public boost::enable_shared_from_this<ServerLobbyThread>
|
2007-11-16 15:24:25 +00:00
|
|
|
{
|
|
|
|
|
public:
|
2009-11-08 09:48:01 +00:00
|
|
|
ServerLobbyThread(GuiInterface &gui, ServerMode mode, ServerIrcBotCallback &ircBotCb, ConfigFile *playerConfig, AvatarManager &avatarManager,
|
2009-05-04 21:11:29 +00:00
|
|
|
boost::shared_ptr<boost::asio::io_service> ioService);
|
2007-11-16 15:24:25 +00:00
|
|
|
virtual ~ServerLobbyThread();
|
|
|
|
|
|
2009-11-08 09:48:01 +00:00
|
|
|
void Init(const std::string &logDir);
|
2009-06-01 19:49:16 +00:00
|
|
|
virtual void SignalTermination();
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-05-04 21:11:29 +00:00
|
|
|
void AddConnection(boost::shared_ptr<boost::asio::ip::tcp::socket> sock);
|
2007-11-16 15:24:25 +00:00
|
|
|
void ReAddSession(SessionWrapper session, int reason);
|
2009-05-31 20:14:46 +00:00
|
|
|
void MoveSessionToGame(ServerGame &game, SessionWrapper session);
|
2007-11-16 15:24:25 +00:00
|
|
|
void RemoveSessionFromGame(SessionWrapper session);
|
|
|
|
|
void SessionError(SessionWrapper session, int errorCode);
|
2008-05-11 21:41:30 +00:00
|
|
|
void ResubscribeLobbyMsg(SessionWrapper session);
|
2009-08-15 22:20:19 +00:00
|
|
|
void NotifyPlayerJoinedLobby(unsigned playerId);
|
|
|
|
|
void NotifyPlayerLeftLobby(unsigned playerId);
|
2007-11-16 15:24:25 +00:00
|
|
|
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);
|
|
|
|
|
|
2009-08-02 15:11:40 +00:00
|
|
|
void HandleGameRetrievePlayerInfo(SessionWrapper session, const PlayerInfoRequestMessage_t &playerInfoRequest);
|
|
|
|
|
void HandleGameRetrieveAvatar(SessionWrapper session, const AvatarRequestMessage_t &retrieveAvatar);
|
2009-08-15 14:51:21 +00:00
|
|
|
void HandleChatRequest(SessionWrapper session, const ChatRequestMessage_t &chatRequest);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
bool KickPlayerByName(const std::string &playerName);
|
2009-04-10 21:54:59 +00:00
|
|
|
std::string GetPlayerIPAddress(const std::string &playerName) const;
|
2008-03-06 15:27:31 +00:00
|
|
|
void RemovePlayer(unsigned playerId, unsigned errorCode);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2008-12-26 16:47:14 +00:00
|
|
|
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);
|
2009-08-17 11:26:03 +00:00
|
|
|
void ReconnectChatBot();
|
2008-12-26 13:50:09 +00:00
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
void AddComputerPlayer(boost::shared_ptr<PlayerData> player);
|
|
|
|
|
void RemoveComputerPlayer(boost::shared_ptr<PlayerData> player);
|
|
|
|
|
|
2010-03-30 13:02:43 +00:00
|
|
|
bool SendToLobbyPlayer(unsigned playerId, boost::shared_ptr<NetPacket> packet);
|
2010-03-30 12:07:48 +00:00
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
u_int32_t GetNextUniquePlayerId();
|
|
|
|
|
u_int32_t GetNextGameId();
|
|
|
|
|
ServerCallback &GetCallback();
|
|
|
|
|
|
2009-10-11 09:20:17 +00:00
|
|
|
void SetGameDBId(u_int32_t gameId, DB_id gameDBId);
|
2009-10-03 20:19:51 +00:00
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
AvatarManager &GetAvatarManager();
|
|
|
|
|
|
|
|
|
|
ServerStats GetStats() const;
|
2007-11-28 23:31:34 +00:00
|
|
|
boost::posix_time::ptime GetStartTime() const;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-06-07 08:51:43 +00:00
|
|
|
SenderHelper &GetSender();
|
2009-06-12 15:48:34 +00:00
|
|
|
boost::asio::io_service &GetIOService();
|
2009-10-03 18:06:56 +00:00
|
|
|
ServerDBInterface &GetDatabase();
|
2009-06-18 20:16:22 +00:00
|
|
|
ServerBanManager &GetBanManager();
|
2009-01-31 22:20:08 +00:00
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
protected:
|
|
|
|
|
|
2009-05-04 21:11:29 +00:00
|
|
|
typedef std::deque<boost::shared_ptr<boost::asio::ip::tcp::socket> > ConnectQueue;
|
2007-11-16 15:24:25 +00:00
|
|
|
typedef std::deque<SessionWrapper> SessionQueue;
|
|
|
|
|
typedef std::list<SessionWrapper> SessionList;
|
2008-05-11 21:41:30 +00:00
|
|
|
typedef std::list<SessionId> SessionIdList;
|
2008-02-24 12:56:39 +00:00
|
|
|
typedef std::map<SessionId, boost::timers::portable::microsec_timer> TimerSessionMap;
|
2009-05-31 20:14:46 +00:00
|
|
|
typedef std::map<unsigned, boost::shared_ptr<ServerGame> > GameMap;
|
2008-02-24 12:56:39 +00:00
|
|
|
typedef std::map<std::string, boost::timers::portable::microsec_timer> TimerClientAddressMap;
|
2007-11-16 15:24:25 +00:00
|
|
|
typedef std::list<unsigned> RemoveGameList;
|
|
|
|
|
|
|
|
|
|
// Main function of the thread.
|
|
|
|
|
virtual void Main();
|
2009-05-10 14:57:51 +00:00
|
|
|
void RegisterTimers();
|
2009-06-12 15:48:34 +00:00
|
|
|
void CancelTimers();
|
2009-10-11 09:20:17 +00:00
|
|
|
void InitAuthContext();
|
|
|
|
|
void ClearAuthContext();
|
2009-08-14 22:05:01 +00:00
|
|
|
void InitChatCleaner();
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-06-14 22:07:41 +00:00
|
|
|
void HandleRead(const boost::system::error_code &ec, SessionId sessionId, size_t bytesRead);
|
2009-05-04 21:11:29 +00:00
|
|
|
void HandlePacket(SessionWrapper session, boost::shared_ptr<NetPacket> packet);
|
2009-08-02 15:11:40 +00:00
|
|
|
void HandleNetPacketInit(SessionWrapper session, const InitMessage_t &initMessage);
|
2009-10-18 20:53:58 +00:00
|
|
|
void HandleNetPacketAuthClientResponse(SessionWrapper session, const AuthClientResponse_t &clientResponse);
|
2009-08-02 15:11:40 +00:00
|
|
|
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);
|
2009-08-15 10:25:13 +00:00
|
|
|
void HandleNetPacketChatRequest(SessionWrapper session, const ChatRequestMessage_t &chatRequest);
|
2010-03-30 17:10:37 +00:00
|
|
|
void HandleNetPacketRejectGameInvitation(SessionWrapper session, const RejectGameInvitationMessage_t &reject);
|
2009-10-18 20:53:58 +00:00
|
|
|
void AuthChallenge(SessionWrapper session, const std::string &secret);
|
2009-09-19 21:01:51 +00:00
|
|
|
void InitAfterLogin(SessionWrapper session);
|
2007-11-16 15:24:25 +00:00
|
|
|
void EstablishSession(SessionWrapper session);
|
2009-10-18 20:53:58 +00:00
|
|
|
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);
|
2007-11-16 15:24:25 +00:00
|
|
|
void RequestPlayerAvatar(SessionWrapper session);
|
2009-06-12 15:48:34 +00:00
|
|
|
void TimerRemoveGame(const boost::system::error_code &ec);
|
|
|
|
|
void TimerRemovePlayer(const boost::system::error_code &ec);
|
2009-12-23 09:53:01 +00:00
|
|
|
void TimerUpdateClientLoginLock(const boost::system::error_code &ec);
|
2009-06-12 15:48:34 +00:00
|
|
|
void TimerCheckSessionTimeouts(const boost::system::error_code &ec);
|
|
|
|
|
void TimerCleanupAvatarCache(const boost::system::error_code &ec);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-06-01 10:23:44 +00:00
|
|
|
boost::shared_ptr<ServerGame> InternalGetGameFromId(unsigned gameId);
|
2009-05-31 20:14:46 +00:00
|
|
|
void InternalAddGame(boost::shared_ptr<ServerGame> game);
|
|
|
|
|
void InternalRemoveGame(boost::shared_ptr<ServerGame> game);
|
2008-03-06 15:27:31 +00:00
|
|
|
void InternalRemovePlayer(unsigned playerId, unsigned errorCode);
|
2008-05-11 21:41:30 +00:00
|
|
|
void InternalResubscribeMsg(SessionWrapper session);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
void HandleReAddedSession(SessionWrapper session);
|
|
|
|
|
|
2008-03-06 15:27:31 +00:00
|
|
|
void InternalCheckSessionTimeouts(SessionWrapper session);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
void CleanupSessionMap();
|
|
|
|
|
|
|
|
|
|
void CloseSession(SessionWrapper session);
|
|
|
|
|
void SendError(boost::shared_ptr<SessionData> s, int errorCode);
|
|
|
|
|
void SendJoinGameFailed(boost::shared_ptr<SessionData> s, int reason);
|
2009-08-15 13:43:00 +00:00
|
|
|
void SendPlayerList(boost::shared_ptr<SessionData> s);
|
2007-11-16 15:24:25 +00:00
|
|
|
void SendGameList(boost::shared_ptr<SessionData> s);
|
|
|
|
|
void UpdateStatisticsNumberOfPlayers();
|
|
|
|
|
void BroadcastStatisticsUpdate(const ServerStats &stats);
|
|
|
|
|
|
|
|
|
|
void ReadStatisticsFile();
|
2009-06-12 15:48:34 +00:00
|
|
|
void TimerSaveStatisticsFile(const boost::system::error_code &ec);
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
ReceiverHelper &GetReceiver();
|
|
|
|
|
|
2009-09-19 20:28:30 +00:00
|
|
|
InternalServerCallback &GetSenderCallback();
|
2007-11-16 15:24:25 +00:00
|
|
|
GuiInterface &GetGui();
|
2009-08-20 11:42:13 +00:00
|
|
|
ServerIrcBotCallback &GetIrcBotCallback();
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-04-07 22:04:38 +00:00
|
|
|
bool IsPlayerConnected(const std::string &name) const;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-08-15 13:43:00 +00:00
|
|
|
static boost::shared_ptr<NetPacket> CreateNetPacketPlayerListNew(unsigned playerId);
|
|
|
|
|
static boost::shared_ptr<NetPacket> CreateNetPacketPlayerListLeft(unsigned playerId);
|
2009-05-31 20:14:46 +00:00
|
|
|
static boost::shared_ptr<NetPacket> CreateNetPacketGameListNew(const ServerGame &game);
|
2007-11-16 15:24:25 +00:00
|
|
|
static boost::shared_ptr<NetPacket> CreateNetPacketGameListUpdate(unsigned gameId, GameMode mode);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
2009-05-04 21:11:29 +00:00
|
|
|
boost::shared_ptr<boost::asio::io_service> m_ioService;
|
2009-06-04 20:37:18 +00:00
|
|
|
|
2009-09-19 20:28:30 +00:00
|
|
|
boost::shared_ptr<InternalServerCallback> m_internalServerCallback;
|
2009-06-07 08:51:43 +00:00
|
|
|
boost::shared_ptr<SenderHelper> m_sender;
|
2009-06-04 20:37:18 +00:00
|
|
|
boost::shared_ptr<ReceiverHelper> m_receiver;
|
2009-05-04 21:11:29 +00:00
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
SessionManager m_sessionManager;
|
|
|
|
|
SessionManager m_gameSessionManager;
|
|
|
|
|
|
2009-10-11 09:20:17 +00:00
|
|
|
Gsasl *m_authContext;
|
|
|
|
|
|
2009-12-23 09:53:01 +00:00
|
|
|
TimerClientAddressMap m_timerClientAddressMap;
|
|
|
|
|
mutable boost::mutex m_timerClientAddressMapMutex;
|
2008-02-24 12:56:39 +00:00
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
RemoveGameList m_removeGameList;
|
|
|
|
|
mutable boost::mutex m_removeGameListMutex;
|
|
|
|
|
|
2008-03-06 15:27:31 +00:00
|
|
|
RemovePlayerList m_removePlayerList;
|
|
|
|
|
mutable boost::mutex m_removePlayerListMutex;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
PlayerDataMap m_computerPlayers;
|
|
|
|
|
mutable boost::mutex m_computerPlayersMutex;
|
|
|
|
|
|
|
|
|
|
GameMap m_gameMap;
|
|
|
|
|
|
|
|
|
|
GuiInterface &m_gui;
|
2009-08-20 11:42:13 +00:00
|
|
|
ServerIrcBotCallback &m_ircBotCb;
|
2007-11-16 15:24:25 +00:00
|
|
|
AvatarManager &m_avatarManager;
|
|
|
|
|
|
2009-11-08 09:48:01 +00:00
|
|
|
ServerMode m_mode;
|
2007-11-16 15:24:25 +00:00
|
|
|
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;
|
|
|
|
|
|
2009-06-18 20:16:22 +00:00
|
|
|
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;
|
2009-06-18 20:16:22 +00:00
|
|
|
|
2009-06-12 15:48:34 +00:00
|
|
|
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;
|
2009-12-23 09:53:01 +00:00
|
|
|
boost::asio::deadline_timer m_loginLockTimer;
|
2009-06-12 15:48:34 +00:00
|
|
|
|
2007-11-28 23:31:34 +00:00
|
|
|
const boost::posix_time::ptime m_startTime;
|
2009-09-24 20:10:40 +00:00
|
|
|
|
|
|
|
|
friend class InternalServerCallback;
|
2007-11-16 15:24:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|