Configuration is transferred. A new game should be started on client side, but signal/slot is not working yet. Oh well.
This commit is contained in:
@@ -21,6 +21,8 @@
|
||||
#ifndef _CLIENTCALLBACK_H_
|
||||
#define _CLIENTCALLBACK_H_
|
||||
|
||||
struct GameData;
|
||||
|
||||
class ClientCallback
|
||||
{
|
||||
public:
|
||||
@@ -29,6 +31,8 @@ public:
|
||||
virtual void SignalNetClientConnect(int actionID) = 0;
|
||||
virtual void SignalNetClientGameInfo(int actionID) = 0;
|
||||
virtual void SignalNetClientError(int errorID, int osErrorID) = 0;
|
||||
|
||||
virtual void SignalNetClientGameStart(const GameData &gameData) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -31,6 +31,7 @@ class ClientState;
|
||||
class SenderThread;
|
||||
class ReceiverHelper;
|
||||
class ClientSenderCallback;
|
||||
struct GameData;
|
||||
|
||||
class ClientThread : public Thread
|
||||
{
|
||||
@@ -64,6 +65,9 @@ protected:
|
||||
SenderThread &GetSender();
|
||||
ReceiverHelper &GetReceiver();
|
||||
|
||||
const GameData &GetGameData() const;
|
||||
void SetGameData(const GameData &gameData);
|
||||
|
||||
ClientSenderCallback &GetSenderCallback();
|
||||
|
||||
private:
|
||||
@@ -72,9 +76,12 @@ private:
|
||||
std::auto_ptr<ClientSenderCallback> m_senderCallback;
|
||||
ClientState *m_curState;
|
||||
ClientCallback &m_callback;
|
||||
|
||||
std::auto_ptr<SenderThread> m_sender;
|
||||
std::auto_ptr<ReceiverHelper> m_receiver;
|
||||
|
||||
std::auto_ptr<GameData> m_gameData;
|
||||
|
||||
friend class ClientStateInit;
|
||||
friend class ClientStateStartResolve;
|
||||
friend class ClientStateResolving;
|
||||
|
||||
@@ -366,6 +366,11 @@ ClientStateWaitSession::Process(ClientThread &client)
|
||||
|
||||
if (tmpPacket.get() && tmpPacket->ToNetPacketJoinGameAck())
|
||||
{
|
||||
// Initialise game configuration.
|
||||
NetPacketJoinGameAck::Data joinGameAckData;
|
||||
tmpPacket->ToNetPacketJoinGameAck()->GetData(joinGameAckData);
|
||||
client.SetGameData(joinGameAckData.gameData);
|
||||
|
||||
client.SetState(ClientStateWaitGame::Instance());
|
||||
retVal = MSG_SOCK_SESSION_DONE;
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
#include <net/receiverhelper.h>
|
||||
#include <net/clientexception.h>
|
||||
#include <net/socket_msg.h>
|
||||
|
||||
#include <gamedata.h>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
@@ -57,6 +57,7 @@ ClientThread::ClientThread(ClientCallback &cb)
|
||||
m_senderCallback.reset(new ClientSenderCallback(*this));
|
||||
m_sender.reset(new SenderThread(GetSenderCallback()));
|
||||
m_receiver.reset(new ReceiverHelper);
|
||||
m_gameData.reset(new GameData);
|
||||
}
|
||||
|
||||
ClientThread::~ClientThread()
|
||||
@@ -103,6 +104,10 @@ ClientThread::Main()
|
||||
GetCallback().SignalNetClientConnect(msg);
|
||||
else
|
||||
GetCallback().SignalNetClientGameInfo(msg);
|
||||
|
||||
// Additionally signal the start of the game.
|
||||
if (msg == MSG_SOCK_GAME_START)
|
||||
GetCallback().SignalNetClientGameStart(GetGameData());
|
||||
}
|
||||
}
|
||||
} catch (const NetException &e)
|
||||
@@ -154,6 +159,20 @@ ClientThread::GetReceiver()
|
||||
return *m_receiver;
|
||||
}
|
||||
|
||||
const GameData &
|
||||
ClientThread::GetGameData() const
|
||||
{
|
||||
assert(m_gameData.get());
|
||||
return *m_gameData;
|
||||
}
|
||||
|
||||
void
|
||||
ClientThread::SetGameData(const GameData &gameData)
|
||||
{
|
||||
assert(m_gameData.get());
|
||||
*m_gameData = gameData;
|
||||
}
|
||||
|
||||
ClientSenderCallback &
|
||||
ClientThread::GetSenderCallback()
|
||||
{
|
||||
|
||||
@@ -78,7 +78,7 @@ struct NetPacketJoinGameAckData
|
||||
u_int16_t numberOfPlayers;
|
||||
u_int16_t smallBlind;
|
||||
u_int16_t handsBeforeRaise;
|
||||
u_int16_t gameSpeed;
|
||||
u_int16_t reserved;
|
||||
u_int32_t startCash;
|
||||
};
|
||||
|
||||
@@ -416,11 +416,10 @@ NetPacketJoinGameAck::SetData(const NetPacketJoinGameAck::Data &inData)
|
||||
tmpData->sessionId = htonl(inData.sessionId);
|
||||
tmpData->playerId = htons(inData.playerId);
|
||||
tmpData->playerNumber = htons(inData.playerNumber);
|
||||
tmpData->numberOfPlayers = htons(inData.numberOfPlayers);
|
||||
tmpData->smallBlind = htons(inData.smallBlind);
|
||||
tmpData->handsBeforeRaise = htons(inData.handsBeforeRaise);
|
||||
tmpData->gameSpeed = htons(inData.gameSpeed);
|
||||
tmpData->startCash = htonl(inData.startCash);
|
||||
tmpData->numberOfPlayers = htons(inData.gameData.numberOfPlayers);
|
||||
tmpData->smallBlind = htons(inData.gameData.smallBlind);
|
||||
tmpData->handsBeforeRaise = htons(inData.gameData.handsBeforeRaise);
|
||||
tmpData->startCash = htonl(inData.gameData.startCash);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -429,14 +428,13 @@ NetPacketJoinGameAck::GetData(NetPacketJoinGameAck::Data &outData) const
|
||||
NetPacketJoinGameAckData *tmpData = (NetPacketJoinGameAckData *)GetRawData();
|
||||
assert(tmpData);
|
||||
|
||||
outData.sessionId = ntohl(tmpData->sessionId);
|
||||
outData.playerId = ntohs(tmpData->playerId);
|
||||
outData.playerNumber = ntohs(tmpData->playerNumber);
|
||||
outData.numberOfPlayers = ntohs(tmpData->numberOfPlayers);
|
||||
outData.smallBlind = ntohs(tmpData->smallBlind);
|
||||
outData.handsBeforeRaise = ntohs(tmpData->handsBeforeRaise);
|
||||
outData.gameSpeed = ntohs(tmpData->gameSpeed);
|
||||
outData.startCash = ntohl(tmpData->startCash);
|
||||
outData.sessionId = ntohl(tmpData->sessionId);
|
||||
outData.playerId = ntohs(tmpData->playerId);
|
||||
outData.playerNumber = ntohs(tmpData->playerNumber);
|
||||
outData.gameData.numberOfPlayers = ntohs(tmpData->numberOfPlayers);
|
||||
outData.gameData.smallBlind = ntohs(tmpData->smallBlind);
|
||||
outData.gameData.handsBeforeRaise = ntohs(tmpData->handsBeforeRaise);
|
||||
outData.gameData.startCash = ntohl(tmpData->startCash);
|
||||
}
|
||||
|
||||
const NetPacketJoinGameAck *
|
||||
|
||||
@@ -23,6 +23,8 @@
|
||||
#include <net/senderthread.h>
|
||||
#include <net/netpacket.h>
|
||||
#include <net/socket_msg.h>
|
||||
#include <gamedata.h>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -43,6 +45,7 @@ ServerRecvStateInit::Instance()
|
||||
}
|
||||
|
||||
ServerRecvStateInit::ServerRecvStateInit()
|
||||
: m_curUniquePlayerId(0)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -76,16 +79,54 @@ ServerRecvStateInit::Process(ServerRecvThread &server)
|
||||
const NetPacketJoinGame *tmpPacket = packet->ToNetPacketJoinGame();
|
||||
if (tmpPacket)
|
||||
{
|
||||
// TODO: check password
|
||||
NetPacketJoinGame::Data playerData;
|
||||
tmpPacket->GetData(playerData);
|
||||
server.GetCallback().SignalNetServerPlayerJoined(playerData.playerName);
|
||||
boost::shared_ptr<NetPacket> answer(new NetPacketJoinGameAck);
|
||||
NetPacketJoinGameAck::Data joinGameAckData;
|
||||
// TODO: set data.
|
||||
static_cast<NetPacketJoinGameAck *>(answer.get())->SetData(joinGameAckData);
|
||||
server.GetSender().Send(answer, recvSock);
|
||||
session->SetState(SessionData::Established);
|
||||
NetPacketJoinGame::Data joinGameData;
|
||||
tmpPacket->GetData(joinGameData);
|
||||
// Check the server password.
|
||||
if (server.CheckPassword(joinGameData.password))
|
||||
{
|
||||
PlayerDataList &playerDataList = server.GetPlayerDataList();
|
||||
// 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 ((*player_i)->GetName() == joinGameData.playerName)
|
||||
break;
|
||||
++player_i;
|
||||
}
|
||||
if (player_i == player_end)
|
||||
{
|
||||
// Create player data object.
|
||||
boost::shared_ptr<PlayerData> tmpPlayerData(new PlayerData(m_curUniquePlayerId++));
|
||||
tmpPlayerData->SetName(joinGameData.playerName);
|
||||
tmpPlayerData->SetPlayerType(joinGameData.ptype);
|
||||
|
||||
// Signal joining player to GUI.
|
||||
server.GetCallback().SignalNetServerPlayerJoined(tmpPlayerData->GetName());
|
||||
|
||||
// Send ACK to client.
|
||||
boost::shared_ptr<NetPacket> answer(new NetPacketJoinGameAck);
|
||||
NetPacketJoinGameAck::Data joinGameAckData;
|
||||
joinGameAckData.playerId = tmpPlayerData->GetUniqueId();
|
||||
joinGameAckData.playerNumber = playerDataList.size();
|
||||
joinGameAckData.sessionId = session->GetId(); // TODO: currently unused.
|
||||
joinGameAckData.gameData = server.GetGameData();
|
||||
static_cast<NetPacketJoinGameAck *>(answer.get())->SetData(joinGameAckData);
|
||||
server.GetSender().Send(answer, recvSock);
|
||||
session->SetState(SessionData::Established);
|
||||
|
||||
// Store player data in list.
|
||||
playerDataList.push_back(tmpPlayerData);
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO send error message, duplicate name
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO send error message, invalid password
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -309,18 +309,24 @@ ServerRecvThread::GetReceiver()
|
||||
}
|
||||
|
||||
const GameData &
|
||||
ServerRecvThread::GetGameData()
|
||||
ServerRecvThread::GetGameData() const
|
||||
{
|
||||
assert(m_gameData.get());
|
||||
return *m_gameData;
|
||||
}
|
||||
|
||||
bool
|
||||
ServerRecvThread::CheckPassword(const std::string &password)
|
||||
ServerRecvThread::CheckPassword(const std::string &password) const
|
||||
{
|
||||
return (password == m_password);
|
||||
}
|
||||
|
||||
PlayerDataList &
|
||||
ServerRecvThread::GetPlayerDataList()
|
||||
{
|
||||
return m_playerDataList;
|
||||
}
|
||||
|
||||
ServerSenderCallback &
|
||||
ServerRecvThread::GetSenderCallback()
|
||||
{
|
||||
|
||||
+2
-5
@@ -22,6 +22,7 @@
|
||||
#define _NETPACKET_H_
|
||||
|
||||
#include <playerdata.h>
|
||||
#include <gamedata.h>
|
||||
#include <net/socket_helper.h>
|
||||
|
||||
#define MIN_PACKET_SIZE 4
|
||||
@@ -112,11 +113,7 @@ public:
|
||||
u_int32_t sessionId;
|
||||
u_int16_t playerId;
|
||||
u_int16_t playerNumber;
|
||||
u_int16_t numberOfPlayers;
|
||||
u_int16_t smallBlind;
|
||||
u_int16_t handsBeforeRaise;
|
||||
u_int16_t gameSpeed;
|
||||
u_int32_t startCash;
|
||||
GameData gameData;
|
||||
};
|
||||
|
||||
NetPacketJoinGameAck();
|
||||
|
||||
@@ -21,8 +21,8 @@
|
||||
#ifndef _SERVERRECVSTATE_H_
|
||||
#define _SERVERRECVSTATE_H_
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <net/connectdata.h>
|
||||
#include <playerdata.h>
|
||||
|
||||
#define SERVER_INITIAL_STATE ServerRecvStateInit
|
||||
#define SERVER_START_GAME_STATE ServerRecvStateStartGame
|
||||
@@ -61,6 +61,10 @@ protected:
|
||||
|
||||
// Protected constructor - this is a singleton.
|
||||
ServerRecvStateInit();
|
||||
|
||||
private:
|
||||
|
||||
unsigned m_curUniquePlayerId;
|
||||
};
|
||||
|
||||
// State: Start server game.
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include <net/connectdata.h>
|
||||
#include <net/sessiondata.h>
|
||||
#include <net/servercallback.h>
|
||||
#include <playerdata.h>
|
||||
|
||||
#define RECEIVER_THREAD_TERMINATE_TIMEOUT 200
|
||||
|
||||
@@ -82,8 +83,10 @@ protected:
|
||||
SenderThread &GetSender();
|
||||
ReceiverHelper &GetReceiver();
|
||||
|
||||
const GameData &GetGameData();
|
||||
bool CheckPassword(const std::string &password);
|
||||
const GameData &GetGameData() const;
|
||||
bool CheckPassword(const std::string &password) const;
|
||||
|
||||
PlayerDataList &GetPlayerDataList();
|
||||
|
||||
ServerSenderCallback &GetSenderCallback();
|
||||
|
||||
@@ -105,6 +108,7 @@ private:
|
||||
std::auto_ptr<ServerSenderCallback> m_senderCallback;
|
||||
std::auto_ptr<GameData> m_gameData;
|
||||
|
||||
PlayerDataList m_playerDataList;
|
||||
std::string m_password;
|
||||
|
||||
ServerCallback &m_callback;
|
||||
|
||||
Reference in New Issue
Block a user