From 89f967a01606901ff20964d68a19213309d57c36 Mon Sep 17 00:00:00 2001 From: Andy2 Date: Mon, 4 Oct 2010 18:39:26 -0700 Subject: [PATCH] pass remaining reconnect params in case connName's missing from db. Should only matter if the relay's db's been deleted. --- xwords4/relay/cref.cpp | 5 ++++- xwords4/relay/crefmgr.cpp | 43 +++++++++++++++++++++++++++------------ xwords4/relay/crefmgr.h | 11 ++++++---- xwords4/relay/dbmgr.cpp | 17 +++++++++------- xwords4/relay/dbmgr.h | 3 ++- xwords4/relay/xwrelay.cpp | 5 +++-- 6 files changed, 56 insertions(+), 28 deletions(-) diff --git a/xwords4/relay/cref.cpp b/xwords4/relay/cref.cpp index e182d192c..16341f163 100644 --- a/xwords4/relay/cref.cpp +++ b/xwords4/relay/cref.cpp @@ -797,8 +797,11 @@ CookieRef::increasePlayerCounts( CRefEvent* evt, bool reconn ) if ( !reconn ) { m_nPlayersHere += nPlayersH; assert( m_nPlayersHere <= m_nPlayersSought ); - evt->u.con.srcID = DBMgr::Get()->AddDevice( ConnName(), nPlayersH, seed ); } + + evt->u.con.srcID = DBMgr::Get()->AddDevice( ConnName(), evt->u.con.srcID, + nPlayersH, seed ); + HostID hostid = evt->u.con.srcID; /* first add the rec here, whether it'll get ack'd or not */ diff --git a/xwords4/relay/crefmgr.cpp b/xwords4/relay/crefmgr.cpp index 29ca31730..b8f5eafb0 100644 --- a/xwords4/relay/crefmgr.cpp +++ b/xwords4/relay/crefmgr.cpp @@ -269,28 +269,42 @@ CRefMgr::getMakeCookieRef( const char* cookie, HostID hid, int socket, /* reconnect case */ CookieRef* -CRefMgr::getMakeCookieRef( const char* connName, HostID hid, int socket, - int nPlayersH, int seed ) +CRefMgr::getMakeCookieRef( const char* connName, const char* cookie, + HostID hid, int socket, int nPlayersH, + int nPlayersS, int seed, int langCode, + bool isPublic ) { CookieRef* cref = NULL; /* fetch these from DB */ - char cookie[MAX_INVITE_LEN+1]; - int langCode; + char curCookie[MAX_INVITE_LEN+1]; + int curLangCode; int nPlayersT = 0; int nAlreadyHere = 0; - CookieID cid = m_db->FindGame( connName, cookie, sizeof(cookie), - &langCode, &nPlayersT, &nAlreadyHere ); + CookieID cid = m_db->FindGame( connName, curCookie, sizeof(curCookie), + &curLangCode, &nPlayersT, &nAlreadyHere ); if ( 0 != cid ) { /* already open */ cref = getCookieRef_impl( cid ); } else { + /* The entry may not even be in the DB, e.g. if it got deleted. + Deal with that possibility by taking the caller's word for it. */ CookieID cid = nextCID( NULL ); - cref = AddNew( cookie, connName, cid, langCode, nPlayersT, nAlreadyHere ); + + if ( nPlayersT == 0 ) { /* wasn't in the DB */ + m_db->AddNew( cookie, connName, cid, langCode, nPlayersS, isPublic ); + curLangCode = langCode; + nPlayersT = nPlayersS; + } else { + cookie = curCookie; + } + + cref = AddNew( cookie, connName, cid, curLangCode, nPlayersT, + nAlreadyHere ); m_db->AddCID( connName, cid ); } return cref; -} +} /* getMakeCookieRef */ bool CRefMgr::Associate( int socket, CookieRef* cref ) @@ -619,8 +633,8 @@ SafeCref::SafeCref( const char* cookie, int socket, int nPlayersH, int nPlayersS CookieRef* cref; cref = m_mgr->getMakeCookieRef( cookie, 0, socket, - nPlayersH, nPlayersS, langCode, - gameSeed, wantsPublic, makePublic ); + nPlayersH, nPlayersS, langCode, + gameSeed, wantsPublic, makePublic ); if ( cref != NULL ) { m_locked = cref->Lock(); m_cref = cref; @@ -629,9 +643,10 @@ SafeCref::SafeCref( const char* cookie, int socket, int nPlayersH, int nPlayersS } /* REconnect case */ -SafeCref::SafeCref( const char* connName, HostID hid, +SafeCref::SafeCref( const char* connName, const char* cookie, HostID hid, int socket, int nPlayersH, int nPlayersS, - unsigned short gameSeed, int langCode ) + unsigned short gameSeed, int langCode, + bool wantsPublic, bool makePublic ) : m_cref( NULL ) , m_mgr( CRefMgr::Get() ) , m_isValid( false ) @@ -639,7 +654,9 @@ SafeCref::SafeCref( const char* connName, HostID hid, CookieRef* cref; assert( hid <= 4 ); /* no more than 4 hosts */ - cref = m_mgr->getMakeCookieRef( connName, hid, socket, nPlayersH, gameSeed ); + cref = m_mgr->getMakeCookieRef( connName, cookie, hid, socket, nPlayersH, + nPlayersS, gameSeed, langCode, + wantsPublic || makePublic ); if ( cref != NULL ) { m_locked = cref->Lock(); m_cref = cref; diff --git a/xwords4/relay/crefmgr.h b/xwords4/relay/crefmgr.h index 830b0ec3c..f8ea49ea0 100644 --- a/xwords4/relay/crefmgr.h +++ b/xwords4/relay/crefmgr.h @@ -142,8 +142,10 @@ class CRefMgr { bool wantsPublic, bool makePublic ); /* reconnect case; just the stuff we don't have in db */ - CookieRef* getMakeCookieRef( const char* connName, HostID hid, int socket, - int nPlayersH, int seed ); + CookieRef* getMakeCookieRef( const char* connName, const char* cookie, + HostID hid, int socket, int nPlayersH, + int nPlayersS, int seed, int langCode, + bool isPublic ); CookieRef* getCookieRef( CookieID cookieID ); CookieRef* getCookieRef( int socket ); @@ -194,9 +196,10 @@ class SafeCref { unsigned short gameSeed, int langCode, bool wantsPublic, bool makePublic ); /* for reconnect */ - SafeCref( const char* connName, HostID hid, + SafeCref( const char* connName, const char* cookie, HostID hid, int socket, int nPlayersH, int nPlayersS, - unsigned short gameSeed, int langCode ); + unsigned short gameSeed, int langCode, + bool wantsPublic, bool makePublic ); SafeCref( CookieID cid, bool failOk = false ); SafeCref( int socket ); SafeCref( CookieRef* cref ); diff --git a/xwords4/relay/dbmgr.cpp b/xwords4/relay/dbmgr.cpp index 9b41dbf09..9b0845a96 100644 --- a/xwords4/relay/dbmgr.cpp +++ b/xwords4/relay/dbmgr.cpp @@ -162,17 +162,20 @@ DBMgr::FindOpen( const char* cookie, int lang, int nPlayersT, int nPlayersH, } /* FindOpen */ HostID -DBMgr::AddDevice( const char* connName, int nToAdd, unsigned short seed ) +DBMgr::AddDevice( const char* connName, HostID curID, int nToAdd, + unsigned short seed ) { - HostID newID = HOST_ID_NONE; - int arr[4]; + HostID newID = curID; MutexLock ml( &m_dbMutex ); - readArray_locked( connName, arr ); - for ( newID = HOST_ID_SERVER; newID <= 4; ++newID ) { - if ( arr[newID-1] == 0 ) { - break; + if ( newID == HOST_ID_NONE ) { + int arr[4]; + readArray_locked( connName, arr ); + for ( newID = HOST_ID_SERVER; newID <= 4; ++newID ) { + if ( arr[newID-1] == 0 ) { + break; + } } } assert( newID <= 4 ); diff --git a/xwords4/relay/dbmgr.h b/xwords4/relay/dbmgr.h index 5829fbace..a879421d8 100644 --- a/xwords4/relay/dbmgr.h +++ b/xwords4/relay/dbmgr.h @@ -46,7 +46,8 @@ class DBMgr { int nPlayersH, bool wantsPublic, char* connNameBuf, int bufLen, int* nPlayersHP ); - HostID AddDevice( const char* const connName, int nToAdd, unsigned short seed ); + HostID AddDevice( const char* const connName, HostID curID, + int nToAdd, unsigned short seed ); void RmDevice( const char* const connName, HostID id ); void AddCID( const char* connName, CookieID cid ); void ClearCID( const char* connName ); diff --git a/xwords4/relay/xwrelay.cpp b/xwords4/relay/xwrelay.cpp index 806660cdd..465fface2 100644 --- a/xwords4/relay/xwrelay.cpp +++ b/xwords4/relay/xwrelay.cpp @@ -376,8 +376,9 @@ processReconnect( unsigned char* bufp, int bufLen, int socket ) && readStr( &bufp, end, connName, sizeof(connName) ) ) { SafeCref scr( connName[0]? connName : NULL, - srcID, socket, nPlayersH, - nPlayersT, gameSeed, langCode ); + cookie, srcID, socket, nPlayersH, + nPlayersT, gameSeed, langCode, + wantsPublic, makePublic ); success = scr.Reconnect( socket, srcID, nPlayersH, nPlayersT, gameSeed ); }