Files
pokerth/src/net/serverrecvthread.h
T

150 lines
4.8 KiB
C++
Raw Normal View History

/***************************************************************************
* 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-03-13 23:27:17 +00:00
#ifndef _SERVERRECVTHREAD_H_
#define _SERVERRECVTHREAD_H_
2007-03-13 23:27:17 +00:00
#include <core/thread.h>
#include <net/connectdata.h>
#include <net/sessiondata.h>
#include <net/servercallback.h>
#include <deque>
#include <map>
#include <list>
#include <string>
#include <boost/shared_ptr.hpp>
#include <core/boost/timer.hpp>
#define RECEIVER_THREAD_TERMINATE_TIMEOUT 200
// Notifications
#define NOTIFY_GAME_START 1
#define NOTIFY_WAIT_FOR_CLIENT_ACTION 2
2007-03-13 23:27:17 +00:00
class ServerRecvState;
class SenderThread;
class ReceiverHelper;
class ServerSenderCallback;
class NetPacket;
struct GameData;
class Game;
2007-03-13 23:27:17 +00:00
class ServerRecvThread : public Thread
{
public:
ServerRecvThread(ServerCallback &gui);
2007-03-13 23:27:17 +00:00
virtual ~ServerRecvThread();
void Init(const std::string &pwd, const GameData &gameData);
2007-03-13 23:27:17 +00:00
void AddConnection(boost::shared_ptr<ConnectData> data);
void AddNotification(unsigned message, unsigned param1, unsigned param2);
2007-03-13 23:27:17 +00:00
ServerCallback &GetCallback();
2007-03-13 23:27:17 +00:00
protected:
struct Notification
{
Notification(unsigned m, unsigned p1, unsigned p2)
: message(m), param1(p1), param2(p2) {}
unsigned message;
unsigned param1;
unsigned param2;
};
typedef std::deque<boost::shared_ptr<ConnectData> > ConnectQueue;
typedef std::map<SOCKET, boost::shared_ptr<SessionData> > SocketSessionMap;
typedef std::deque<Notification> NotificationQueue;
typedef std::list<std::pair<boost::microsec_timer, boost::shared_ptr<SessionData> > > CloseSessionList;
2007-03-13 23:27:17 +00:00
// Main function of the thread.
virtual void Main();
void NotificationLoop();
void CloseSessionLoop();
SOCKET Select();
void CleanupConnectQueue();
void CleanupSessionMap();
void InternalStartGame();
boost::shared_ptr<SessionData> GetSession(SOCKET sock);
void AddSession(boost::shared_ptr<SessionData> sessionData);
2007-05-05 21:40:24 +00:00
void SessionError(boost::shared_ptr<SessionData> sessionData, int errorCode);
void CloseSessionDelayed(boost::shared_ptr<SessionData> sessionData);
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
void SendError(SOCKET s, int errorCode);
2007-05-05 21:40:24 +00:00
void SendToAllPlayers(boost::shared_ptr<NetPacket> packet);
void SendToAllButOnePlayers(boost::shared_ptr<NetPacket> packet, SOCKET except);
ServerRecvState &GetState();
void SetState(ServerRecvState &newState);
2007-03-13 23:27:17 +00:00
SenderThread &GetSender();
ReceiverHelper &GetReceiver();
Game &GetGame();
const GameData &GetGameData() const;
bool CheckPassword(const std::string &password) const;
2007-03-13 23:27:17 +00:00
ServerSenderCallback &GetSenderCallback();
private:
ConnectQueue m_connectQueue;
2007-03-13 23:27:17 +00:00
mutable boost::mutex m_connectQueueMutex;
ServerRecvState *m_curState;
NotificationQueue m_notificationQueue;
mutable boost::mutex m_notificationQueueMutex;
SocketSessionMap m_sessionMap;
mutable boost::mutex m_sessionMapMutex;
CloseSessionList m_closeSessionList;
2007-03-13 23:27:17 +00:00
std::auto_ptr<ReceiverHelper> m_receiver;
std::auto_ptr<SenderThread> m_sender;
std::auto_ptr<Game> m_game;
std::auto_ptr<GameData> m_gameData;
std::auto_ptr<ServerSenderCallback> m_senderCallback;
std::string m_password;
ServerCallback &m_callback;
friend class ServerRecvStateInit;
2007-05-05 21:40:24 +00:00
friend class ServerRecvStateStartGame;
};
#endif