From 4e3e67c7b2c5be4153e13625ebd357ba525f30b0 Mon Sep 17 00:00:00 2001 From: ehouse Date: Wed, 6 Jul 2005 00:54:38 +0000 Subject: [PATCH] Honor cookieID passed in connect message, falling back to cookie only when the ID isn't set, and using it rather than assigning a new one even if the game can't be found. --- relay/cref.cpp | 39 +++++++++++++++++++++++++++------------ relay/cref.h | 5 +++-- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/relay/cref.cpp b/relay/cref.cpp index ff471b73e..9df16ac0e 100644 --- a/relay/cref.cpp +++ b/relay/cref.cpp @@ -35,14 +35,15 @@ using namespace std; static CookieMap gCookieMap; pthread_rwlock_t gCookieMapRWLock = PTHREAD_RWLOCK_INITIALIZER; +pthread_mutex_t g_IdsMutex = PTHREAD_MUTEX_INITIALIZER; CookieID CookieRef::ms_nextConnectionID = 1000; /* static */ CookieRef* -CookieRef::AddNew( string s ) +CookieRef::AddNew( string s, CookieID id ) { RWWriteLock rwl( &gCookieMapRWLock ); - CookieRef* ref = new CookieRef( s ); + CookieRef* ref = new CookieRef( s, id ); gCookieMap.insert( pair(ref->GetCookieID(), ref ) ); logf( "paired cookie %s with id %d", s.c_str(), ref->GetCookieID() ); return ref; @@ -97,17 +98,25 @@ CheckHeartbeats( time_t now, vector* sockets ) logf( "CheckHeartbeats done" ); } /* CheckHeartbeats */ +/* [Re]connecting. If there was a game in progress and this host disconnected + * briefly then we can just reconnect. Otherwise we have to create just as if + * it were a from-scratch connect, but without choosing the CookieID. + */ CookieRef* -get_make_cookieRef( const char* cookie, - CookieID connID ) /* connID ignored for now */ +get_make_cookieRef( const char* cookie, CookieID cookieID ) { - CookieID id = CookieIdForName( cookie ); - CookieRef* cref; + /* start with the cookieID if it's set */ + CookieRef* cref = cookieID == 0 ? NULL: get_cookieRef( cookieID ); + + if ( cref == NULL ) { /* need to keep looking? */ + + CookieID newId = CookieIdForName( cookie ); - if ( id == 0 ) { - cref = CookieRef::AddNew( string(cookie) ); - } else { - cref = get_cookieRef( id ); + if ( newId == 0 ) { /* not in the system */ + cref = CookieRef::AddNew( string(cookie), cookieID ); + } else { + cref = get_cookieRef( newId ); + } } return cref; @@ -268,14 +277,20 @@ SocketsIterator::Next() * CookieRef class *****************************************************************************/ -CookieRef::CookieRef(string s) +CookieRef::CookieRef( string s, CookieID id ) : m_name(s) , m_totalSent(0) , m_curState(XW_ST_INITED) { pthread_rwlock_init( &m_sockets_rwlock, NULL ); pthread_mutex_init( &m_EventsMutex, NULL ); - m_connectionID = ms_nextConnectionID++; /* needs a mutex!!! */ + + if ( id == 0 ) { + MutexLock ml( &g_IdsMutex ); + m_connectionID = ms_nextConnectionID++; /* needs a mutex!!! */ + } else { + m_connectionID = id; + } } CookieRef::~CookieRef() diff --git a/relay/cref.h b/relay/cref.h index 7700d0e31..cf09f3e94 100644 --- a/relay/cref.h +++ b/relay/cref.h @@ -54,13 +54,12 @@ class CookieRef { void PrintSocketInfo( string& out, int socket ); static CookieMapIterator GetCookieIterator(); - static CookieRef* AddNew( string s ); + static CookieRef* AddNew( string s, CookieID id ); /* Nuke an existing */ static void Delete( CookieID id ); static void Delete( const char* name ); private: - CookieRef( string s ); typedef struct CRefEvent { XW_RELAY_EVENT type; union { @@ -88,6 +87,8 @@ class CookieRef { } u; } CRefEvent; + CookieRef( string s, CookieID id ); + void RecordSent( int nBytes, int socket ) { /* This really needs a lock.... */ m_totalSent += nBytes;