From d0c34c572baf0e7736c2fab1ef511a8f22429759 Mon Sep 17 00:00:00 2001 From: lotodore Date: Thu, 25 Oct 2007 21:20:35 +0000 Subject: [PATCH] Automatically cleanup cache once a day on the dedicated server. --- src/core/avatarmanager.h | 8 -------- src/core/common/avatarmanager.cpp | 8 ++++++++ src/net/common/serverlobbythread.cpp | 24 ++++++++++++++++++++---- src/net/serverlobbythread.h | 3 +++ 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/core/avatarmanager.h b/src/core/avatarmanager.h index ffdb48cb..af80b4a6 100644 --- a/src/core/avatarmanager.h +++ b/src/core/avatarmanager.h @@ -34,14 +34,6 @@ #define MIN_AVATAR_FILE_SIZE 32 #define MAX_AVATAR_FILE_SIZE 30720 -#ifdef POKERTH_DEDICATED_SERVER - #define MAX_NUMBER_OF_FILES 1024 - #define MAX_AVATAR_CACHE_AGE 2592000 // 1 Month -#else - #define MAX_NUMBER_OF_FILES 256 - #define MAX_AVATAR_CACHE_AGE 2592000 -#endif - struct AvatarFileState; class AvatarManager diff --git a/src/core/common/avatarmanager.cpp b/src/core/common/avatarmanager.cpp index f0784457..96450dc2 100644 --- a/src/core/common/avatarmanager.cpp +++ b/src/core/common/avatarmanager.cpp @@ -29,6 +29,14 @@ #include #include +#ifdef POKERTH_DEDICATED_SERVER + #define MAX_NUMBER_OF_FILES 1024 + #define MAX_AVATAR_CACHE_AGE 2592000 // 1 Month +#else + #define MAX_NUMBER_OF_FILES 256 + #define MAX_AVATAR_CACHE_AGE 2592000 +#endif + #define PNG_HEADER "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a" #define PNG_HEADER_SIZE (sizeof(PNG_HEADER) - 1) #define JPG_HEADER "\xff\xd8" diff --git a/src/net/common/serverlobbythread.cpp b/src/net/common/serverlobbythread.cpp index 90d3e1db..677847d4 100644 --- a/src/net/common/serverlobbythread.cpp +++ b/src/net/common/serverlobbythread.cpp @@ -30,10 +30,11 @@ #include -#define SERVER_CLOSE_SESSION_DELAY_SEC 1 -#define SERVER_MAX_NUM_SESSIONS 512 // Maximum number of idle users in lobby. +#define SERVER_CLOSE_SESSION_DELAY_SEC 1 +#define SERVER_MAX_NUM_SESSIONS 512 // Maximum number of idle users in lobby. +#define SERVER_CACHE_CLEANUP_INTERVAL_SEC 86400 // 1 day -#define SERVER_COMPUTER_PLAYER_NAME "Computer" +#define SERVER_COMPUTER_PLAYER_NAME "Computer" using namespace std; @@ -286,6 +287,8 @@ ServerLobbyThread::Main() CloseSessionLoop(); // Remove games. RemoveGameLoop(); + // Cleanup cache. + CleanupAvatarCache(); } } catch (const PokerTHException &e) { @@ -705,6 +708,19 @@ ServerLobbyThread::RemoveGameLoop() m_removeGameList.clear(); } +void +ServerLobbyThread::CleanupAvatarCache() +{ + // Only act on timer and if there are no sessions. + if (m_cacheCleanupTimer.elapsed().total_seconds() > SERVER_CACHE_CLEANUP_INTERVAL_SEC + && !m_sessionManager.HasSessions() && !m_gameSessionManager.HasSessions()) + { + m_avatarManager.RemoveOldAvatarCacheEntries(); + m_cacheCleanupTimer.reset(); + m_cacheCleanupTimer.start(); + } +} + void ServerLobbyThread::InternalAddGame(boost::shared_ptr game) { @@ -867,7 +883,7 @@ ServerLobbyThread::BroadcastStatisticsUpdate() m_sessionManager.SendToAllSessions(GetSender(), packet, SessionData::Established); m_gameSessionManager.SendToAllSessions(GetSender(), packet, SessionData::Game); - } catch (const NetException &e) + } catch (const NetException &) { // Ignore errors for now. //LOG_ERROR("ServerLobbyThread::BroadcastStatisticsUpdate: " << e.what()); diff --git a/src/net/serverlobbythread.h b/src/net/serverlobbythread.h index 53944ffc..4c811f82 100644 --- a/src/net/serverlobbythread.h +++ b/src/net/serverlobbythread.h @@ -102,6 +102,7 @@ protected: void RequestPlayerAvatar(SessionWrapper session); void CloseSessionLoop(); void RemoveGameLoop(); + void CleanupAvatarCache(); void InternalAddGame(boost::shared_ptr game); void InternalRemoveGame(boost::shared_ptr game); @@ -173,6 +174,8 @@ private: unsigned m_totalPlayersLoggedIn; unsigned m_totalGamesStarted; ServerStats m_lastStatData; + + boost::timers::portable::microsec_timer m_cacheCleanupTimer; }; #endif