mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-02-04 20:46:28 +01:00
remove special-case states and handling of first connect; track number
of players sought in the cref itself, adding in constructor, and not in HostRec instances. This is cleanup with no behavior change.
This commit is contained in:
parent
100ed8532f
commit
f8c1c016cc
8 changed files with 31 additions and 86 deletions
|
@ -81,7 +81,7 @@ SocketsIterator::Next()
|
|||
|
||||
void
|
||||
CookieRef::ReInit( const char* cookie, const char* connName, CookieID id,
|
||||
int langCode )
|
||||
int langCode, int nPlayers )
|
||||
{
|
||||
m_cookie = cookie==NULL?"":cookie;
|
||||
m_connName = connName==NULL?"":connName;
|
||||
|
@ -89,7 +89,7 @@ CookieRef::ReInit( const char* cookie, const char* connName, CookieID id,
|
|||
m_totalSent = 0;
|
||||
m_curState = XWS_INITED;
|
||||
m_nextHostID = HOST_ID_SERVER;
|
||||
m_nPlayersSought = 0;
|
||||
m_nPlayersSought = nPlayers;
|
||||
m_nPlayersHere = 0;
|
||||
m_locking_thread = 0;
|
||||
m_starttime = uptime();
|
||||
|
@ -111,10 +111,10 @@ CookieRef::ReInit( const char* cookie, const char* connName, CookieID id,
|
|||
|
||||
|
||||
CookieRef::CookieRef( const char* cookie, const char* connName, CookieID id,
|
||||
int langCode )
|
||||
int langCode, int nPlayers )
|
||||
{
|
||||
pthread_mutex_init( &m_mutex, NULL );
|
||||
ReInit( cookie, connName, id, langCode );
|
||||
ReInit( cookie, connName, id, langCode, nPlayers );
|
||||
}
|
||||
|
||||
CookieRef::~CookieRef()
|
||||
|
@ -553,11 +553,6 @@ CookieRef::handleEvents()
|
|||
checkHaveRoom( &evt );
|
||||
break;
|
||||
|
||||
case XWA_SEND_INITRSP:
|
||||
setAllConnectedTimer();
|
||||
sendResponse( &evt, true );
|
||||
break;
|
||||
|
||||
case XWA_SEND_CONNRSP:
|
||||
if ( increasePlayerCounts( &evt, false ) ) {
|
||||
setAllConnectedTimer();
|
||||
|
@ -659,14 +654,6 @@ CookieRef::handleEvents()
|
|||
}
|
||||
break;
|
||||
|
||||
case XWA_INITGAME: {
|
||||
initPlayerCounts( &evt );
|
||||
CRefEvent initedevt( XWE_INITTEDGAME );
|
||||
initedevt.u.con = evt.u.con;
|
||||
m_eventQueue.push_back( initedevt );
|
||||
}
|
||||
break;
|
||||
|
||||
case XWA_NONE:
|
||||
/* nothing to do for these */
|
||||
break;
|
||||
|
@ -759,37 +746,19 @@ CookieRef::send_stored_messages( HostID dest, int socket )
|
|||
}
|
||||
} /* send_stored_messages */
|
||||
|
||||
void
|
||||
CookieRef::initPlayerCounts( const CRefEvent* evt )
|
||||
{
|
||||
assert( evt->type == XWE_DEVCONNECT );
|
||||
assert( m_nPlayersSought == 0 && m_nPlayersHere == 0 );
|
||||
m_nPlayersSought = evt->u.con.nPlayersS;
|
||||
m_nPlayersHere = evt->u.con.nPlayersH;
|
||||
|
||||
HostRec hr( evt->u.con.srcID, evt->u.con.socket, evt->u.con.nPlayersH,
|
||||
evt->u.con.nPlayersS, evt->u.con.seed );
|
||||
m_sockets.push_back( hr );
|
||||
|
||||
assert( !AlreadyHere( evt->u.con.seed, -1 ) );
|
||||
m_seeds.push_back( evt->u.con.seed );
|
||||
}
|
||||
|
||||
bool
|
||||
CookieRef::increasePlayerCounts( const CRefEvent* evt, bool reconn )
|
||||
{
|
||||
int nPlayersH = evt->u.con.nPlayersH;
|
||||
int nPlayersS = evt->u.con.nPlayersS;
|
||||
assert( nPlayersS > 0 ); /* catch earlier; assert here */
|
||||
int socket = evt->u.con.socket;
|
||||
int seed = evt->u.con.seed;
|
||||
bool addHost = false;
|
||||
/* XW_RELAY_EVENT newEvt = XWE_NONE; */
|
||||
|
||||
assert( m_nPlayersSought > 0 );
|
||||
assert( m_nPlayersSought == evt->u.con.nPlayersS );
|
||||
|
||||
logf( XW_LOGINFO, "%s: nPlayersH=%d, "
|
||||
"nPlayersS=%d", __func__, nPlayersH, nPlayersS );
|
||||
logf( XW_LOGINFO, "%s: nPlayersH=%d", __func__, nPlayersH );
|
||||
|
||||
ASSERT_LOCKED();
|
||||
|
||||
|
@ -804,15 +773,6 @@ CookieRef::increasePlayerCounts( const CRefEvent* evt, bool reconn )
|
|||
mucking with me. */
|
||||
|
||||
if ( reconn ) {
|
||||
if ( nPlayersS > 0 ) {
|
||||
if ( 0 != m_nPlayersSought ) {
|
||||
logf( XW_LOGERROR,
|
||||
"already have m_nPlayersSought: %d but new value %d",
|
||||
m_nPlayersSought, nPlayersS );
|
||||
goto drop;
|
||||
}
|
||||
m_nPlayersSought = nPlayersS;
|
||||
}
|
||||
if ( 0 != m_nPlayersSought &&
|
||||
m_nPlayersHere + nPlayersH > m_nPlayersSought ) {
|
||||
logf( XW_LOGERROR, "too many new players provided: %d > %d",
|
||||
|
@ -828,7 +788,6 @@ CookieRef::increasePlayerCounts( const CRefEvent* evt, bool reconn )
|
|||
/* } */
|
||||
addHost = true;
|
||||
} else { /* a host; init values */
|
||||
assert( m_nPlayersSought == nPlayersS );
|
||||
m_nPlayersHere += nPlayersH;
|
||||
assert( m_nPlayersHere <= m_nPlayersSought );
|
||||
addHost = true;
|
||||
|
@ -872,7 +831,7 @@ CookieRef::increasePlayerCounts( const CRefEvent* evt, bool reconn )
|
|||
"socket=%d (size=%d)",
|
||||
__func__, hostid, socket, m_sockets.size());
|
||||
|
||||
HostRec hr( hostid, socket, nPlayersH, nPlayersS, seed );
|
||||
HostRec hr( hostid, socket, nPlayersH, seed );
|
||||
m_sockets.push_back( hr );
|
||||
|
||||
assert( !AlreadyHere( evt->u.con.seed, -1 ) );
|
||||
|
@ -1122,23 +1081,19 @@ CookieRef::checkSomeMissing( void )
|
|||
{
|
||||
logf( XW_LOGINFO, "%s", __func__ );
|
||||
int count = 0;
|
||||
int nPlayersS;
|
||||
|
||||
vector<HostRec>::iterator iter;
|
||||
for ( iter = m_sockets.begin(); iter != m_sockets.end(); ++iter ) {
|
||||
count += iter->m_nPlayersH;
|
||||
if ( iter != m_sockets.begin() ) {
|
||||
assert( iter->m_nPlayersS == nPlayersS );
|
||||
} else {
|
||||
nPlayersS = iter->m_nPlayersS;
|
||||
}
|
||||
}
|
||||
/* Don't really need the iterator above.... */
|
||||
assert( count == m_nPlayersHere );
|
||||
|
||||
logf( XW_LOGINFO, "%s; count=%d; nPlayersS=%d", __func__,
|
||||
count, nPlayersS );
|
||||
count, m_nPlayersSought );
|
||||
|
||||
assert( count <= nPlayersS );
|
||||
if ( count < nPlayersS ) {
|
||||
assert( count <= m_nPlayersSought );
|
||||
if ( count < m_nPlayersSought ) {
|
||||
CRefEvent evt( XWE_SOMEMISSING );
|
||||
m_eventQueue.push_back( evt );
|
||||
}
|
||||
|
|
|
@ -40,12 +40,10 @@ class CookieMapIterator; /* forward */
|
|||
|
||||
struct HostRec {
|
||||
public:
|
||||
HostRec(HostID hostID, int socket, int nPlayersH, int nPlayersS,
|
||||
int seed )
|
||||
HostRec(HostID hostID, int socket, int nPlayersH, int seed )
|
||||
: m_hostID(hostID)
|
||||
, m_socket(socket)
|
||||
, m_nPlayersH(nPlayersH)
|
||||
, m_nPlayersS(nPlayersS)
|
||||
, m_seed(seed)
|
||||
, m_lastHeartbeat(uptime())
|
||||
{
|
||||
|
@ -54,7 +52,6 @@ struct HostRec {
|
|||
HostID m_hostID;
|
||||
int m_socket;
|
||||
int m_nPlayersH;
|
||||
int m_nPlayersS;
|
||||
int m_seed;
|
||||
time_t m_lastHeartbeat;
|
||||
};
|
||||
|
@ -69,9 +66,9 @@ class CookieRef {
|
|||
friend class CookieMapIterator;
|
||||
|
||||
CookieRef( const char* cookie, const char* connName, CookieID id,
|
||||
int langCode );
|
||||
int langCode, int nPlayersH );
|
||||
void ReInit( const char* cookie, const char* connName, CookieID id,
|
||||
int langCode );
|
||||
int langCode, int nPlayers );
|
||||
~CookieRef();
|
||||
|
||||
void Clear(void); /* make clear it's unused */
|
||||
|
|
|
@ -352,9 +352,8 @@ CRefMgr::getMakeCookieRef_locked( const char* cookie, const char* connName,
|
|||
gameSeed, langCode, &alreadyHere );
|
||||
if ( cref == NULL && !alreadyHere ) {
|
||||
CookieID cid = nextCID( NULL );
|
||||
cref = AddNew( cookie, connName, cid, langCode );
|
||||
m_db->AddNew( cookie, cref->ConnName(), cid, langCode,
|
||||
nPlayersT, nPlayersH );
|
||||
cref = AddNew( cookie, connName, cid, langCode, nPlayersT );
|
||||
m_db->AddNew( cookie, cref->ConnName(), cid, langCode, nPlayersT );
|
||||
}
|
||||
|
||||
return cref;
|
||||
|
@ -507,7 +506,7 @@ CRefMgr::heartbeatProc( void* closure )
|
|||
|
||||
CookieRef*
|
||||
CRefMgr::AddNew( const char* cookie, const char* connName, CookieID id,
|
||||
int langCode )
|
||||
int langCode, int nPlayers )
|
||||
{
|
||||
/* PENDING: should this return a locked cref? */
|
||||
logf( XW_LOGINFO, "%s( cookie=%s, connName=%s, cid=%d)", __func__,
|
||||
|
@ -520,10 +519,10 @@ CRefMgr::AddNew( const char* cookie, const char* connName, CookieID id,
|
|||
|
||||
if ( !!ref ) {
|
||||
logf( XW_LOGVERBOSE1, "using from free list" );
|
||||
ref->ReInit( cookie, connName, id, langCode );
|
||||
ref->ReInit( cookie, connName, id, langCode, nPlayers );
|
||||
} else {
|
||||
logf( XW_LOGVERBOSE1, "calling constructor" );
|
||||
ref = new CookieRef( cookie, connName, id, langCode );
|
||||
ref = new CookieRef( cookie, connName, id, langCode, nPlayers );
|
||||
}
|
||||
|
||||
ref->assignConnName();
|
||||
|
|
|
@ -144,7 +144,7 @@ class CRefMgr {
|
|||
bool checkCookieRef_locked( CookieRef* cref );
|
||||
CookieRef* getCookieRef_impl( CookieID cookieID );
|
||||
CookieRef* AddNew( const char* cookie, const char* connName, CookieID id,
|
||||
int langCode );
|
||||
int langCode, int nPlayers );
|
||||
CookieRef* FindOpenGameFor( const char* cookie, const char* connName,
|
||||
HostID hid, int socket, int nPlayersH,
|
||||
int nPlayersS, int gameSeed, int langCode,
|
||||
|
|
|
@ -71,7 +71,7 @@ DBMgr::~DBMgr()
|
|||
|
||||
void
|
||||
DBMgr::AddNew( const char* cookie, const char* connName, CookieID cid,
|
||||
int langCode, int nPlayersT, int nPlayersH )
|
||||
int langCode, int nPlayersT )
|
||||
{
|
||||
|
||||
#if 1
|
||||
|
@ -83,14 +83,13 @@ DBMgr::AddNew( const char* cookie, const char* connName, CookieID cid,
|
|||
"VALUES( %d, '%s', '%s', %d, %d, %d, 'now' )";
|
||||
char buf[256];
|
||||
snprintf( buf, sizeof(buf), fmt, cid/*m_nextCID++*/, cookie, connName,
|
||||
nPlayersT, nPlayersH, langCode );
|
||||
nPlayersT, 0, langCode );
|
||||
logf( XW_LOGINFO, "passing %s", buf );
|
||||
PGresult* result = PQexec( m_pgconn, buf );
|
||||
PQclear( result );
|
||||
#else
|
||||
const char* command = "INSERT INTO games (cookie, connName, ntotal, nhere, lang) "
|
||||
"VALUES( $1, $2, $3, $4, $5 )";
|
||||
char nPlayersHBuf[4];
|
||||
char nPlayersTBuf[4];
|
||||
char langBuf[4];
|
||||
|
||||
|
@ -161,5 +160,10 @@ DBMgr::AddPlayers( const char* connName, int nToAdd )
|
|||
ctime TIMESTAMP,
|
||||
mtime TIMESTAMP
|
||||
);
|
||||
|
||||
May also want
|
||||
seeds INTEGER ARRAY,
|
||||
ipAddresses INTEGER ARRAY,
|
||||
|
||||
|
||||
*/
|
||||
|
|
|
@ -31,7 +31,7 @@ class DBMgr {
|
|||
~DBMgr();
|
||||
|
||||
void AddNew( const char* cookie, const char* connName, CookieID cid,
|
||||
int langCode, int nPlayersT, int nPlayersH );
|
||||
int langCode, int nPlayersT );
|
||||
|
||||
CookieID FindOpen( const char* cookie, int lang, int nPlayersT,
|
||||
int nPlayersH );
|
||||
|
|
|
@ -62,10 +62,9 @@ typedef struct StateTable {
|
|||
|
||||
static StateTable g_stateTable[] = {
|
||||
|
||||
{ XWS_INITED, XWE_DEVCONNECT, XWA_INITGAME, XWS_INITINGGAME },
|
||||
{ XWS_INITINGGAME, XWE_INITTEDGAME, XWA_SEND_INITRSP, XWS_WAITMORE },
|
||||
|
||||
{ XWS_INITED, XWE_DEVCONNECT, XWA_SEND_CONNRSP, XWS_CHK_ALLHERE },
|
||||
{ XWS_WAITMORE, XWE_DEVCONNECT, XWA_SEND_CONNRSP, XWS_CHK_ALLHERE },
|
||||
|
||||
{ XWS_WAITMORE, XWE_RECONNECT, XWA_SEND_RERSP, XWS_WAITMORE },
|
||||
|
||||
{ XWS_ALLCONND, XWE_RECONNECT, XWA_SEND_RERSP, XWS_ALLCONND },
|
||||
|
@ -182,7 +181,6 @@ stateString( XW_RELAY_STATE state )
|
|||
CASESTR(XWS_CHK_ALLHERE_2);
|
||||
CASESTR(XWS_CHKCOUNTS_INIT);
|
||||
CASESTR(XWS_ROOMCHK);
|
||||
CASESTR(XWS_INITINGGAME);
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
|
@ -217,7 +215,6 @@ eventString( XW_RELAY_EVENT evt )
|
|||
CASESTR(XWE_SOMEMISSING);
|
||||
CASESTR(XWE_TOO_MANY);
|
||||
CASESTR(XWE_HAVE_ROOM);
|
||||
CASESTR(XWE_INITTEDGAME);
|
||||
|
||||
CASESTR(XWE_SHUTDOWN);
|
||||
default:
|
||||
|
@ -251,7 +248,6 @@ actString( XW_RELAY_ACTION act )
|
|||
CASESTR(XWA_SHUTDOWN);
|
||||
CASESTR(XWA_CHECK_HAVE_ROOM);
|
||||
CASESTR(XWA_NOTE_EMPTY);
|
||||
CASESTR(XWA_INITGAME);
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
|
|
|
@ -64,8 +64,6 @@ enum {
|
|||
,XWS_ROOMCHK /* do we have room for as many players as are
|
||||
being provided */
|
||||
|
||||
,XWS_INITINGGAME
|
||||
|
||||
,XWS_DEAD /* About to kill the object */
|
||||
} XW_RELAY_STATE;
|
||||
|
||||
|
@ -105,8 +103,6 @@ typedef enum {
|
|||
hosts */
|
||||
,XWE_SHUTDOWN /* shutdown this game */
|
||||
|
||||
,XWE_INITTEDGAME
|
||||
|
||||
,XWE_ANY /* wildcard; matches all */
|
||||
} XW_RELAY_EVENT;
|
||||
|
||||
|
@ -149,8 +145,6 @@ typedef enum {
|
|||
|
||||
,XWA_SHUTDOWN
|
||||
|
||||
,XWA_INITGAME
|
||||
|
||||
} XW_RELAY_ACTION;
|
||||
|
||||
bool getFromTable( XW_RELAY_STATE curState, XW_RELAY_EVENT curEvent,
|
||||
|
|
Loading…
Add table
Reference in a new issue