Adding some error handling. Some is still missing.

This commit is contained in:
lotodore
2009-06-01 10:23:44 +00:00
parent 24abedbbab
commit cee2d787d0
2 changed files with 29 additions and 32 deletions
+28 -30
View File
@@ -521,8 +521,6 @@ ServerLobbyThread::Main()
LOG_ERROR(e.what()); LOG_ERROR(e.what());
} }
// Stop all running games.
TerminateGames();
// Remove all sessions. // Remove all sessions.
m_gameSessionManager.Clear(); m_gameSessionManager.Clear();
m_sessionManager.Clear(); m_sessionManager.Clear();
@@ -572,16 +570,6 @@ ServerLobbyThread::HandleRead(SessionId sessionId, const boost::system::error_co
session = m_gameSessionManager.GetSessionById(sessionId); session = m_gameSessionManager.GetSessionById(sessionId);
if (session.sessionData) if (session.sessionData)
{ {
// Retrieve current game, if applicable.
unsigned gameId = session.sessionData->GetGameId();
boost::shared_ptr<ServerGame> game;
if (gameId)
{
GameMap::iterator pos = m_gameMap.find(gameId);
if (pos != m_gameMap.end())
game = pos->second;
}
if (!error) if (!error)
{ {
@@ -593,11 +581,22 @@ ServerLobbyThread::HandleRead(SessionId sessionId, const boost::system::error_co
{ {
boost::shared_ptr<NetPacket> packet = buf.receivedPackets.front(); boost::shared_ptr<NetPacket> packet = buf.receivedPackets.front();
buf.receivedPackets.pop_front(); buf.receivedPackets.pop_front();
// Retrieve current game, if applicable.
boost::shared_ptr<ServerGame> game = InternalGetGameFromId(session.sessionData->GetGameId());
if (game) if (game)
game->HandlePacket(session, packet); {
// We need to catch game-specific exceptions, so that they do not affect the server.
try
{
game->HandlePacket(session, packet);
} catch (const PokerTHException &e)
{
LOG_ERROR(e.what());
InternalRemoveGame(game);
}
}
else else
HandlePacket(session, packet); HandlePacket(session, packet);
// TODO state may have changed between.
} }
session.sessionData->GetAsioSocket()->async_read_some( session.sessionData->GetAsioSocket()->async_read_some(
boost::asio::buffer(buf.recvBuf + buf.recvBufUsed, RECV_BUF_SIZE - buf.recvBufUsed), boost::asio::buffer(buf.recvBuf + buf.recvBufUsed, RECV_BUF_SIZE - buf.recvBufUsed),
@@ -611,6 +610,7 @@ ServerLobbyThread::HandleRead(SessionId sessionId, const boost::system::error_co
else else
{ {
// On error: Close this session. // On error: Close this session.
boost::shared_ptr<ServerGame> game = InternalGetGameFromId(session.sessionData->GetGameId());
if (game) if (game)
game->ErrorRemoveSession(session); game->ErrorRemoveSession(session);
else else
@@ -1144,6 +1144,20 @@ ServerLobbyThread::TimerCleanupAvatarCache()
} }
} }
boost::shared_ptr<ServerGame>
ServerLobbyThread::InternalGetGameFromId(unsigned gameId)
{
boost::shared_ptr<ServerGame> game;
if (gameId)
{
GameMap::iterator pos = m_gameMap.find(gameId);
if (pos != m_gameMap.end())
game = pos->second;
}
return game;
}
void void
ServerLobbyThread::InternalAddGame(boost::shared_ptr<ServerGame> game) ServerLobbyThread::InternalAddGame(boost::shared_ptr<ServerGame> game)
{ {
@@ -1233,22 +1247,6 @@ ServerLobbyThread::InternalResubscribeMsg(SessionWrapper session)
} }
} }
void
ServerLobbyThread::TerminateGames()
{
// TODO deprecated
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 void
ServerLobbyThread::HandleReAddedSession(SessionWrapper session) ServerLobbyThread::HandleReAddedSession(SessionWrapper session)
{ {
+1 -2
View File
@@ -134,13 +134,12 @@ protected:
void TimerCheckSessionTimeouts(); void TimerCheckSessionTimeouts();
void TimerCleanupAvatarCache(); void TimerCleanupAvatarCache();
boost::shared_ptr<ServerGame> InternalGetGameFromId(unsigned gameId);
void InternalAddGame(boost::shared_ptr<ServerGame> game); void InternalAddGame(boost::shared_ptr<ServerGame> game);
void InternalRemoveGame(boost::shared_ptr<ServerGame> game); void InternalRemoveGame(boost::shared_ptr<ServerGame> game);
void InternalRemovePlayer(unsigned playerId, unsigned errorCode); void InternalRemovePlayer(unsigned playerId, unsigned errorCode);
void InternalResubscribeMsg(SessionWrapper session); void InternalResubscribeMsg(SessionWrapper session);
void TerminateGames();
void HandleReAddedSession(SessionWrapper session); void HandleReAddedSession(SessionWrapper session);
void InternalCheckSessionTimeouts(SessionWrapper session); void InternalCheckSessionTimeouts(SessionWrapper session);