2007-02-17 23:44:45 +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. *
|
|
|
|
|
***************************************************************************/
|
2007-03-13 23:27:17 +00:00
|
|
|
/* Network server receive thread. */
|
2007-02-17 23:44:45 +00:00
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
#ifndef _SERVERRECVTHREAD_H_
|
|
|
|
|
#define _SERVERRECVTHREAD_H_
|
2007-02-17 23:44:45 +00:00
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
#include <net/connectdata.h>
|
2007-08-15 13:28:28 +00:00
|
|
|
#include <net/sessionmanager.h>
|
|
|
|
|
#include <net/netpacket.h>
|
2007-05-13 22:25:59 +00:00
|
|
|
#include <gui/guiinterface.h>
|
2007-05-20 00:16:29 +00:00
|
|
|
#include <gamedata.h>
|
2007-02-17 23:44:45 +00:00
|
|
|
|
2007-05-06 23:27:08 +00:00
|
|
|
#include <deque>
|
|
|
|
|
#include <list>
|
2007-09-05 00:06:20 +00:00
|
|
|
#include <core/boost/timers.hpp>
|
2007-05-06 23:27:08 +00:00
|
|
|
|
2007-08-15 13:36:29 +00:00
|
|
|
#define LOBBY_THREAD_TERMINATE_TIMEOUT 200
|
2007-03-20 11:56:30 +00:00
|
|
|
|
2007-03-20 14:05:16 +00:00
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
class SenderThread;
|
|
|
|
|
class ReceiverHelper;
|
|
|
|
|
class ServerSenderCallback;
|
2007-08-20 20:37:54 +00:00
|
|
|
class ServerGameThread;
|
2007-05-13 22:25:59 +00:00
|
|
|
class ConfigFile;
|
2007-04-26 23:07:08 +00:00
|
|
|
struct GameData;
|
2007-05-12 22:00:59 +00:00
|
|
|
class Game;
|
2007-03-13 23:27:17 +00:00
|
|
|
|
2007-08-15 13:28:28 +00:00
|
|
|
class ServerLobbyThread : public Thread
|
2007-02-17 23:44:45 +00:00
|
|
|
{
|
|
|
|
|
public:
|
2007-08-15 13:28:28 +00:00
|
|
|
ServerLobbyThread(GuiInterface &gui, ConfigFile *playerConfig);
|
|
|
|
|
virtual ~ServerLobbyThread();
|
2007-02-17 23:44:45 +00:00
|
|
|
|
2007-08-15 13:28:28 +00:00
|
|
|
void Init(const std::string &pwd);
|
2007-04-26 23:07:08 +00:00
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
void AddConnection(boost::shared_ptr<ConnectData> data);
|
2007-09-06 19:09:05 +00:00
|
|
|
void ReAddSession(SessionWrapper session, int reason);
|
2007-09-29 14:05:41 +00:00
|
|
|
void MoveSessionToGame(ServerGameThread &game, SessionWrapper session);
|
|
|
|
|
void RemoveSessionFromGame(SessionWrapper session);
|
2007-09-06 19:09:05 +00:00
|
|
|
void SessionError(SessionWrapper session, int errorCode);
|
2007-09-02 16:36:52 +00:00
|
|
|
void NotifyPlayerJoinedGame(unsigned gameId, unsigned playerId);
|
|
|
|
|
void NotifyPlayerLeftGame(unsigned gameId, unsigned playerId);
|
2007-03-13 23:27:17 +00:00
|
|
|
|
2007-09-29 14:05:41 +00:00
|
|
|
void HandleGameRetrievePlayerInfo(SessionWrapper session, const NetPacketRetrievePlayerInfo &tmpPacket);
|
|
|
|
|
|
2007-08-21 17:27:13 +00:00
|
|
|
void RemoveGame(unsigned id);
|
|
|
|
|
|
2007-08-15 13:28:28 +00:00
|
|
|
u_int32_t GetNextUniquePlayerId();
|
2007-08-20 20:37:54 +00:00
|
|
|
u_int32_t GetNextGameId();
|
2007-04-08 20:35:42 +00:00
|
|
|
ServerCallback &GetCallback();
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
protected:
|
|
|
|
|
|
2007-03-20 14:05:16 +00:00
|
|
|
typedef std::deque<boost::shared_ptr<ConnectData> > ConnectQueue;
|
2007-09-06 19:09:05 +00:00
|
|
|
typedef std::deque<SessionWrapper> SessionQueue;
|
2007-05-23 08:57:34 +00:00
|
|
|
typedef std::list<SessionWrapper> SessionList;
|
2007-09-05 00:06:20 +00:00
|
|
|
typedef std::list<std::pair<boost::timers::portable::microsec_timer, boost::shared_ptr<SessionData> > > CloseSessionList;
|
2007-08-20 20:37:54 +00:00
|
|
|
typedef std::map<unsigned, boost::shared_ptr<ServerGameThread> > GameMap;
|
2007-08-21 17:27:13 +00:00
|
|
|
typedef std::list<unsigned> RemoveGameList;
|
2007-03-20 02:20:50 +00:00
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
// Main function of the thread.
|
|
|
|
|
virtual void Main();
|
|
|
|
|
|
2007-08-15 13:28:28 +00:00
|
|
|
void ProcessLoop();
|
|
|
|
|
void HandleNetPacketInit(SessionWrapper session, const NetPacketInit &tmpPacket);
|
2007-08-26 22:41:37 +00:00
|
|
|
void HandleNetPacketRetrievePlayerInfo(SessionWrapper session, const NetPacketRetrievePlayerInfo &tmpPacket);
|
2007-08-15 13:28:28 +00:00
|
|
|
void HandleNetPacketCreateGame(SessionWrapper session, const NetPacketCreateGame &tmpPacket);
|
|
|
|
|
void HandleNetPacketJoinGame(SessionWrapper session, const NetPacketJoinGame &tmpPacket);
|
2007-05-06 23:27:08 +00:00
|
|
|
void CloseSessionLoop();
|
2007-08-21 17:27:13 +00:00
|
|
|
void RemoveGameLoop();
|
|
|
|
|
|
2007-08-21 19:50:13 +00:00
|
|
|
void InternalAddGame(boost::shared_ptr<ServerGameThread> game);
|
|
|
|
|
void InternalRemoveGame(boost::shared_ptr<ServerGameThread> game);
|
|
|
|
|
|
2007-08-21 12:34:33 +00:00
|
|
|
void TerminateGames();
|
2007-03-20 14:05:16 +00:00
|
|
|
|
2007-08-15 13:28:28 +00:00
|
|
|
void HandleNewConnection(boost::shared_ptr<ConnectData> connData);
|
2007-09-29 14:05:41 +00:00
|
|
|
void HandleReAddedSession(SessionWrapper session);
|
2007-08-15 13:28:28 +00:00
|
|
|
|
2007-03-20 02:20:50 +00:00
|
|
|
SOCKET Select();
|
|
|
|
|
|
2007-03-20 11:56:30 +00:00
|
|
|
void CleanupConnectQueue();
|
|
|
|
|
void CleanupSessionMap();
|
|
|
|
|
|
2007-09-06 19:09:05 +00:00
|
|
|
void CloseSessionDelayed(SessionWrapper session);
|
2007-08-15 13:28:28 +00:00
|
|
|
void SendError(SOCKET s, int errorCode);
|
2007-09-06 19:09:05 +00:00
|
|
|
void SendJoinGameFailed(SOCKET s, int reason);
|
|
|
|
|
void SendGameList(SOCKET s);
|
2007-05-08 06:45:46 +00:00
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
SenderThread &GetSender();
|
|
|
|
|
ReceiverHelper &GetReceiver();
|
|
|
|
|
|
2007-04-27 21:35:18 +00:00
|
|
|
bool CheckPassword(const std::string &password) const;
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
ServerSenderCallback &GetSenderCallback();
|
2007-05-13 22:25:59 +00:00
|
|
|
GuiInterface &GetGui();
|
2007-03-13 23:27:17 +00:00
|
|
|
|
2007-08-21 17:27:13 +00:00
|
|
|
bool IsPlayerConnected(const std::string &name);
|
|
|
|
|
|
2007-08-21 12:34:33 +00:00
|
|
|
static boost::shared_ptr<NetPacket> CreateNetPacketGameListNew(const ServerGameThread &game);
|
2007-08-21 19:50:13 +00:00
|
|
|
static boost::shared_ptr<NetPacket> CreateNetPacketGameListUpdate(const ServerGameThread &game, GameMode mode);
|
2007-08-21 12:34:33 +00:00
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
private:
|
|
|
|
|
|
2007-03-20 14:05:16 +00:00
|
|
|
ConnectQueue m_connectQueue;
|
2007-03-13 23:27:17 +00:00
|
|
|
mutable boost::mutex m_connectQueueMutex;
|
|
|
|
|
|
2007-09-06 19:09:05 +00:00
|
|
|
SessionQueue m_sessionQueue;
|
|
|
|
|
mutable boost::mutex m_sessionQueueMutex;
|
|
|
|
|
|
2007-08-15 13:28:28 +00:00
|
|
|
SessionManager m_sessionManager;
|
2007-09-29 14:05:41 +00:00
|
|
|
SessionManager m_gameSessionManager;
|
2007-08-06 21:38:09 +00:00
|
|
|
|
2007-05-06 23:27:08 +00:00
|
|
|
CloseSessionList m_closeSessionList;
|
2007-08-20 15:30:22 +00:00
|
|
|
mutable boost::mutex m_closeSessionListMutex;
|
2007-05-06 23:27:08 +00:00
|
|
|
|
2007-08-21 17:27:13 +00:00
|
|
|
RemoveGameList m_removeGameList;
|
|
|
|
|
mutable boost::mutex m_removeGameListMutex;
|
|
|
|
|
|
2007-08-20 20:37:54 +00:00
|
|
|
GameMap m_gameMap;
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
std::auto_ptr<ReceiverHelper> m_receiver;
|
|
|
|
|
std::auto_ptr<SenderThread> m_sender;
|
2007-05-12 22:00:59 +00:00
|
|
|
std::auto_ptr<ServerSenderCallback> m_senderCallback;
|
2007-05-13 22:25:59 +00:00
|
|
|
GuiInterface &m_gui;
|
|
|
|
|
|
2007-08-15 13:28:28 +00:00
|
|
|
std::string m_password;
|
2007-05-13 22:25:59 +00:00
|
|
|
ConfigFile *m_playerConfig;
|
2007-08-20 20:37:54 +00:00
|
|
|
u_int32_t m_curGameId;
|
2007-09-02 20:59:16 +00:00
|
|
|
|
|
|
|
|
u_int32_t m_curUniquePlayerId;
|
|
|
|
|
mutable boost::mutex m_curUniquePlayerIdMutex;
|
2007-02-17 23:44:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|