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>
|
|
|
|
|
#include <boost/regex.hpp>
|
|
|
|
|
|
2009-05-04 21:11:29 +00:00
|
|
|
#include <core/timermanager.h>
|
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-01-07 19:37:05 +00:00
|
|
|
class SenderInterface;
|
2007-11-16 15:24:25 +00:00
|
|
|
class ReceiverHelper;
|
|
|
|
|
class ServerSenderCallback;
|
|
|
|
|
class ServerGameThread;
|
|
|
|
|
class ConfigFile;
|
|
|
|
|
class AvatarManager;
|
|
|
|
|
struct GameData;
|
|
|
|
|
class Game;
|
|
|
|
|
|
|
|
|
|
class ServerLobbyThread : public Thread
|
|
|
|
|
{
|
|
|
|
|
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-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);
|
|
|
|
|
void MoveSessionToGame(ServerGameThread &game, SessionWrapper session);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
void HandleGameRetrievePlayerInfo(SessionWrapper session, const NetPacketRetrievePlayerInfo &tmpPacket);
|
|
|
|
|
void HandleGameRetrieveAvatar(SessionWrapper session, const NetPacketRetrieveAvatar &tmpPacket);
|
|
|
|
|
|
|
|
|
|
bool KickPlayerByName(const std::string &playerName);
|
2009-04-07 22:04:38 +00:00
|
|
|
void BanPlayerRegex(const std::string &playerRegex);
|
2009-04-11 11:03:22 +00:00
|
|
|
void BanIPAddress(const std::string &ipAddress);
|
|
|
|
|
bool UnBan(unsigned banId);
|
|
|
|
|
void GetBanList(std::list<std::string> &list) const;
|
|
|
|
|
void ClearBanList();
|
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);
|
|
|
|
|
|
|
|
|
|
void RemoveGame(unsigned id);
|
|
|
|
|
|
|
|
|
|
u_int32_t GetNextUniquePlayerId();
|
|
|
|
|
u_int32_t GetNextGameId();
|
|
|
|
|
ServerCallback &GetCallback();
|
|
|
|
|
|
2009-05-30 22:20:40 +00:00
|
|
|
TimerManager &GetTimerManager();
|
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-01-31 22:20:08 +00:00
|
|
|
SenderInterface &GetSender();
|
|
|
|
|
|
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;
|
2007-11-16 15:24:25 +00:00
|
|
|
typedef std::map<unsigned, boost::shared_ptr<ServerGameThread> > 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;
|
2009-04-10 21:04:08 +00:00
|
|
|
typedef std::map<unsigned, boost::regex> RegexMap;
|
2009-04-11 11:03:22 +00:00
|
|
|
typedef std::map<unsigned, std::string> IPAddressMap;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
// Main function of the thread.
|
|
|
|
|
virtual void Main();
|
2009-05-10 14:57:51 +00:00
|
|
|
void RegisterTimers();
|
2007-11-16 15:24:25 +00:00
|
|
|
|
2009-05-04 21:11:29 +00:00
|
|
|
void HandleRead(SessionId sessionId, const boost::system::error_code& error, size_t bytesRead);
|
|
|
|
|
void HandlePacket(SessionWrapper session, boost::shared_ptr<NetPacket> packet);
|
2007-11-16 15:24:25 +00:00
|
|
|
void HandleNetPacketInit(SessionWrapper session, const NetPacketInit &tmpPacket);
|
|
|
|
|
void HandleNetPacketAvatarHeader(SessionWrapper session, const NetPacketAvatarHeader &tmpPacket);
|
|
|
|
|
void HandleNetPacketUnknownAvatar(SessionWrapper session, const NetPacketUnknownAvatar &tmpPacket);
|
|
|
|
|
void HandleNetPacketAvatarFile(SessionWrapper session, const NetPacketAvatarFile &tmpPacket);
|
|
|
|
|
void HandleNetPacketAvatarEnd(SessionWrapper session, const NetPacketAvatarEnd &tmpPacket);
|
|
|
|
|
void HandleNetPacketRetrievePlayerInfo(SessionWrapper session, const NetPacketRetrievePlayerInfo &tmpPacket);
|
|
|
|
|
void HandleNetPacketRetrieveAvatar(SessionWrapper session, const NetPacketRetrieveAvatar &tmpPacket);
|
|
|
|
|
void HandleNetPacketCreateGame(SessionWrapper session, const NetPacketCreateGame &tmpPacket);
|
|
|
|
|
void HandleNetPacketJoinGame(SessionWrapper session, const NetPacketJoinGame &tmpPacket);
|
|
|
|
|
void EstablishSession(SessionWrapper session);
|
|
|
|
|
void RequestPlayerAvatar(SessionWrapper session);
|
|
|
|
|
void NewSessionLoop();
|
2009-05-10 14:57:51 +00:00
|
|
|
void TimerRemoveGame();
|
|
|
|
|
void TimerRemovePlayer();
|
2008-05-11 21:41:30 +00:00
|
|
|
void ResubscribeLobbyMsgLoop();
|
2009-05-10 17:53:37 +00:00
|
|
|
void TimerUpdateClientAvatarLock();
|
2009-05-04 21:11:29 +00:00
|
|
|
void TimerCheckSessionTimeouts();
|
|
|
|
|
void TimerCleanupAvatarCache();
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
void InternalAddGame(boost::shared_ptr<ServerGameThread> game);
|
|
|
|
|
void InternalRemoveGame(boost::shared_ptr<ServerGameThread> 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 TerminateGames();
|
|
|
|
|
|
|
|
|
|
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-05-04 21:11:29 +00:00
|
|
|
void TimerSaveStatisticsFile();
|
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;
|
|
|
|
|
bool IsPlayerBanned(const std::string &name) const;
|
2009-04-11 11:03:22 +00:00
|
|
|
bool IsIPAddressBanned(const std::string &ipAddress) const;
|
2007-11-16 15:24:25 +00:00
|
|
|
|
|
|
|
|
static boost::shared_ptr<NetPacket> CreateNetPacketGameListNew(const ServerGameThread &game);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
TimerManager m_timerManager;
|
|
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
SessionQueue m_sessionQueue;
|
|
|
|
|
mutable boost::mutex m_sessionQueueMutex;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2008-05-11 21:41:30 +00:00
|
|
|
SessionIdList m_resubscribeList;
|
|
|
|
|
mutable boost::mutex m_resubscribeListMutex;
|
2008-02-24 12:56:39 +00:00
|
|
|
|
2009-04-10 21:04:08 +00:00
|
|
|
RegexMap m_banPlayerNameMap;
|
2009-04-11 11:03:22 +00:00
|
|
|
IPAddressMap m_banIPAddressMap;
|
2009-04-10 21:04:08 +00:00
|
|
|
unsigned m_curBanId;
|
|
|
|
|
mutable boost::mutex m_banMutex;
|
2009-04-07 22:04:38 +00:00
|
|
|
|
2007-11-16 15:24:25 +00:00
|
|
|
GameMap m_gameMap;
|
|
|
|
|
|
2009-01-25 20:19:09 +00:00
|
|
|
boost::shared_ptr<SenderInterface> m_sender;
|
2007-11-16 15:24:25 +00:00
|
|
|
boost::shared_ptr<ReceiverHelper> m_receiver;
|
|
|
|
|
boost::shared_ptr<ServerSenderCallback> m_senderCallback;
|
|
|
|
|
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;
|
|
|
|
|
|
2007-11-28 23:31:34 +00:00
|
|
|
const boost::posix_time::ptime m_startTime;
|
2007-11-16 15:24:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|