diff --git a/xwords4/relay/crefmgr.cpp b/xwords4/relay/crefmgr.cpp index 0547497f4..76363669b 100644 --- a/xwords4/relay/crefmgr.cpp +++ b/xwords4/relay/crefmgr.cpp @@ -25,6 +25,8 @@ #include "cref.h" #include "mlock.h" #include "permid.h" +#include "configs.h" +#include "timermgr.h" class SocketStuff { public: @@ -314,6 +316,13 @@ CRefMgr::UnlockCref( CookieRef* cref ) pthread_mutex_unlock( cref_mutex ); } +/* static */ void +CRefMgr::heartbeatProc( void* closure ) +{ + CRefMgr* self = (CRefMgr*)closure; + self->checkHeartbeats( now() ); +} /* heartbeatProc */ + CookieRef* CRefMgr::AddNew( const char* cookie, const char* connName, CookieID id ) { @@ -326,6 +335,14 @@ CRefMgr::AddNew( const char* cookie, const char* connName, CookieID id ) m_cookieMap.insert( pair(ref->GetCookieID(), ref ) ); logf( XW_LOGINFO, "paired cookie %s/connName %s with id %d", (cookie?cookie:"NULL"), connName, ref->GetCookieID() ); + + if ( m_cookieMap.size() == 1 ) { + RelayConfigs* cfg = RelayConfigs::GetConfigs(); + short heartbeat = cfg->GetHeartbeatInterval(); + TimerMgr::GetTimerMgr()->SetTimer( heartbeat, heartbeatProc, this, + heartbeat ); + } + return ref; } /* AddNew */ @@ -354,6 +371,12 @@ CRefMgr::Delete( CookieRef* cref ) iter2 = m_crefMutexes.find(cref); m_crefMutexes.erase( iter2 ); + delete cref; + + if ( m_cookieMap.size() == 0 ) { + TimerMgr::GetTimerMgr()->ClearTimer( heartbeatProc, this ); + } + logf( XW_LOGINFO, "CRefMgr::Delete done" ); } @@ -392,7 +415,7 @@ CRefMgr::getCookieRef_impl( CookieID cookieID ) } void -CRefMgr::CheckHeartbeats( time_t now ) +CRefMgr::checkHeartbeats( time_t now ) { vector crefs; @@ -410,7 +433,7 @@ CRefMgr::CheckHeartbeats( time_t now ) SafeCref scr( crefs[i] ); scr.CheckHeartbeats( now ); } -} /* CheckHeartbeats */ +} /* checkHeartbeats */ /* static */ CookieMapIterator CRefMgr::GetCookieIterator() diff --git a/xwords4/relay/crefmgr.h b/xwords4/relay/crefmgr.h index 84cf57533..b58da77a2 100644 --- a/xwords4/relay/crefmgr.h +++ b/xwords4/relay/crefmgr.h @@ -57,7 +57,6 @@ class CRefMgr { CookieMapIterator GetCookieIterator(); /* PENDING. These need to go through SafeCref */ - void CheckHeartbeats( time_t now ); void Delete( CookieID id ); void Delete( CookieRef* cref ); void Delete( const char* connName ); @@ -91,6 +90,9 @@ class CRefMgr { CookieID cookieIDForConnName( const char* connName ); CookieID nextCID( const char* connName ); + static void heartbeatProc( void* closure ); + void checkHeartbeats( time_t now ); + CookieID m_nextCID; int LockCref( CookieRef* cref ); diff --git a/xwords4/relay/xwrelay.cpp b/xwords4/relay/xwrelay.cpp index 1771a1a96..4466fc976 100644 --- a/xwords4/relay/xwrelay.cpp +++ b/xwords4/relay/xwrelay.cpp @@ -387,12 +387,6 @@ make_socket( unsigned long addr, unsigned short port ) return sock; } /* make_socket */ -static void -HeartbeatProc( void* closure ) -{ - CRefMgr::Get()->CheckHeartbeats( now() ); -} /* HeartbeatProc */ - enum { FLAG_HELP ,FLAG_CONFFILE ,FLAG_PORT @@ -534,10 +528,6 @@ int main( int argc, char** argv ) XWThreadPool* tPool = XWThreadPool::GetTPool(); tPool->Setup( nWorkerThreads, processMessage ); - short heartbeat = cfg->GetHeartbeatInterval(); - TimerMgr::GetTimerMgr()->SetTimer( heartbeat, HeartbeatProc, NULL, - heartbeat ); - /* set up select call */ fd_set rfds; for ( ; ; ) {