Fixed two race conditions.

This commit is contained in:
lotodore
2007-10-30 21:25:10 +00:00
parent fee8450a74
commit b0524a59ff
3 changed files with 46 additions and 27 deletions
+4 -2
View File
@@ -22,6 +22,7 @@
#include <net/socket_msg.h> #include <net/socket_msg.h>
#include <net/socket_helper.h> #include <net/socket_helper.h>
#include <cstring> #include <cstring>
#include <cassert>
#include <core/boost/timers.hpp> #include <core/boost/timers.hpp>
#include <boost/bind.hpp> #include <boost/bind.hpp>
@@ -203,15 +204,15 @@ SenderThread::Main()
Msleep(SEND_TIMEOUT_MSEC); Msleep(SEND_TIMEOUT_MSEC);
} }
} }
else else // other errors than would block
{ {
// Skip this packet - this is bad, and is therefore reported. // Skip this packet - this is bad, and is therefore reported.
// Ignore invalid or not connected sockets. // Ignore invalid or not connected sockets.
if (errCode != SOCKET_ERR_NOTCONN && errCode != SOCKET_ERR_NOTSOCK) if (errCode != SOCKET_ERR_NOTCONN && errCode != SOCKET_ERR_NOTSOCK)
m_callback.SignalNetError(m_curSession->GetId(), ERR_SOCK_SEND_FAILED, errCode); m_callback.SignalNetError(m_curSession->GetId(), ERR_SOCK_SEND_FAILED, errCode);
RemoveCurSendData(); RemoveCurSendData();
Msleep(SEND_TIMEOUT_MSEC);
} }
Msleep(SEND_TIMEOUT_MSEC);
} }
else if ((unsigned)bytesSent < m_tmpOutBufSize) else if ((unsigned)bytesSent < m_tmpOutBufSize)
{ {
@@ -225,6 +226,7 @@ SenderThread::Main()
} }
else else
{ {
assert(bytesSent == m_tmpOutBufSize);
m_tmpOutBufSize = 0; m_tmpOutBufSize = 0;
m_curSession.reset(); m_curSession.reset();
sendTimer.reset(); sendTimer.reset();
+38 -24
View File
@@ -121,7 +121,10 @@ ServerLobbyThread::RemoveSessionFromGame(SessionWrapper session)
void void
ServerLobbyThread::CloseSession(SessionWrapper session) ServerLobbyThread::CloseSession(SessionWrapper session)
{ {
m_initTimerSessionMap.erase(session.sessionData->GetId()); {
boost::mutex::scoped_lock lock(m_initTimerSessionMapMutex);
m_initTimerSessionMap.erase(session.sessionData->GetId());
}
m_sessionManager.RemoveSession(session.sessionData->GetId()); m_sessionManager.RemoveSession(session.sessionData->GetId());
m_gameSessionManager.RemoveSession(session.sessionData->GetId()); m_gameSessionManager.RemoveSession(session.sessionData->GetId());
@@ -616,10 +619,16 @@ ServerLobbyThread::EstablishSession(SessionWrapper session)
SendGameList(session.sessionData); SendGameList(session.sessionData);
// Session is now established. // Session is now established.
m_initTimerSessionMap.erase(session.sessionData->GetId()); {
boost::mutex::scoped_lock lock(m_initTimerSessionMapMutex);
m_initTimerSessionMap.erase(session.sessionData->GetId());
}
session.sessionData->SetState(SessionData::Established); session.sessionData->SetState(SessionData::Established);
++m_totalPlayersLoggedIn; {
boost::mutex::scoped_lock lock(m_statMutex);
++m_totalPlayersLoggedIn;
}
BroadcastStatisticsUpdate(); BroadcastStatisticsUpdate();
} }
@@ -674,22 +683,21 @@ ServerLobbyThread::NewSessionLoop()
void void
ServerLobbyThread::CloseSessionLoop() ServerLobbyThread::CloseSessionLoop()
{ {
{ boost::mutex::scoped_lock lock(m_initTimerSessionMapMutex);
InitTimerSessionMap::iterator i = m_initTimerSessionMap.begin(); InitTimerSessionMap::iterator i = m_initTimerSessionMap.begin();
InitTimerSessionMap::iterator end = m_initTimerSessionMap.end(); InitTimerSessionMap::iterator end = m_initTimerSessionMap.end();
// Remove sessions if they do not initialize within a certain period. // Remove sessions if they do not initialize within a certain period.
while (i != end) while (i != end)
{
InitTimerSessionMap::iterator next = i;
++next;
if (i->second.elapsed().total_seconds() > SERVER_INIT_SESSION_TIMEOUT_SEC)
{ {
InitTimerSessionMap::iterator next = i; m_sessionManager.RemoveSession(i->first);
++next; m_initTimerSessionMap.erase(i);
if (i->second.elapsed().total_seconds() > SERVER_INIT_SESSION_TIMEOUT_SEC)
{
m_sessionManager.RemoveSession(i->first);
m_initTimerSessionMap.erase(i);
}
i = next;
} }
i = next;
} }
} }
@@ -740,7 +748,10 @@ ServerLobbyThread::InternalAddGame(boost::shared_ptr<ServerGameThread> game)
m_sessionManager.SendToAllSessionsLowPrio(GetSender(), CreateNetPacketGameListNew(*game), SessionData::Established); m_sessionManager.SendToAllSessionsLowPrio(GetSender(), CreateNetPacketGameListNew(*game), SessionData::Established);
m_gameSessionManager.SendToAllSessionsLowPrio(GetSender(), CreateNetPacketGameListNew(*game), SessionData::Game); m_gameSessionManager.SendToAllSessionsLowPrio(GetSender(), CreateNetPacketGameListNew(*game), SessionData::Game);
++m_totalGamesStarted; {
boost::mutex::scoped_lock lock(m_statMutex);
++m_totalGamesStarted;
}
BroadcastStatisticsUpdate(); BroadcastStatisticsUpdate();
} }
@@ -791,6 +802,7 @@ ServerLobbyThread::HandleNewConnection(boost::shared_ptr<ConnectData> connData)
if (m_sessionManager.GetRawSessionCount() <= SERVER_MAX_NUM_SESSIONS) if (m_sessionManager.GetRawSessionCount() <= SERVER_MAX_NUM_SESSIONS)
{ {
boost::mutex::scoped_lock lock(m_initTimerSessionMapMutex);
m_initTimerSessionMap[sessionData->GetId()] = boost::timers::portable::microsec_timer(); m_initTimerSessionMap[sessionData->GetId()] = boost::timers::portable::microsec_timer();
} }
else else
@@ -877,14 +889,16 @@ ServerLobbyThread::BroadcastStatisticsUpdate()
{ {
boost::shared_ptr<NetPacket> packet(new NetPacketStatisticsChanged); boost::shared_ptr<NetPacket> packet(new NetPacketStatisticsChanged);
NetPacketStatisticsChanged::Data statData; NetPacketStatisticsChanged::Data statData;
unsigned curNumberOfPlayersOnServer = m_sessionManager.GetRawSessionCount() + m_gameSessionManager.GetRawSessionCount(); unsigned curNumberOfPlayersOnServer = m_sessionManager.GetRawSessionCount() + m_gameSessionManager.GetRawSessionCount();
if (curNumberOfPlayersOnServer != m_lastStatData.numberOfPlayersOnServer) {
m_lastStatData.numberOfPlayersOnServer = statData.stats.numberOfPlayersOnServer = curNumberOfPlayersOnServer; boost::mutex::scoped_lock lock(m_statMutex);
if (m_totalPlayersLoggedIn != m_lastStatData.totalPlayersEverLoggedIn) if (curNumberOfPlayersOnServer != m_lastStatData.numberOfPlayersOnServer)
m_lastStatData.totalPlayersEverLoggedIn = statData.stats.totalPlayersEverLoggedIn = m_totalPlayersLoggedIn; m_lastStatData.numberOfPlayersOnServer = statData.stats.numberOfPlayersOnServer = curNumberOfPlayersOnServer;
if (m_totalGamesStarted != m_lastStatData.totalGamesEverStarted) if (m_totalPlayersLoggedIn != m_lastStatData.totalPlayersEverLoggedIn)
m_lastStatData.totalGamesEverStarted = statData.stats.totalGamesEverStarted = m_totalGamesStarted; m_lastStatData.totalPlayersEverLoggedIn = statData.stats.totalPlayersEverLoggedIn = m_totalPlayersLoggedIn;
if (m_totalGamesStarted != m_lastStatData.totalGamesEverStarted)
m_lastStatData.totalGamesEverStarted = statData.stats.totalGamesEverStarted = m_totalGamesStarted;
}
if (curNumberOfPlayersOnServer) if (curNumberOfPlayersOnServer)
{ {
+4 -1
View File
@@ -150,6 +150,7 @@ private:
SessionManager m_gameSessionManager; SessionManager m_gameSessionManager;
InitTimerSessionMap m_initTimerSessionMap; InitTimerSessionMap m_initTimerSessionMap;
mutable boost::mutex m_initTimerSessionMapMutex;
RemoveGameList m_removeGameList; RemoveGameList m_removeGameList;
mutable boost::mutex m_removeGameListMutex; mutable boost::mutex m_removeGameListMutex;
@@ -173,9 +174,11 @@ private:
u_int32_t m_curSessionId; u_int32_t m_curSessionId;
mutable boost::mutex m_curUniquePlayerIdMutex; mutable boost::mutex m_curUniquePlayerIdMutex;
ServerStats m_lastStatData;
unsigned m_totalPlayersLoggedIn; unsigned m_totalPlayersLoggedIn;
unsigned m_totalGamesStarted; unsigned m_totalGamesStarted;
ServerStats m_lastStatData; mutable boost::mutex m_statMutex;
boost::timers::portable::microsec_timer m_cacheCleanupTimer; boost::timers::portable::microsec_timer m_cacheCleanupTimer;
}; };