Work on dedicated server. This svn revision is totally broken, it is part of a complete redesign of the server.
There is a lobby thread for the server, and each game has its own game processing thread and a sender thread. Connections can be accepted from different sources, i.e. TCP/SCTP over IPv4, IPv6, all those work well in parallel.
This commit is contained in:
@@ -344,12 +344,12 @@ ClientStateStartSession::Process(ClientThread &client)
|
||||
{
|
||||
ClientContext &context = client.GetContext();
|
||||
|
||||
NetPacketJoinGame::Data initData;
|
||||
NetPacketInit::Data initData;
|
||||
initData.password = context.GetPassword();
|
||||
initData.playerName = context.GetPlayerName();
|
||||
|
||||
boost::shared_ptr<NetPacket> packet(new NetPacketJoinGame);
|
||||
((NetPacketJoinGame *)packet.get())->SetData(initData);
|
||||
boost::shared_ptr<NetPacket> packet(new NetPacketInit);
|
||||
((NetPacketInit *)packet.get())->SetData(initData);
|
||||
|
||||
client.GetSender().Send(context.GetSocket(), packet);
|
||||
|
||||
@@ -435,19 +435,16 @@ ClientStateWaitSession::InternalProcess(ClientThread &client, boost::shared_ptr<
|
||||
int retVal = MSG_SOCK_INTERNAL_PENDING;
|
||||
ClientContext &context = client.GetContext();
|
||||
|
||||
if (packet->ToNetPacketJoinGameAck())
|
||||
if (packet->ToNetPacketInitAck())
|
||||
{
|
||||
// Everything is fine - we joined the game.
|
||||
// Initialize game configuration.
|
||||
NetPacketJoinGameAck::Data joinGameAckData;
|
||||
packet->ToNetPacketJoinGameAck()->GetData(joinGameAckData);
|
||||
client.SetGameData(joinGameAckData.gameData);
|
||||
client.SetGuiPlayerId(joinGameAckData.yourPlayerUniqueId);
|
||||
// Everything is fine - we are in the lobby.
|
||||
NetPacketInitAck::Data initAckData;
|
||||
packet->ToNetPacketInitAck()->GetData(initAckData);
|
||||
client.SetGuiPlayerId(initAckData.yourPlayerUniqueId);
|
||||
|
||||
// TODO: Type Human is fixed here.
|
||||
// Player number is 0 on join. Will be set when the game starts.
|
||||
// Player number is 0 on init. Will be set when the game starts.
|
||||
boost::shared_ptr<PlayerData> playerData(
|
||||
new PlayerData(joinGameAckData.yourPlayerUniqueId, 0, joinGameAckData.ptype, joinGameAckData.prights));
|
||||
new PlayerData(initAckData.yourPlayerUniqueId, 0, PLAYER_TYPE_HUMAN, PLAYER_RIGHTS_NORMAL));
|
||||
playerData->SetName(context.GetPlayerName());
|
||||
client.AddPlayerData(playerData);
|
||||
|
||||
|
||||
+695
-115
File diff suppressed because it is too large
Load Diff
+169
-494
@@ -19,18 +19,18 @@
|
||||
|
||||
#include <net/serverrecvthread.h>
|
||||
#include <net/serverexception.h>
|
||||
#include <net/serverrecvstate.h>
|
||||
#include <net/senderthread.h>
|
||||
#include <net/sendercallback.h>
|
||||
#include <net/receiverhelper.h>
|
||||
#include <net/socket_msg.h>
|
||||
#include <game.h>
|
||||
#include <tools.h>
|
||||
#include <localenginefactory.h>
|
||||
#include <core/rand.h>
|
||||
|
||||
#include <boost/lambda/lambda.hpp>
|
||||
|
||||
#define SERVER_CLOSE_SESSION_DELAY_SEC 10
|
||||
#define SERVER_MAX_NUM_SESSIONS 64 // Maximum number of idle users in lobby.
|
||||
|
||||
#define SERVER_COMPUTER_PLAYER_NAME "Computer"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -38,60 +38,56 @@ using namespace std;
|
||||
class ServerSenderCallback : public SenderCallback
|
||||
{
|
||||
public:
|
||||
ServerSenderCallback(ServerRecvThread &server) : m_server(server) {}
|
||||
ServerSenderCallback(ServerLobbyThread &server) : m_server(server) {}
|
||||
virtual ~ServerSenderCallback() {}
|
||||
|
||||
virtual void SignalNetError(SOCKET sock, int errorID, int osErrorID)
|
||||
{
|
||||
// We just ignore send errors for now, on server side.
|
||||
// A send error should trigger a read error or a read
|
||||
// A serious send error should trigger a read error or a read
|
||||
// returning 0 afterwards, and we will handle this error.
|
||||
}
|
||||
|
||||
private:
|
||||
ServerRecvThread &m_server;
|
||||
ServerLobbyThread &m_server;
|
||||
};
|
||||
|
||||
|
||||
ServerRecvThread::ServerRecvThread(GuiInterface &gui, ConfigFile *playerConfig)
|
||||
: m_curGameId(1), m_gui(gui), m_playerConfig(playerConfig)
|
||||
ServerLobbyThread::ServerLobbyThread(GuiInterface &gui, ConfigFile *playerConfig)
|
||||
: m_gui(gui), m_playerConfig(playerConfig)
|
||||
{
|
||||
m_senderCallback.reset(new ServerSenderCallback(*this));
|
||||
m_sender.reset(new SenderThread(GetSenderCallback()));
|
||||
m_receiver.reset(new ReceiverHelper);
|
||||
}
|
||||
|
||||
ServerRecvThread::~ServerRecvThread()
|
||||
ServerLobbyThread::~ServerLobbyThread()
|
||||
{
|
||||
CleanupConnectQueue();
|
||||
CleanupSessionMap();
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::Init(const string &pwd, const GameData &gameData)
|
||||
ServerLobbyThread::Init(const string &pwd)
|
||||
{
|
||||
m_password = pwd;
|
||||
m_gameData = gameData;
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::AddConnection(boost::shared_ptr<ConnectData> data)
|
||||
ServerLobbyThread::AddConnection(boost::shared_ptr<ConnectData> data)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_connectQueueMutex);
|
||||
m_connectQueue.push_back(data);
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::AddNotification(unsigned message, const string ¶m)
|
||||
u_int32_t
|
||||
ServerLobbyThread::GetNextUniquePlayerId()
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_notificationQueueMutex);
|
||||
m_notificationQueue.push_back(Notification(message, param));
|
||||
return m_curUniquePlayerId++;
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::Main()
|
||||
ServerLobbyThread::Main()
|
||||
{
|
||||
SetState(SERVER_INITIAL_STATE::Instance());
|
||||
GetSender().Run();
|
||||
|
||||
try
|
||||
@@ -110,12 +106,10 @@ ServerRecvThread::Main()
|
||||
}
|
||||
}
|
||||
if (tmpData.get())
|
||||
GetState().HandleNewConnection(*this, tmpData);
|
||||
HandleNewConnection(tmpData);
|
||||
}
|
||||
// Process current state.
|
||||
GetState().Process(*this);
|
||||
// Process thread-safe notifications.
|
||||
NotificationLoop();
|
||||
// Process loop.
|
||||
ProcessLoop();
|
||||
// Close sessions.
|
||||
CloseSessionLoop();
|
||||
}
|
||||
@@ -127,28 +121,131 @@ ServerRecvThread::Main()
|
||||
GetSender().Join(SENDER_THREAD_TERMINATE_TIMEOUT);
|
||||
|
||||
CleanupConnectQueue();
|
||||
CleanupSessionMap();
|
||||
m_sessionManager.Clear();
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::NotificationLoop()
|
||||
ServerLobbyThread::ProcessLoop()
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_notificationQueueMutex);
|
||||
// Process all notifications.
|
||||
while (!m_notificationQueue.empty())
|
||||
{
|
||||
Notification notification = m_notificationQueue.front();
|
||||
m_notificationQueue.pop_front();
|
||||
// Wait for data.
|
||||
SessionWrapper session = m_sessionManager.Select(RECV_TIMEOUT_MSEC);
|
||||
|
||||
// switch(notification.message)
|
||||
// {
|
||||
// break;
|
||||
// }
|
||||
if (session.sessionData.get())
|
||||
{
|
||||
boost::shared_ptr<NetPacket> packet;
|
||||
try
|
||||
{
|
||||
// Receive the next packet.
|
||||
packet = GetReceiver().Recv(session.sessionData->GetSocket());
|
||||
} catch (const NetException &)
|
||||
{
|
||||
// On error: Close this session.
|
||||
CloseSessionDelayed(session);
|
||||
return;
|
||||
}
|
||||
if (packet.get())
|
||||
{
|
||||
if (packet->ToNetPacketInit())
|
||||
{
|
||||
// Session should be in initial state.
|
||||
if (session.sessionData->GetState() != SessionData::Init)
|
||||
SessionError(session, ERR_SOCK_INVALID_STATE);
|
||||
else
|
||||
HandleNetPacketInit(session, *packet->ToNetPacketInit());
|
||||
}
|
||||
// Session should be established.
|
||||
else if (session.sessionData->GetState() != SessionData::Established)
|
||||
SessionError(session, ERR_SOCK_INVALID_STATE);
|
||||
else
|
||||
{
|
||||
if (packet->ToNetPacketCreateGame())
|
||||
HandleNetPacketCreateGame(session, *packet->ToNetPacketCreateGame());
|
||||
else if (packet->ToNetPacketJoinGame())
|
||||
HandleNetPacketJoinGame(session, *packet->ToNetPacketJoinGame());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::CloseSessionLoop()
|
||||
ServerLobbyThread::HandleNetPacketInit(SessionWrapper session, const NetPacketInit &tmpPacket)
|
||||
{
|
||||
NetPacketInit::Data initData;
|
||||
tmpPacket.GetData(initData);
|
||||
|
||||
// Check the protocol version.
|
||||
if (initData.versionMajor != NET_VERSION_MAJOR)
|
||||
{
|
||||
SessionError(session, ERR_NET_VERSION_NOT_SUPPORTED);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check the server password.
|
||||
if (!CheckPassword(initData.password))
|
||||
{
|
||||
SessionError(session, ERR_NET_INVALID_PASSWORD);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check whether the player name is correct.
|
||||
// Partly, this is also done in netpacket.
|
||||
// However, some disallowed names are checked only here.
|
||||
if (initData.playerName.empty() || initData.playerName.size() > MAX_NAME_SIZE
|
||||
|| initData.playerName.substr(0, sizeof(SERVER_COMPUTER_PLAYER_NAME) - 1) == SERVER_COMPUTER_PLAYER_NAME)
|
||||
{
|
||||
SessionError(session, ERR_NET_INVALID_PLAYER_NAME);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check whether this player is already connected.
|
||||
if (IsPlayerConnected(initData.playerName))
|
||||
{
|
||||
SessionError(session, ERR_NET_PLAYER_NAME_IN_USE);
|
||||
return;
|
||||
}
|
||||
|
||||
// Create player data object.
|
||||
boost::shared_ptr<PlayerData> tmpPlayerData(
|
||||
new PlayerData(GetNextUniquePlayerId(), 0, PLAYER_TYPE_HUMAN, PLAYER_RIGHTS_NORMAL));
|
||||
tmpPlayerData->SetName(initData.playerName);
|
||||
tmpPlayerData->SetNetSessionData(session.sessionData);
|
||||
|
||||
// Send ACK to client.
|
||||
boost::shared_ptr<NetPacket> initAck(new NetPacketInitAck);
|
||||
NetPacketInitAck::Data initAckData;
|
||||
initAckData.sessionId = session.sessionData->GetId(); // TODO: currently unused.
|
||||
initAckData.playerId = tmpPlayerData->GetUniqueId();
|
||||
static_cast<NetPacketInitAck *>(initAck.get())->SetData(initAckData);
|
||||
GetSender().Send(session.sessionData->GetSocket(), initAck);
|
||||
|
||||
// Send the game list to the client.
|
||||
/*GameThreadList::iterator game_i = m_gameList.begin();
|
||||
GameThreadList::iterator game_end = m_gameList.end();
|
||||
while (game_i != game_end)
|
||||
{
|
||||
GetSender().Send(session.sessionData->GetSocket(), CreateNetPacketGameListUpdate(*(*game_i)));
|
||||
++game_i;
|
||||
}*/
|
||||
|
||||
// Set player data for session.
|
||||
m_sessionManager.SetSessionPlayerData(session.sessionData->GetSocket(), tmpPlayerData);
|
||||
|
||||
// Session is now established.
|
||||
session.sessionData->SetState(SessionData::Established);
|
||||
}
|
||||
|
||||
void
|
||||
ServerLobbyThread::HandleNetPacketCreateGame(SessionWrapper session, const NetPacketCreateGame &tmpPacket)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
ServerLobbyThread::HandleNetPacketJoinGame(SessionWrapper session, const NetPacketJoinGame &tmpPacket)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
ServerLobbyThread::CloseSessionLoop()
|
||||
{
|
||||
CloseSessionList::iterator i = m_closeSessionList.begin();
|
||||
CloseSessionList::iterator end = m_closeSessionList.end();
|
||||
@@ -162,69 +259,32 @@ ServerRecvThread::CloseSessionLoop()
|
||||
}
|
||||
}
|
||||
|
||||
SOCKET
|
||||
ServerRecvThread::Select()
|
||||
void
|
||||
ServerLobbyThread::HandleNewConnection(boost::shared_ptr<ConnectData> connData)
|
||||
{
|
||||
SOCKET retSock = INVALID_SOCKET;
|
||||
|
||||
SOCKET maxSock = INVALID_SOCKET;
|
||||
fd_set rdset;
|
||||
FD_ZERO(&rdset);
|
||||
|
||||
if (m_sessionManager.GetRawSessionCount() <= SERVER_MAX_NUM_SESSIONS)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
SocketSessionMap::iterator i = m_sessionMap.begin();
|
||||
SocketSessionMap::iterator end = m_sessionMap.end();
|
||||
// Create a random session id.
|
||||
// This id can be used to reconnect to the server if the connection was lost.
|
||||
unsigned sessionId;
|
||||
RandomBytes((unsigned char *)&sessionId, sizeof(sessionId)); // TODO: check for collisions.
|
||||
|
||||
while (i != end)
|
||||
{
|
||||
SOCKET tmpSock = i->first;
|
||||
FD_SET(tmpSock, &rdset);
|
||||
if (tmpSock > maxSock || maxSock == INVALID_SOCKET)
|
||||
maxSock = tmpSock;
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
if (maxSock == INVALID_SOCKET)
|
||||
{
|
||||
Msleep(RECV_TIMEOUT_MSEC); // just sleep if there is no session
|
||||
// Create a new session.
|
||||
boost::shared_ptr<SessionData> sessionData(new SessionData(connData->ReleaseSocket(), sessionId));
|
||||
m_sessionManager.AddSession(sessionData);
|
||||
}
|
||||
else
|
||||
{
|
||||
// wait for data
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = 0;
|
||||
timeout.tv_usec = RECV_TIMEOUT_MSEC * 1000;
|
||||
int selectResult = select(maxSock + 1, &rdset, NULL, NULL, &timeout);
|
||||
if (!IS_VALID_SELECT(selectResult))
|
||||
{
|
||||
throw ServerException(ERR_SOCK_SELECT_FAILED, SOCKET_ERRNO());
|
||||
}
|
||||
if (selectResult > 0) // one (or more) of the sockets is readable
|
||||
{
|
||||
// Check which socket is readable, return the first.
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
SocketSessionMap::iterator i = m_sessionMap.begin();
|
||||
SocketSessionMap::iterator end = m_sessionMap.end();
|
||||
|
||||
while (i != end)
|
||||
{
|
||||
SOCKET tmpSock = i->first;
|
||||
if (FD_ISSET(tmpSock, &rdset))
|
||||
{
|
||||
retSock = tmpSock;
|
||||
break;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
}
|
||||
// Server is full.
|
||||
// Create a generic session with Id 0.
|
||||
boost::shared_ptr<SessionData> sessionData(new SessionData(connData->ReleaseSocket(), 0));
|
||||
// Gracefully close this session.
|
||||
SessionError(SessionWrapper(sessionData, boost::shared_ptr<PlayerData>()), ERR_NET_SERVER_FULL);
|
||||
}
|
||||
return retSock;
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::CleanupConnectQueue()
|
||||
ServerLobbyThread::CleanupConnectQueue()
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_connectQueueMutex);
|
||||
|
||||
@@ -233,155 +293,7 @@ ServerRecvThread::CleanupConnectQueue()
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::CleanupSessionMap()
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
// Sockets will be closed automatically.
|
||||
m_sessionMap.clear();
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::InternalStartGame()
|
||||
{
|
||||
// Kick all players which are not fully connected.
|
||||
RemoveNotEstablishedSessions();
|
||||
// Set order of players.
|
||||
AssignPlayerNumbers();
|
||||
|
||||
// Initialize the game.
|
||||
GuiInterface &gui = GetGui();
|
||||
PlayerDataList playerData = GetPlayerDataList();
|
||||
|
||||
// Create EngineFactory
|
||||
boost::shared_ptr<EngineFactory> factory(new LocalEngineFactory(m_playerConfig)); // LocalEngine erstellen
|
||||
|
||||
// Set start data.
|
||||
StartData startData;
|
||||
startData.numberOfPlayers = playerData.size();
|
||||
|
||||
int tmpDealerPos = 0;
|
||||
Tools::getRandNumber(0, startData.numberOfPlayers-1, 1, &tmpDealerPos, 0);
|
||||
// The Player Id is not continuous. Therefore, the start dealer position
|
||||
// needs to be converted to a player Id, and cannot be directly generated
|
||||
// as player Id.
|
||||
PlayerDataList::const_iterator player_i = playerData.begin();
|
||||
PlayerDataList::const_iterator player_end = playerData.end();
|
||||
|
||||
bool randDealerFound = false;
|
||||
while (player_i != player_end)
|
||||
{
|
||||
if ((*player_i)->GetNumber() == tmpDealerPos)
|
||||
{
|
||||
// Get ID of the dealer.
|
||||
startData.startDealerPlayerId = static_cast<unsigned>((*player_i)->GetUniqueId());
|
||||
randDealerFound = true;
|
||||
break;
|
||||
}
|
||||
++player_i;
|
||||
}
|
||||
assert(randDealerFound); // TODO: Throw exception.
|
||||
|
||||
SetStartData(startData);
|
||||
|
||||
m_game.reset(new Game(&gui, factory, playerData, GetGameData(), GetStartData(), m_curGameId++));
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::InternalKickPlayer(unsigned uniqueId)
|
||||
{
|
||||
SessionWrapper tmpSession = GetSessionByUniquePlayerId(uniqueId);
|
||||
SessionError(tmpSession, ERR_NET_PLAYER_KICKED);
|
||||
}
|
||||
|
||||
SessionWrapper
|
||||
ServerRecvThread::GetSession(SOCKET sock) const
|
||||
{
|
||||
SessionWrapper tmpSession;
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
SocketSessionMap::const_iterator pos = m_sessionMap.find(sock);
|
||||
if (pos != m_sessionMap.end())
|
||||
{
|
||||
tmpSession = pos->second;
|
||||
}
|
||||
return tmpSession;
|
||||
}
|
||||
|
||||
SessionWrapper
|
||||
ServerRecvThread::GetSessionByPlayerName(const string playerName) const
|
||||
{
|
||||
SessionWrapper tmpSession;
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
SocketSessionMap::const_iterator session_i = m_sessionMap.begin();
|
||||
SocketSessionMap::const_iterator session_end = m_sessionMap.end();
|
||||
|
||||
while (session_i != session_end)
|
||||
{
|
||||
// Check all players which are fully connected.
|
||||
if (session_i->second.sessionData->GetState() == SessionData::Established)
|
||||
{
|
||||
boost::shared_ptr<PlayerData> tmpPlayer(session_i->second.playerData);
|
||||
assert(tmpPlayer.get());
|
||||
if (tmpPlayer->GetName() == playerName)
|
||||
{
|
||||
tmpSession = session_i->second;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
++session_i;
|
||||
}
|
||||
return tmpSession;
|
||||
}
|
||||
|
||||
SessionWrapper
|
||||
ServerRecvThread::GetSessionByUniquePlayerId(unsigned uniqueId) const
|
||||
{
|
||||
SessionWrapper tmpSession;
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
SocketSessionMap::const_iterator session_i = m_sessionMap.begin();
|
||||
SocketSessionMap::const_iterator session_end = m_sessionMap.end();
|
||||
|
||||
while (session_i != session_end)
|
||||
{
|
||||
// Check all players which are fully connected.
|
||||
if (session_i->second.sessionData->GetState() == SessionData::Established)
|
||||
{
|
||||
boost::shared_ptr<PlayerData> tmpPlayer(session_i->second.playerData);
|
||||
assert(tmpPlayer.get());
|
||||
if (tmpPlayer->GetUniqueId() == uniqueId)
|
||||
{
|
||||
tmpSession = session_i->second;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
++session_i;
|
||||
}
|
||||
return tmpSession;
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::AddSession(boost::shared_ptr<SessionData> sessionData)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
SocketSessionMap::iterator pos = m_sessionMap.lower_bound(sessionData->GetSocket());
|
||||
|
||||
// If pos points to a pair whose key is equivalent to the socket, this handle
|
||||
// already exists within the list.
|
||||
if (pos != m_sessionMap.end() && sessionData->GetSocket() == pos->first)
|
||||
{
|
||||
throw ServerException(ERR_SOCK_CONN_EXISTS, 0);
|
||||
}
|
||||
m_sessionMap.insert(pos, SocketSessionMap::value_type(sessionData->GetSocket(), SessionWrapper(sessionData, boost::shared_ptr<PlayerData>())));
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::SessionError(SessionWrapper session, int errorCode)
|
||||
ServerLobbyThread::SessionError(SessionWrapper session, int errorCode)
|
||||
{
|
||||
if (session.sessionData.get())
|
||||
{
|
||||
@@ -391,22 +303,9 @@ ServerRecvThread::SessionError(SessionWrapper session, int errorCode)
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::RejectNewConnection(boost::shared_ptr<ConnectData> connData)
|
||||
ServerLobbyThread::CloseSessionDelayed(SessionWrapper session)
|
||||
{
|
||||
// Create a generic session with Id 0.
|
||||
boost::shared_ptr<SessionData> sessionData(new SessionData(connData->ReleaseSocket(), 0));
|
||||
// Gracefully close this session.
|
||||
SessionError(SessionWrapper(sessionData, boost::shared_ptr<PlayerData>()), ERR_NET_GAME_ALREADY_RUNNING);
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::CloseSessionDelayed(SessionWrapper session)
|
||||
{
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
m_sessionMap.erase(session.sessionData->GetSocket());
|
||||
}
|
||||
m_sessionManager.RemoveSession(session.sessionData->GetSocket());
|
||||
|
||||
boost::shared_ptr<PlayerData> tmpPlayerData = session.playerData;
|
||||
if (tmpPlayerData.get() && !tmpPlayerData->GetName().empty())
|
||||
@@ -416,7 +315,7 @@ ServerRecvThread::CloseSessionDelayed(SessionWrapper session)
|
||||
NetPacketPlayerLeft::Data thisPlayerLeftData;
|
||||
thisPlayerLeftData.playerId = tmpPlayerData->GetUniqueId();
|
||||
static_cast<NetPacketPlayerLeft *>(thisPlayerLeft.get())->SetData(thisPlayerLeftData);
|
||||
SendToAllPlayers(thisPlayerLeft);
|
||||
m_sessionManager.SendToAllSessions(GetSender(), thisPlayerLeft);
|
||||
|
||||
GetCallback().SignalNetServerPlayerLeft(tmpPlayerData->GetName());
|
||||
}
|
||||
@@ -428,166 +327,7 @@ ServerRecvThread::CloseSessionDelayed(SessionWrapper session)
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::RemoveNotEstablishedSessions()
|
||||
{
|
||||
SessionList removeList;
|
||||
|
||||
SocketSessionMap::iterator session_i = m_sessionMap.begin();
|
||||
SocketSessionMap::iterator session_end = m_sessionMap.end();
|
||||
|
||||
while (session_i != session_end)
|
||||
{
|
||||
// Remove all players which are not fully connected.
|
||||
assert(session_i->second.sessionData.get());
|
||||
if (session_i->second.sessionData->GetState() != SessionData::Established)
|
||||
{
|
||||
// Do not mess with the map within this loop.
|
||||
// Just store what needs to be removed.
|
||||
removeList.push_back(session_i->second);
|
||||
}
|
||||
++session_i;
|
||||
}
|
||||
|
||||
SessionList::iterator remove_i = removeList.begin();
|
||||
SessionList::iterator remove_end = removeList.end();
|
||||
|
||||
while (remove_i != remove_end)
|
||||
{
|
||||
// Inform the players that we are starting without them.
|
||||
// Gracefully remove them from the server.
|
||||
SessionError(*remove_i, ERR_NET_GAME_ALREADY_RUNNING);
|
||||
++remove_i;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::RemoveDisconnectedPlayers()
|
||||
{
|
||||
// This should only be called between hands.
|
||||
if (m_game.get())
|
||||
{
|
||||
for (int i = 0; i < m_game->getStartQuantityPlayers(); i++)
|
||||
{
|
||||
boost::shared_ptr<PlayerInterface> tmpPlayer = m_game->getPlayerArray()[i];
|
||||
if (!IsPlayerConnected(tmpPlayer->getMyUniqueID()) && tmpPlayer->getMyType() == PLAYER_TYPE_HUMAN)
|
||||
{
|
||||
tmpPlayer->setMyCash(0);
|
||||
tmpPlayer->setMyActiveStatus(false);
|
||||
tmpPlayer->setNetSessionData(boost::shared_ptr<SessionData>());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::AddComputerPlayer(boost::shared_ptr<PlayerData> player)
|
||||
{
|
||||
m_computerPlayers.push_back(player);
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::ResetComputerPlayerList()
|
||||
{
|
||||
m_computerPlayers.clear();
|
||||
}
|
||||
|
||||
size_t
|
||||
ServerRecvThread::GetCurNumberOfPlayers() const
|
||||
{
|
||||
PlayerDataList playerList = GetPlayerDataList();
|
||||
return playerList.size();
|
||||
}
|
||||
|
||||
bool
|
||||
ServerRecvThread::IsPlayerConnected(const string &playerName) const
|
||||
{
|
||||
bool retVal = false;
|
||||
|
||||
SessionWrapper tmpSession = GetSessionByPlayerName(playerName);
|
||||
|
||||
if (tmpSession.sessionData.get() && tmpSession.playerData.get())
|
||||
retVal = true;
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
bool
|
||||
ServerRecvThread::IsPlayerConnected(unsigned uniquePlayerId) const
|
||||
{
|
||||
bool retVal = false;
|
||||
|
||||
SessionWrapper tmpSession = GetSessionByUniquePlayerId(uniquePlayerId);
|
||||
|
||||
if (tmpSession.sessionData.get() && tmpSession.playerData.get())
|
||||
retVal = true;
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::SetSessionPlayerData(boost::shared_ptr<SessionData> sessionData, boost::shared_ptr<PlayerData> playerData)
|
||||
{
|
||||
assert(playerData.get());
|
||||
assert(!playerData->GetName().empty());
|
||||
assert(sessionData.get());
|
||||
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
SocketSessionMap::iterator pos = m_sessionMap.find(sessionData->GetSocket());
|
||||
if (pos != m_sessionMap.end())
|
||||
{
|
||||
pos->second.playerData = playerData;
|
||||
|
||||
// Signal joining player to GUI.
|
||||
GetCallback().SignalNetServerPlayerJoined(playerData->GetName());
|
||||
}
|
||||
}
|
||||
|
||||
PlayerDataList
|
||||
ServerRecvThread::GetPlayerDataList() const
|
||||
{
|
||||
PlayerDataList playerList;
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
SocketSessionMap::const_iterator session_i = m_sessionMap.begin();
|
||||
SocketSessionMap::const_iterator session_end = m_sessionMap.end();
|
||||
|
||||
while (session_i != session_end)
|
||||
{
|
||||
// Get all players which are fully connected.
|
||||
if (session_i->second.sessionData->GetState() == SessionData::Established)
|
||||
{
|
||||
boost::shared_ptr<PlayerData> tmpPlayer(session_i->second.playerData);
|
||||
assert(tmpPlayer.get());
|
||||
assert(!tmpPlayer->GetName().empty());
|
||||
playerList.push_back(tmpPlayer);
|
||||
}
|
||||
++session_i;
|
||||
}
|
||||
if (!m_computerPlayers.empty())
|
||||
playerList.insert(playerList.end(), m_computerPlayers.begin(), m_computerPlayers.end());
|
||||
return playerList;
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::AssignPlayerNumbers()
|
||||
{
|
||||
int playerNumber = 0;
|
||||
|
||||
PlayerDataList playerList = GetPlayerDataList();
|
||||
PlayerDataList::iterator player_i = playerList.begin();
|
||||
PlayerDataList::iterator player_end = playerList.end();
|
||||
|
||||
while (player_i != player_end)
|
||||
{
|
||||
(*player_i)->SetNumber(playerNumber);
|
||||
++playerNumber;
|
||||
++player_i;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::SendError(SOCKET s, int errorCode)
|
||||
ServerLobbyThread::SendError(SOCKET s, int errorCode)
|
||||
{
|
||||
boost::shared_ptr<NetPacket> packet(new NetPacketError);
|
||||
NetPacketError::Data errorData;
|
||||
@@ -596,119 +336,54 @@ ServerRecvThread::SendError(SOCKET s, int errorCode)
|
||||
GetSender().Send(s, packet);
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::SendToAllPlayers(boost::shared_ptr<NetPacket> packet)
|
||||
bool
|
||||
ServerLobbyThread::IsPlayerConnected(const string &playerName) const
|
||||
{
|
||||
// This function needs to be thread safe.
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
bool retVal = false;
|
||||
|
||||
SocketSessionMap::iterator i = m_sessionMap.begin();
|
||||
SocketSessionMap::iterator end = m_sessionMap.end();
|
||||
SessionWrapper tmpSession = m_sessionManager.GetSessionByPlayerName(playerName);
|
||||
|
||||
while (i != end)
|
||||
{
|
||||
assert(i->second.sessionData.get());
|
||||
if (tmpSession.sessionData.get() && tmpSession.playerData.get())
|
||||
retVal = true;
|
||||
|
||||
// Send each fully connected client a copy of the packet.
|
||||
if (i->second.sessionData->GetState() == SessionData::Established)
|
||||
GetSender().Send(i->first, boost::shared_ptr<NetPacket>(packet->Clone()));
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::SendToAllButOnePlayers(boost::shared_ptr<NetPacket> packet, SOCKET except)
|
||||
{
|
||||
// This function needs to be thread safe.
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
SocketSessionMap::iterator i = m_sessionMap.begin();
|
||||
SocketSessionMap::iterator end = m_sessionMap.end();
|
||||
|
||||
while (i != end)
|
||||
{
|
||||
// Send each fully connected client but one a copy of the packet.
|
||||
if (i->second.sessionData->GetState() == SessionData::Established)
|
||||
if (i->first != except)
|
||||
GetSender().Send(i->first, boost::shared_ptr<NetPacket>(packet->Clone()));
|
||||
++i;
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
ServerCallback &
|
||||
ServerRecvThread::GetCallback()
|
||||
ServerLobbyThread::GetCallback()
|
||||
{
|
||||
return m_gui;
|
||||
}
|
||||
|
||||
ServerRecvState &
|
||||
ServerRecvThread::GetState()
|
||||
{
|
||||
assert(m_curState);
|
||||
return *m_curState;
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::SetState(ServerRecvState &newState)
|
||||
{
|
||||
newState.Init();
|
||||
m_curState = &newState;
|
||||
}
|
||||
|
||||
SenderThread &
|
||||
ServerRecvThread::GetSender()
|
||||
ServerLobbyThread::GetSender()
|
||||
{
|
||||
assert(m_sender.get());
|
||||
return *m_sender;
|
||||
}
|
||||
|
||||
ReceiverHelper &
|
||||
ServerRecvThread::GetReceiver()
|
||||
ServerLobbyThread::GetReceiver()
|
||||
{
|
||||
assert(m_receiver.get());
|
||||
return *m_receiver;
|
||||
}
|
||||
|
||||
Game &
|
||||
ServerRecvThread::GetGame()
|
||||
{
|
||||
assert(m_game.get());
|
||||
return *m_game;
|
||||
}
|
||||
|
||||
const GameData &
|
||||
ServerRecvThread::GetGameData() const
|
||||
{
|
||||
return m_gameData;
|
||||
}
|
||||
|
||||
const StartData &
|
||||
ServerRecvThread::GetStartData() const
|
||||
{
|
||||
return m_startData;
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::SetStartData(const StartData &startData)
|
||||
{
|
||||
m_startData = startData;
|
||||
}
|
||||
|
||||
bool
|
||||
ServerRecvThread::CheckPassword(const string &password) const
|
||||
ServerLobbyThread::CheckPassword(const string &password) const
|
||||
{
|
||||
return (password == m_password);
|
||||
}
|
||||
|
||||
ServerSenderCallback &
|
||||
ServerRecvThread::GetSenderCallback()
|
||||
ServerLobbyThread::GetSenderCallback()
|
||||
{
|
||||
assert(m_senderCallback.get());
|
||||
return *m_senderCallback;
|
||||
}
|
||||
|
||||
GuiInterface &
|
||||
ServerRecvThread::GetGui()
|
||||
ServerLobbyThread::GetGui()
|
||||
{
|
||||
return m_gui;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,237 @@
|
||||
/***************************************************************************
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
|
||||
#include <net/sessionmanager.h>
|
||||
#include <net/senderthread.h>
|
||||
#include <net/serverexception.h>
|
||||
#include <net/socket_msg.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
SessionManager::SessionManager()
|
||||
{
|
||||
}
|
||||
|
||||
SessionManager::~SessionManager()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
void
|
||||
SessionManager::AddSession(boost::shared_ptr<SessionData> sessionData)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
SessionMap::iterator pos = m_sessionMap.lower_bound(sessionData->GetSocket());
|
||||
|
||||
// If pos points to a pair whose key is equivalent to the socket, this handle
|
||||
// already exists within the list.
|
||||
if (pos != m_sessionMap.end() && sessionData->GetSocket() == pos->first)
|
||||
{
|
||||
throw ServerException(ERR_SOCK_CONN_EXISTS, 0);
|
||||
}
|
||||
m_sessionMap.insert(pos, SessionMap::value_type(sessionData->GetSocket(), SessionWrapper(sessionData, boost::shared_ptr<PlayerData>())));
|
||||
}
|
||||
|
||||
void
|
||||
SessionManager::SetSessionPlayerData(SOCKET session, boost::shared_ptr<PlayerData> playerData)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
SessionMap::iterator pos = m_sessionMap.find(session);
|
||||
|
||||
if (pos != m_sessionMap.end())
|
||||
pos->second.playerData = playerData;
|
||||
}
|
||||
|
||||
void
|
||||
SessionManager::RemoveSession(SOCKET session)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
m_sessionMap.erase(session);
|
||||
}
|
||||
|
||||
SessionWrapper
|
||||
SessionManager::Select(unsigned timeoutMsec)
|
||||
{
|
||||
SessionWrapper retSession;
|
||||
SOCKET maxSock = INVALID_SOCKET;
|
||||
fd_set rdset;
|
||||
FD_ZERO(&rdset);
|
||||
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
SessionMap::iterator i = m_sessionMap.begin();
|
||||
SessionMap::iterator end = m_sessionMap.end();
|
||||
|
||||
while (i != end)
|
||||
{
|
||||
SOCKET tmpSock = i->first;
|
||||
FD_SET(tmpSock, &rdset);
|
||||
if (tmpSock > maxSock || maxSock == INVALID_SOCKET)
|
||||
maxSock = tmpSock;
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
if (maxSock == INVALID_SOCKET)
|
||||
{
|
||||
Thread::Msleep(timeoutMsec); // just sleep if there is no session
|
||||
}
|
||||
else
|
||||
{
|
||||
// wait for data
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = timeoutMsec / 1000;
|
||||
timeout.tv_usec = (timeoutMsec % 1000) * 1000;
|
||||
int selectResult = select(maxSock + 1, &rdset, NULL, NULL, &timeout);
|
||||
if (!IS_VALID_SELECT(selectResult))
|
||||
{
|
||||
throw ServerException(ERR_SOCK_SELECT_FAILED, SOCKET_ERRNO());
|
||||
}
|
||||
if (selectResult > 0) // one (or more) of the sockets is readable
|
||||
{
|
||||
// Check which socket is readable, return the first.
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
SessionMap::iterator i = m_sessionMap.begin();
|
||||
SessionMap::iterator end = m_sessionMap.end();
|
||||
|
||||
while (i != end)
|
||||
{
|
||||
if (FD_ISSET(i->first, &rdset))
|
||||
{
|
||||
retSession = i->second;
|
||||
break;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return retSession;
|
||||
}
|
||||
|
||||
SessionWrapper
|
||||
SessionManager::GetSessionByPlayerName(const string playerName) const
|
||||
{
|
||||
SessionWrapper tmpSession;
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
SessionMap::const_iterator session_i = m_sessionMap.begin();
|
||||
SessionMap::const_iterator session_end = m_sessionMap.end();
|
||||
|
||||
while (session_i != session_end)
|
||||
{
|
||||
// Check all players which are fully connected.
|
||||
if (session_i->second.sessionData->GetState() == SessionData::Established)
|
||||
{
|
||||
boost::shared_ptr<PlayerData> tmpPlayer(session_i->second.playerData);
|
||||
assert(tmpPlayer.get());
|
||||
if (tmpPlayer->GetName() == playerName)
|
||||
{
|
||||
tmpSession = session_i->second;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
++session_i;
|
||||
}
|
||||
return tmpSession;
|
||||
}
|
||||
|
||||
SessionWrapper
|
||||
SessionManager::GetSessionByUniquePlayerId(unsigned uniqueId) const
|
||||
{
|
||||
SessionWrapper tmpSession;
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
SessionMap::const_iterator session_i = m_sessionMap.begin();
|
||||
SessionMap::const_iterator session_end = m_sessionMap.end();
|
||||
|
||||
while (session_i != session_end)
|
||||
{
|
||||
// Check all players which are fully connected.
|
||||
if (session_i->second.sessionData->GetState() == SessionData::Established)
|
||||
{
|
||||
boost::shared_ptr<PlayerData> tmpPlayer(session_i->second.playerData);
|
||||
assert(tmpPlayer.get());
|
||||
if (tmpPlayer->GetUniqueId() == uniqueId)
|
||||
{
|
||||
tmpSession = session_i->second;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
++session_i;
|
||||
}
|
||||
return tmpSession;
|
||||
}
|
||||
|
||||
void
|
||||
SessionManager::Clear()
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
// Sockets will be closed automatically.
|
||||
m_sessionMap.clear();
|
||||
}
|
||||
|
||||
unsigned
|
||||
SessionManager::GetRawSessionCount()
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
return m_sessionMap.size();
|
||||
}
|
||||
|
||||
void
|
||||
SessionManager::SendToAllSessions(SenderThread &sender, boost::shared_ptr<NetPacket> packet)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
SessionMap::iterator i = m_sessionMap.begin();
|
||||
SessionMap::iterator end = m_sessionMap.end();
|
||||
|
||||
while (i != end)
|
||||
{
|
||||
assert(i->second.sessionData.get());
|
||||
|
||||
// Send each fully connected client a copy of the packet.
|
||||
if (i->second.sessionData->GetState() == SessionData::Established)
|
||||
sender.Send(i->first, boost::shared_ptr<NetPacket>(packet->Clone()));
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
SessionManager::SendToAllButOneSessions(SenderThread &sender, boost::shared_ptr<NetPacket> packet, SOCKET except)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
SessionMap::iterator i = m_sessionMap.begin();
|
||||
SessionMap::iterator end = m_sessionMap.end();
|
||||
|
||||
while (i != end)
|
||||
{
|
||||
// Send each fully connected client but one a copy of the packet.
|
||||
if (i->second.sessionData->GetState() == SessionData::Established)
|
||||
if (i->first != except)
|
||||
sender.Send(i->first, boost::shared_ptr<NetPacket>(packet->Clone()));
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
+169
-16
@@ -40,6 +40,12 @@
|
||||
|
||||
struct NetPacketHeader;
|
||||
|
||||
class NetPacketInit;
|
||||
class NetPacketInitAck;
|
||||
class NetPacketGameListNew;
|
||||
class NetPacketGameListUpdate;
|
||||
class NetPacketCreateGame;
|
||||
class NetPacketCreateGameAck;
|
||||
class NetPacketJoinGame;
|
||||
class NetPacketJoinGameAck;
|
||||
class NetPacketPlayerJoined;
|
||||
@@ -80,6 +86,12 @@ public:
|
||||
u_int16_t GetType() const;
|
||||
u_int16_t GetLen() const;
|
||||
|
||||
virtual const NetPacketInit *ToNetPacketInit() const;
|
||||
virtual const NetPacketInitAck *ToNetPacketInitAck() const;
|
||||
virtual const NetPacketGameListNew *ToNetPacketGameListNew() const;
|
||||
virtual const NetPacketGameListUpdate *ToNetPacketGameListUpdate() const;
|
||||
virtual const NetPacketCreateGame *ToNetPacketCreateGame() const;
|
||||
virtual const NetPacketCreateGameAck *ToNetPacketCreateGameAck() const;
|
||||
virtual const NetPacketJoinGame *ToNetPacketJoinGame() const;
|
||||
virtual const NetPacketJoinGameAck *ToNetPacketJoinGameAck() const;
|
||||
virtual const NetPacketPlayerJoined *ToNetPacketPlayerJoined() const;
|
||||
@@ -117,7 +129,7 @@ private:
|
||||
const u_int16_t m_maxSize;
|
||||
};
|
||||
|
||||
class NetPacketJoinGame : public NetPacket
|
||||
class NetPacketInit : public NetPacket
|
||||
{
|
||||
public:
|
||||
struct Data
|
||||
@@ -128,6 +140,151 @@ public:
|
||||
std::string password;
|
||||
};
|
||||
|
||||
NetPacketInit();
|
||||
virtual ~NetPacketInit();
|
||||
|
||||
virtual boost::shared_ptr<NetPacket> Clone() const;
|
||||
|
||||
void SetData(const Data &inData);
|
||||
void GetData(Data &outData) const;
|
||||
|
||||
virtual const NetPacketInit *ToNetPacketInit() const;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void InternalCheck(const NetPacketHeader* data) const;
|
||||
};
|
||||
|
||||
class NetPacketInitAck : public NetPacket
|
||||
{
|
||||
public:
|
||||
struct Data
|
||||
{
|
||||
u_int32_t sessionId;
|
||||
u_int32_t playerId;
|
||||
};
|
||||
|
||||
NetPacketInitAck();
|
||||
virtual ~NetPacketInitAck();
|
||||
|
||||
virtual boost::shared_ptr<NetPacket> Clone() const;
|
||||
|
||||
void SetData(const Data &inData);
|
||||
void GetData(Data &outData) const;
|
||||
|
||||
virtual const NetPacketInitAck *ToNetPacketInitAck() const;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void InternalCheck(const NetPacketHeader* data) const;
|
||||
};
|
||||
|
||||
class NetPacketGameListNew : public NetPacket
|
||||
{
|
||||
public:
|
||||
struct Data
|
||||
{
|
||||
u_int32_t gameId;
|
||||
GameMode gameMode;
|
||||
std::string gameName;
|
||||
};
|
||||
|
||||
NetPacketGameListNew();
|
||||
virtual ~NetPacketGameListNew();
|
||||
|
||||
virtual boost::shared_ptr<NetPacket> Clone() const;
|
||||
|
||||
void SetData(const Data &inData);
|
||||
void GetData(Data &outData) const;
|
||||
|
||||
virtual const NetPacketGameListNew *ToNetPacketGameListNew() const;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void InternalCheck(const NetPacketHeader* data) const;
|
||||
};
|
||||
|
||||
class NetPacketGameListUpdate : public NetPacket
|
||||
{
|
||||
public:
|
||||
struct Data
|
||||
{
|
||||
u_int32_t gameId;
|
||||
GameMode gameMode;
|
||||
};
|
||||
|
||||
NetPacketGameListUpdate();
|
||||
virtual ~NetPacketGameListUpdate();
|
||||
|
||||
virtual boost::shared_ptr<NetPacket> Clone() const;
|
||||
|
||||
void SetData(const Data &inData);
|
||||
void GetData(Data &outData) const;
|
||||
|
||||
virtual const NetPacketGameListUpdate *ToNetPacketGameListUpdate() const;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void InternalCheck(const NetPacketHeader* data) const;
|
||||
};
|
||||
|
||||
class NetPacketCreateGame : public NetPacket
|
||||
{
|
||||
public:
|
||||
struct Data
|
||||
{
|
||||
std::string gameName;
|
||||
std::string password;
|
||||
GameData gameData;
|
||||
};
|
||||
|
||||
NetPacketCreateGame();
|
||||
virtual ~NetPacketCreateGame();
|
||||
|
||||
virtual boost::shared_ptr<NetPacket> Clone() const;
|
||||
|
||||
void SetData(const Data &inData);
|
||||
void GetData(Data &outData) const;
|
||||
|
||||
virtual const NetPacketCreateGame *ToNetPacketCreateGame() const;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void InternalCheck(const NetPacketHeader* data) const;
|
||||
};
|
||||
|
||||
class NetPacketCreateGameAck : public NetPacket
|
||||
{
|
||||
public:
|
||||
struct Data
|
||||
{
|
||||
u_int32_t gameId;
|
||||
};
|
||||
|
||||
NetPacketCreateGameAck();
|
||||
virtual ~NetPacketCreateGameAck();
|
||||
|
||||
virtual boost::shared_ptr<NetPacket> Clone() const;
|
||||
|
||||
void SetData(const Data &inData);
|
||||
void GetData(Data &outData) const;
|
||||
|
||||
virtual const NetPacketCreateGameAck *ToNetPacketCreateGameAck() const;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void InternalCheck(const NetPacketHeader* data) const;
|
||||
};
|
||||
|
||||
class NetPacketJoinGame : public NetPacket
|
||||
{
|
||||
public:
|
||||
struct Data
|
||||
{
|
||||
u_int32_t gameId;
|
||||
std::string password;
|
||||
};
|
||||
|
||||
NetPacketJoinGame();
|
||||
virtual ~NetPacketJoinGame();
|
||||
|
||||
@@ -148,10 +305,6 @@ class NetPacketJoinGameAck : public NetPacket
|
||||
public:
|
||||
struct Data
|
||||
{
|
||||
u_int32_t sessionId;
|
||||
u_int16_t yourPlayerUniqueId;
|
||||
PlayerType ptype;
|
||||
PlayerRights prights;
|
||||
GameData gameData;
|
||||
};
|
||||
|
||||
@@ -175,7 +328,7 @@ class NetPacketPlayerJoined : public NetPacket
|
||||
public:
|
||||
struct Data
|
||||
{
|
||||
u_int16_t playerId;
|
||||
u_int32_t playerId;
|
||||
PlayerType ptype;
|
||||
PlayerRights prights;
|
||||
std::string playerName;
|
||||
@@ -201,7 +354,7 @@ class NetPacketPlayerLeft : public NetPacket
|
||||
public:
|
||||
struct Data
|
||||
{
|
||||
u_int16_t playerId;
|
||||
u_int32_t playerId;
|
||||
};
|
||||
|
||||
NetPacketPlayerLeft();
|
||||
@@ -224,7 +377,7 @@ class NetPacketKickPlayer : public NetPacket
|
||||
public:
|
||||
struct Data
|
||||
{
|
||||
u_int16_t playerId;
|
||||
u_int32_t playerId;
|
||||
};
|
||||
|
||||
NetPacketKickPlayer();
|
||||
@@ -264,7 +417,7 @@ public:
|
||||
|
||||
struct PlayerSlot
|
||||
{
|
||||
unsigned playerId;
|
||||
u_int32_t playerId;
|
||||
};
|
||||
|
||||
typedef std::list<PlayerSlot> PlayerSlotList;
|
||||
@@ -319,7 +472,7 @@ public:
|
||||
struct Data
|
||||
{
|
||||
GameState gameState;
|
||||
u_int16_t playerId;
|
||||
u_int32_t playerId;
|
||||
};
|
||||
|
||||
NetPacketPlayersTurn();
|
||||
@@ -368,7 +521,7 @@ public:
|
||||
struct Data
|
||||
{
|
||||
GameState gameState;
|
||||
u_int16_t playerId;
|
||||
u_int32_t playerId;
|
||||
PlayerAction playerAction;
|
||||
u_int32_t totalPlayerBet;
|
||||
u_int32_t playerMoney;
|
||||
@@ -490,7 +643,7 @@ class NetPacketAllInShowCards : public NetPacket
|
||||
public:
|
||||
struct PlayerCards
|
||||
{
|
||||
u_int16_t playerId;
|
||||
u_int32_t playerId;
|
||||
u_int16_t cards[2];
|
||||
};
|
||||
|
||||
@@ -521,7 +674,7 @@ class NetPacketEndOfHandShowCards : public NetPacket
|
||||
public:
|
||||
struct PlayerResult
|
||||
{
|
||||
u_int16_t playerId;
|
||||
u_int32_t playerId;
|
||||
u_int16_t cards[2];
|
||||
u_int16_t bestHandPos[5];
|
||||
u_int32_t valueOfCards;
|
||||
@@ -556,7 +709,7 @@ class NetPacketEndOfHandHideCards : public NetPacket
|
||||
public:
|
||||
struct Data
|
||||
{
|
||||
u_int16_t playerId;
|
||||
u_int32_t playerId;
|
||||
u_int32_t moneyWon;
|
||||
u_int32_t playerMoney;
|
||||
};
|
||||
@@ -581,7 +734,7 @@ class NetPacketEndOfGame : public NetPacket
|
||||
public:
|
||||
struct Data
|
||||
{
|
||||
u_int16_t winnerPlayerId;
|
||||
u_int32_t winnerPlayerId;
|
||||
};
|
||||
|
||||
NetPacketEndOfGame();
|
||||
@@ -627,7 +780,7 @@ class NetPacketChatText : public NetPacket
|
||||
public:
|
||||
struct Data
|
||||
{
|
||||
u_int16_t playerId;
|
||||
u_int32_t playerId;
|
||||
std::string text;
|
||||
};
|
||||
|
||||
|
||||
@@ -124,7 +124,7 @@ protected:
|
||||
|
||||
private:
|
||||
|
||||
u_int16_t m_curUniquePlayerId;
|
||||
u_int32_t m_curUniquePlayerId;
|
||||
static boost::thread_specific_ptr<ServerRecvStateInit> Ptr;
|
||||
};
|
||||
|
||||
|
||||
+17
-92
@@ -21,119 +21,70 @@
|
||||
#ifndef _SERVERRECVTHREAD_H_
|
||||
#define _SERVERRECVTHREAD_H_
|
||||
|
||||
#include <core/thread.h>
|
||||
#include <net/connectdata.h>
|
||||
#include <net/sessiondata.h>
|
||||
#include <net/sessionmanager.h>
|
||||
#include <net/netpacket.h>
|
||||
#include <gui/guiinterface.h>
|
||||
#include <gamedata.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
|
||||
|
||||
|
||||
class ServerRecvState;
|
||||
class SenderThread;
|
||||
class ReceiverHelper;
|
||||
class ServerSenderCallback;
|
||||
class NetPacket;
|
||||
class ConfigFile;
|
||||
struct GameData;
|
||||
class Game;
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
class ServerRecvThread : public Thread
|
||||
class ServerLobbyThread : public Thread
|
||||
{
|
||||
public:
|
||||
ServerRecvThread(GuiInterface &gui, ConfigFile *playerConfig);
|
||||
virtual ~ServerRecvThread();
|
||||
ServerLobbyThread(GuiInterface &gui, ConfigFile *playerConfig);
|
||||
virtual ~ServerLobbyThread();
|
||||
|
||||
void Init(const std::string &pwd, const GameData &gameData);
|
||||
void Init(const std::string &pwd);
|
||||
|
||||
void AddConnection(boost::shared_ptr<ConnectData> data);
|
||||
void AddNotification(unsigned message, const std::string ¶m);
|
||||
|
||||
u_int32_t GetNextUniquePlayerId();
|
||||
ServerCallback &GetCallback();
|
||||
|
||||
void SendError(SOCKET s, int errorCode);
|
||||
void SendToAllPlayers(boost::shared_ptr<NetPacket> packet);
|
||||
void SendToAllButOnePlayers(boost::shared_ptr<NetPacket> packet, SOCKET except);
|
||||
|
||||
Game &GetGame();
|
||||
|
||||
protected:
|
||||
|
||||
struct Notification
|
||||
{
|
||||
Notification(unsigned m, std::string p)
|
||||
: message(m), param(p) {}
|
||||
unsigned message;
|
||||
std::string param;
|
||||
};
|
||||
|
||||
typedef std::deque<boost::shared_ptr<ConnectData> > ConnectQueue;
|
||||
typedef std::map<SOCKET, SessionWrapper> SocketSessionMap;
|
||||
typedef std::list<SessionWrapper> SessionList;
|
||||
typedef std::deque<Notification> NotificationQueue;
|
||||
typedef std::list<std::pair<boost::microsec_timer, boost::shared_ptr<SessionData> > > CloseSessionList;
|
||||
|
||||
// Main function of the thread.
|
||||
virtual void Main();
|
||||
|
||||
void NotificationLoop();
|
||||
void ProcessLoop();
|
||||
void HandleNetPacketInit(SessionWrapper session, const NetPacketInit &tmpPacket);
|
||||
void HandleNetPacketCreateGame(SessionWrapper session, const NetPacketCreateGame &tmpPacket);
|
||||
void HandleNetPacketJoinGame(SessionWrapper session, const NetPacketJoinGame &tmpPacket);
|
||||
void CloseSessionLoop();
|
||||
|
||||
void HandleNewConnection(boost::shared_ptr<ConnectData> connData);
|
||||
|
||||
SOCKET Select();
|
||||
|
||||
void CleanupConnectQueue();
|
||||
void CleanupSessionMap();
|
||||
|
||||
void InternalStartGame();
|
||||
void InternalKickPlayer(unsigned uniqueId);
|
||||
|
||||
SessionWrapper GetSession(SOCKET sock) const;
|
||||
SessionWrapper GetSessionByPlayerName(const std::string playerName) const;
|
||||
SessionWrapper GetSessionByUniquePlayerId(unsigned uniqueId) const;
|
||||
void AddSession(boost::shared_ptr<SessionData> sessionData); // new Sessions have no player data
|
||||
void SessionError(SessionWrapper session, int errorCode);
|
||||
void RejectNewConnection(boost::shared_ptr<ConnectData> connData);
|
||||
void CloseSessionDelayed(SessionWrapper session);
|
||||
void RemoveNotEstablishedSessions();
|
||||
void RemoveDisconnectedPlayers();
|
||||
void SendError(SOCKET s, int errorCode);
|
||||
|
||||
void AddComputerPlayer(boost::shared_ptr<PlayerData> player);
|
||||
void ResetComputerPlayerList();
|
||||
|
||||
size_t GetCurNumberOfPlayers() const;
|
||||
bool IsPlayerConnected(const std::string &playerName) const;
|
||||
bool IsPlayerConnected(unsigned uniquePlayerId) const;
|
||||
void SetSessionPlayerData(boost::shared_ptr<SessionData> sessionData, boost::shared_ptr<PlayerData> playerData);
|
||||
PlayerDataList GetPlayerDataList() const;
|
||||
|
||||
void AssignPlayerNumbers();
|
||||
|
||||
ServerRecvState &GetState();
|
||||
void SetState(ServerRecvState &newState);
|
||||
|
||||
SenderThread &GetSender();
|
||||
ReceiverHelper &GetReceiver();
|
||||
|
||||
const GameData &GetGameData() const;
|
||||
const StartData &GetStartData() const;
|
||||
void SetStartData(const StartData &startData);
|
||||
bool CheckPassword(const std::string &password) const;
|
||||
|
||||
ServerSenderCallback &GetSenderCallback();
|
||||
@@ -143,45 +94,19 @@ private:
|
||||
|
||||
ConnectQueue m_connectQueue;
|
||||
mutable boost::mutex m_connectQueueMutex;
|
||||
ServerRecvState *m_curState;
|
||||
|
||||
NotificationQueue m_notificationQueue;
|
||||
mutable boost::mutex m_notificationQueueMutex;
|
||||
|
||||
SocketSessionMap m_sessionMap;
|
||||
mutable boost::mutex m_sessionMapMutex;
|
||||
|
||||
PlayerDataList m_computerPlayers;
|
||||
SessionManager m_sessionManager;
|
||||
|
||||
CloseSessionList m_closeSessionList;
|
||||
|
||||
std::auto_ptr<ReceiverHelper> m_receiver;
|
||||
std::auto_ptr<SenderThread> m_sender;
|
||||
|
||||
std::auto_ptr<Game> m_game;
|
||||
GameData m_gameData;
|
||||
StartData m_startData;
|
||||
unsigned m_curGameId;
|
||||
std::auto_ptr<ServerSenderCallback> m_senderCallback;
|
||||
|
||||
std::string m_password;
|
||||
|
||||
GuiInterface &m_gui;
|
||||
|
||||
std::string m_password;
|
||||
ConfigFile *m_playerConfig;
|
||||
|
||||
friend class ServerRecvStateInit;
|
||||
friend class AbstractServerRecvStateReceiving;
|
||||
friend class AbstractServerRecvStateRunning;
|
||||
friend class ServerRecvStateStartGame;
|
||||
friend class ServerRecvStateStartHand;
|
||||
friend class ServerRecvStateStartRound;
|
||||
friend class ServerRecvStateWaitPlayerAction;
|
||||
friend class ServerRecvStateComputerAction;
|
||||
friend class ServerRecvStateShowCardsDelay;
|
||||
friend class ServerRecvStateDealCardsDelay;
|
||||
friend class ServerRecvStateNextHandDelay;
|
||||
friend class ServerRecvStateNextGameDelay;
|
||||
u_int32_t m_curUniquePlayerId;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
#ifndef _SESSIONDATA_H_
|
||||
#define _SESSIONDATA_H_
|
||||
|
||||
#include <playerdata.h>
|
||||
#include <net/socket_helper.h>
|
||||
#include <string>
|
||||
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
/***************************************************************************
|
||||
* 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. *
|
||||
***************************************************************************/
|
||||
/* Thread safe server session manager. */
|
||||
|
||||
#ifndef _SESSIONMANAGER_H_
|
||||
#define _SESSIONMANAGER_H_
|
||||
|
||||
#include <net/sessiondata.h>
|
||||
#include <playerdata.h>
|
||||
#include <core/thread.h>
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
class SenderThread;
|
||||
class NetPacket;
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
class SessionManager
|
||||
{
|
||||
public:
|
||||
SessionManager();
|
||||
virtual ~SessionManager();
|
||||
|
||||
void AddSession(boost::shared_ptr<SessionData> sessionData); // new Sessions have no player data
|
||||
void SetSessionPlayerData(SOCKET session, boost::shared_ptr<PlayerData> playerData);
|
||||
void RemoveSession(SOCKET session);
|
||||
|
||||
SessionWrapper Select(unsigned timeoutMsec);
|
||||
SessionWrapper GetSessionByPlayerName(const std::string playerName) const;
|
||||
SessionWrapper GetSessionByUniquePlayerId(unsigned uniqueId) const;
|
||||
|
||||
void Clear();
|
||||
unsigned GetRawSessionCount();
|
||||
|
||||
void SendToAllSessions(SenderThread &sender, boost::shared_ptr<NetPacket> packet);
|
||||
void SendToAllButOneSessions(SenderThread &sender, boost::shared_ptr<NetPacket> packet, SOCKET except);
|
||||
|
||||
protected:
|
||||
|
||||
typedef std::map<SOCKET, SessionWrapper> SessionMap;
|
||||
|
||||
private:
|
||||
|
||||
SessionMap m_sessionMap;
|
||||
mutable boost::mutex m_sessionMapMutex;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -51,12 +51,13 @@
|
||||
#define ERR_NET_INVALID_PLAYER_NAME 107
|
||||
#define ERR_NET_INVALID_PLAYER_CARDS 108
|
||||
#define ERR_NET_INVALID_PLAYER_RESULTS 109
|
||||
#define ERR_NET_INVALID_CHAT_TEXT 110
|
||||
#define ERR_NET_UNKNOWN_PLAYER_ID 111
|
||||
#define ERR_NET_INVALID_ROUND 112
|
||||
#define ERR_NET_PLAYER_KICKED 113
|
||||
#define ERR_NET_INVALID_PLAYER_COUNT 114
|
||||
#define ERR_NET_PLAYER_NOT_IN_GAME 115
|
||||
#define ERR_NET_INVALID_GAME_NAME 110
|
||||
#define ERR_NET_INVALID_CHAT_TEXT 111
|
||||
#define ERR_NET_UNKNOWN_PLAYER_ID 112
|
||||
#define ERR_NET_INVALID_ROUND 113
|
||||
#define ERR_NET_PLAYER_KICKED 114
|
||||
#define ERR_NET_INVALID_PLAYER_COUNT 115
|
||||
#define ERR_NET_PLAYER_NOT_IN_GAME 116
|
||||
|
||||
// This is an internal message which is not reported.
|
||||
#define MSG_SOCK_INTERNAL_PENDING 0
|
||||
|
||||
Reference in New Issue
Block a user