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.
This commit is contained in:
ehouse 2005-07-06 00:54:38 +00:00
parent 326a639ea5
commit 4e3e67c7b2
2 changed files with 30 additions and 14 deletions

View file

@ -35,14 +35,15 @@ using namespace std;
static CookieMap gCookieMap; static CookieMap gCookieMap;
pthread_rwlock_t gCookieMapRWLock = PTHREAD_RWLOCK_INITIALIZER; pthread_rwlock_t gCookieMapRWLock = PTHREAD_RWLOCK_INITIALIZER;
pthread_mutex_t g_IdsMutex = PTHREAD_MUTEX_INITIALIZER;
CookieID CookieRef::ms_nextConnectionID = 1000; CookieID CookieRef::ms_nextConnectionID = 1000;
/* static */ CookieRef* /* static */ CookieRef*
CookieRef::AddNew( string s ) CookieRef::AddNew( string s, CookieID id )
{ {
RWWriteLock rwl( &gCookieMapRWLock ); RWWriteLock rwl( &gCookieMapRWLock );
CookieRef* ref = new CookieRef( s ); CookieRef* ref = new CookieRef( s, id );
gCookieMap.insert( pair<CookieID, CookieRef*>(ref->GetCookieID(), ref ) ); gCookieMap.insert( pair<CookieID, CookieRef*>(ref->GetCookieID(), ref ) );
logf( "paired cookie %s with id %d", s.c_str(), ref->GetCookieID() ); logf( "paired cookie %s with id %d", s.c_str(), ref->GetCookieID() );
return ref; return ref;
@ -97,17 +98,25 @@ CheckHeartbeats( time_t now, vector<int>* sockets )
logf( "CheckHeartbeats done" ); logf( "CheckHeartbeats done" );
} /* CheckHeartbeats */ } /* 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* CookieRef*
get_make_cookieRef( const char* cookie, get_make_cookieRef( const char* cookie, CookieID cookieID )
CookieID connID ) /* connID ignored for now */
{ {
CookieID id = CookieIdForName( cookie ); /* start with the cookieID if it's set */
CookieRef* cref; CookieRef* cref = cookieID == 0 ? NULL: get_cookieRef( cookieID );
if ( cref == NULL ) { /* need to keep looking? */
CookieID newId = CookieIdForName( cookie );
if ( id == 0 ) { if ( newId == 0 ) { /* not in the system */
cref = CookieRef::AddNew( string(cookie) ); cref = CookieRef::AddNew( string(cookie), cookieID );
} else { } else {
cref = get_cookieRef( id ); cref = get_cookieRef( newId );
}
} }
return cref; return cref;
@ -268,14 +277,20 @@ SocketsIterator::Next()
* CookieRef class * CookieRef class
*****************************************************************************/ *****************************************************************************/
CookieRef::CookieRef(string s) CookieRef::CookieRef( string s, CookieID id )
: m_name(s) : m_name(s)
, m_totalSent(0) , m_totalSent(0)
, m_curState(XW_ST_INITED) , m_curState(XW_ST_INITED)
{ {
pthread_rwlock_init( &m_sockets_rwlock, NULL ); pthread_rwlock_init( &m_sockets_rwlock, NULL );
pthread_mutex_init( &m_EventsMutex, 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() CookieRef::~CookieRef()

View file

@ -54,13 +54,12 @@ class CookieRef {
void PrintSocketInfo( string& out, int socket ); void PrintSocketInfo( string& out, int socket );
static CookieMapIterator GetCookieIterator(); static CookieMapIterator GetCookieIterator();
static CookieRef* AddNew( string s ); static CookieRef* AddNew( string s, CookieID id );
/* Nuke an existing */ /* Nuke an existing */
static void Delete( CookieID id ); static void Delete( CookieID id );
static void Delete( const char* name ); static void Delete( const char* name );
private: private:
CookieRef( string s );
typedef struct CRefEvent { typedef struct CRefEvent {
XW_RELAY_EVENT type; XW_RELAY_EVENT type;
union { union {
@ -88,6 +87,8 @@ class CookieRef {
} u; } u;
} CRefEvent; } CRefEvent;
CookieRef( string s, CookieID id );
void RecordSent( int nBytes, int socket ) { void RecordSent( int nBytes, int socket ) {
/* This really needs a lock.... */ /* This really needs a lock.... */
m_totalSent += nBytes; m_totalSent += nBytes;