2007-11-16 15:24:25 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
* Copyright (C) 2007 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. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
/* Network server receive thread. */
|
|
|
|
|
|
|
|
|
|
#ifndef _SERVERRECVTHREAD_H_
|
|
|
|
|
#define _SERVERRECVTHREAD_H_
|
|
|
|
|
|
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>
|
|
|
|
|
#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;
|
|
|
|
|
class ServerSenderCallback;
|
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;
|
2007-11-16 15:24:25 +00:00
|
|
|
struct GameData;
|
|
|
|
|
class Game;
|
|
|
|
|
|
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-05-04 21:11:29 +00:00
|
|
|
ServerLobbyThread(GuiInterface &gui, ConfigFile *playerConfig, AvatarManager &avatarManager,
|
|
|
|
|
boost::shared_ptr<boost::asio::io_service> ioService);
|
2007-11-16 15:24:25 +00:00
|
|
|
virtual ~ServerLobbyThread();
|
|
|
|
|
|
|
|
|
|
void Init(const std::string &pwd, 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);
|
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);
|
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);
|
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);
|
|
|
|
|
|
|
|
|
|
u_int32_t GetNextUniquePlayerId();
|
|
|
|
|
u_int32_t GetNextGameId();
|
|
|
|
|
ServerCallback &GetCallback();
|
|
|
|
|
|
|
|
|
|
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-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-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);
|
|
|
|
|
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);
|
2007-11-16 15:24:25 +00:00
|
|
|
void EstablishSession(SessionWrapper session);
|
|
|
|
|
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);
|
|
|
|
|
void TimerUpdateClientAvatarLock(const boost::system::error_code &ec);
|
|
|
|
|
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);
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
bool CheckPassword(const std::string &password) const;
|
|
|
|
|
|
|
|
|
|
ServerSenderCallback &GetSenderCallback();
|
|
|
|
|
GuiInterface &GetGui();
|
|
|
|
|
|
2009-04-07 22:04:38 +00:00
|
|
|
bool IsPlayerConnected(const std::string &name) const;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
boost::shared_ptr<ServerSenderCallback> m_senderCallback;
|
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;
|
|
|
|
|
|
2008-02-24 12:56:39 +00:00
|
|
|
TimerClientAddressMap m_timerAvatarClientAddressMap;
|
|
|
|
|
mutable boost::mutex m_timerAvatarClientAddressMapMutex;
|
|
|
|
|
|
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;
|
|
|
|
|
AvatarManager &m_avatarManager;
|
|
|
|
|
|
|
|
|
|
std::string m_password;
|
|
|
|
|
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-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;
|
|
|
|
|
boost::asio::deadline_timer m_avatarLockTimer;
|
|
|
|
|
|
2007-11-28 23:31:34 +00:00
|
|
|
const boost::posix_time::ptime m_startTime;
|
2007-11-16 15:24:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|