Major design change in network protocol: Player information is now requested by clients whenever it is needed. The server only sends player ids. The client has a cache with player information.
This commit is contained in:
@@ -42,6 +42,7 @@ public:
|
||||
virtual void SignalNetClientGameStart(boost::shared_ptr<Game> game) = 0;
|
||||
virtual void SignalNetClientSelfJoined(const std::string &playerName, PlayerRights rights) = 0;
|
||||
virtual void SignalNetClientPlayerJoined(const std::string &playerName, PlayerRights rights) = 0;
|
||||
virtual void SignalNetClientPlayerChanged(const std::string &oldPlayerName, const std::string &newPlayerName) = 0;
|
||||
virtual void SignalNetClientPlayerLeft(const std::string &playerName) = 0;
|
||||
|
||||
virtual void SignalNetClientChatMsg(const std::string &playerName, const std::string &msg) = 0;
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
#include <gamedata.h>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
class ClientContext;
|
||||
class ClientState;
|
||||
@@ -68,6 +68,7 @@ public:
|
||||
protected:
|
||||
typedef std::map<unsigned, std::string> GameMap;
|
||||
typedef std::list<boost::shared_ptr<NetPacket> > NetPacketList;
|
||||
typedef std::map<unsigned, PlayerInfo> PlayerInfoMap;
|
||||
|
||||
// Main function of the thread.
|
||||
virtual void Main();
|
||||
@@ -75,6 +76,10 @@ protected:
|
||||
void AddPacket(boost::shared_ptr<NetPacket> packet);
|
||||
void SendPacketLoop();
|
||||
|
||||
PlayerInfo GetCachedPlayerInfo(unsigned id) const;
|
||||
void RequestPlayerInfo(unsigned id, const PlayerInfo &tempInfo);
|
||||
void SetPlayerInfo(unsigned id, const PlayerInfo &info);
|
||||
|
||||
const ClientContext &GetContext() const;
|
||||
ClientContext &GetContext();
|
||||
|
||||
@@ -132,6 +137,7 @@ private:
|
||||
mutable boost::mutex m_gameMapMutex;
|
||||
|
||||
boost::shared_ptr<Game> m_game;
|
||||
PlayerInfoMap m_playerInfoMap;
|
||||
|
||||
unsigned m_curGameId;
|
||||
unsigned m_guiPlayerId;
|
||||
|
||||
@@ -31,6 +31,8 @@
|
||||
#include <game.h>
|
||||
#include <playerinterface.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define CLIENT_WAIT_TIMEOUT_MSEC 50
|
||||
@@ -378,7 +380,13 @@ AbstractClientStateReceiving::Process(ClientThread &client)
|
||||
|
||||
if (tmpPacket.get())
|
||||
{
|
||||
if (tmpPacket->ToNetPacketError())
|
||||
if (tmpPacket->ToNetPacketPlayerInfo())
|
||||
{
|
||||
NetPacketPlayerInfo::Data infoData;
|
||||
tmpPacket->ToNetPacketPlayerInfo()->GetData(infoData);
|
||||
client.SetPlayerInfo(infoData.playerId, infoData.playerInfo);
|
||||
}
|
||||
else if (tmpPacket->ToNetPacketError())
|
||||
{
|
||||
// Server reported an error.
|
||||
NetPacketError::Data errorData;
|
||||
@@ -564,9 +572,27 @@ ClientStateWaitGame::InternalProcess(ClientThread &client, boost::shared_ptr<Net
|
||||
NetPacketPlayerJoined::Data netPlayerData;
|
||||
packet->ToNetPacketPlayerJoined()->GetData(netPlayerData);
|
||||
|
||||
boost::shared_ptr<PlayerData> playerData(
|
||||
new PlayerData(netPlayerData.playerId, 0, netPlayerData.ptype, netPlayerData.prights));
|
||||
playerData->SetName(netPlayerData.playerName);
|
||||
boost::shared_ptr<PlayerData> playerData;
|
||||
try
|
||||
{
|
||||
PlayerInfo info = client.GetCachedPlayerInfo(netPlayerData.playerId);
|
||||
playerData.reset(
|
||||
new PlayerData(netPlayerData.playerId, 0, info.ptype, netPlayerData.prights));
|
||||
playerData->SetName(info.playerName);
|
||||
} catch (const NetException &)
|
||||
{
|
||||
ostringstream name;
|
||||
name << "#" << netPlayerData.playerId;
|
||||
|
||||
// Request player info.
|
||||
PlayerInfo info;
|
||||
info.playerName = name.str();
|
||||
client.RequestPlayerInfo(netPlayerData.playerId, info);
|
||||
// Use temporary data until the PlayerInfo request is completed.
|
||||
playerData.reset(
|
||||
new PlayerData(netPlayerData.playerId, 0, info.ptype, netPlayerData.prights));
|
||||
playerData->SetName(info.playerName);
|
||||
}
|
||||
client.AddPlayerData(playerData);
|
||||
}
|
||||
|
||||
|
||||
@@ -300,6 +300,53 @@ ClientThread::SendPacketLoop()
|
||||
}
|
||||
}
|
||||
|
||||
PlayerInfo
|
||||
ClientThread::GetCachedPlayerInfo(unsigned id) const
|
||||
{
|
||||
PlayerInfoMap::const_iterator pos = m_playerInfoMap.find(id);
|
||||
if (pos == m_playerInfoMap.end())
|
||||
throw NetException(ERR_NET_UNKNOWN_PLAYER_ID, 0);
|
||||
return pos->second;
|
||||
}
|
||||
|
||||
void
|
||||
ClientThread::RequestPlayerInfo(unsigned id, const PlayerInfo &tempInfo)
|
||||
{
|
||||
boost::shared_ptr<NetPacket> req(new NetPacketRetrievePlayerInfo);
|
||||
NetPacketRetrievePlayerInfo::Data reqData;
|
||||
reqData.playerId = id;
|
||||
static_cast<NetPacketRetrievePlayerInfo *>(req.get())->SetData(reqData);
|
||||
GetSender().Send(GetContext().GetSocket(), req);
|
||||
|
||||
m_playerInfoMap[id] = tempInfo;
|
||||
}
|
||||
|
||||
void
|
||||
ClientThread::SetPlayerInfo(unsigned id, const PlayerInfo &info)
|
||||
{
|
||||
PlayerInfoMap::iterator pos = m_playerInfoMap.find(id);
|
||||
|
||||
// Update info cache.
|
||||
if (pos != m_playerInfoMap.end())
|
||||
{
|
||||
GetCallback().SignalNetClientPlayerChanged(pos->second.playerName, info.playerName);
|
||||
|
||||
pos->second = info;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_playerInfoMap.insert(PlayerInfoMap::value_type(id, info));
|
||||
}
|
||||
|
||||
// Update player data for current game.
|
||||
boost::shared_ptr<PlayerData> playerData = GetPlayerDataByUniqueId(id);
|
||||
if (playerData.get())
|
||||
{
|
||||
playerData->SetName(info.playerName);
|
||||
playerData->SetType(info.ptype);
|
||||
}
|
||||
}
|
||||
|
||||
const ClientContext &
|
||||
ClientThread::GetContext() const
|
||||
{
|
||||
|
||||
+384
-61
@@ -31,26 +31,30 @@ using namespace std;
|
||||
#define NET_TYPE_INIT_ACK 0x0002
|
||||
#define NET_TYPE_GAME_LIST_NEW 0x0003
|
||||
#define NET_TYPE_GAME_LIST_UPDATE 0x0004
|
||||
#define NET_TYPE_CREATE_GAME 0x0005
|
||||
#define NET_TYPE_JOIN_GAME 0x0006
|
||||
#define NET_TYPE_JOIN_GAME_ACK 0x0007
|
||||
#define NET_TYPE_PLAYER_JOINED 0x0008
|
||||
#define NET_TYPE_PLAYER_LEFT 0x0009
|
||||
#define NET_TYPE_KICK_PLAYER 0x000A
|
||||
#define NET_TYPE_START_EVENT 0x000B
|
||||
#define NET_TYPE_GAME_START 0x000C
|
||||
#define NET_TYPE_HAND_START 0x000D
|
||||
#define NET_TYPE_PLAYERS_TURN 0x000E
|
||||
#define NET_TYPE_PLAYERS_ACTION 0x000F
|
||||
#define NET_TYPE_PLAYERS_ACTION_DONE 0x0010
|
||||
#define NET_TYPE_PLAYERS_ACTION_REJECTED 0x0011
|
||||
#define NET_TYPE_DEAL_FLOP_CARDS 0x0012
|
||||
#define NET_TYPE_DEAL_TURN_CARD 0x0013
|
||||
#define NET_TYPE_DEAL_RIVER_CARD 0x0014
|
||||
#define NET_TYPE_ALL_IN_SHOW_CARDS 0x0015
|
||||
#define NET_TYPE_END_OF_HAND_SHOW_CARDS 0x0016
|
||||
#define NET_TYPE_END_OF_HAND_HIDE_CARDS 0x0017
|
||||
#define NET_TYPE_END_OF_GAME 0x0018
|
||||
#define NET_TYPE_GAME_LIST_PLAYER_JOINED 0x0005
|
||||
#define NET_TYPE_GAME_LIST_PLAYER_LEFT 0x0006
|
||||
#define NET_TYPE_RETRIEVE_PLAYER_INFO 0x0007
|
||||
#define NET_TYPE_PLAYER_INFO 0x0008
|
||||
#define NET_TYPE_CREATE_GAME 0x0009
|
||||
#define NET_TYPE_JOIN_GAME 0x000A
|
||||
#define NET_TYPE_JOIN_GAME_ACK 0x000B
|
||||
#define NET_TYPE_PLAYER_JOINED 0x000C
|
||||
#define NET_TYPE_PLAYER_LEFT 0x000D
|
||||
#define NET_TYPE_KICK_PLAYER 0x000E
|
||||
#define NET_TYPE_START_EVENT 0x000F
|
||||
#define NET_TYPE_GAME_START 0x0010
|
||||
#define NET_TYPE_HAND_START 0x0011
|
||||
#define NET_TYPE_PLAYERS_TURN 0x0012
|
||||
#define NET_TYPE_PLAYERS_ACTION 0x0013
|
||||
#define NET_TYPE_PLAYERS_ACTION_DONE 0x0014
|
||||
#define NET_TYPE_PLAYERS_ACTION_REJECTED 0x0015
|
||||
#define NET_TYPE_DEAL_FLOP_CARDS 0x0016
|
||||
#define NET_TYPE_DEAL_TURN_CARD 0x0017
|
||||
#define NET_TYPE_DEAL_RIVER_CARD 0x0018
|
||||
#define NET_TYPE_ALL_IN_SHOW_CARDS 0x0019
|
||||
#define NET_TYPE_END_OF_HAND_SHOW_CARDS 0x001A
|
||||
#define NET_TYPE_END_OF_HAND_HIDE_CARDS 0x001B
|
||||
#define NET_TYPE_END_OF_GAME 0x001C
|
||||
|
||||
#define NET_TYPE_SEND_CHAT_TEXT 0x0200
|
||||
#define NET_TYPE_CHAT_TEXT 0x0201
|
||||
@@ -108,6 +112,13 @@ struct GCC_PACKED NetPacketGameListNewData
|
||||
u_int32_t gameId;
|
||||
u_int16_t gameMode;
|
||||
u_int16_t gameNameLength;
|
||||
u_int16_t maxNumberOfPlayers;
|
||||
u_int16_t smallBlind;
|
||||
u_int16_t handsBeforeRaise;
|
||||
u_int16_t proposedGuiSpeed;
|
||||
u_int16_t playerActionTimeout;
|
||||
u_int16_t reserved;
|
||||
u_int32_t startMoney;
|
||||
};
|
||||
|
||||
struct GCC_PACKED NetPacketGameListUpdateData
|
||||
@@ -118,6 +129,34 @@ struct GCC_PACKED NetPacketGameListUpdateData
|
||||
u_int16_t reserved;
|
||||
};
|
||||
|
||||
struct GCC_PACKED NetPacketGameListPlayerJoinedData
|
||||
{
|
||||
NetPacketHeader head;
|
||||
u_int32_t gameId;
|
||||
u_int32_t playerId;
|
||||
};
|
||||
|
||||
struct GCC_PACKED NetPacketGameListPlayerLeftData
|
||||
{
|
||||
NetPacketHeader head;
|
||||
u_int32_t gameId;
|
||||
u_int32_t playerId;
|
||||
};
|
||||
|
||||
struct GCC_PACKED NetPacketRetrievePlayerInfoData
|
||||
{
|
||||
NetPacketHeader head;
|
||||
u_int32_t playerId;
|
||||
};
|
||||
|
||||
struct GCC_PACKED NetPacketPlayerInfoData
|
||||
{
|
||||
NetPacketHeader head;
|
||||
u_int32_t playerId;
|
||||
u_int16_t playerFlags;
|
||||
u_int16_t playerNameLength;
|
||||
};
|
||||
|
||||
struct GCC_PACKED NetPacketCreateGameData
|
||||
{
|
||||
NetPacketHeader head;
|
||||
@@ -157,9 +196,7 @@ struct GCC_PACKED NetPacketPlayerJoinedData
|
||||
{
|
||||
NetPacketHeader head;
|
||||
u_int32_t playerId;
|
||||
u_int16_t playerFlags;
|
||||
u_int16_t playerRights;
|
||||
u_int16_t playerNameLength;
|
||||
u_int16_t reserved;
|
||||
};
|
||||
|
||||
@@ -377,6 +414,18 @@ NetPacket::Create(char *data, unsigned &dataSize)
|
||||
case NET_TYPE_GAME_LIST_UPDATE:
|
||||
tmpPacket = boost::shared_ptr<NetPacket>(new NetPacketGameListUpdate);
|
||||
break;
|
||||
case NET_TYPE_GAME_LIST_PLAYER_JOINED:
|
||||
tmpPacket = boost::shared_ptr<NetPacket>(new NetPacketGameListPlayerJoined);
|
||||
break;
|
||||
case NET_TYPE_GAME_LIST_PLAYER_LEFT:
|
||||
tmpPacket = boost::shared_ptr<NetPacket>(new NetPacketGameListPlayerLeft);
|
||||
break;
|
||||
case NET_TYPE_RETRIEVE_PLAYER_INFO:
|
||||
tmpPacket = boost::shared_ptr<NetPacket>(new NetPacketRetrievePlayerInfo);
|
||||
break;
|
||||
case NET_TYPE_PLAYER_INFO:
|
||||
tmpPacket = boost::shared_ptr<NetPacket>(new NetPacketPlayerInfo);
|
||||
break;
|
||||
case NET_TYPE_CREATE_GAME:
|
||||
tmpPacket = boost::shared_ptr<NetPacket>(new NetPacketCreateGame);
|
||||
break;
|
||||
@@ -554,6 +603,30 @@ NetPacket::ToNetPacketGameListUpdate() const
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const NetPacketGameListPlayerJoined *
|
||||
NetPacket::ToNetPacketGameListPlayerJoined() const
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const NetPacketGameListPlayerLeft *
|
||||
NetPacket::ToNetPacketGameListPlayerLeft() const
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const NetPacketRetrievePlayerInfo *
|
||||
NetPacket::ToNetPacketRetrievePlayerInfo() const
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const NetPacketPlayerInfo *
|
||||
NetPacket::ToNetPacketPlayerInfo() const
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const NetPacketCreateGame *
|
||||
NetPacket::ToNetPacketCreateGame() const
|
||||
{
|
||||
@@ -951,9 +1024,16 @@ NetPacketGameListNew::SetData(const NetPacketGameListNew::Data &inData)
|
||||
NetPacketGameListNewData *tmpData = (NetPacketGameListNewData *)GetRawData();
|
||||
|
||||
// Set the data.
|
||||
tmpData->gameId = htonl(inData.gameId);
|
||||
tmpData->gameMode = htons(inData.gameMode);
|
||||
tmpData->gameNameLength = htons(gameNameLen);
|
||||
tmpData->gameId = htonl(inData.gameId);
|
||||
tmpData->gameMode = htons(inData.gameMode);
|
||||
tmpData->gameNameLength = htons(gameNameLen);
|
||||
tmpData->maxNumberOfPlayers = htons(inData.gameData.maxNumberOfPlayers);
|
||||
tmpData->smallBlind = htons(inData.gameData.smallBlind);
|
||||
tmpData->handsBeforeRaise = htons(inData.gameData.handsBeforeRaise);
|
||||
tmpData->proposedGuiSpeed = htons(inData.gameData.guiSpeed);
|
||||
tmpData->playerActionTimeout = htons(inData.gameData.playerActionTimeoutSec);
|
||||
tmpData->startMoney = htonl(inData.gameData.startMoney);
|
||||
|
||||
char *gameNamePtr = (char *)tmpData + sizeof(NetPacketGameListNewData);
|
||||
memcpy(gameNamePtr, inData.gameName.c_str(), gameNameLen);
|
||||
|
||||
@@ -967,9 +1047,16 @@ NetPacketGameListNew::GetData(NetPacketGameListNew::Data &outData) const
|
||||
// We assume that the data is valid. Validity has already been checked.
|
||||
NetPacketGameListNewData *tmpData = (NetPacketGameListNewData *)GetRawData();
|
||||
|
||||
outData.gameId = ntohl(tmpData->gameId);
|
||||
outData.gameMode = static_cast<GameMode>(ntohs(tmpData->gameMode));
|
||||
u_int16_t gameNameLen = ntohs(tmpData->gameNameLength);
|
||||
outData.gameId = ntohl(tmpData->gameId);
|
||||
outData.gameMode = static_cast<GameMode>(ntohs(tmpData->gameMode));
|
||||
u_int16_t gameNameLen = ntohs(tmpData->gameNameLength);
|
||||
outData.gameData.maxNumberOfPlayers = ntohs(tmpData->maxNumberOfPlayers);
|
||||
outData.gameData.smallBlind = ntohs(tmpData->smallBlind);
|
||||
outData.gameData.handsBeforeRaise = ntohs(tmpData->handsBeforeRaise);
|
||||
outData.gameData.guiSpeed = ntohs(tmpData->proposedGuiSpeed);
|
||||
outData.gameData.playerActionTimeoutSec = ntohs(tmpData->playerActionTimeout);
|
||||
outData.gameData.startMoney = ntohl(tmpData->startMoney);
|
||||
|
||||
char *gameNamePtr = (char *)tmpData + sizeof(NetPacketGameListNewData);
|
||||
outData.gameName = string(gameNamePtr, gameNameLen);
|
||||
}
|
||||
@@ -1069,6 +1156,273 @@ NetPacketGameListUpdate::InternalCheck(const NetPacketHeader* data) const
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
NetPacketGameListPlayerJoined::NetPacketGameListPlayerJoined()
|
||||
: NetPacket(NET_TYPE_GAME_LIST_PLAYER_JOINED, sizeof(NetPacketGameListPlayerJoinedData), sizeof(NetPacketGameListPlayerJoinedData))
|
||||
{
|
||||
}
|
||||
|
||||
NetPacketGameListPlayerJoined::~NetPacketGameListPlayerJoined()
|
||||
{
|
||||
}
|
||||
|
||||
boost::shared_ptr<NetPacket>
|
||||
NetPacketGameListPlayerJoined::Clone() const
|
||||
{
|
||||
boost::shared_ptr<NetPacket> newPacket(new NetPacketGameListPlayerJoined);
|
||||
try
|
||||
{
|
||||
newPacket->SetRawData(GetRawData());
|
||||
} catch (const NetException &)
|
||||
{
|
||||
// Need to return the new packet anyway.
|
||||
}
|
||||
return newPacket;
|
||||
}
|
||||
|
||||
void
|
||||
NetPacketGameListPlayerJoined::SetData(const NetPacketGameListPlayerJoined::Data &inData)
|
||||
{
|
||||
NetPacketGameListPlayerJoinedData *tmpData = (NetPacketGameListPlayerJoinedData *)GetRawData();
|
||||
|
||||
// Set the data.
|
||||
tmpData->gameId = htonl(inData.gameId);
|
||||
tmpData->playerId = htonl(inData.playerId);
|
||||
|
||||
// Check the packet - just in case.
|
||||
Check(GetRawData());
|
||||
}
|
||||
|
||||
void
|
||||
NetPacketGameListPlayerJoined::GetData(NetPacketGameListPlayerJoined::Data &outData) const
|
||||
{
|
||||
// We assume that the data is valid. Validity has already been checked.
|
||||
NetPacketGameListPlayerJoinedData *tmpData = (NetPacketGameListPlayerJoinedData *)GetRawData();
|
||||
|
||||
outData.gameId = ntohl(tmpData->gameId);
|
||||
outData.playerId = ntohl(tmpData->playerId);
|
||||
}
|
||||
|
||||
const NetPacketGameListPlayerJoined *
|
||||
NetPacketGameListPlayerJoined::ToNetPacketGameListPlayerJoined() const
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
void
|
||||
NetPacketGameListPlayerJoined::InternalCheck(const NetPacketHeader* data) const
|
||||
{
|
||||
// Nothing to do.
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
NetPacketGameListPlayerLeft::NetPacketGameListPlayerLeft()
|
||||
: NetPacket(NET_TYPE_GAME_LIST_PLAYER_LEFT, sizeof(NetPacketGameListPlayerLeftData), sizeof(NetPacketGameListPlayerLeftData))
|
||||
{
|
||||
}
|
||||
|
||||
NetPacketGameListPlayerLeft::~NetPacketGameListPlayerLeft()
|
||||
{
|
||||
}
|
||||
|
||||
boost::shared_ptr<NetPacket>
|
||||
NetPacketGameListPlayerLeft::Clone() const
|
||||
{
|
||||
boost::shared_ptr<NetPacket> newPacket(new NetPacketGameListPlayerLeft);
|
||||
try
|
||||
{
|
||||
newPacket->SetRawData(GetRawData());
|
||||
} catch (const NetException &)
|
||||
{
|
||||
// Need to return the new packet anyway.
|
||||
}
|
||||
return newPacket;
|
||||
}
|
||||
|
||||
void
|
||||
NetPacketGameListPlayerLeft::SetData(const NetPacketGameListPlayerLeft::Data &inData)
|
||||
{
|
||||
NetPacketGameListPlayerLeftData *tmpData = (NetPacketGameListPlayerLeftData *)GetRawData();
|
||||
|
||||
// Set the data.
|
||||
tmpData->gameId = htonl(inData.gameId);
|
||||
tmpData->playerId = htonl(inData.playerId);
|
||||
|
||||
// Check the packet - just in case.
|
||||
Check(GetRawData());
|
||||
}
|
||||
|
||||
void
|
||||
NetPacketGameListPlayerLeft::GetData(NetPacketGameListPlayerLeft::Data &outData) const
|
||||
{
|
||||
// We assume that the data is valid. Validity has already been checked.
|
||||
NetPacketGameListPlayerLeftData *tmpData = (NetPacketGameListPlayerLeftData *)GetRawData();
|
||||
|
||||
outData.gameId = ntohl(tmpData->gameId);
|
||||
outData.playerId = ntohl(tmpData->playerId);
|
||||
}
|
||||
|
||||
const NetPacketGameListPlayerLeft *
|
||||
NetPacketGameListPlayerLeft::ToNetPacketGameListPlayerLeft() const
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
void
|
||||
NetPacketGameListPlayerLeft::InternalCheck(const NetPacketHeader* data) const
|
||||
{
|
||||
// Nothing to do.
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
NetPacketRetrievePlayerInfo::NetPacketRetrievePlayerInfo()
|
||||
: NetPacket(NET_TYPE_RETRIEVE_PLAYER_INFO, sizeof(NetPacketRetrievePlayerInfoData), sizeof(NetPacketRetrievePlayerInfoData))
|
||||
{
|
||||
}
|
||||
|
||||
NetPacketRetrievePlayerInfo::~NetPacketRetrievePlayerInfo()
|
||||
{
|
||||
}
|
||||
|
||||
boost::shared_ptr<NetPacket>
|
||||
NetPacketRetrievePlayerInfo::Clone() const
|
||||
{
|
||||
boost::shared_ptr<NetPacket> newPacket(new NetPacketRetrievePlayerInfo);
|
||||
try
|
||||
{
|
||||
newPacket->SetRawData(GetRawData());
|
||||
} catch (const NetException &)
|
||||
{
|
||||
// Need to return the new packet anyway.
|
||||
}
|
||||
return newPacket;
|
||||
}
|
||||
|
||||
void
|
||||
NetPacketRetrievePlayerInfo::SetData(const NetPacketRetrievePlayerInfo::Data &inData)
|
||||
{
|
||||
NetPacketRetrievePlayerInfoData *tmpData = (NetPacketRetrievePlayerInfoData *)GetRawData();
|
||||
|
||||
// Set the data.
|
||||
tmpData->playerId = htonl(inData.playerId);
|
||||
|
||||
// Check the packet - just in case.
|
||||
Check(GetRawData());
|
||||
}
|
||||
|
||||
void
|
||||
NetPacketRetrievePlayerInfo::GetData(NetPacketRetrievePlayerInfo::Data &outData) const
|
||||
{
|
||||
// We assume that the data is valid. Validity has already been checked.
|
||||
NetPacketRetrievePlayerInfoData *tmpData = (NetPacketRetrievePlayerInfoData *)GetRawData();
|
||||
|
||||
outData.playerId = ntohl(tmpData->playerId);
|
||||
}
|
||||
|
||||
const NetPacketRetrievePlayerInfo *
|
||||
NetPacketRetrievePlayerInfo::ToNetPacketRetrievePlayerInfo() const
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
void
|
||||
NetPacketRetrievePlayerInfo::InternalCheck(const NetPacketHeader* data) const
|
||||
{
|
||||
// Nothing to do.
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
NetPacketPlayerInfo::NetPacketPlayerInfo()
|
||||
: NetPacket(NET_TYPE_PLAYER_INFO, sizeof(NetPacketPlayerInfoData), MAX_PACKET_SIZE)
|
||||
{
|
||||
}
|
||||
|
||||
NetPacketPlayerInfo::~NetPacketPlayerInfo()
|
||||
{
|
||||
}
|
||||
|
||||
boost::shared_ptr<NetPacket>
|
||||
NetPacketPlayerInfo::Clone() const
|
||||
{
|
||||
boost::shared_ptr<NetPacket> newPacket(new NetPacketPlayerInfo);
|
||||
try
|
||||
{
|
||||
newPacket->SetRawData(GetRawData());
|
||||
} catch (const NetException &)
|
||||
{
|
||||
// Need to return the new packet anyway.
|
||||
}
|
||||
return newPacket;
|
||||
}
|
||||
|
||||
void
|
||||
NetPacketPlayerInfo::SetData(const NetPacketPlayerInfo::Data &inData)
|
||||
{
|
||||
u_int16_t playerNameLen = (u_int16_t)inData.playerInfo.playerName.length();
|
||||
|
||||
if (!playerNameLen || playerNameLen > MAX_NAME_SIZE)
|
||||
throw NetException(ERR_NET_INVALID_PLAYER_NAME, 0);
|
||||
|
||||
// Resize the packet so that the data fits in.
|
||||
Resize((u_int16_t)
|
||||
(sizeof(NetPacketPlayerInfoData) + ADD_PADDING(playerNameLen)));
|
||||
|
||||
NetPacketPlayerInfoData *tmpData = (NetPacketPlayerInfoData *)GetRawData();
|
||||
|
||||
// Set the data.
|
||||
tmpData->playerId = htonl(inData.playerId);
|
||||
tmpData->playerFlags = htons(inData.playerInfo.ptype);
|
||||
tmpData->playerNameLength = htons(playerNameLen);
|
||||
char *namePtr = (char *)tmpData + sizeof(NetPacketPlayerInfoData);
|
||||
memcpy(namePtr, inData.playerInfo.playerName.c_str(), playerNameLen);
|
||||
|
||||
// Check the packet - just in case.
|
||||
Check(GetRawData());
|
||||
}
|
||||
|
||||
void
|
||||
NetPacketPlayerInfo::GetData(NetPacketPlayerInfo::Data &outData) const
|
||||
{
|
||||
// We assume that the data is valid. Validity has already been checked.
|
||||
NetPacketPlayerInfoData *tmpData = (NetPacketPlayerInfoData *)GetRawData();
|
||||
|
||||
outData.playerId = ntohl(tmpData->playerId);
|
||||
outData.playerInfo.ptype = static_cast<PlayerType>(ntohs(tmpData->playerFlags));
|
||||
char *namePtr = (char *)tmpData + sizeof(NetPacketPlayerInfoData);
|
||||
outData.playerInfo.playerName = string(namePtr, ntohs(tmpData->playerNameLength));
|
||||
}
|
||||
|
||||
const NetPacketPlayerInfo *
|
||||
NetPacketPlayerInfo::ToNetPacketPlayerInfo() const
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
void
|
||||
NetPacketPlayerInfo::InternalCheck(const NetPacketHeader* data) const
|
||||
{
|
||||
u_int16_t dataLen = ntohs(data->length);
|
||||
NetPacketPlayerInfoData *tmpData = (NetPacketPlayerInfoData *)data;
|
||||
int playerNameLength = ntohs(tmpData->playerNameLength);
|
||||
// Exact checking this time.
|
||||
if (dataLen !=
|
||||
sizeof(NetPacketPlayerInfoData)
|
||||
+ ADD_PADDING(playerNameLength))
|
||||
{
|
||||
throw NetException(ERR_SOCK_INVALID_PACKET, 0);
|
||||
}
|
||||
// Check string sizes.
|
||||
if (!playerNameLength
|
||||
|| playerNameLength > MAX_NAME_SIZE)
|
||||
{
|
||||
throw NetException(ERR_SOCK_INVALID_PACKET, 0);
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
NetPacketCreateGame::NetPacketCreateGame()
|
||||
: NetPacket(NET_TYPE_CREATE_GAME, sizeof(NetPacketCreateGameData), MAX_PACKET_SIZE)
|
||||
{
|
||||
@@ -1377,7 +1731,7 @@ NetPacketJoinGameAck::InternalCheck(const NetPacketHeader* data) const
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
NetPacketPlayerJoined::NetPacketPlayerJoined()
|
||||
: NetPacket(NET_TYPE_PLAYER_JOINED, sizeof(NetPacketPlayerJoinedData), MAX_PACKET_SIZE)
|
||||
: NetPacket(NET_TYPE_PLAYER_JOINED, sizeof(NetPacketPlayerJoinedData), sizeof(NetPacketPlayerJoinedData))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1402,24 +1756,11 @@ NetPacketPlayerJoined::Clone() const
|
||||
void
|
||||
NetPacketPlayerJoined::SetData(const NetPacketPlayerJoined::Data &inData)
|
||||
{
|
||||
u_int16_t playerNameLen = (u_int16_t)inData.playerName.length();
|
||||
|
||||
if (!playerNameLen || playerNameLen > MAX_NAME_SIZE)
|
||||
throw NetException(ERR_NET_INVALID_PLAYER_NAME, 0);
|
||||
|
||||
// Resize the packet so that the data fits in.
|
||||
Resize((u_int16_t)
|
||||
(sizeof(NetPacketPlayerJoinedData) + ADD_PADDING(playerNameLen)));
|
||||
|
||||
NetPacketPlayerJoinedData *tmpData = (NetPacketPlayerJoinedData *)GetRawData();
|
||||
|
||||
// Set the data.
|
||||
tmpData->playerId = htonl(inData.playerId);
|
||||
tmpData->playerFlags = htons(inData.ptype);
|
||||
tmpData->playerRights = htons(inData.prights);
|
||||
tmpData->playerNameLength = htons(playerNameLen);
|
||||
char *namePtr = (char *)tmpData + sizeof(NetPacketPlayerJoinedData);
|
||||
memcpy(namePtr, inData.playerName.c_str(), playerNameLen);
|
||||
|
||||
// Check the packet - just in case.
|
||||
Check(GetRawData());
|
||||
@@ -1432,10 +1773,7 @@ NetPacketPlayerJoined::GetData(NetPacketPlayerJoined::Data &outData) const
|
||||
NetPacketPlayerJoinedData *tmpData = (NetPacketPlayerJoinedData *)GetRawData();
|
||||
|
||||
outData.playerId = ntohl(tmpData->playerId);
|
||||
outData.ptype = static_cast<PlayerType>(ntohs(tmpData->playerFlags));
|
||||
outData.prights = static_cast<PlayerRights>(ntohs(tmpData->playerRights));
|
||||
char *namePtr = (char *)tmpData + sizeof(NetPacketPlayerJoinedData);
|
||||
outData.playerName = string(namePtr, ntohs(tmpData->playerNameLength));
|
||||
}
|
||||
|
||||
const NetPacketPlayerJoined *
|
||||
@@ -1447,22 +1785,7 @@ NetPacketPlayerJoined::ToNetPacketPlayerJoined() const
|
||||
void
|
||||
NetPacketPlayerJoined::InternalCheck(const NetPacketHeader* data) const
|
||||
{
|
||||
u_int16_t dataLen = ntohs(data->length);
|
||||
NetPacketPlayerJoinedData *tmpData = (NetPacketPlayerJoinedData *)data;
|
||||
int playerNameLength = ntohs(tmpData->playerNameLength);
|
||||
// Exact checking this time.
|
||||
if (dataLen !=
|
||||
sizeof(NetPacketPlayerJoinedData)
|
||||
+ ADD_PADDING(playerNameLength))
|
||||
{
|
||||
throw NetException(ERR_SOCK_INVALID_PACKET, 0);
|
||||
}
|
||||
// Check string sizes.
|
||||
if (!playerNameLength
|
||||
|| playerNameLength > MAX_NAME_SIZE)
|
||||
{
|
||||
throw NetException(ERR_SOCK_INVALID_PACKET, 0);
|
||||
}
|
||||
// Nothing to do.
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
@@ -148,8 +148,26 @@ AbstractServerGameStateReceiving::Process(ServerGameThread &server)
|
||||
// Process packet if one was received.
|
||||
if (packet.get())
|
||||
{
|
||||
if (packet->ToNetPacketRetrievePlayerInfo())
|
||||
{
|
||||
NetPacketRetrievePlayerInfo::Data reqData;
|
||||
packet->ToNetPacketRetrievePlayerInfo()->GetData(reqData);
|
||||
|
||||
SessionWrapper tmpSession = server.GetSessionManager().GetSessionByUniquePlayerId(reqData.playerId);
|
||||
if (tmpSession.sessionData.get() && tmpSession.playerData.get())
|
||||
{
|
||||
// Send player info to client.
|
||||
boost::shared_ptr<NetPacket> info(new NetPacketPlayerInfo);
|
||||
NetPacketPlayerInfo::Data infoData;
|
||||
infoData.playerId = tmpSession.playerData->GetUniqueId();
|
||||
infoData.playerInfo.ptype = tmpSession.playerData->GetType();
|
||||
infoData.playerInfo.playerName = tmpSession.playerData->GetName();
|
||||
static_cast<NetPacketPlayerInfo *>(info.get())->SetData(infoData);
|
||||
server.GetSender().Send(session.sessionData->GetSocket(), info);
|
||||
}
|
||||
}
|
||||
// Chat text is always allowed.
|
||||
if (packet->ToNetPacketSendChatText())
|
||||
else if (packet->ToNetPacketSendChatText())
|
||||
{
|
||||
if (session.playerData.get()) // Only forward if this player is known.
|
||||
{
|
||||
@@ -312,8 +330,6 @@ ServerGameStateInit::CreateNetPacketPlayerJoined(const PlayerData &playerData)
|
||||
boost::shared_ptr<NetPacket> thisPlayerJoined(new NetPacketPlayerJoined);
|
||||
NetPacketPlayerJoined::Data thisPlayerJoinedData;
|
||||
thisPlayerJoinedData.playerId = playerData.GetUniqueId();
|
||||
thisPlayerJoinedData.playerName = playerData.GetName();
|
||||
thisPlayerJoinedData.ptype = playerData.GetType();
|
||||
thisPlayerJoinedData.prights = playerData.GetRights();
|
||||
static_cast<NetPacketPlayerJoined *>(thisPlayerJoined.get())->SetData(thisPlayerJoinedData);
|
||||
return thisPlayerJoined;
|
||||
|
||||
@@ -190,7 +190,9 @@ ServerLobbyThread::ProcessLoop()
|
||||
SessionError(session, ERR_SOCK_INVALID_STATE);
|
||||
else
|
||||
{
|
||||
if (packet->ToNetPacketCreateGame())
|
||||
if (packet->ToNetPacketRetrievePlayerInfo())
|
||||
HandleNetPacketRetrievePlayerInfo(session, *packet->ToNetPacketRetrievePlayerInfo());
|
||||
else if (packet->ToNetPacketCreateGame())
|
||||
HandleNetPacketCreateGame(session, *packet->ToNetPacketCreateGame());
|
||||
else if (packet->ToNetPacketJoinGame())
|
||||
HandleNetPacketJoinGame(session, *packet->ToNetPacketJoinGame());
|
||||
@@ -266,6 +268,41 @@ ServerLobbyThread::HandleNetPacketInit(SessionWrapper session, const NetPacketIn
|
||||
session.sessionData->SetState(SessionData::Established);
|
||||
}
|
||||
|
||||
void
|
||||
ServerLobbyThread::HandleNetPacketRetrievePlayerInfo(SessionWrapper session, const NetPacketRetrievePlayerInfo &tmpPacket)
|
||||
{
|
||||
NetPacketRetrievePlayerInfo::Data request;
|
||||
tmpPacket.GetData(request);
|
||||
|
||||
// Find player in lobby or in a game.
|
||||
SessionWrapper tmpSession = m_sessionManager.GetSessionByUniquePlayerId(request.playerId);
|
||||
if (!tmpSession.sessionData.get() || !tmpSession.playerData.get())
|
||||
{
|
||||
GameMap::const_iterator game_i = m_gameMap.begin();
|
||||
GameMap::const_iterator game_end = m_gameMap.end();
|
||||
while (game_i != game_end)
|
||||
{
|
||||
tmpSession = game_i->second->GetSessionManager().GetSessionByUniquePlayerId(request.playerId);
|
||||
if (tmpSession.sessionData.get() && tmpSession.playerData.get())
|
||||
break;
|
||||
++game_i;
|
||||
}
|
||||
}
|
||||
|
||||
if (tmpSession.sessionData.get() && tmpSession.playerData.get())
|
||||
{
|
||||
// Send player info to client.
|
||||
boost::shared_ptr<NetPacket> info(new NetPacketPlayerInfo);
|
||||
NetPacketPlayerInfo::Data infoData;
|
||||
infoData.playerId = tmpSession.playerData->GetUniqueId();
|
||||
infoData.playerInfo.ptype = tmpSession.playerData->GetType();
|
||||
infoData.playerInfo.playerName = tmpSession.playerData->GetName();
|
||||
static_cast<NetPacketPlayerInfo *>(info.get())->SetData(infoData);
|
||||
GetSender().Send(session.sessionData->GetSocket(), info);
|
||||
}
|
||||
// TODO: handle error
|
||||
}
|
||||
|
||||
void
|
||||
ServerLobbyThread::HandleNetPacketCreateGame(SessionWrapper session, const NetPacketCreateGame &tmpPacket)
|
||||
{
|
||||
@@ -517,6 +554,7 @@ ServerLobbyThread::CreateNetPacketGameListNew(const ServerGameThread &game)
|
||||
packetData.gameId = game.GetId();
|
||||
packetData.gameMode = GAME_MODE_CREATED;
|
||||
packetData.gameName = game.GetName();
|
||||
packetData.gameData = game.GetGameData();
|
||||
static_cast<NetPacketGameListNew *>(packet.get())->SetData(packetData);
|
||||
return packet;
|
||||
}
|
||||
|
||||
+104
-2
@@ -44,6 +44,10 @@ class NetPacketInit;
|
||||
class NetPacketInitAck;
|
||||
class NetPacketGameListNew;
|
||||
class NetPacketGameListUpdate;
|
||||
class NetPacketGameListPlayerJoined;
|
||||
class NetPacketGameListPlayerLeft;
|
||||
class NetPacketRetrievePlayerInfo;
|
||||
class NetPacketPlayerInfo;
|
||||
class NetPacketCreateGame;
|
||||
class NetPacketJoinGame;
|
||||
class NetPacketJoinGameAck;
|
||||
@@ -89,6 +93,10 @@ public:
|
||||
virtual const NetPacketInitAck *ToNetPacketInitAck() const;
|
||||
virtual const NetPacketGameListNew *ToNetPacketGameListNew() const;
|
||||
virtual const NetPacketGameListUpdate *ToNetPacketGameListUpdate() const;
|
||||
virtual const NetPacketGameListPlayerJoined *ToNetPacketGameListPlayerJoined() const;
|
||||
virtual const NetPacketGameListPlayerLeft *ToNetPacketGameListPlayerLeft() const;
|
||||
virtual const NetPacketRetrievePlayerInfo *ToNetPacketRetrievePlayerInfo() const;
|
||||
virtual const NetPacketPlayerInfo *ToNetPacketPlayerInfo() const;
|
||||
virtual const NetPacketCreateGame *ToNetPacketCreateGame() const;
|
||||
virtual const NetPacketJoinGame *ToNetPacketJoinGame() const;
|
||||
virtual const NetPacketJoinGameAck *ToNetPacketJoinGameAck() const;
|
||||
@@ -185,6 +193,7 @@ public:
|
||||
u_int32_t gameId;
|
||||
GameMode gameMode;
|
||||
std::string gameName;
|
||||
GameData gameData;
|
||||
};
|
||||
|
||||
NetPacketGameListNew();
|
||||
@@ -226,6 +235,101 @@ protected:
|
||||
virtual void InternalCheck(const NetPacketHeader* data) const;
|
||||
};
|
||||
|
||||
class NetPacketGameListPlayerJoined : public NetPacket
|
||||
{
|
||||
public:
|
||||
struct Data
|
||||
{
|
||||
u_int32_t gameId;
|
||||
u_int32_t playerId;
|
||||
};
|
||||
|
||||
NetPacketGameListPlayerJoined();
|
||||
virtual ~NetPacketGameListPlayerJoined();
|
||||
|
||||
virtual boost::shared_ptr<NetPacket> Clone() const;
|
||||
|
||||
void SetData(const Data &inData);
|
||||
void GetData(Data &outData) const;
|
||||
|
||||
virtual const NetPacketGameListPlayerJoined *ToNetPacketGameListPlayerJoined() const;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void InternalCheck(const NetPacketHeader* data) const;
|
||||
};
|
||||
|
||||
class NetPacketGameListPlayerLeft : public NetPacket
|
||||
{
|
||||
public:
|
||||
struct Data
|
||||
{
|
||||
u_int32_t gameId;
|
||||
u_int32_t playerId;
|
||||
};
|
||||
|
||||
NetPacketGameListPlayerLeft();
|
||||
virtual ~NetPacketGameListPlayerLeft();
|
||||
|
||||
virtual boost::shared_ptr<NetPacket> Clone() const;
|
||||
|
||||
void SetData(const Data &inData);
|
||||
void GetData(Data &outData) const;
|
||||
|
||||
virtual const NetPacketGameListPlayerLeft *ToNetPacketGameListPlayerLeft() const;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void InternalCheck(const NetPacketHeader* data) const;
|
||||
};
|
||||
|
||||
class NetPacketRetrievePlayerInfo : public NetPacket
|
||||
{
|
||||
public:
|
||||
struct Data
|
||||
{
|
||||
u_int32_t playerId;
|
||||
};
|
||||
|
||||
NetPacketRetrievePlayerInfo();
|
||||
virtual ~NetPacketRetrievePlayerInfo();
|
||||
|
||||
virtual boost::shared_ptr<NetPacket> Clone() const;
|
||||
|
||||
void SetData(const Data &inData);
|
||||
void GetData(Data &outData) const;
|
||||
|
||||
virtual const NetPacketRetrievePlayerInfo *ToNetPacketRetrievePlayerInfo() const;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void InternalCheck(const NetPacketHeader* data) const;
|
||||
};
|
||||
|
||||
class NetPacketPlayerInfo : public NetPacket
|
||||
{
|
||||
public:
|
||||
struct Data
|
||||
{
|
||||
u_int32_t playerId;
|
||||
PlayerInfo playerInfo;
|
||||
};
|
||||
|
||||
NetPacketPlayerInfo();
|
||||
virtual ~NetPacketPlayerInfo();
|
||||
|
||||
virtual boost::shared_ptr<NetPacket> Clone() const;
|
||||
|
||||
void SetData(const Data &inData);
|
||||
void GetData(Data &outData) const;
|
||||
|
||||
virtual const NetPacketPlayerInfo *ToNetPacketPlayerInfo() const;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void InternalCheck(const NetPacketHeader* data) const;
|
||||
};
|
||||
|
||||
class NetPacketCreateGame : public NetPacket
|
||||
{
|
||||
public:
|
||||
@@ -306,9 +410,7 @@ public:
|
||||
struct Data
|
||||
{
|
||||
u_int32_t playerId;
|
||||
PlayerType ptype;
|
||||
PlayerRights prights;
|
||||
std::string playerName;
|
||||
};
|
||||
|
||||
NetPacketPlayerJoined();
|
||||
|
||||
@@ -72,6 +72,7 @@ protected:
|
||||
|
||||
void ProcessLoop();
|
||||
void HandleNetPacketInit(SessionWrapper session, const NetPacketInit &tmpPacket);
|
||||
void HandleNetPacketRetrievePlayerInfo(SessionWrapper session, const NetPacketRetrievePlayerInfo &tmpPacket);
|
||||
void HandleNetPacketCreateGame(SessionWrapper session, const NetPacketCreateGame &tmpPacket);
|
||||
void HandleNetPacketJoinGame(SessionWrapper session, const NetPacketJoinGame &tmpPacket);
|
||||
void CloseSessionLoop();
|
||||
|
||||
Reference in New Issue
Block a user