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:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user