Added new boost timer. Prepared fixes for removing players.
This commit is contained in:
@@ -80,7 +80,7 @@ struct NetPacketJoinGameAckData
|
||||
u_int16_t numberOfPlayers;
|
||||
u_int16_t smallBlind;
|
||||
u_int16_t handsBeforeRaise;
|
||||
u_int16_t reserved;
|
||||
u_int16_t proposedGuiSpeed;
|
||||
u_int32_t startCash;
|
||||
};
|
||||
|
||||
@@ -443,6 +443,7 @@ NetPacketJoinGameAck::SetData(const NetPacketJoinGameAck::Data &inData)
|
||||
tmpData->numberOfPlayers = htons(inData.gameData.numberOfPlayers);
|
||||
tmpData->smallBlind = htons(inData.gameData.smallBlind);
|
||||
tmpData->handsBeforeRaise = htons(inData.gameData.handsBeforeRaise);
|
||||
tmpData->proposedGuiSpeed = htons(inData.gameData.guiSpeed);
|
||||
tmpData->startCash = htonl(inData.gameData.startCash);
|
||||
}
|
||||
|
||||
@@ -458,6 +459,7 @@ NetPacketJoinGameAck::GetData(NetPacketJoinGameAck::Data &outData) const
|
||||
outData.gameData.numberOfPlayers = ntohs(tmpData->numberOfPlayers);
|
||||
outData.gameData.smallBlind = ntohs(tmpData->smallBlind);
|
||||
outData.gameData.handsBeforeRaise = ntohs(tmpData->handsBeforeRaise);
|
||||
outData.gameData.guiSpeed = ntohs(tmpData->proposedGuiSpeed);
|
||||
outData.gameData.startCash = ntohl(tmpData->startCash);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,9 +23,12 @@
|
||||
#include <net/senderthread.h>
|
||||
#include <net/netpacket.h>
|
||||
#include <net/socket_msg.h>
|
||||
#include <net/netexception.h>
|
||||
#include <core/rand.h>
|
||||
#include <gamedata.h>
|
||||
|
||||
#include <core/boost/timer.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define SERVER_WAIT_TIMEOUT_MSEC 50
|
||||
@@ -47,6 +50,9 @@ ServerRecvStateInit::Instance()
|
||||
ServerRecvStateInit::ServerRecvStateInit()
|
||||
: m_curUniquePlayerId(0)
|
||||
{
|
||||
boost::microsec_timer::time_duration_type dur;
|
||||
boost::microsec_timer t(dur);
|
||||
t.start();
|
||||
}
|
||||
|
||||
ServerRecvStateInit::~ServerRecvStateInit()
|
||||
@@ -62,8 +68,8 @@ ServerRecvStateInit::HandleNewConnection(ServerRecvThread &server, boost::shared
|
||||
RandomBytes((unsigned char *)&sessionId, sizeof(sessionId)); // TODO: check for collisions.
|
||||
|
||||
// Create a new session.
|
||||
boost::shared_ptr<SessionData> sessionData(new SessionData(sessionId));
|
||||
server.AddSession(connData, sessionData);
|
||||
boost::shared_ptr<SessionData> sessionData(new SessionData(connData->ReleaseSocket(), sessionId));
|
||||
server.AddSession(sessionData);
|
||||
}
|
||||
|
||||
int
|
||||
@@ -74,8 +80,19 @@ ServerRecvStateInit::Process(ServerRecvThread &server)
|
||||
|
||||
if (recvSock != INVALID_SOCKET)
|
||||
{
|
||||
boost::shared_ptr<NetPacket> packet = server.GetReceiver().Recv(recvSock);
|
||||
boost::shared_ptr<SessionData> session = server.GetSession(recvSock);
|
||||
boost::shared_ptr<NetPacket> packet;
|
||||
try
|
||||
{
|
||||
packet = server.GetReceiver().Recv(recvSock);
|
||||
} catch (const NetException &e)
|
||||
{
|
||||
if (session.get())
|
||||
{
|
||||
//server.CloseSessionDelayed(session);
|
||||
return retVal;
|
||||
}
|
||||
}
|
||||
|
||||
// Ignore if no session / no packet.
|
||||
if (packet.get() && session.get())
|
||||
@@ -83,7 +100,7 @@ ServerRecvStateInit::Process(ServerRecvThread &server)
|
||||
// Session should be in initial state.
|
||||
if (session->GetState() != SessionData::Init)
|
||||
{
|
||||
server.SendError(ERR_SOCK_INVALID_STATE, recvSock);
|
||||
//server.SessionError(session, ERR_SOCK_INVALID_STATE);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
@@ -91,7 +108,7 @@ ServerRecvStateInit::Process(ServerRecvThread &server)
|
||||
const NetPacketJoinGame *tmpPacket = packet->ToNetPacketJoinGame();
|
||||
if (!tmpPacket)
|
||||
{
|
||||
server.SendError(ERR_SOCK_INVALID_PACKET, recvSock);
|
||||
//server.SessionError(session, ERR_SOCK_INVALID_PACKET);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
@@ -101,38 +118,30 @@ ServerRecvStateInit::Process(ServerRecvThread &server)
|
||||
// Check the protocol version.
|
||||
if (joinGameData.versionMajor != NET_VERSION_MAJOR)
|
||||
{
|
||||
server.SendError(ERR_NET_VERSION_NOT_SUPPORTED, recvSock);
|
||||
//server.SessionError(session, ERR_NET_VERSION_NOT_SUPPORTED);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
// Check the server password.
|
||||
if (!server.CheckPassword(joinGameData.password))
|
||||
{
|
||||
server.SendError(ERR_NET_INVALID_PASSWORD, recvSock);
|
||||
//server.SessionError(session, ERR_NET_INVALID_PASSWORD);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
PlayerDataList &playerDataList = server.GetPlayerDataList();
|
||||
size_t curNumPlayers = server.GetCurNumberOfPlayers();
|
||||
|
||||
// Check the number of players.
|
||||
if (playerDataList.size() >= (size_t)server.GetGameData().numberOfPlayers)
|
||||
if (curNumPlayers >= (size_t)server.GetGameData().numberOfPlayers)
|
||||
{
|
||||
server.SendError(ERR_NET_SERVER_FULL, recvSock);
|
||||
//server.SessionError(session, ERR_NET_SERVER_FULL);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
// Check whether this player is already connected.
|
||||
PlayerDataList::const_iterator player_i = playerDataList.begin();
|
||||
PlayerDataList::const_iterator player_end = playerDataList.end();
|
||||
while (player_i != player_end)
|
||||
if (server.IsPlayerConnected(joinGameData.playerName))
|
||||
{
|
||||
if ((*player_i)->GetName() == joinGameData.playerName)
|
||||
break;
|
||||
++player_i;
|
||||
}
|
||||
if (player_i != player_end)
|
||||
{
|
||||
server.SendError(ERR_NET_PLAYER_NAME_IN_USE, recvSock);
|
||||
//server.SessionError(session, ERR_NET_PLAYER_NAME_IN_USE);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
@@ -148,7 +157,7 @@ ServerRecvStateInit::Process(ServerRecvThread &server)
|
||||
boost::shared_ptr<NetPacket> answer(new NetPacketJoinGameAck);
|
||||
NetPacketJoinGameAck::Data joinGameAckData;
|
||||
joinGameAckData.playerId = tmpPlayerData->GetUniqueId();
|
||||
joinGameAckData.playerNumber = playerDataList.size();
|
||||
joinGameAckData.playerNumber = 0;//playerDataList.size();
|
||||
joinGameAckData.sessionId = session->GetId(); // TODO: currently unused.
|
||||
joinGameAckData.gameData = server.GetGameData();
|
||||
static_cast<NetPacketJoinGameAck *>(answer.get())->SetData(joinGameAckData);
|
||||
@@ -156,7 +165,7 @@ ServerRecvStateInit::Process(ServerRecvThread &server)
|
||||
session->SetState(SessionData::Established);
|
||||
|
||||
// Store player data in list.
|
||||
playerDataList.push_back(tmpPlayerData);
|
||||
//playerDataList.push_back(tmpPlayerData);
|
||||
}
|
||||
}
|
||||
return retVal;
|
||||
|
||||
@@ -36,7 +36,9 @@ public:
|
||||
|
||||
virtual void SignalNetError(SOCKET sock, int errorID, int osErrorID)
|
||||
{
|
||||
// TODO
|
||||
// We just ignore send errors for now, on server side.
|
||||
// A send error should trigger a read error or a read
|
||||
// returning 0 afterwards, and we will handle this error.
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -247,17 +249,7 @@ ServerRecvThread::CleanupSessionMap()
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
// We need to manually close all sockets for the sessions.
|
||||
// This is "not great", but there are some issues when
|
||||
// automatically closing them.
|
||||
SocketSessionMap::iterator i = m_sessionMap.begin();
|
||||
SocketSessionMap::iterator end = m_sessionMap.end();
|
||||
|
||||
while (i != end)
|
||||
{
|
||||
CLOSESOCKET(i->first);
|
||||
++i;
|
||||
}
|
||||
// Sockets will be closed automatically.
|
||||
m_sessionMap.clear();
|
||||
}
|
||||
|
||||
@@ -289,19 +281,19 @@ ServerRecvThread::GetSession(SOCKET sock)
|
||||
}
|
||||
|
||||
void
|
||||
ServerRecvThread::AddSession(boost::shared_ptr<ConnectData> connData, boost::shared_ptr<SessionData> sessionData)
|
||||
ServerRecvThread::AddSession(boost::shared_ptr<SessionData> sessionData)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
|
||||
SocketSessionMap::iterator pos = m_sessionMap.lower_bound(connData->GetSocket());
|
||||
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() && connData->GetSocket() == pos->first)
|
||||
if (pos != m_sessionMap.end() && sessionData->GetSocket() == pos->first)
|
||||
{
|
||||
throw ServerException(ERR_SOCK_CONN_EXISTS, 0);
|
||||
}
|
||||
m_sessionMap.insert(pos, SocketSessionMap::value_type(connData->ReleaseSocket(), sessionData));
|
||||
m_sessionMap.insert(pos, SocketSessionMap::value_type(sessionData->GetSocket(), sessionData));
|
||||
}
|
||||
|
||||
SenderThread &
|
||||
@@ -331,10 +323,34 @@ ServerRecvThread::CheckPassword(const std::string &password) const
|
||||
return (password == m_password);
|
||||
}
|
||||
|
||||
PlayerDataList &
|
||||
ServerRecvThread::GetPlayerDataList()
|
||||
size_t
|
||||
ServerRecvThread::GetCurNumberOfPlayers() const
|
||||
{
|
||||
return m_playerDataList;
|
||||
boost::mutex::scoped_lock lock(m_sessionMapMutex);
|
||||
return m_sessionMap.size();
|
||||
}
|
||||
|
||||
bool
|
||||
ServerRecvThread::IsPlayerConnected(const std::string &playerName) const
|
||||
{
|
||||
bool retVal = false;
|
||||
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)
|
||||
{
|
||||
const boost::shared_ptr<PlayerData> tmpPlayerData = session_i->second->GetPlayerData();
|
||||
if (tmpPlayerData.get() && tmpPlayerData->GetName() == playerName)
|
||||
{
|
||||
retVal = true;
|
||||
break;
|
||||
}
|
||||
|
||||
++session_i;
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
ServerSenderCallback &
|
||||
|
||||
@@ -19,12 +19,14 @@
|
||||
|
||||
#include <net/sessiondata.h>
|
||||
|
||||
SessionData::SessionData(unsigned id)
|
||||
: m_id(id), m_state(SessionData::Init)
|
||||
SessionData::SessionData(SOCKET sockfd, unsigned id)
|
||||
: m_sockfd(sockfd), m_id(id), m_state(SessionData::Init)
|
||||
{
|
||||
}
|
||||
|
||||
SessionData::~SessionData()
|
||||
{
|
||||
if (m_sockfd != INVALID_SOCKET)
|
||||
CLOSESOCKET(m_sockfd);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user