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 <core/thread.h>
|
|
|
|
|
#include <net/connectdata.h>
|
2007-03-20 02:20:50 +00:00
|
|
|
#include <net/sessiondata.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 <map>
|
|
|
|
|
#include <list>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
|
#include <core/boost/timer.hpp>
|
|
|
|
|
|
2007-03-20 11:56:30 +00:00
|
|
|
#define RECEIVER_THREAD_TERMINATE_TIMEOUT 200
|
|
|
|
|
|
2007-03-20 14:05:16 +00:00
|
|
|
// Notifications
|
2007-05-11 12:01:13 +00:00
|
|
|
#define NOTIFY_GAME_START 1
|
2007-03-20 14:05:16 +00:00
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
class ServerRecvState;
|
|
|
|
|
class SenderThread;
|
|
|
|
|
class ReceiverHelper;
|
|
|
|
|
class ServerSenderCallback;
|
2007-03-20 02:20:50 +00:00
|
|
|
class NetPacket;
|
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-05-13 22:25:59 +00:00
|
|
|
struct SessionWrapper
|
|
|
|
|
{
|
|
|
|
|
SessionWrapper() {}
|
|
|
|
|
SessionWrapper(boost::shared_ptr<SessionData> s, boost::shared_ptr<PlayerData> p)
|
|
|
|
|
: sessionData(s), playerData(p) {}
|
|
|
|
|
boost::shared_ptr<SessionData> sessionData;
|
|
|
|
|
boost::shared_ptr<PlayerData> playerData;
|
|
|
|
|
};
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
class ServerRecvThread : public Thread
|
2007-02-17 23:44:45 +00:00
|
|
|
{
|
|
|
|
|
public:
|
2007-05-13 22:25:59 +00:00
|
|
|
ServerRecvThread(GuiInterface &gui, ConfigFile *playerConfig);
|
2007-03-13 23:27:17 +00:00
|
|
|
virtual ~ServerRecvThread();
|
2007-02-17 23:44:45 +00:00
|
|
|
|
2007-04-26 23:07:08 +00:00
|
|
|
void Init(const std::string &pwd, const GameData &gameData);
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
void AddConnection(boost::shared_ptr<ConnectData> data);
|
2007-05-11 12:01:13 +00:00
|
|
|
void AddNotification(unsigned message, unsigned param1, unsigned param2);
|
2007-03-13 23:27:17 +00:00
|
|
|
|
2007-04-08 20:35:42 +00:00
|
|
|
ServerCallback &GetCallback();
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
protected:
|
|
|
|
|
|
2007-05-11 12:01:13 +00:00
|
|
|
struct Notification
|
|
|
|
|
{
|
|
|
|
|
Notification(unsigned m, unsigned p1, unsigned p2)
|
|
|
|
|
: message(m), param1(p1), param2(p2) {}
|
|
|
|
|
unsigned message;
|
|
|
|
|
unsigned param1;
|
|
|
|
|
unsigned param2;
|
|
|
|
|
};
|
|
|
|
|
|
2007-03-20 14:05:16 +00:00
|
|
|
typedef std::deque<boost::shared_ptr<ConnectData> > ConnectQueue;
|
2007-05-13 22:25:59 +00:00
|
|
|
typedef std::map<SOCKET, SessionWrapper> SocketSessionMap;
|
2007-05-23 08:57:34 +00:00
|
|
|
typedef std::list<SessionWrapper> SessionList;
|
2007-05-11 12:01:13 +00:00
|
|
|
typedef std::deque<Notification> NotificationQueue;
|
2007-05-06 23:27:08 +00:00
|
|
|
typedef std::list<std::pair<boost::microsec_timer, boost::shared_ptr<SessionData> > > CloseSessionList;
|
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-03-20 14:05:16 +00:00
|
|
|
void NotificationLoop();
|
2007-05-06 23:27:08 +00:00
|
|
|
void CloseSessionLoop();
|
2007-03-20 14:05:16 +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-05-12 22:00:59 +00:00
|
|
|
void InternalStartGame();
|
|
|
|
|
|
2007-05-13 22:25:59 +00:00
|
|
|
SessionWrapper GetSession(SOCKET sock);
|
|
|
|
|
void AddSession(boost::shared_ptr<SessionData> sessionData); // new Sessions have no player data
|
|
|
|
|
void SessionError(SessionWrapper session, int errorCode);
|
2007-05-23 08:57:34 +00:00
|
|
|
void RejectNewConnection(boost::shared_ptr<ConnectData> connData);
|
2007-05-13 22:25:59 +00:00
|
|
|
void CloseSessionDelayed(SessionWrapper session);
|
2007-05-23 08:57:34 +00:00
|
|
|
void RemoveNotEstablishedSessions();
|
2007-05-08 06:45:46 +00:00
|
|
|
|
|
|
|
|
size_t GetCurNumberOfPlayers() const;
|
|
|
|
|
bool IsPlayerConnected(const std::string &playerName) const;
|
|
|
|
|
void SetSessionPlayerData(boost::shared_ptr<SessionData> sessionData, boost::shared_ptr<PlayerData> playerData);
|
|
|
|
|
PlayerDataList GetPlayerDataList() const;
|
|
|
|
|
|
|
|
|
|
int GetNextPlayerNumber() const;
|
2007-05-05 21:40:24 +00:00
|
|
|
|
2007-05-06 23:27:08 +00:00
|
|
|
void SendError(SOCKET s, int errorCode);
|
2007-05-05 21:40:24 +00:00
|
|
|
void SendToAllPlayers(boost::shared_ptr<NetPacket> packet);
|
2007-05-06 23:27:08 +00:00
|
|
|
void SendToAllButOnePlayers(boost::shared_ptr<NetPacket> packet, SOCKET except);
|
|
|
|
|
|
2007-05-08 06:45:46 +00:00
|
|
|
ServerRecvState &GetState();
|
|
|
|
|
void SetState(ServerRecvState &newState);
|
2007-03-13 23:27:17 +00:00
|
|
|
|
|
|
|
|
SenderThread &GetSender();
|
|
|
|
|
ReceiverHelper &GetReceiver();
|
|
|
|
|
|
2007-05-12 22:00:59 +00:00
|
|
|
Game &GetGame();
|
2007-05-20 00:16:29 +00:00
|
|
|
const GameData &GetGameData() const;
|
|
|
|
|
const StartData &GetStartData() const;
|
|
|
|
|
void SetStartData(const StartData &startData);
|
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
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
ServerRecvState *m_curState;
|
|
|
|
|
|
2007-03-20 14:05:16 +00:00
|
|
|
NotificationQueue m_notificationQueue;
|
|
|
|
|
mutable boost::mutex m_notificationQueueMutex;
|
|
|
|
|
|
2007-03-20 11:56:30 +00:00
|
|
|
SocketSessionMap m_sessionMap;
|
|
|
|
|
mutable boost::mutex m_sessionMapMutex;
|
2007-03-20 02:20:50 +00:00
|
|
|
|
2007-05-06 23:27:08 +00:00
|
|
|
CloseSessionList m_closeSessionList;
|
|
|
|
|
|
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<Game> m_game;
|
2007-05-20 00:16:29 +00:00
|
|
|
GameData m_gameData;
|
|
|
|
|
StartData m_startData;
|
2007-05-13 22:25:59 +00:00
|
|
|
unsigned m_curGameId;
|
2007-05-12 22:00:59 +00:00
|
|
|
std::auto_ptr<ServerSenderCallback> m_senderCallback;
|
2007-04-26 23:07:08 +00:00
|
|
|
|
|
|
|
|
std::string m_password;
|
2007-03-20 02:20:50 +00:00
|
|
|
|
2007-05-13 22:25:59 +00:00
|
|
|
GuiInterface &m_gui;
|
|
|
|
|
|
|
|
|
|
ConfigFile *m_playerConfig;
|
2007-04-08 20:35:42 +00:00
|
|
|
|
2007-03-20 02:20:50 +00:00
|
|
|
friend class ServerRecvStateInit;
|
2007-05-25 16:03:12 +00:00
|
|
|
friend class ServerRecvStateReceiving;
|
2007-05-24 20:26:23 +00:00
|
|
|
friend class ServerRecvStateRunning;
|
2007-05-05 21:40:24 +00:00
|
|
|
friend class ServerRecvStateStartGame;
|
2007-05-13 22:25:59 +00:00
|
|
|
friend class ServerRecvStateStartHand;
|
2007-05-14 12:40:42 +00:00
|
|
|
friend class ServerRecvStateStartRound;
|
2007-05-23 22:31:56 +00:00
|
|
|
friend class ServerRecvStateWaitPlayerAction;
|
2007-05-26 17:06:03 +00:00
|
|
|
friend class ServerRecvStateNextHand;
|
2007-02-17 23:44:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|