diff --git a/src/net/common/senderthread.cpp b/src/net/common/senderthread.cpp index 181335c5..930e93b3 100644 --- a/src/net/common/senderthread.cpp +++ b/src/net/common/senderthread.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -203,15 +204,15 @@ SenderThread::Main() Msleep(SEND_TIMEOUT_MSEC); } } - else + else // other errors than would block { // Skip this packet - this is bad, and is therefore reported. // Ignore invalid or not connected sockets. if (errCode != SOCKET_ERR_NOTCONN && errCode != SOCKET_ERR_NOTSOCK) m_callback.SignalNetError(m_curSession->GetId(), ERR_SOCK_SEND_FAILED, errCode); RemoveCurSendData(); + Msleep(SEND_TIMEOUT_MSEC); } - Msleep(SEND_TIMEOUT_MSEC); } else if ((unsigned)bytesSent < m_tmpOutBufSize) { @@ -225,6 +226,7 @@ SenderThread::Main() } else { + assert(bytesSent == m_tmpOutBufSize); m_tmpOutBufSize = 0; m_curSession.reset(); sendTimer.reset(); diff --git a/src/net/common/serverlobbythread.cpp b/src/net/common/serverlobbythread.cpp index 8afd7cd1..c003956e 100644 --- a/src/net/common/serverlobbythread.cpp +++ b/src/net/common/serverlobbythread.cpp @@ -121,7 +121,10 @@ ServerLobbyThread::RemoveSessionFromGame(SessionWrapper session) void 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_gameSessionManager.RemoveSession(session.sessionData->GetId()); @@ -616,10 +619,16 @@ ServerLobbyThread::EstablishSession(SessionWrapper session) SendGameList(session.sessionData); // 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); - ++m_totalPlayersLoggedIn; + { + boost::mutex::scoped_lock lock(m_statMutex); + ++m_totalPlayersLoggedIn; + } BroadcastStatisticsUpdate(); } @@ -674,22 +683,21 @@ ServerLobbyThread::NewSessionLoop() void ServerLobbyThread::CloseSessionLoop() { - { - InitTimerSessionMap::iterator i = m_initTimerSessionMap.begin(); - InitTimerSessionMap::iterator end = m_initTimerSessionMap.end(); + boost::mutex::scoped_lock lock(m_initTimerSessionMapMutex); + InitTimerSessionMap::iterator i = m_initTimerSessionMap.begin(); + InitTimerSessionMap::iterator end = m_initTimerSessionMap.end(); - // Remove sessions if they do not initialize within a certain period. - while (i != end) + // Remove sessions if they do not initialize within a certain period. + while (i != end) + { + InitTimerSessionMap::iterator next = i; + ++next; + if (i->second.elapsed().total_seconds() > SERVER_INIT_SESSION_TIMEOUT_SEC) { - InitTimerSessionMap::iterator next = i; - ++next; - if (i->second.elapsed().total_seconds() > SERVER_INIT_SESSION_TIMEOUT_SEC) - { - m_sessionManager.RemoveSession(i->first); - m_initTimerSessionMap.erase(i); - } - i = next; + m_sessionManager.RemoveSession(i->first); + m_initTimerSessionMap.erase(i); } + i = next; } } @@ -740,7 +748,10 @@ ServerLobbyThread::InternalAddGame(boost::shared_ptr game) m_sessionManager.SendToAllSessionsLowPrio(GetSender(), CreateNetPacketGameListNew(*game), SessionData::Established); m_gameSessionManager.SendToAllSessionsLowPrio(GetSender(), CreateNetPacketGameListNew(*game), SessionData::Game); - ++m_totalGamesStarted; + { + boost::mutex::scoped_lock lock(m_statMutex); + ++m_totalGamesStarted; + } BroadcastStatisticsUpdate(); } @@ -791,6 +802,7 @@ ServerLobbyThread::HandleNewConnection(boost::shared_ptr connData) if (m_sessionManager.GetRawSessionCount() <= SERVER_MAX_NUM_SESSIONS) { + boost::mutex::scoped_lock lock(m_initTimerSessionMapMutex); m_initTimerSessionMap[sessionData->GetId()] = boost::timers::portable::microsec_timer(); } else @@ -877,14 +889,16 @@ ServerLobbyThread::BroadcastStatisticsUpdate() { boost::shared_ptr packet(new NetPacketStatisticsChanged); NetPacketStatisticsChanged::Data statData; - unsigned curNumberOfPlayersOnServer = m_sessionManager.GetRawSessionCount() + m_gameSessionManager.GetRawSessionCount(); - if (curNumberOfPlayersOnServer != m_lastStatData.numberOfPlayersOnServer) - m_lastStatData.numberOfPlayersOnServer = statData.stats.numberOfPlayersOnServer = curNumberOfPlayersOnServer; - if (m_totalPlayersLoggedIn != m_lastStatData.totalPlayersEverLoggedIn) - m_lastStatData.totalPlayersEverLoggedIn = statData.stats.totalPlayersEverLoggedIn = m_totalPlayersLoggedIn; - if (m_totalGamesStarted != m_lastStatData.totalGamesEverStarted) - m_lastStatData.totalGamesEverStarted = statData.stats.totalGamesEverStarted = m_totalGamesStarted; + { + boost::mutex::scoped_lock lock(m_statMutex); + if (curNumberOfPlayersOnServer != m_lastStatData.numberOfPlayersOnServer) + m_lastStatData.numberOfPlayersOnServer = statData.stats.numberOfPlayersOnServer = curNumberOfPlayersOnServer; + if (m_totalPlayersLoggedIn != m_lastStatData.totalPlayersEverLoggedIn) + m_lastStatData.totalPlayersEverLoggedIn = statData.stats.totalPlayersEverLoggedIn = m_totalPlayersLoggedIn; + if (m_totalGamesStarted != m_lastStatData.totalGamesEverStarted) + m_lastStatData.totalGamesEverStarted = statData.stats.totalGamesEverStarted = m_totalGamesStarted; + } if (curNumberOfPlayersOnServer) { diff --git a/src/net/serverlobbythread.h b/src/net/serverlobbythread.h index 6d683ab6..2dd91263 100644 --- a/src/net/serverlobbythread.h +++ b/src/net/serverlobbythread.h @@ -150,6 +150,7 @@ private: SessionManager m_gameSessionManager; InitTimerSessionMap m_initTimerSessionMap; + mutable boost::mutex m_initTimerSessionMapMutex; RemoveGameList m_removeGameList; mutable boost::mutex m_removeGameListMutex; @@ -173,9 +174,11 @@ private: u_int32_t m_curSessionId; mutable boost::mutex m_curUniquePlayerIdMutex; + + ServerStats m_lastStatData; unsigned m_totalPlayersLoggedIn; unsigned m_totalGamesStarted; - ServerStats m_lastStatData; + mutable boost::mutex m_statMutex; boost::timers::portable::microsec_timer m_cacheCleanupTimer; };