Game list in Lobby is now shown and updated even after joining or creating a game. Game updates will be sent by the server at all times (even during the game).

This commit is contained in:
lotodore
2007-09-29 14:05:41 +00:00
parent 390837f332
commit 3c88ba8bbe
19 changed files with 407 additions and 197 deletions
+49 -50
View File
@@ -399,7 +399,6 @@ AbstractClientStateReceiving::Process(ClientThread &client)
NetPacketRemovedFromGame::Data removedData;
tmpPacket->ToNetPacketRemovedFromGame()->GetData(removedData);
client.ClearPlayerDataList();
client.ClearGameInfoMap();
client.GetCallback().SignalNetClientRemovedFromGame(removedData.removeReason);
client.SetState(ClientStateWaitJoin::Instance());
}
@@ -430,6 +429,54 @@ AbstractClientStateReceiving::Process(ClientThread &client)
// Signal to GUI and remove from data list.
client.RemovePlayerData(playerLeftData.playerId);
}
else if (tmpPacket->ToNetPacketGameListNew())
{
// A new game was created on the server.
NetPacketGameListNew::Data gameListNewData;
tmpPacket->ToNetPacketGameListNew()->GetData(gameListNewData);
// Request player info for players if needed.
PlayerIdList::const_iterator i = gameListNewData.gameInfo.players.begin();
PlayerIdList::const_iterator end = gameListNewData.gameInfo.players.end();
while (i != end)
{
PlayerInfo info;
if (!client.GetCachedPlayerInfo(*i, info))
{
// Request player info.
client.RequestPlayerInfo(*i);
}
++i;
}
client.AddGameInfo(gameListNewData.gameId, gameListNewData.gameInfo);
}
else if (tmpPacket->ToNetPacketGameListUpdate())
{
// An existing game was updated on the server.
NetPacketGameListUpdate::Data gameListUpdateData;
tmpPacket->ToNetPacketGameListUpdate()->GetData(gameListUpdateData);
if (gameListUpdateData.gameMode == GAME_MODE_CLOSED)
client.RemoveGameInfo(gameListUpdateData.gameId);
}
else if (tmpPacket->ToNetPacketGameListPlayerJoined())
{
NetPacketGameListPlayerJoined::Data playerJoinedData;
tmpPacket->ToNetPacketGameListPlayerJoined()->GetData(playerJoinedData);
client.ModifyGameInfoAddPlayer(playerJoinedData.gameId, playerJoinedData.playerId);
// Request player info if needed.
PlayerInfo info;
if (!client.GetCachedPlayerInfo(playerJoinedData.playerId, info))
{
client.RequestPlayerInfo(playerJoinedData.playerId);
}
}
else if (tmpPacket->ToNetPacketGameListPlayerLeft())
{
NetPacketGameListPlayerLeft::Data playerLeftData;
tmpPacket->ToNetPacketGameListPlayerLeft()->GetData(playerLeftData);
client.ModifyGameInfoRemovePlayer(playerLeftData.gameId, playerLeftData.playerId);
}
else
retVal = InternalProcess(client, tmpPacket);
}
@@ -497,55 +544,7 @@ ClientStateWaitJoin::InternalProcess(ClientThread &client, boost::shared_ptr<Net
int retVal = MSG_SOCK_INTERNAL_PENDING;
ClientContext &context = client.GetContext();
if (packet->ToNetPacketGameListNew())
{
// A new game was created on the server.
NetPacketGameListNew::Data gameListNewData;
packet->ToNetPacketGameListNew()->GetData(gameListNewData);
// Request player info for players if needed.
PlayerIdList::const_iterator i = gameListNewData.gameInfo.players.begin();
PlayerIdList::const_iterator end = gameListNewData.gameInfo.players.end();
while (i != end)
{
PlayerInfo info;
if (!client.GetCachedPlayerInfo(*i, info))
{
// Request player info.
client.RequestPlayerInfo(*i);
}
++i;
}
client.AddGameInfo(gameListNewData.gameId, gameListNewData.gameInfo);
}
else if (packet->ToNetPacketGameListUpdate())
{
// An existing game was updated on the server.
NetPacketGameListUpdate::Data gameListUpdateData;
packet->ToNetPacketGameListUpdate()->GetData(gameListUpdateData);
if (gameListUpdateData.gameMode == GAME_MODE_CLOSED)
client.RemoveGameInfo(gameListUpdateData.gameId);
}
else if (packet->ToNetPacketGameListPlayerJoined())
{
NetPacketGameListPlayerJoined::Data playerJoinedData;
packet->ToNetPacketGameListPlayerJoined()->GetData(playerJoinedData);
client.ModifyGameInfoAddPlayer(playerJoinedData.gameId, playerJoinedData.playerId);
// Request player info if needed.
PlayerInfo info;
if (!client.GetCachedPlayerInfo(playerJoinedData.playerId, info))
{
client.RequestPlayerInfo(playerJoinedData.playerId);
}
}
else if (packet->ToNetPacketGameListPlayerLeft())
{
NetPacketGameListPlayerLeft::Data playerLeftData;
packet->ToNetPacketGameListPlayerLeft()->GetData(playerLeftData);
client.ModifyGameInfoRemovePlayer(playerLeftData.gameId, playerLeftData.playerId);
}
else if (packet->ToNetPacketJoinGameAck())
if (packet->ToNetPacketJoinGameAck())
{
// Successfully joined a game.
NetPacketJoinGameAck::Data joinGameAckData;
+1 -2
View File
@@ -70,8 +70,7 @@ void irc_notify_player_list(irc_session_t *session, const char *players)
input >> name;
while (!input.fail() && !input.eof())
{
if (name != context->nick)
context->ircThread.GetCallback().SignalIrcPlayerJoined(name);
context->ircThread.GetCallback().SignalIrcPlayerJoined(name);
input >> name;
}
}
+3 -22
View File
@@ -141,7 +141,7 @@ AbstractServerGameStateReceiving::Process(ServerGameThread &server)
packet = server.GetReceiver().Recv(session.sessionData->GetSocket(), session.sessionData->GetReceiveBuffer());
} catch (const NetException &)
{
server.RemoveSession(session);
server.ErrorRemoveSession(session);
return retVal;
}
@@ -150,25 +150,8 @@ AbstractServerGameStateReceiving::Process(ServerGameThread &server)
{
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.
// TODO this is a copy and paste
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();
infoData.playerInfo.hasAvatar = !tmpSession.playerData->GetAvatarFile().empty();
if (infoData.playerInfo.hasAvatar)
infoData.playerInfo.avatar.FromString(tmpSession.playerData->GetAvatarFile());
static_cast<NetPacketPlayerInfo *>(info.get())->SetData(infoData);
server.GetSender().Send(session.sessionData->GetSocket(), info);
}
// Delegate to Lobby.
server.GetLobbyThread().HandleGameRetrievePlayerInfo(session, *packet->ToNetPacketRetrievePlayerInfo());
}
else if (packet->ToNetPacketLeaveCurrentGame())
{
@@ -282,8 +265,6 @@ ServerGameStateInit::HandleNewSession(ServerGameThread &server, SessionWrapper s
// Send "Player Joined" to other fully connected clients.
server.SendToAllPlayers(CreateNetPacketPlayerJoined(*session.playerData), SessionData::Game);
// Session is now in game state.
session.sessionData->SetState(SessionData::Game);
// Accept session.
server.GetSessionManager().AddSession(session);
+28 -3
View File
@@ -29,6 +29,8 @@
#include <localenginefactory.h>
#include <tools.h>
#include <boost/bind.hpp>
using namespace std;
@@ -100,6 +102,22 @@ ServerGameThread::SendToAllPlayers(boost::shared_ptr<NetPacket> packet, SessionD
GetSessionManager().SendToAllSessions(GetSender(), packet, state);
}
void
ServerGameThread::RemoveAllSessions()
{
// Called from lobby thread.
// Clean up ALL sessions which are left.
ServerLobbyThread &lobbyThread = GetLobbyThread();
boost::mutex::scoped_lock lock(m_sessionQueueMutex);
while (!m_sessionQueue.empty())
{
SessionWrapper tmpSession = m_sessionQueue.front();
m_sessionQueue.pop_front();
lobbyThread.RemoveSessionFromGame(tmpSession);
}
GetSessionManager().ForEach(boost::bind(&ServerLobbyThread::RemoveSessionFromGame, boost::ref(lobbyThread), _1));
}
void
ServerGameThread::Main()
{
@@ -262,7 +280,7 @@ ServerGameThread::ResetComputerPlayerList()
}
void
ServerGameThread::RemoveSession(SessionWrapper session)
ServerGameThread::GracefulRemoveSession(SessionWrapper session)
{
assert(session.sessionData.get());
GetSessionManager().RemoveSession(session.sessionData->GetSocket());
@@ -281,18 +299,25 @@ ServerGameThread::RemoveSession(SessionWrapper session)
}
}
void
ServerGameThread::ErrorRemoveSession(SessionWrapper session)
{
GetLobbyThread().RemoveSessionFromGame(session);
GracefulRemoveSession(session);
}
void
ServerGameThread::SessionError(SessionWrapper session, int errorCode)
{
assert(session.sessionData.get());
RemoveSession(session);
ErrorRemoveSession(session);
GetLobbyThread().SessionError(session, errorCode);
}
void
ServerGameThread::MoveSessionToLobby(SessionWrapper session, int reason)
{
RemoveSession(session);
GracefulRemoveSession(session);
GetLobbyThread().ReAddSession(session, reason);
}
+49 -52
View File
@@ -29,7 +29,7 @@
#include <boost/lambda/lambda.hpp>
#define SERVER_CLOSE_SESSION_DELAY_SEC 10
#define SERVER_MAX_NUM_SESSIONS 64 // Maximum number of idle users in lobby.
#define SERVER_MAX_NUM_SESSIONS 512 // Maximum number of idle users in lobby.
#define SERVER_COMPUTER_PLAYER_NAME "Computer"
@@ -93,10 +93,31 @@ ServerLobbyThread::ReAddSession(SessionWrapper session, int reason)
m_sessionQueue.push_back(session);
}
void
ServerLobbyThread::MoveSessionToGame(ServerGameThread &game, SessionWrapper session)
{
// Remove session from the lobby.
m_sessionManager.RemoveSession(session.sessionData->GetSocket());
// Session is now in game state.
session.sessionData->SetState(SessionData::Game);
// Store it in the list of game sessions.
m_gameSessionManager.AddSession(session);
// Add session to the game.
game.AddSession(session);
}
void
ServerLobbyThread::RemoveSessionFromGame(SessionWrapper session)
{
// Just remove the session. Only for fatal errors.
m_gameSessionManager.RemoveSession(session.sessionData->GetSocket());
}
void
ServerLobbyThread::CloseSessionDelayed(SessionWrapper session)
{
m_sessionManager.RemoveSession(session.sessionData->GetSocket());
m_gameSessionManager.RemoveSession(session.sessionData->GetSocket());
boost::timers::portable::microsec_timer closeTimer;
closeTimer.start();
@@ -116,6 +137,7 @@ ServerLobbyThread::NotifyPlayerJoinedGame(unsigned gameId, unsigned playerId)
packetData.playerId = playerId;
static_cast<NetPacketGameListPlayerJoined *>(packet.get())->SetData(packetData);
m_sessionManager.SendToAllSessions(GetSender(), packet, SessionData::Established);
m_gameSessionManager.SendToAllSessions(GetSender(), packet, SessionData::Game);
}
void
@@ -128,6 +150,14 @@ ServerLobbyThread::NotifyPlayerLeftGame(unsigned gameId, unsigned playerId)
packetData.playerId = playerId;
static_cast<NetPacketGameListPlayerLeft *>(packet.get())->SetData(packetData);
m_sessionManager.SendToAllSessions(GetSender(), packet, SessionData::Established);
m_gameSessionManager.SendToAllSessions(GetSender(), packet, SessionData::Game);
}
void
ServerLobbyThread::HandleGameRetrievePlayerInfo(SessionWrapper session, const NetPacketRetrievePlayerInfo &tmpPacket)
{
// Someone within a game requested player info.
HandleNetPacketRetrievePlayerInfo(session, tmpPacket);
}
void
@@ -185,7 +215,7 @@ ServerLobbyThread::Main()
}
}
if (tmpSession.sessionData.get() && tmpSession.playerData.get())
HandleNewSession(tmpSession);
HandleReAddedSession(tmpSession);
}
// Process loop.
ProcessLoop();
@@ -205,7 +235,6 @@ ServerLobbyThread::Main()
GetSender().Join(SENDER_THREAD_TERMINATE_TIMEOUT);
CleanupConnectQueue();
m_sessionManager.Clear();
}
void
@@ -326,17 +355,7 @@ ServerLobbyThread::HandleNetPacketRetrievePlayerInfo(SessionWrapper session, con
// Find player in lobby or in a game.
boost::shared_ptr<PlayerData> tmpPlayer = m_sessionManager.GetSessionByUniquePlayerId(request.playerId).playerData;
if (!tmpPlayer.get())
{
GameMap::const_iterator game_i = m_gameMap.begin();
GameMap::const_iterator game_end = m_gameMap.end();
while (game_i != game_end)
{
tmpPlayer = game_i->second->GetPlayerDataByUniqueId(request.playerId);
if (tmpPlayer.get())
break;
++game_i;
}
}
tmpPlayer = m_gameSessionManager.GetSessionByUniquePlayerId(request.playerId).playerData;
if (tmpPlayer.get())
{
@@ -371,10 +390,7 @@ ServerLobbyThread::HandleNetPacketCreateGame(SessionWrapper session, const NetPa
m_playerConfig));
game->Init(createGameData.gameData);
// Remove session from the lobby.
m_sessionManager.RemoveSession(session.sessionData->GetSocket());
// Add session to the game.
game->AddSession(session);
MoveSessionToGame(*game, session);
// Add game to list of games.
InternalAddGame(game);
@@ -397,10 +413,7 @@ ServerLobbyThread::HandleNetPacketJoinGame(SessionWrapper session, const NetPack
ServerGameThread &game = *pos->second;
if (game.CheckPassword(joinGameData.password))
{
// Remove session from the lobby.
m_sessionManager.RemoveSession(session.sessionData->GetSocket());
// Add session to the game.
game.AddSession(session);
MoveSessionToGame(game, session);
}
else
{
@@ -461,6 +474,7 @@ ServerLobbyThread::InternalAddGame(boost::shared_ptr<ServerGameThread> game)
m_gameMap.insert(GameMap::value_type(game->GetId(), game));
// Notify all players.
m_sessionManager.SendToAllSessions(GetSender(), CreateNetPacketGameListNew(*game), SessionData::Established);
m_gameSessionManager.SendToAllSessions(GetSender(), CreateNetPacketGameListNew(*game), SessionData::Game);
}
void
@@ -468,8 +482,11 @@ ServerLobbyThread::InternalRemoveGame(boost::shared_ptr<ServerGameThread> game)
{
// Remove game from list.
m_gameMap.erase(game->GetId());
// Remove all sessions left in the game.
game->RemoveAllSessions();
// Notify all players.
m_sessionManager.SendToAllSessions(GetSender(), CreateNetPacketGameListUpdate(*game, GAME_MODE_CLOSED), SessionData::Established);
m_gameSessionManager.SendToAllSessions(GetSender(), CreateNetPacketGameListUpdate(*game, GAME_MODE_CLOSED), SessionData::Game);
}
void
@@ -517,26 +534,17 @@ ServerLobbyThread::HandleNewConnection(boost::shared_ptr<ConnectData> connData)
}
void
ServerLobbyThread::HandleNewSession(SessionWrapper session)
ServerLobbyThread::HandleReAddedSession(SessionWrapper session)
{
// Remove session from game session list.
m_gameSessionManager.RemoveSession(session.sessionData->GetSocket());
if (m_sessionManager.GetRawSessionCount() <= SERVER_MAX_NUM_SESSIONS)
{
// This session has been temporarily stored - check if no
// one else got the player name during that time.
if (!m_sessionManager.IsPlayerConnected(session.playerData->GetName()))
{
// Send the list of games.
SendGameList(session.sessionData->GetSocket());
// Set state (back) to established.
session.sessionData->SetState(SessionData::Established);
// Add session to lobby list.
m_sessionManager.AddSession(session);
}
else
{
// Gracefully close this session.
SessionError(session, ERR_NET_PLAYER_NAME_IN_USE);
}
// Set state (back) to established.
session.sessionData->SetState(SessionData::Established);
// Add session to lobby list.
m_sessionManager.AddSession(session);
}
else
{
@@ -643,19 +651,8 @@ ServerLobbyThread::IsPlayerConnected(const string &name)
retVal = m_sessionManager.IsPlayerConnected(name);
if (!retVal)
{
GameMap::const_iterator game_i = m_gameMap.begin();
GameMap::const_iterator game_end = m_gameMap.end();
while (game_i != game_end)
{
if (game_i->second->IsPlayerConnected(name))
{
retVal = true;
break;
}
++game_i;
}
}
retVal = m_gameSessionManager.IsPlayerConnected(name);
return retVal;
}
+49
View File
@@ -30,3 +30,52 @@ SessionData::~SessionData()
CLOSESOCKET(m_sockfd);
}
unsigned
SessionData::GetId() const
{
// const value - no mutex needed.
return m_id;
}
SessionData::State
SessionData::GetState() const
{
boost::mutex::scoped_lock lock(m_dataMutex);
return m_state;
}
void
SessionData::SetState(SessionData::State state)
{
boost::mutex::scoped_lock lock(m_dataMutex);
m_state = state;
}
SOCKET
SessionData::GetSocket() const
{
// value never modified - no mutex needed.
return m_sockfd;
}
const std::string &
SessionData::GetClientAddr() const
{
boost::mutex::scoped_lock lock(m_dataMutex);
return m_clientAddr;
}
void
SessionData::SetClientAddr(const std::string &addr)
{
boost::mutex::scoped_lock lock(m_dataMutex);
m_clientAddr = addr;
}
ReceiveBuffer &
SessionData::GetReceiveBuffer()
{
// mutex protection, if needed, within buffer.
return m_receiveBuffer;
}
+15
View File
@@ -277,6 +277,21 @@ SessionManager::IsPlayerConnected(unsigned uniqueId) const
return retVal;
}
void
SessionManager::ForEach(boost::function<void (SessionWrapper)> func)
{
boost::mutex::scoped_lock lock(m_sessionMapMutex);
SessionMap::iterator i = m_sessionMap.begin();
SessionMap::iterator end = m_sessionMap.end();
while (i != end)
{
func((*i).second);
++i;
}
}
void
SessionManager::Clear()
{
+1
View File
@@ -21,6 +21,7 @@
#define _GENERICSOCKET_H_
#ifdef _WIN32
#define FD_SETSIZE 512
#include <winsock2.h>
#include <ws2tcpip.h>
#else
+3 -1
View File
@@ -56,6 +56,7 @@ public:
GameState GetCurRound() const;
void SendToAllPlayers(boost::shared_ptr<NetPacket> packet, SessionData::State state);
void RemoveAllSessions();
bool IsPasswordProtected() const;
bool CheckPassword(const std::string &password) const;
@@ -80,7 +81,8 @@ protected:
void AddComputerPlayer(boost::shared_ptr<PlayerData> player);
void ResetComputerPlayerList();
void RemoveSession(SessionWrapper session);
void GracefulRemoveSession(SessionWrapper session);
void ErrorRemoveSession(SessionWrapper session);
void SessionError(SessionWrapper session, int errorCode);
void MoveSessionToLobby(SessionWrapper session, int reason);
+6 -1
View File
@@ -52,10 +52,14 @@ public:
void AddConnection(boost::shared_ptr<ConnectData> data);
void ReAddSession(SessionWrapper session, int reason);
void MoveSessionToGame(ServerGameThread &game, SessionWrapper session);
void RemoveSessionFromGame(SessionWrapper session);
void SessionError(SessionWrapper session, int errorCode);
void NotifyPlayerJoinedGame(unsigned gameId, unsigned playerId);
void NotifyPlayerLeftGame(unsigned gameId, unsigned playerId);
void HandleGameRetrievePlayerInfo(SessionWrapper session, const NetPacketRetrievePlayerInfo &tmpPacket);
void RemoveGame(unsigned id);
u_int32_t GetNextUniquePlayerId();
@@ -88,7 +92,7 @@ protected:
void TerminateGames();
void HandleNewConnection(boost::shared_ptr<ConnectData> connData);
void HandleNewSession(SessionWrapper session);
void HandleReAddedSession(SessionWrapper session);
SOCKET Select();
@@ -122,6 +126,7 @@ private:
mutable boost::mutex m_sessionQueueMutex;
SessionManager m_sessionManager;
SessionManager m_gameSessionManager;
CloseSessionList m_closeSessionList;
mutable boost::mutex m_closeSessionListMutex;
+11 -15
View File
@@ -24,6 +24,7 @@
#include <net/socket_helper.h>
#include <net/receivebuffer.h>
#include <string>
#include <boost/thread.hpp>
#define SESSION_ID_INIT 0
@@ -35,30 +36,25 @@ public:
SessionData(SOCKET sockfd, unsigned id);
~SessionData();
unsigned GetId() const
{return m_id;}
State GetState() const
{return m_state;}
void SetState(State state)
{m_state = state;}
unsigned GetId() const;
State GetState() const;
void SetState(State state);
SOCKET GetSocket() const
{return m_sockfd;}
SOCKET GetSocket() const;
const std::string &GetClientAddr() const
{return m_clientAddr;}
void SetClientAddr(const std::string &addr)
{m_clientAddr = addr;}
const std::string &GetClientAddr() const;
void SetClientAddr(const std::string &addr);
ReceiveBuffer &GetReceiveBuffer()
{return m_receiveBuffer;}
ReceiveBuffer &GetReceiveBuffer();
private:
SOCKET m_sockfd;
unsigned m_id;
const unsigned m_id;
State m_state;
std::string m_clientAddr;
ReceiveBuffer m_receiveBuffer;
mutable boost::mutex m_dataMutex;
};
#endif
+3
View File
@@ -25,6 +25,7 @@
#include <playerdata.h>
#include <core/thread.h>
#include <boost/function.hpp>
#include <map>
#include <string>
@@ -62,6 +63,8 @@ public:
bool IsPlayerConnected(const std::string &playerName) const;
bool IsPlayerConnected(unsigned uniqueId) const;
void ForEach(boost::function<void (SessionWrapper)> func);
void Clear();
unsigned GetRawSessionCount();