More steps towards supporting multiple games per server. Player rights still broken, so starting the game is not possible. Games are not yet terminated after all players have left.

This commit is contained in:
lotodore
2007-08-21 12:34:33 +00:00
parent 697f063eb1
commit 839a4f7112
21 changed files with 237 additions and 31 deletions
+3
View File
@@ -36,6 +36,9 @@ public:
virtual void SignalNetClientGameInfo(int actionID) = 0;
virtual void SignalNetClientError(int errorID, int osErrorID) = 0;
virtual void SignalNetClientGameListNew(const std::string &gameName) = 0;
virtual void SignalNetClientGameListRemove(const std::string &gameName) = 0;
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;
+9 -3
View File
@@ -56,14 +56,14 @@ public:
void SendStartEvent();
void SendPlayerAction();
void SendChatMessage(const std::string &msg);
void SendJoinGame(const std::string &name);
void SendCreateGame(const GameData &gameData);
void SendJoinGame(const std::string &name, const std::string &password);
void SendCreateGame(const GameData &gameData, const std::string &name, const std::string &password);
ClientCallback &GetCallback();
GuiInterface &GetGui();
protected:
typedef std::map<unsigned, std::string> PlayerMap;
typedef std::map<std::string, unsigned> GameMap;
// Main function of the thread.
virtual void Main();
@@ -97,6 +97,9 @@ protected:
void RemoveDisconnectedPlayers();
unsigned GetGameIdByName(const std::string &name) const;
void AddGameInformation(const std::string &name, unsigned id);
private:
std::auto_ptr<ClientContext> m_context;
@@ -111,6 +114,9 @@ private:
StartData m_startData;
PlayerDataList m_playerDataList;
GameMap m_gameMap;
mutable boost::mutex m_gameMapMutex;
boost::shared_ptr<Game> m_game;
unsigned m_curGameId;
+8 -1
View File
@@ -477,7 +477,14 @@ ClientStateWaitJoin::InternalProcess(ClientThread &client, boost::shared_ptr<Net
{
int retVal = MSG_SOCK_INTERNAL_PENDING;
if (packet->ToNetPacketJoinGameAck())
if (packet->ToNetPacketGameListNew())
{
// A new game was created on the server.
NetPacketGameListNew::Data gameListNewData;
packet->ToNetPacketGameListNew()->GetData(gameListNewData);
client.AddGameInformation(gameListNewData.gameName, gameListNewData.gameId);
}
else if (packet->ToNetPacketJoinGameAck())
{
// Successfully joined a game.
NetPacketJoinGameAck::Data joinGameAckData;
+53 -4
View File
@@ -147,15 +147,42 @@ ClientThread::SendChatMessage(const std::string &msg)
}
void
ClientThread::SendJoinGame(const std::string &name)
ClientThread::SendJoinGame(const std::string &name, const std::string &password)
{
// TODO
// Warning: This function is called in the context of the GUI thread.
// Create a network packet to request joining a game.
boost::shared_ptr<NetPacket> join(new NetPacketJoinGame);
NetPacketJoinGame::Data joinData;
joinData.password = password;
try
{
joinData.gameId = GetGameIdByName(name);
static_cast<NetPacketJoinGame *>(join.get())->SetData(joinData);
GetSender().Send(GetContext().GetSocket(), join);
} catch (const NetException &)
{
// TODO
}
}
void
ClientThread::SendCreateGame(const GameData &gameData)
ClientThread::SendCreateGame(const GameData &gameData, const std::string &name, const std::string &password)
{
// TODO
// Warning: This function is called in the context of the GUI thread.
// Create a network packet to request creating a new game.
boost::shared_ptr<NetPacket> create(new NetPacketCreateGame);
NetPacketCreateGame::Data createData;
createData.gameData = gameData;
createData.gameName = name;
createData.password = password;
try
{
static_cast<NetPacketCreateGame *>(create.get())->SetData(createData);
GetSender().Send(GetContext().GetSocket(), create);
} catch (const NetException &)
{
// TODO
}
}
ClientCallback &
@@ -444,3 +471,25 @@ ClientThread::RemoveDisconnectedPlayers()
}
}
unsigned
ClientThread::GetGameIdByName(const std::string &name) const
{
// Find the game.
bool retVal = false;
boost::mutex::scoped_lock lock(m_gameMapMutex);
GameMap::const_iterator pos = m_gameMap.find(name);
if (pos == m_gameMap.end())
throw NetException(ERR_NET_UNKNOWN_GAME, 0);
return pos->second;
}
void
ClientThread::AddGameInformation(const std::string &name, unsigned id)
{
{
boost::mutex::scoped_lock lock(m_gameMapMutex);
m_gameMap.insert(GameMap::value_type(name, id));
}
GetCallback().SignalNetClientGameListNew(name);
}
+2 -2
View File
@@ -273,7 +273,7 @@ ServerGameStateInit::InternalProcess(ServerGameThread &server, SessionWrapper se
if (packet->ToNetPacketStartEvent())
{
server.ResetComputerPlayerList();
int remainingSlots = server.GetGameData().maxNumberOfPlayers - server.GetCurNumberOfPlayers();
/* int remainingSlots = server.GetGameData().maxNumberOfPlayers - server.GetCurNumberOfPlayers();
for (int i = 1; i <= remainingSlots; i++)
{
boost::shared_ptr<PlayerData> tmpPlayerData(
@@ -286,7 +286,7 @@ ServerGameStateInit::InternalProcess(ServerGameThread &server, SessionWrapper se
// Send "Player Joined" to other fully connected clients.
server.SendToAllPlayers(CreateNetPacketPlayerJoined(*tmpPlayerData));
server.AddComputerPlayer(tmpPlayerData);
}
}*/
server.InternalStartGame();
server.SetState(SERVER_START_GAME_STATE::Instance());
+34 -4
View File
@@ -137,6 +137,9 @@ ServerLobbyThread::Main()
{
GetCallback().SignalNetServerError(e.GetErrorId(), e.GetOsErrorCode());
}
TerminateGames();
GetSender().SignalTermination();
GetSender().Join(SENDER_THREAD_TERMINATE_TIMEOUT);
@@ -239,13 +242,13 @@ ServerLobbyThread::HandleNetPacketInit(SessionWrapper session, const NetPacketIn
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();
GameMap::const_iterator game_i = m_gameMap.begin();
GameMap::const_iterator game_end = m_gameMap.end();
while (game_i != game_end)
{
GetSender().Send(session.sessionData->GetSocket(), CreateNetPacketGameListUpdate(*(*game_i)));
GetSender().Send(session.sessionData->GetSocket(), CreateNetPacketGameListNew(*game_i->second));
++game_i;
}*/
}
// Set player data for session.
m_sessionManager.SetSessionPlayerData(session.sessionData->GetSocket(), tmpPlayerData);
@@ -317,6 +320,21 @@ ServerLobbyThread::CloseSessionLoop()
}
}
void
ServerLobbyThread::TerminateGames()
{
GameMap::iterator i = m_gameMap.begin();
GameMap::iterator end = m_gameMap.end();
while (i != end)
{
i->second->SignalTermination();
i->second->Join(GAME_THREAD_TERMINATE_TIMEOUT);
++i;
}
m_gameMap.clear();
}
void
ServerLobbyThread::HandleNewConnection(boost::shared_ptr<ConnectData> connData)
{
@@ -409,3 +427,15 @@ ServerLobbyThread::GetGui()
return m_gui;
}
boost::shared_ptr<NetPacket>
ServerLobbyThread::CreateNetPacketGameListNew(const ServerGameThread &game)
{
boost::shared_ptr<NetPacket> packet(new NetPacketGameListNew);
NetPacketGameListNew::Data packetData;
packetData.gameId = game.GetId();
packetData.gameMode = GAME_MODE_CREATED;
packetData.gameName = game.GetName();
static_cast<NetPacketGameListNew *>(packet.get())->SetData(packetData);
return packet;
}
+1 -1
View File
@@ -121,7 +121,7 @@ private:
StartData m_startData;
std::auto_ptr<Game> m_game;
const u_int32_t m_id;
std::string m_name;
const std::string m_name;
std::string m_password;
ConfigFile *m_playerConfig;
ServerGameState *m_curState;
+3
View File
@@ -72,6 +72,7 @@ protected:
void HandleNetPacketCreateGame(SessionWrapper session, const NetPacketCreateGame &tmpPacket);
void HandleNetPacketJoinGame(SessionWrapper session, const NetPacketJoinGame &tmpPacket);
void CloseSessionLoop();
void TerminateGames();
void HandleNewConnection(boost::shared_ptr<ConnectData> connData);
@@ -91,6 +92,8 @@ protected:
ServerSenderCallback &GetSenderCallback();
GuiInterface &GetGui();
static boost::shared_ptr<NetPacket> CreateNetPacketGameListNew(const ServerGameThread &game);
private:
ConnectQueue m_connectQueue;
+7 -6
View File
@@ -52,12 +52,13 @@
#define ERR_NET_INVALID_PLAYER_CARDS 108
#define ERR_NET_INVALID_PLAYER_RESULTS 109
#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
#define ERR_NET_UNKNOWN_GAME 111
#define ERR_NET_INVALID_CHAT_TEXT 112
#define ERR_NET_UNKNOWN_PLAYER_ID 113
#define ERR_NET_INVALID_ROUND 114
#define ERR_NET_PLAYER_KICKED 115
#define ERR_NET_INVALID_PLAYER_COUNT 116
#define ERR_NET_PLAYER_NOT_IN_GAME 117
// This is an internal message which is not reported.
#define MSG_SOCK_INTERNAL_PENDING 0