mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-02-05 20:45:49 +01:00
rename states so can format to 80 cols; add log level param. No
change to functionality in this checkin.
This commit is contained in:
parent
2b58da4cdf
commit
bf1264b2ee
12 changed files with 275 additions and 335 deletions
|
@ -102,7 +102,7 @@ RelayConfigs::RelayConfigs( const char* cfile )
|
|||
} else if ( 0 == strcmp( line, "SERVERNAME" ) ) {
|
||||
m_serverName = value;
|
||||
} else {
|
||||
logf( "unknown key %s with value %s\n",
|
||||
logf( XW_LOGERROR, "unknown key %s with value %s\n",
|
||||
line, value );
|
||||
assert( 0 );
|
||||
}
|
||||
|
|
138
relay/cref.cpp
138
relay/cref.cpp
|
@ -74,8 +74,8 @@ CookieRef::CookieRef( const char* cookie, const char* connName, CookieID id )
|
|||
, m_connName(connName)
|
||||
, m_cookieID(id)
|
||||
, m_totalSent(0)
|
||||
, m_curState(XW_ST_INITED)
|
||||
, m_nextState(XW_ST_INITED)
|
||||
, m_curState(XWS_INITED)
|
||||
, m_nextState(XWS_INITED)
|
||||
, m_eventQueue()
|
||||
, m_nextHostID(HOST_ID_SERVER)
|
||||
, m_nPlayersTotal(0)
|
||||
|
@ -102,7 +102,7 @@ CookieRef::~CookieRef()
|
|||
m_sockets.erase( iter );
|
||||
}
|
||||
|
||||
logf( "CookieRef for %d being deleted; sent %d bytes",
|
||||
logf( XW_LOGINFO, "CookieRef for %d being deleted; sent %d bytes",
|
||||
m_cookieID, m_totalSent );
|
||||
} /* ~CookieRef */
|
||||
|
||||
|
@ -112,7 +112,7 @@ CookieRef::_Connect( int socket, HostID hid, int nPlayersH, int nPlayersT )
|
|||
CRefMgr::Get()->Associate( socket, this );
|
||||
if ( hid == HOST_ID_NONE ) {
|
||||
hid = nextHostID();
|
||||
logf( "assigned host id: %x", hid );
|
||||
logf( XW_LOGINFO, "assigned host id: %x", hid );
|
||||
}
|
||||
pushConnectEvent( socket, hid, nPlayersH, nPlayersT );
|
||||
handleEvents();
|
||||
|
@ -135,7 +135,7 @@ CookieRef::_Disconnect( int socket, HostID hostID )
|
|||
/* MutexLock ml( &m_EventsMutex ); */
|
||||
|
||||
CRefEvent evt;
|
||||
evt.type = XW_EVT_DISCONNECTMSG;
|
||||
evt.type = XWE_DISCONNMSG;
|
||||
evt.u.discon.socket = socket;
|
||||
evt.u.discon.srcID = hostID;
|
||||
m_eventQueue.push_back( evt );
|
||||
|
@ -152,9 +152,9 @@ CookieRef::SocketForHost( HostID dest )
|
|||
socket = -1;
|
||||
} else {
|
||||
socket = iter->second.m_socket;
|
||||
logf( "socketForHost(%x) => %d", dest, socket );
|
||||
logf( XW_LOGINFO, "socketForHost(%x) => %d", dest, socket );
|
||||
}
|
||||
logf( "returning socket=%d for hostid=%x", socket, dest );
|
||||
logf( XW_LOGINFO, "returning socket=%d for hostid=%x", socket, dest );
|
||||
return socket;
|
||||
}
|
||||
|
||||
|
@ -166,8 +166,8 @@ CookieRef::SocketForHost( HostID dest )
|
|||
int
|
||||
CookieRef::NeverFullyConnected()
|
||||
{
|
||||
return m_curState != XW_ST_ALLCONNECTED
|
||||
&& m_curState != XW_ST_MISSING;
|
||||
return m_curState != XWS_ALLCONNECTED
|
||||
&& m_curState != XWS_MISSING;
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -176,13 +176,13 @@ CookieRef::AcceptingReconnections( HostID hid, int nPlayersH, int nPlayersT )
|
|||
int accept = 0;
|
||||
/* First, do we have room. Second, are we missing this guy? */
|
||||
|
||||
if ( m_curState != XW_ST_INITED
|
||||
&& m_curState != XW_ST_CONNECTING
|
||||
&& m_curState != XW_ST_MISSING ) {
|
||||
if ( m_curState != XWS_INITED
|
||||
&& m_curState != XWS_CONNECTING
|
||||
&& m_curState != XWS_MISSING ) {
|
||||
/* do nothing; reject */
|
||||
logf( "reject: bad state %s", stateString(m_curState) );
|
||||
logf( XW_LOGINFO, "reject: bad state %s", stateString(m_curState) );
|
||||
} else if ( HostKnown( hid ) ) {
|
||||
logf( "reject: known hid" );
|
||||
logf( XW_LOGINFO, "reject: known hid" );
|
||||
/* do nothing: reject */
|
||||
} else {
|
||||
if ( m_nPlayersTotal == 0 ) {
|
||||
|
@ -190,7 +190,7 @@ CookieRef::AcceptingReconnections( HostID hid, int nPlayersH, int nPlayersT )
|
|||
} else if ( nPlayersH + m_nPlayersHere <= m_nPlayersTotal ) {
|
||||
accept = 1;
|
||||
} else {
|
||||
logf( "reject: m_nPlayersTotal=%d, m_nPlayersHere=%d",
|
||||
logf( XW_LOGINFO, "reject: m_nPlayersTotal=%d, m_nPlayersHere=%d",
|
||||
m_nPlayersTotal, m_nPlayersHere );
|
||||
}
|
||||
}
|
||||
|
@ -239,8 +239,6 @@ int
|
|||
CookieRef::HasSocket( int socket )
|
||||
{
|
||||
int found = 0;
|
||||
logf( "CookieRef::HasSocket" );
|
||||
/* RWReadLock rwl( &m_sockets_rwlock ); */
|
||||
|
||||
map<HostID,HostRec>::iterator iter = m_sockets.begin();
|
||||
while ( iter != m_sockets.end() ) {
|
||||
|
@ -264,7 +262,7 @@ CookieRef::_HandleHeartbeat( HostID id, int socket )
|
|||
void
|
||||
CookieRef::_CheckHeartbeats( time_t now )
|
||||
{
|
||||
logf( "CookieRef::_CheckHeartbeats" );
|
||||
logf( XW_LOGINFO, "CookieRef::_CheckHeartbeats" );
|
||||
/* MutexLock ml( &m_EventsMutex ); */
|
||||
{
|
||||
/* RWReadLock rwl( &m_sockets_rwlock ); */
|
||||
|
@ -301,7 +299,7 @@ CookieRef::pushConnectEvent( int socket, HostID srcID,
|
|||
int nPlayersH, int nPlayersT )
|
||||
{
|
||||
CRefEvent evt;
|
||||
evt.type = XW_EVT_CONNECTMSG;
|
||||
evt.type = XWE_CONNECTMSG;
|
||||
evt.u.con.socket = socket;
|
||||
evt.u.con.srcID = srcID;
|
||||
evt.u.con.nPlayersH = nPlayersH;
|
||||
|
@ -314,7 +312,7 @@ CookieRef::pushReconnectEvent( int socket, HostID srcID,
|
|||
int nPlayersH, int nPlayersT )
|
||||
{
|
||||
CRefEvent evt;
|
||||
evt.type = XW_EVT_RECONNECTMSG;
|
||||
evt.type = XWE_RECONNECTMSG;
|
||||
evt.u.con.socket = socket;
|
||||
evt.u.con.srcID = srcID;
|
||||
evt.u.con.nPlayersH = nPlayersH;
|
||||
|
@ -326,7 +324,7 @@ void
|
|||
CookieRef::pushHeartbeatEvent( HostID id, int socket )
|
||||
{
|
||||
CRefEvent evt;
|
||||
evt.type = XW_EVT_HEARTRCVD;
|
||||
evt.type = XWE_HEARTRCVD;
|
||||
evt.u.heart.id = id;
|
||||
evt.u.heart.socket = socket;
|
||||
m_eventQueue.push_back( evt );
|
||||
|
@ -336,7 +334,7 @@ void
|
|||
CookieRef::pushHeartFailedEvent( int socket )
|
||||
{
|
||||
CRefEvent evt;
|
||||
evt.type = XW_EVT_HEARTFAILED;
|
||||
evt.type = XWE_HEARTFAILED;
|
||||
evt.u.heart.socket = socket;
|
||||
m_eventQueue.push_back( evt );
|
||||
}
|
||||
|
@ -345,9 +343,9 @@ void
|
|||
CookieRef::pushForwardEvent( HostID src, HostID dest,
|
||||
unsigned char* buf, int buflen )
|
||||
{
|
||||
logf( "pushForwardEvent: %d -> %d", src, dest );
|
||||
logf( XW_LOGINFO, "pushForwardEvent: %d -> %d", src, dest );
|
||||
CRefEvent evt;
|
||||
evt.type = XW_EVT_FORWARDMSG;
|
||||
evt.type = XWE_FORWARDMSG;
|
||||
evt.u.fwd.src = src;
|
||||
evt.u.fwd.dest = dest;
|
||||
evt.u.fwd.buf = buf;
|
||||
|
@ -359,7 +357,7 @@ void
|
|||
CookieRef::pushRemoveSocketEvent( int socket )
|
||||
{
|
||||
CRefEvent evt;
|
||||
evt.type = XW_EVT_REMOVESOCKET;
|
||||
evt.type = XWE_REMOVESOCKET;
|
||||
evt.u.rmsock.socket = socket;
|
||||
m_eventQueue.push_back( evt );
|
||||
}
|
||||
|
@ -368,7 +366,7 @@ void
|
|||
CookieRef::pushNotifyDisconEvent( int socket, XWREASON why )
|
||||
{
|
||||
CRefEvent evt;
|
||||
evt.type = XW_EVT_NOTIFYDISCON;
|
||||
evt.type = XWE_NOTIFYDISCON;
|
||||
evt.u.disnote.socket = socket;
|
||||
evt.u.disnote.why = why;
|
||||
m_eventQueue.push_back( evt );
|
||||
|
@ -378,7 +376,7 @@ void
|
|||
CookieRef::pushDestBadEvent()
|
||||
{
|
||||
CRefEvent evt;
|
||||
evt.type = XW_EVT_DESTBAD;
|
||||
evt.type = XWE_DESTBAD;
|
||||
m_eventQueue.push_back( evt );
|
||||
}
|
||||
|
||||
|
@ -387,7 +385,7 @@ CookieRef::pushDestOkEvent( const CRefEvent* oldEvt )
|
|||
{
|
||||
CRefEvent evt;
|
||||
memcpy( &evt, oldEvt, sizeof(evt) );
|
||||
evt.type = XW_EVT_DESTOK;
|
||||
evt.type = XWE_DESTOK;
|
||||
m_eventQueue.push_back( evt );
|
||||
}
|
||||
|
||||
|
@ -396,7 +394,7 @@ CookieRef::pushCanLockEvent( const CRefEvent* oldEvt )
|
|||
{
|
||||
CRefEvent evt;
|
||||
memcpy( &evt, oldEvt, sizeof(evt) );
|
||||
evt.type = XW_EVT_CAN_LOCK;
|
||||
evt.type = XWE_CAN_LOCK;
|
||||
m_eventQueue.push_back( evt );
|
||||
}
|
||||
|
||||
|
@ -405,7 +403,7 @@ CookieRef::pushCantLockEvent( const CRefEvent* oldEvt )
|
|||
{
|
||||
CRefEvent evt;
|
||||
memcpy( &evt, oldEvt, sizeof(evt) );
|
||||
evt.type = XW_EVT_CANT_LOCK;
|
||||
evt.type = XWE_CANT_LOCK;
|
||||
m_eventQueue.push_back( evt );
|
||||
}
|
||||
|
||||
|
@ -413,7 +411,7 @@ void
|
|||
CookieRef::pushLastSocketGoneEvent()
|
||||
{
|
||||
CRefEvent evt;
|
||||
evt.type = XW_EVT_NOMORESOCKETS;
|
||||
evt.type = XWE_NOMORESOCKETS;
|
||||
m_eventQueue.push_back( evt );
|
||||
}
|
||||
|
||||
|
@ -428,83 +426,83 @@ CookieRef::handleEvents()
|
|||
XW_RELAY_ACTION takeAction;
|
||||
if ( getFromTable( m_curState, evt.type, &takeAction, &m_nextState ) ) {
|
||||
|
||||
logf( "cid %d: moving from state %s to state %s", m_cookieID,
|
||||
logf( XW_LOGINFO, "cid %d: moving from state %s to state %s", m_cookieID,
|
||||
stateString(m_curState), stateString(m_nextState) );
|
||||
logf( "event = %s, action = %s", eventString(evt.type),
|
||||
logf( XW_LOGINFO, "event = %s, action = %s", eventString(evt.type),
|
||||
actString(takeAction) );
|
||||
|
||||
switch( takeAction ) {
|
||||
|
||||
case XW_ACT_CHKCOUNTS:
|
||||
case XWA_CHKCOUNTS:
|
||||
checkCounts( &evt );
|
||||
break;
|
||||
|
||||
case XW_ACT_SEND_1ST_RSP:
|
||||
case XW_ACT_SEND_1ST_RERSP:
|
||||
case XWA_SEND_1ST_RSP:
|
||||
case XWA_SEND_1ST_RERSP:
|
||||
setAllConnectedTimer();
|
||||
increasePlayerCounts( &evt );
|
||||
sendResponse( &evt, takeAction == XW_ACT_SEND_1ST_RSP );
|
||||
sendResponse( &evt, takeAction == XWA_SEND_1ST_RSP );
|
||||
break;
|
||||
|
||||
case XW_ACT_SEND_RSP:
|
||||
case XW_ACT_SEND_RERSP:
|
||||
case XWA_SEND_RSP:
|
||||
case XWA_SEND_RERSP:
|
||||
increasePlayerCounts( &evt );
|
||||
sendResponse( &evt, takeAction == XW_ACT_SEND_RSP );
|
||||
sendResponse( &evt, takeAction == XWA_SEND_RSP );
|
||||
break;
|
||||
|
||||
case XW_ACT_FWD:
|
||||
case XWA_FWD:
|
||||
forward( &evt );
|
||||
break;
|
||||
|
||||
case XW_ACT_CHECKDEST:
|
||||
case XWA_CHECKDEST:
|
||||
checkDest( &evt );
|
||||
break;
|
||||
|
||||
case XW_ACT_CHECK_CAN_LOCK:
|
||||
case XWA_CHECK_CAN_LOCK:
|
||||
checkFromServer( &evt );
|
||||
break;
|
||||
|
||||
case XW_ACT_TIMERDISCONNECT:
|
||||
case XWA_TIMERDISCONN:
|
||||
disconnectSockets( 0, XWRELAY_ERROR_TIMEOUT );
|
||||
break;
|
||||
case XW_ACT_HEARTDISCONNECT:
|
||||
case XWA_HEARTDISCONN:
|
||||
notifyOthers( evt.u.heart.socket, XWRELAY_DISCONNECT_OTHER,
|
||||
XWRELAY_ERROR_HEART_OTHER );
|
||||
disconnectSockets( evt.u.heart.socket,
|
||||
XWRELAY_ERROR_HEART_YOU );
|
||||
break;
|
||||
|
||||
case XW_ACT_DISCONNECT:
|
||||
case XWA_DISCONNECT:
|
||||
reducePlayerCounts( evt.u.discon.socket );
|
||||
removeSocket( evt.u.discon.socket );
|
||||
/* Don't notify. This is a normal part of a game ending. */
|
||||
break;
|
||||
|
||||
case XW_ACT_NOTEHEART:
|
||||
case XWA_NOTEHEART:
|
||||
noteHeartbeat( &evt );
|
||||
break;
|
||||
|
||||
case XW_ACT_NOTIFYDISCON:
|
||||
case XWA_NOTIFYDISCON:
|
||||
notifyDisconn( &evt );
|
||||
break;
|
||||
|
||||
case XW_ACT_REMOVESOCKET:
|
||||
case XWA_REMOVESOCKET:
|
||||
reducePlayerCounts( evt.u.rmsock.socket );
|
||||
notifyOthers( evt.u.rmsock.socket, XWRELAY_DISCONNECT_OTHER,
|
||||
XWRELAY_ERROR_LOST_OTHER );
|
||||
removeSocket( evt.u.rmsock.socket );
|
||||
break;
|
||||
|
||||
case XW_ACT_SENDALLHERE:
|
||||
case XWA_SENDALLHERE:
|
||||
sendAllHere( 1 );
|
||||
break;
|
||||
case XW_ACT_2ND_SNDALLHERE:
|
||||
case XWA_SNDALLHERE_2:
|
||||
sendAllHere( 0 );
|
||||
break;
|
||||
|
||||
case XW_ACT_REJECT:
|
||||
case XWA_REJECT:
|
||||
|
||||
case XW_ACT_NONE:
|
||||
case XWA_NONE:
|
||||
/* nothing to do for these */
|
||||
break;
|
||||
|
||||
|
@ -549,7 +547,7 @@ CookieRef::increasePlayerCounts( const CRefEvent* evt )
|
|||
int nPlayersT = evt->u.con.nPlayersT;
|
||||
HostID hid = evt->u.con.srcID;
|
||||
|
||||
logf( "increasePlayerCounts: hid=%d, nPlayersH=%d, nPlayersT=%d",
|
||||
logf( XW_LOGINFO, "increasePlayerCounts: hid=%d, nPlayersH=%d, nPlayersT=%d",
|
||||
hid, nPlayersH, nPlayersT );
|
||||
|
||||
if ( hid == HOST_ID_SERVER ) {
|
||||
|
@ -561,19 +559,19 @@ CookieRef::increasePlayerCounts( const CRefEvent* evt )
|
|||
}
|
||||
m_nPlayersHere += nPlayersH;
|
||||
|
||||
logf( "increasePlayerCounts: here=%d; total=%d",
|
||||
logf( XW_LOGINFO, "increasePlayerCounts: here=%d; total=%d",
|
||||
m_nPlayersHere, m_nPlayersTotal );
|
||||
|
||||
CRefEvent newevt;
|
||||
newevt.type = (m_nPlayersHere == m_nPlayersTotal) ?
|
||||
XW_EVT_ALLHERE : XW_EVT_SOMEMISSING;
|
||||
XWE_ALLHERE : XWE_SOMEMISSING;
|
||||
m_eventQueue.push_back( newevt );
|
||||
} /* increasePlayerCounts */
|
||||
|
||||
void
|
||||
CookieRef::reducePlayerCounts( int socket )
|
||||
{
|
||||
logf( "reducePlayerCounts on socket %d", socket );
|
||||
logf( XW_LOGINFO, "reducePlayerCounts on socket %d", socket );
|
||||
map<HostID,HostRec>::iterator iter = m_sockets.begin();
|
||||
while ( iter != m_sockets.end() ) {
|
||||
|
||||
|
@ -587,7 +585,7 @@ CookieRef::reducePlayerCounts( int socket )
|
|||
}
|
||||
m_nPlayersHere -= iter->second.m_nPlayersH;
|
||||
|
||||
logf( "reducePlayerCounts: m_nPlayersHere=%d; m_nPlayersTotal=%d",
|
||||
logf( XW_LOGINFO, "reducePlayerCounts: m_nPlayersHere=%d; m_nPlayersTotal=%d",
|
||||
m_nPlayersHere, m_nPlayersTotal );
|
||||
|
||||
break;
|
||||
|
@ -606,7 +604,7 @@ CookieRef::checkCounts( const CRefEvent* evt )
|
|||
HostID hid = evt->u.con.srcID;
|
||||
int success;
|
||||
|
||||
logf( "checkCounts: hid=%d, nPlayers=%d, m_nPlayersTotal = %d, "
|
||||
logf( XW_LOGINFO, "checkCounts: hid=%d, nPlayers=%d, m_nPlayersTotal = %d, "
|
||||
"m_nPlayersHere = %d",
|
||||
hid, nPlayersH, m_nPlayersTotal, m_nPlayersHere );
|
||||
|
||||
|
@ -616,14 +614,14 @@ CookieRef::checkCounts( const CRefEvent* evt )
|
|||
success = (m_nPlayersTotal == 0) /* if no server present yet */
|
||||
|| (m_nPlayersTotal >= m_nPlayersHere + nPlayersH);
|
||||
}
|
||||
logf( "success = %d", success );
|
||||
logf( XW_LOGINFO, "success = %d", success );
|
||||
|
||||
CRefEvent newevt;
|
||||
if ( success ) {
|
||||
newevt = *evt;
|
||||
newevt.type = XW_EVT_OKTOSEND;
|
||||
newevt.type = XWE_OKTOSEND;
|
||||
} else {
|
||||
newevt.type = XW_EVT_COUNTSBAD;
|
||||
newevt.type = XWE_COUNTSBAD;
|
||||
}
|
||||
m_eventQueue.push_back( newevt );
|
||||
} /* checkCounts */
|
||||
|
@ -652,7 +650,7 @@ CookieRef::sendResponse( const CRefEvent* evt, int initial )
|
|||
int nPlayersT = evt->u.con.nPlayersT;
|
||||
|
||||
assert( id != HOST_ID_NONE );
|
||||
logf( "remembering pair: hostid=%x, socket=%d", id, socket );
|
||||
logf( XW_LOGINFO, "remembering pair: hostid=%x, socket=%d", id, socket );
|
||||
HostRec hr(socket, nPlayersH, nPlayersT);
|
||||
m_sockets.insert( pair<HostID,HostRec>(id,hr) );
|
||||
|
||||
|
@ -668,11 +666,11 @@ CookieRef::sendResponse( const CRefEvent* evt, int initial )
|
|||
*bufp++ = initial ? XWRELAY_CONNECT_RESP : XWRELAY_RECONNECT_RESP;
|
||||
putNetShort( &bufp, GetHeartbeat() );
|
||||
putNetShort( &bufp, GetCookieID() );
|
||||
logf( "writing hostID of %d into mgs", id );
|
||||
logf( XW_LOGINFO, "writing hostID of %d into mgs", id );
|
||||
*bufp++ = (char)id;
|
||||
|
||||
send_with_length( socket, buf, bufp - buf );
|
||||
logf( "sent XWRELAY_CONNECTRESP" );
|
||||
logf( XW_LOGINFO, "sent XWRELAY_CONNECTRESP" );
|
||||
} /* sendResponse */
|
||||
|
||||
void
|
||||
|
@ -736,7 +734,7 @@ CookieRef::send_msg( int socket, HostID id, XWRelayMsg msg, XWREASON why )
|
|||
len += 2;
|
||||
break;
|
||||
default:
|
||||
logf( "not handling message %d", msg );
|
||||
logf( XW_LOGINFO, "not handling message %d", msg );
|
||||
assert(0);
|
||||
}
|
||||
|
||||
|
@ -812,14 +810,14 @@ CookieRef::noteHeartbeat( const CRefEvent* evt )
|
|||
|
||||
map<HostID,HostRec>::iterator iter = m_sockets.find(id);
|
||||
if ( iter == m_sockets.end() ) {
|
||||
logf( "no socket for HostID %x", id );
|
||||
logf( XW_LOGERROR, "no socket for HostID %x", id );
|
||||
} else {
|
||||
|
||||
/* PENDING If the message came on an unexpected socket, kill the
|
||||
connection. An attack is the most likely explanation. */
|
||||
assert( iter->second.m_socket == socket );
|
||||
|
||||
logf( "upping m_lastHeartbeat from %d to %d",
|
||||
logf( XW_LOGINFO, "upping m_lastHeartbeat from %d to %d",
|
||||
iter->second.m_lastHeartbeat, now() );
|
||||
iter->second.m_lastHeartbeat = now();
|
||||
}
|
||||
|
@ -838,10 +836,10 @@ CookieRef::s_checkAllConnected( void* closure )
|
|||
void
|
||||
CookieRef::_CheckAllConnected()
|
||||
{
|
||||
logf( "checkAllConnected" );
|
||||
logf( XW_LOGINFO, "checkAllConnected" );
|
||||
/* MutexLock ml( &m_EventsMutex ); */
|
||||
CRefEvent newEvt;
|
||||
newEvt.type = XW_EVT_CONNTIMER;
|
||||
newEvt.type = XWE_CONNTIMER;
|
||||
m_eventQueue.push_back( newEvt );
|
||||
handleEvents();
|
||||
}
|
||||
|
|
|
@ -97,7 +97,7 @@ class CookieRef {
|
|||
void _Remove( int socket );
|
||||
void _CheckAllConnected();
|
||||
|
||||
int ShouldDie() { return m_curState == XW_ST_DEAD; }
|
||||
int ShouldDie() { return m_curState == XWS_DEAD; }
|
||||
|
||||
typedef struct CRefEvent {
|
||||
XW_RELAY_EVENT type;
|
||||
|
|
|
@ -64,7 +64,7 @@ CookieRef*
|
|||
CRefMgr::FindOpenGameFor( const char* cORn, int isCookie,
|
||||
HostID hid, int nPlayersH, int nPlayersT )
|
||||
{
|
||||
logf( "FindOpenGameFor with %s", cORn );
|
||||
logf( XW_LOGINFO, "FindOpenGameFor with %s", cORn );
|
||||
CookieRef* cref = NULL;
|
||||
RWReadLock rwl( &m_cookieMapRWLock );
|
||||
|
||||
|
@ -168,7 +168,7 @@ CRefMgr::Associate( int socket, CookieRef* cref )
|
|||
MutexLock ml( &m_SocketStuffMutex );
|
||||
SocketMap::iterator iter = m_SocketStuff.find( socket );
|
||||
if ( iter != m_SocketStuff.end() ) {
|
||||
logf( "replacing existing cref/threadID pair for socket %d", socket );
|
||||
logf( XW_LOGINFO, "replacing existing cref/threadID pair for socket %d", socket );
|
||||
}
|
||||
|
||||
SocketStuff* stuff = new SocketStuff( cref );
|
||||
|
@ -181,7 +181,7 @@ CRefMgr::Disassociate( int socket, CookieRef* cref )
|
|||
MutexLock ml( &m_SocketStuffMutex );
|
||||
SocketMap::iterator iter = m_SocketStuff.find( socket );
|
||||
if ( iter == m_SocketStuff.end() ) {
|
||||
logf( "can't find cref/threadID pair for socket %d", socket );
|
||||
logf( XW_LOGERROR, "can't find cref/threadID pair for socket %d", socket );
|
||||
} else {
|
||||
SocketStuff* stuff = iter->second;
|
||||
assert( stuff->m_cref == cref );
|
||||
|
@ -199,7 +199,7 @@ CRefMgr::GetWriteMutexForSocket( int socket )
|
|||
SocketStuff* stuff = iter->second;
|
||||
return &stuff->m_writeMutex;
|
||||
}
|
||||
logf( "GetWriteMutexForSocket: not found" );
|
||||
logf( XW_LOGERROR, "GetWriteMutexForSocket: not found" );
|
||||
return NULL;
|
||||
} /* GetWriteMutexForSocket */
|
||||
|
||||
|
@ -280,7 +280,7 @@ CRefMgr::checkCookieRef_locked( CookieRef* cref )
|
|||
pthread_mutex_lock( &m_guard );
|
||||
|
||||
pthread_mutex_t* cref_mutex = m_crefMutexes[cref];
|
||||
logf( "checkCookieRef_locked: cref_mutex=%p", cref_mutex );
|
||||
logf( XW_LOGINFO, "checkCookieRef_locked: cref_mutex=%p", cref_mutex );
|
||||
|
||||
if ( cref_mutex == NULL ) {
|
||||
pthread_mutex_unlock( &m_guard );
|
||||
|
@ -321,10 +321,10 @@ CRefMgr::AddNew( const char* cookie, const char* connName, CookieID id )
|
|||
assert( exists == NULL );
|
||||
|
||||
RWWriteLock rwl( &m_cookieMapRWLock );
|
||||
logf( "making new cref: %d", id );
|
||||
logf( XW_LOGINFO, "making new cref: %d", id );
|
||||
CookieRef* ref = new CookieRef( cookie, connName, id );
|
||||
m_cookieMap.insert( pair<CookieID, CookieRef*>(ref->GetCookieID(), ref ) );
|
||||
logf( "paired cookie %s/connName %s with id %d",
|
||||
logf( XW_LOGINFO, "paired cookie %s/connName %s with id %d",
|
||||
(cookie?cookie:"NULL"), connName, ref->GetCookieID() );
|
||||
return ref;
|
||||
} /* AddNew */
|
||||
|
@ -338,7 +338,7 @@ CRefMgr::Delete( CookieRef* cref )
|
|||
while ( iter != m_cookieMap.end() ) {
|
||||
CookieRef* ref = iter->second;
|
||||
if ( ref == cref ) {
|
||||
logf( "erasing cref" );
|
||||
logf( XW_LOGINFO, "erasing cref" );
|
||||
m_cookieMap.erase( iter );
|
||||
break;
|
||||
}
|
||||
|
@ -354,7 +354,7 @@ CRefMgr::Delete( CookieRef* cref )
|
|||
iter2 = m_crefMutexes.find(cref);
|
||||
m_crefMutexes.erase( iter2 );
|
||||
|
||||
logf( "CRefMgr::Delete done" );
|
||||
logf( XW_LOGINFO, "CRefMgr::Delete done" );
|
||||
}
|
||||
|
||||
void
|
||||
|
|
|
@ -237,7 +237,7 @@ print_sockets( int out, int sought )
|
|||
static int
|
||||
cmd_print( int socket, const char** args )
|
||||
{
|
||||
logf( "cmd_print called" );
|
||||
logf( XW_LOGINFO, "cmd_print called" );
|
||||
int found = 0;
|
||||
if ( 0 == strcmp( "cookie", args[1] ) ) {
|
||||
if ( 0 == strcmp( "all", args[2] ) ) {
|
||||
|
@ -363,12 +363,12 @@ ctrl_thread_main( void* arg )
|
|||
void
|
||||
run_ctrl_thread( int ctrl_listener )
|
||||
{
|
||||
logf( "calling accept on socket %d\n", ctrl_listener );
|
||||
logf( XW_LOGINFO, "calling accept on socket %d\n", ctrl_listener );
|
||||
|
||||
sockaddr newaddr;
|
||||
socklen_t siz = sizeof(newaddr);
|
||||
int newSock = accept( ctrl_listener, &newaddr, &siz );
|
||||
logf( "got one for ctrl: %d", newSock );
|
||||
logf( XW_LOGINFO, "got one for ctrl: %d", newSock );
|
||||
|
||||
pthread_t thread;
|
||||
int result = pthread_create( &thread, NULL,
|
||||
|
|
255
relay/states.cpp
255
relay/states.cpp
|
@ -61,125 +61,62 @@ typedef struct StateTable {
|
|||
|
||||
StateTable g_stateTable[] = {
|
||||
|
||||
{ XW_ST_INITED, XW_EVT_CONNECTMSG, XW_ACT_CHKCOUNTS, XW_ST_CHKCOUNTS_INIT },
|
||||
{ XW_ST_CHKCOUNTS_INIT, XW_EVT_OKTOSEND, XW_ACT_SEND_1ST_RSP, XW_ST_CHK_ALLHERE },
|
||||
{ XW_ST_CHKCOUNTS_INIT, XW_EVT_COUNTSBAD, XW_ACT_REJECT, XW_ST_INITED },
|
||||
{ XWS_INITED, XWE_CONNECTMSG, XWA_CHKCOUNTS, XWS_CHKCOUNTS_INIT },
|
||||
{ XWS_CHKCOUNTS_INIT, XWE_OKTOSEND, XWA_SEND_1ST_RSP, XWS_CHK_ALLHERE },
|
||||
{ XWS_CHKCOUNTS_INIT, XWE_COUNTSBAD, XWA_REJECT, XWS_INITED },
|
||||
{ XWS_CONNECTING, XWE_CONNECTMSG, XWA_CHKCOUNTS, XWS_CHKCOUNTS },
|
||||
{ XWS_CHKCOUNTS, XWE_OKTOSEND, XWA_SEND_1ST_RSP, XWS_CHK_ALLHERE },
|
||||
{ XWS_CHKCOUNTS, XWE_COUNTSBAD, XWA_REJECT, XWS_CONNECTING },
|
||||
|
||||
{ XW_ST_CONNECTING, XW_EVT_CONNECTMSG, XW_ACT_CHKCOUNTS, XW_ST_CHKCOUNTS },
|
||||
{ XW_ST_CHKCOUNTS, XW_EVT_OKTOSEND, XW_ACT_SEND_1ST_RSP, XW_ST_CHK_ALLHERE },
|
||||
{ XW_ST_CHKCOUNTS, XW_EVT_COUNTSBAD, XW_ACT_REJECT, XW_ST_CONNECTING },
|
||||
|
||||
{ XW_ST_MISSING, XW_EVT_CONNECTMSG, XW_ACT_CHKCOUNTS, XW_ST_CHKCOUNTS_MISS },
|
||||
{ XW_ST_CHKCOUNTS_MISS, XW_EVT_OKTOSEND, XW_ACT_SEND_1ST_RSP, XW_ST_CHK_2ND_ALLHERE },
|
||||
{ XW_ST_CHKCOUNTS_MISS, XW_EVT_COUNTSBAD, XW_ACT_REJECT, XW_ST_MISSING },
|
||||
{ XWS_MISSING, XWE_CONNECTMSG, XWA_CHKCOUNTS, XWS_CHKCOUNTS_MISS },
|
||||
{ XWS_CHKCOUNTS_MISS, XWE_OKTOSEND, XWA_SEND_1ST_RSP, XWS_CHK_ALLHERE_2 },
|
||||
{ XWS_CHKCOUNTS_MISS, XWE_COUNTSBAD, XWA_REJECT, XWS_MISSING },
|
||||
|
||||
|
||||
{ XW_ST_CONNECTING, XW_EVT_CONNECTMSG, XW_ACT_SEND_RSP, XW_ST_CHK_ALLHERE },
|
||||
{ XW_ST_CHK_ALLHERE, XW_EVT_ALLHERE, XW_ACT_SENDALLHERE, XW_ST_ALLCONNECTED },
|
||||
{ XW_ST_CHK_ALLHERE, XW_EVT_SOMEMISSING, XW_ACT_NONE, XW_ST_CONNECTING },
|
||||
{ XWS_CONNECTING, XWE_CONNECTMSG, XWA_SEND_RSP, XWS_CHK_ALLHERE },
|
||||
{ XWS_CHK_ALLHERE, XWE_ALLHERE, XWA_SENDALLHERE, XWS_ALLCONNECTED },
|
||||
{ XWS_CHK_ALLHERE, XWE_SOMEMISSING, XWA_NONE, XWS_CONNECTING },
|
||||
|
||||
{ XW_ST_ALLCONNECTED, XW_EVT_DISCONNECTMSG, XW_ACT_DISCONNECT, XW_ST_MISSING },
|
||||
{ XW_ST_CONNECTING, XW_EVT_DISCONNECTMSG, XW_ACT_DISCONNECT, XW_ST_CONNECTING },
|
||||
{ XW_ST_MISSING, XW_EVT_DISCONNECTMSG, XW_ACT_DISCONNECT, XW_ST_MISSING },
|
||||
{ XW_ST_MISSING, XW_EVT_NOMORESOCKETS, XW_ACT_NONE, XW_ST_DEAD },
|
||||
{ XW_ST_CONNECTING, XW_EVT_NOMORESOCKETS, XW_ACT_NONE, XW_ST_DEAD },
|
||||
{ XWS_ALLCONNECTED, XWE_DISCONNMSG, XWA_DISCONNECT, XWS_MISSING },
|
||||
{ XWS_CONNECTING, XWE_DISCONNMSG, XWA_DISCONNECT, XWS_CONNECTING },
|
||||
{ XWS_MISSING, XWE_DISCONNMSG, XWA_DISCONNECT, XWS_MISSING },
|
||||
{ XWS_MISSING, XWE_NOMORESOCKETS, XWA_NONE, XWS_DEAD },
|
||||
{ XWS_CONNECTING, XWE_NOMORESOCKETS, XWA_NONE, XWS_DEAD },
|
||||
|
||||
{ XW_ST_INITED, XW_EVT_RECONNECTMSG, XW_ACT_SEND_RERSP, XW_ST_CHK_2ND_ALLHERE },
|
||||
{ XW_ST_MISSING, XW_EVT_RECONNECTMSG, XW_ACT_SEND_RERSP, XW_ST_CHK_2ND_ALLHERE },
|
||||
{ XW_ST_CHK_2ND_ALLHERE, XW_EVT_ALLHERE, XW_ACT_2ND_SNDALLHERE,XW_ST_ALLCONNECTED },
|
||||
{ XW_ST_CHK_2ND_ALLHERE, XW_EVT_SOMEMISSING, XW_ACT_NONE, XW_ST_MISSING },
|
||||
{ XWS_INITED, XWE_RECONNECTMSG, XWA_SEND_RERSP, XWS_CHK_ALLHERE_2 },
|
||||
{ XWS_MISSING, XWE_RECONNECTMSG, XWA_SEND_RERSP, XWS_CHK_ALLHERE_2 },
|
||||
{ XWS_CHK_ALLHERE_2, XWE_ALLHERE, XWA_SNDALLHERE_2, XWS_ALLCONNECTED },
|
||||
{ XWS_CHK_ALLHERE_2, XWE_SOMEMISSING, XWA_NONE, XWS_MISSING },
|
||||
|
||||
{ XW_ST_CONNECTING, XW_EVT_REMOVESOCKET, XW_ACT_REMOVESOCKET, XW_ST_CONNECTING },
|
||||
{ XW_ST_ALLCONNECTED, XW_EVT_REMOVESOCKET, XW_ACT_REMOVESOCKET, XW_ST_MISSING },
|
||||
{ XW_ST_MISSING, XW_EVT_REMOVESOCKET, XW_ACT_REMOVESOCKET, XW_ST_MISSING },
|
||||
{ XWS_CONNECTING, XWE_REMOVESOCKET, XWA_REMOVESOCKET, XWS_CONNECTING },
|
||||
{ XWS_ALLCONNECTED, XWE_REMOVESOCKET, XWA_REMOVESOCKET, XWS_MISSING },
|
||||
{ XWS_MISSING, XWE_REMOVESOCKET, XWA_REMOVESOCKET, XWS_MISSING },
|
||||
|
||||
{ XW_ST_ALLCONNECTED, XW_EVT_HEARTFAILED, XW_ACT_HEARTDISCONNECT, XW_ST_MISSING },
|
||||
{ XW_ST_CONNECTING, XW_EVT_HEARTFAILED, XW_ACT_HEARTDISCONNECT, XW_ST_CONNECTING },
|
||||
{ XW_ST_MISSING, XW_EVT_HEARTFAILED, XW_ACT_HEARTDISCONNECT, XW_ST_MISSING },
|
||||
{ XWS_ALLCONNECTED, XWE_HEARTFAILED, XWA_HEARTDISCONN, XWS_MISSING },
|
||||
{ XWS_CONNECTING, XWE_HEARTFAILED, XWA_HEARTDISCONN, XWS_CONNECTING },
|
||||
{ XWS_MISSING, XWE_HEARTFAILED, XWA_HEARTDISCONN, XWS_MISSING },
|
||||
|
||||
/* Heartbeat arrived */
|
||||
{ XW_ST_CONNECTING, XW_EVT_HEARTRCVD, XW_ACT_NOTEHEART, XW_ST_CONNECTING },
|
||||
{ XW_ST_ALLCONNECTED, XW_EVT_HEARTRCVD, XW_ACT_NOTEHEART, XW_ST_ALLCONNECTED },
|
||||
{ XW_ST_MISSING, XW_EVT_HEARTRCVD, XW_ACT_NOTEHEART, XW_ST_MISSING },
|
||||
{ XWS_CONNECTING, XWE_HEARTRCVD, XWA_NOTEHEART, XWS_CONNECTING },
|
||||
{ XWS_ALLCONNECTED, XWE_HEARTRCVD, XWA_NOTEHEART, XWS_ALLCONNECTED },
|
||||
{ XWS_MISSING, XWE_HEARTRCVD, XWA_NOTEHEART, XWS_MISSING },
|
||||
|
||||
/* Connect timer */
|
||||
{ XW_ST_CONNECTING, XW_EVT_CONNTIMER, XW_ACT_TIMERDISCONNECT, XW_ST_DEAD },
|
||||
{ XW_ST_ALLCONNECTED, XW_EVT_CONNTIMER, XW_ACT_NONE, XW_ST_ALLCONNECTED },
|
||||
{ XWS_CONNECTING, XWE_CONNTIMER, XWA_TIMERDISCONN, XWS_DEAD },
|
||||
{ XWS_ALLCONNECTED, XWE_CONNTIMER, XWA_NONE, XWS_ALLCONNECTED },
|
||||
|
||||
{ XW_ST_CONNECTING, XW_EVT_NOTIFYDISCON,XW_ACT_NOTIFYDISCON, XW_ST_CONNECTING },
|
||||
{ XW_ST_ALLCONNECTED, XW_EVT_NOTIFYDISCON,XW_ACT_NOTIFYDISCON, XW_ST_MISSING },
|
||||
{ XW_ST_MISSING, XW_EVT_NOTIFYDISCON,XW_ACT_NOTIFYDISCON, XW_ST_DEAD },
|
||||
{ XW_ST_DEAD, XW_EVT_NOTIFYDISCON,XW_ACT_NOTIFYDISCON, XW_ST_DEAD },
|
||||
{ XWS_CONNECTING, XWE_NOTIFYDISCON, XWA_NOTIFYDISCON, XWS_CONNECTING },
|
||||
{ XWS_ALLCONNECTED, XWE_NOTIFYDISCON, XWA_NOTIFYDISCON, XWS_MISSING },
|
||||
{ XWS_MISSING, XWE_NOTIFYDISCON, XWA_NOTIFYDISCON, XWS_DEAD },
|
||||
{ XWS_DEAD, XWE_NOTIFYDISCON, XWA_NOTIFYDISCON, XWS_DEAD },
|
||||
|
||||
/* This is our bread-n-butter */
|
||||
{ XW_ST_ALLCONNECTED, XW_EVT_FORWARDMSG, XW_ACT_FWD, XW_ST_ALLCONNECTED },
|
||||
{ XWS_ALLCONNECTED, XWE_FORWARDMSG, XWA_FWD, XWS_ALLCONNECTED },
|
||||
|
||||
{ XW_ST_DEAD, XW_EVT_REMOVESOCKET, XW_ACT_REMOVESOCKET, XW_ST_DEAD },
|
||||
|
||||
#if 0
|
||||
/* Initial msg comes in. Managing object created in init state, sends response */
|
||||
{ XW_ST_INITED, XW_EVT_CONNECTMSG, XW_ACT_SEND_1ST_RSP, XW_ST_CONNECTING },
|
||||
{ XW_ST_INITED, XW_EVT_RECONNECTMSG, XW_ACT_SEND_1ST_RERSP,XW_ST_CONNECTING },
|
||||
|
||||
/* Another connect msg comes in */
|
||||
{ XW_ST_CONNECTING, XW_EVT_CONNECTMSG, XW_ACT_SEND_RSP, XW_ST_CONNECTING },
|
||||
{ XW_ST_CONNECTING, XW_EVT_RECONNECTMSG, XW_ACT_SEND_RERSP, XW_ST_CONNECTING },
|
||||
|
||||
{ XW_ST_MISSING, XW_EVT_CONNECTMSG, XW_ACT_SEND_RSP, XW_ST_CONNECTING },
|
||||
{ XW_ST_MISSING, XW_EVT_RECONNECTMSG, XW_ACT_SEND_RERSP, XW_ST_CONNECTING },
|
||||
|
||||
/* Disconnect. */
|
||||
{ XW_ST_ALLCONNECTED, XW_EVT_DISCONNECTMSG, XW_ACT_DISCONNECT, XW_ST_MISSING },
|
||||
{ XW_ST_CONNECTING, XW_EVT_DISCONNECTMSG, XW_ACT_DISCONNECT, XW_ST_CONNECTING },
|
||||
{ XW_ST_MISSING, XW_EVT_DISCONNECTMSG, XW_ACT_DISCONNECT, XW_ST_MISSING },
|
||||
{ XW_ST_MISSING, XW_EVT_NOMORESOCKETS, XW_ACT_NONE, XW_ST_DEAD },
|
||||
|
||||
/* Forward requests while not locked are ok -- but we must check that the
|
||||
target is actually present. If no socket available must drop the message */
|
||||
{ XW_ST_CONNECTING, XW_EVT_FORWARDMSG, XW_ACT_CHECKDEST, XW_ST_CHECKINGDEST },
|
||||
{ XW_ST_CHECKINGDEST, XW_EVT_DESTOK, XW_ACT_CHECK_CAN_LOCK, XW_ST_CHECKING_CAN_LOCK },
|
||||
|
||||
{ XW_ST_CHECKING_CAN_LOCK, XW_EVT_CAN_LOCK, XW_ACT_FWD, XW_ST_ALLCONNECTED },
|
||||
{ XW_ST_CHECKING_CAN_LOCK, XW_EVT_CANT_LOCK, XW_ACT_FWD, XW_ST_CONNECTING },
|
||||
|
||||
{ XW_ST_CHECKINGDEST, XW_EVT_DESTBAD, XW_ACT_NONE, XW_ST_CONNECTING },
|
||||
|
||||
/* Timeout before all connected */
|
||||
|
||||
{ XW_ST_ALLCONNECTED, XW_EVT_HEARTFAILED, XW_ACT_HEARTDISCONNECT, XW_ST_MISSING },
|
||||
{ XW_ST_CONNECTING, XW_EVT_HEARTFAILED, XW_ACT_HEARTDISCONNECT, XW_ST_CONNECTING },
|
||||
{ XW_ST_MISSING, XW_EVT_HEARTFAILED, XW_ACT_HEARTDISCONNECT, XW_ST_MISSING },
|
||||
|
||||
{ XW_ST_CONNECTING, XW_EVT_REMOVESOCKET, XW_ACT_REMOVESOCKET, XW_ST_CONNECTING },
|
||||
{ XW_ST_ALLCONNECTED, XW_EVT_REMOVESOCKET, XW_ACT_REMOVESOCKET, XW_ST_MISSING },
|
||||
{ XW_ST_MISSING, XW_EVT_REMOVESOCKET, XW_ACT_REMOVESOCKET, XW_ST_MISSING },
|
||||
|
||||
{ XW_ST_CONNECTING, XW_EVT_NOMORESOCKETS, XW_ACT_NONE, XW_ST_DEAD },
|
||||
{ XW_ST_DEAD, XW_EVT_NOMORESOCKETS, XW_ACT_NONE, XW_ST_DEAD },
|
||||
|
||||
/* This is the entry we'll use most of the time */
|
||||
{ XW_ST_ALLCONNECTED, XW_EVT_FORWARDMSG, XW_ACT_FWD, XW_ST_ALLCONNECTED },
|
||||
|
||||
/* Heartbeat arrived */
|
||||
{ XW_ST_CONNECTING, XW_EVT_HEARTRCVD, XW_ACT_NOTEHEART, XW_ST_CONNECTING },
|
||||
{ XW_ST_ALLCONNECTED, XW_EVT_HEARTRCVD, XW_ACT_NOTEHEART, XW_ST_ALLCONNECTED },
|
||||
{ XW_ST_MISSING, XW_EVT_HEARTRCVD, XW_ACT_NOTEHEART, XW_ST_MISSING },
|
||||
|
||||
/* I think we need a state XW_ST_SOMEMISSING. The game can't be played,
|
||||
but we're open to XWRELAY_RECONNECT (but not to XWRELAY_CONNECT) */
|
||||
{ XW_ST_CONNECTING, XW_EVT_NOTIFYDISCON, XW_ACT_NOTIFYDISCON, XW_ST_CONNECTING },
|
||||
{ XW_ST_ALLCONNECTED, XW_EVT_NOTIFYDISCON, XW_ACT_NOTIFYDISCON, XW_ST_MISSING },
|
||||
{ XW_ST_MISSING, XW_EVT_NOTIFYDISCON, XW_ACT_NOTIFYDISCON, XW_ST_DEAD },
|
||||
{ XW_ST_DEAD, XW_EVT_NOTIFYDISCON, XW_ACT_NOTIFYDISCON, XW_ST_DEAD },
|
||||
{ XW_ST_DEAD, XW_EVT_REMOVESOCKET, XW_ACT_REMOVESOCKET, XW_ST_DEAD },
|
||||
#endif
|
||||
// { XW_ST_DEAD, XW_EVT_ANY, XW_ACT_NONE, XW_ST_DEAD },
|
||||
|
||||
/* Reconnect. Just like a connect but cookieID is supplied. Can it
|
||||
happen in the middle of a game when state is XW_ST_ALLCONNECTED? */
|
||||
{ XWS_DEAD, XWE_REMOVESOCKET, XWA_REMOVESOCKET, XWS_DEAD },
|
||||
|
||||
/* Marks end of table */
|
||||
{ XW_ST_NONE, XW_EVT_NONE, XW_ACT_NONE, XW_ST_NONE }
|
||||
{ XWS_NONE, XWE_NONE, XWA_NONE, XWS_NONE }
|
||||
};
|
||||
|
||||
|
||||
|
@ -188,9 +125,9 @@ getFromTable( XW_RELAY_STATE curState, XW_RELAY_EVENT curEvent,
|
|||
XW_RELAY_ACTION* takeAction, XW_RELAY_STATE* nextState )
|
||||
{
|
||||
StateTable* stp = g_stateTable;
|
||||
while ( stp->stateStart != XW_ST_NONE ) {
|
||||
while ( stp->stateStart != XWS_NONE ) {
|
||||
if ( stp->stateStart == curState ) {
|
||||
if ( stp->stateEvent == curEvent || stp->stateEvent == XW_EVT_ANY ) {
|
||||
if ( stp->stateEvent == curEvent || stp->stateEvent == XWE_ANY ) {
|
||||
*takeAction = stp->stateAction;
|
||||
*nextState = stp->stateEnd;
|
||||
return 1;
|
||||
|
@ -199,7 +136,7 @@ getFromTable( XW_RELAY_STATE curState, XW_RELAY_EVENT curEvent,
|
|||
++stp;
|
||||
}
|
||||
|
||||
logf( "==> ERROR :: unable to find transition from %s on event %s",
|
||||
logf( XW_LOGERROR, "==> ERROR :: unable to find transition from %s on event %s",
|
||||
stateString(curState), eventString(curEvent) );
|
||||
|
||||
return 0;
|
||||
|
@ -211,21 +148,21 @@ char*
|
|||
stateString( XW_RELAY_STATE state )
|
||||
{
|
||||
switch( state ) {
|
||||
CASESTR(XW_ST_NONE);
|
||||
CASESTR(XW_ST_INITED);
|
||||
CASESTR(XW_ST_CONNECTING);
|
||||
CASESTR(XW_ST_ALLCONNECTED);
|
||||
CASESTR(XW_ST_WAITING_RECON);
|
||||
CASESTR(XW_ST_DEAD);
|
||||
CASESTR(XW_ST_CHECKING_CONN);
|
||||
CASESTR(XW_ST_CHECKINGDEST);
|
||||
CASESTR(XW_ST_CHECKING_CAN_LOCK);
|
||||
CASESTR(XW_ST_MISSING);
|
||||
CASESTR(XW_ST_CHK_ALLHERE);
|
||||
CASESTR(XW_ST_CHK_2ND_ALLHERE);
|
||||
CASESTR(XW_ST_CHKCOUNTS_INIT);
|
||||
CASESTR(XW_ST_CHKCOUNTS_MISS);
|
||||
CASESTR(XW_ST_CHKCOUNTS);
|
||||
CASESTR(XWS_NONE);
|
||||
CASESTR(XWS_INITED);
|
||||
CASESTR(XWS_CONNECTING);
|
||||
CASESTR(XWS_ALLCONNECTED);
|
||||
CASESTR(XWS_WAITING_RECON);
|
||||
CASESTR(XWS_DEAD);
|
||||
CASESTR(XWS_CHECKING_CONN);
|
||||
CASESTR(XWS_CHECKINGDEST);
|
||||
CASESTR(XWS_CHECKING_CAN_LOCK);
|
||||
CASESTR(XWS_MISSING);
|
||||
CASESTR(XWS_CHK_ALLHERE);
|
||||
CASESTR(XWS_CHK_ALLHERE_2);
|
||||
CASESTR(XWS_CHKCOUNTS_INIT);
|
||||
CASESTR(XWS_CHKCOUNTS_MISS);
|
||||
CASESTR(XWS_CHKCOUNTS);
|
||||
}
|
||||
assert(0);
|
||||
return "";
|
||||
|
@ -235,26 +172,26 @@ char*
|
|||
eventString( XW_RELAY_EVENT evt )
|
||||
{
|
||||
switch( evt ) {
|
||||
CASESTR(XW_EVT_NONE);
|
||||
CASESTR(XW_EVT_CONNECTMSG);
|
||||
CASESTR(XW_EVT_RECONNECTMSG);
|
||||
CASESTR(XW_EVT_DISCONNECTMSG);
|
||||
CASESTR(XW_EVT_FORWARDMSG);
|
||||
CASESTR(XW_EVT_HEARTRCVD);
|
||||
CASESTR(XW_EVT_CONNTIMER);
|
||||
CASESTR(XW_EVT_HEARTFAILED);
|
||||
CASESTR(XW_EVT_DESTOK);
|
||||
CASESTR(XW_EVT_DESTBAD);
|
||||
CASESTR(XW_EVT_CAN_LOCK);
|
||||
CASESTR(XW_EVT_CANT_LOCK);
|
||||
CASESTR(XW_EVT_ANY);
|
||||
CASESTR(XW_EVT_REMOVESOCKET);
|
||||
CASESTR(XW_EVT_NOMORESOCKETS);
|
||||
CASESTR(XW_EVT_NOTIFYDISCON);
|
||||
CASESTR(XW_EVT_ALLHERE);
|
||||
CASESTR(XW_EVT_SOMEMISSING);
|
||||
CASESTR(XW_EVT_OKTOSEND);
|
||||
CASESTR(XW_EVT_COUNTSBAD);
|
||||
CASESTR(XWE_NONE);
|
||||
CASESTR(XWE_CONNECTMSG);
|
||||
CASESTR(XWE_RECONNECTMSG);
|
||||
CASESTR(XWE_DISCONNMSG);
|
||||
CASESTR(XWE_FORWARDMSG);
|
||||
CASESTR(XWE_HEARTRCVD);
|
||||
CASESTR(XWE_CONNTIMER);
|
||||
CASESTR(XWE_HEARTFAILED);
|
||||
CASESTR(XWE_DESTOK);
|
||||
CASESTR(XWE_DESTBAD);
|
||||
CASESTR(XWE_CAN_LOCK);
|
||||
CASESTR(XWE_CANT_LOCK);
|
||||
CASESTR(XWE_ANY);
|
||||
CASESTR(XWE_REMOVESOCKET);
|
||||
CASESTR(XWE_NOMORESOCKETS);
|
||||
CASESTR(XWE_NOTIFYDISCON);
|
||||
CASESTR(XWE_ALLHERE);
|
||||
CASESTR(XWE_SOMEMISSING);
|
||||
CASESTR(XWE_OKTOSEND);
|
||||
CASESTR(XWE_COUNTSBAD);
|
||||
}
|
||||
assert(0);
|
||||
return "";
|
||||
|
@ -264,25 +201,25 @@ char*
|
|||
actString( XW_RELAY_ACTION act )
|
||||
{
|
||||
switch ( act ) {
|
||||
CASESTR(XW_ACT_NONE);
|
||||
CASESTR(XW_ACT_SEND_1ST_RSP);
|
||||
CASESTR(XW_ACT_SEND_1ST_RERSP);
|
||||
CASESTR(XW_ACT_CHKCOUNTS);
|
||||
CASESTR(XW_ACT_REJECT);
|
||||
CASESTR(XW_ACT_SEND_RSP);
|
||||
CASESTR(XW_ACT_SEND_RERSP);
|
||||
CASESTR(XW_ACT_SENDALLHERE);
|
||||
CASESTR(XW_ACT_2ND_SNDALLHERE);
|
||||
CASESTR(XW_ACT_FWD);
|
||||
CASESTR(XW_ACT_NOTEHEART);
|
||||
CASESTR(XW_ACT_DISCONNECTALL);
|
||||
CASESTR(XW_ACT_TIMERDISCONNECT);
|
||||
CASESTR(XW_ACT_CHECKDEST);
|
||||
CASESTR(XW_ACT_DISCONNECT);
|
||||
CASESTR(XW_ACT_NOTIFYDISCON);
|
||||
CASESTR(XW_ACT_REMOVESOCKET);
|
||||
CASESTR(XW_ACT_CHECK_CAN_LOCK);
|
||||
CASESTR(XW_ACT_HEARTDISCONNECT);
|
||||
CASESTR(XWA_NONE);
|
||||
CASESTR(XWA_SEND_1ST_RSP);
|
||||
CASESTR(XWA_SEND_1ST_RERSP);
|
||||
CASESTR(XWA_CHKCOUNTS);
|
||||
CASESTR(XWA_REJECT);
|
||||
CASESTR(XWA_SEND_RSP);
|
||||
CASESTR(XWA_SEND_RERSP);
|
||||
CASESTR(XWA_SENDALLHERE);
|
||||
CASESTR(XWA_SNDALLHERE_2);
|
||||
CASESTR(XWA_FWD);
|
||||
CASESTR(XWA_NOTEHEART);
|
||||
CASESTR(XWA_DISCONNECTALL);
|
||||
CASESTR(XWA_TIMERDISCONN);
|
||||
CASESTR(XWA_CHECKDEST);
|
||||
CASESTR(XWA_DISCONNECT);
|
||||
CASESTR(XWA_NOTIFYDISCON);
|
||||
CASESTR(XWA_REMOVESOCKET);
|
||||
CASESTR(XWA_CHECK_CAN_LOCK);
|
||||
CASESTR(XWA_HEARTDISCONN);
|
||||
}
|
||||
assert(0);
|
||||
return "";
|
||||
|
|
108
relay/states.h
108
relay/states.h
|
@ -24,137 +24,137 @@
|
|||
/* states */
|
||||
typedef
|
||||
enum {
|
||||
XW_ST_NONE
|
||||
XWS_NONE
|
||||
|
||||
,XW_ST_CHKCOUNTS_INIT /* from initial state, check if all players
|
||||
,XWS_CHKCOUNTS_INIT /* from initial state, check if all players
|
||||
are here. Success should be an error,
|
||||
actually: 1-device game. */
|
||||
|
||||
,XW_ST_CHKCOUNTS_MISS /* from the missing state */
|
||||
,XW_ST_CHKCOUNTS /* check from any other state */
|
||||
,XWS_CHKCOUNTS_MISS /* from the missing state */
|
||||
,XWS_CHKCOUNTS /* check from any other state */
|
||||
|
||||
,XW_ST_CHK_ALLHERE /* Need to see if all expected devices/players
|
||||
,XWS_CHK_ALLHERE /* Need to see if all expected devices/players
|
||||
are on board. */
|
||||
|
||||
,XW_ST_CHK_2ND_ALLHERE /* same as above, but triggered by a reconnect
|
||||
,XWS_CHK_ALLHERE_2 /* same as above, but triggered by a reconnect
|
||||
rather than a connect request */
|
||||
|
||||
,XW_ST_INITED /* Relay's running and the object's been
|
||||
,XWS_INITED /* Relay's running and the object's been
|
||||
created, but nobody's signed up yet. This
|
||||
is a very short-lived state since an
|
||||
incoming connection is why the object was
|
||||
created. */
|
||||
|
||||
,XW_ST_CONNECTING /* At least one device has connected, but no
|
||||
,XWS_CONNECTING /* At least one device has connected, but no
|
||||
packets have yet arrived to be
|
||||
forwarded. */
|
||||
|
||||
,XW_ST_CHECKING_CONN /* While we're still not fully connected a
|
||||
,XWS_CHECKING_CONN /* While we're still not fully connected a
|
||||
message comes in */
|
||||
|
||||
,XW_ST_ALLCONNECTED /* All devices are connected and ready for the
|
||||
,XWS_ALLCONNECTED /* All devices are connected and ready for the
|
||||
relay to do its work. This is the state
|
||||
we're in most of the time. */
|
||||
|
||||
,XW_ST_MISSING /* We've been fully connected before but lost
|
||||
,XWS_MISSING /* We've been fully connected before but lost
|
||||
somebody. Once [s]he's back we can be
|
||||
fully connected again. */
|
||||
|
||||
,XW_ST_WAITING_RECON /* At least one device has been timed out or
|
||||
,XWS_WAITING_RECON /* At least one device has been timed out or
|
||||
sent a disconnect message. We can't flow
|
||||
messages in this state, and will be killing
|
||||
all connections if we don't hear back from
|
||||
the missing guy soon. */
|
||||
|
||||
,XW_ST_CHECKINGDEST /* Checking for valid socket */
|
||||
,XWS_CHECKINGDEST /* Checking for valid socket */
|
||||
|
||||
,XW_ST_CHECKING_CAN_LOCK /* Is this message one that implies all
|
||||
,XWS_CHECKING_CAN_LOCK /* Is this message one that implies all
|
||||
players are present? */
|
||||
|
||||
,XW_ST_DEAD /* About to kill the object */
|
||||
,XWS_DEAD /* About to kill the object */
|
||||
} XW_RELAY_STATE;
|
||||
|
||||
|
||||
/* events */
|
||||
typedef enum {
|
||||
XW_EVT_NONE
|
||||
XWE_NONE
|
||||
|
||||
,XW_EVT_OKTOSEND
|
||||
,XW_EVT_COUNTSBAD
|
||||
,XWE_OKTOSEND
|
||||
,XWE_COUNTSBAD
|
||||
|
||||
,XW_EVT_ALLHERE /* notify that all expected players are arrived */
|
||||
,XW_EVT_SOMEMISSING /* notify that some expected players are still missing */
|
||||
,XWE_ALLHERE /* notify that all expected players are arrived */
|
||||
,XWE_SOMEMISSING /* notify that some expected players are still missing */
|
||||
|
||||
,XW_EVT_CONNECTMSG /* A device is connecting using the cookie for
|
||||
,XWE_CONNECTMSG /* A device is connecting using the cookie for
|
||||
this object */
|
||||
|
||||
,XW_EVT_RECONNECTMSG /* A device is re-connecting using the
|
||||
,XWE_RECONNECTMSG /* A device is re-connecting using the
|
||||
connID for this object */
|
||||
|
||||
,XW_EVT_DISCONNECTMSG /* disconnect socket from this game/cref */
|
||||
,XWE_DISCONNMSG /* disconnect socket from this game/cref */
|
||||
|
||||
,XW_EVT_FORWARDMSG /* A message needs forwarding */
|
||||
,XWE_FORWARDMSG /* A message needs forwarding */
|
||||
|
||||
,XW_EVT_HEARTRCVD /* A heartbeat message arrived */
|
||||
,XWE_HEARTRCVD /* A heartbeat message arrived */
|
||||
|
||||
,XW_EVT_CONNTIMER /* timer for did we get all players hooked
|
||||
,XWE_CONNTIMER /* timer for did we get all players hooked
|
||||
up */
|
||||
|
||||
,XW_EVT_DESTOK
|
||||
,XWE_DESTOK
|
||||
|
||||
,XW_EVT_DESTBAD
|
||||
,XWE_DESTBAD
|
||||
|
||||
,XW_EVT_CAN_LOCK /* ready to stop allowing new connections */
|
||||
,XW_EVT_CANT_LOCK /* can't disallow new connections yet */
|
||||
,XWE_CAN_LOCK /* ready to stop allowing new connections */
|
||||
,XWE_CANT_LOCK /* can't disallow new connections yet */
|
||||
|
||||
,XW_EVT_HEARTFAILED
|
||||
,XWE_HEARTFAILED
|
||||
|
||||
,XW_EVT_REMOVESOCKET /* Need to remove socket from this cref */
|
||||
,XWE_REMOVESOCKET /* Need to remove socket from this cref */
|
||||
|
||||
,XW_EVT_NOTIFYDISCON /* Send a discon */
|
||||
,XWE_NOTIFYDISCON /* Send a discon */
|
||||
|
||||
,XW_EVT_NOMORESOCKETS /* last socket's been removed */
|
||||
,XWE_NOMORESOCKETS /* last socket's been removed */
|
||||
|
||||
,XW_EVT_ANY /* wildcard; matches all */
|
||||
,XWE_ANY /* wildcard; matches all */
|
||||
} XW_RELAY_EVENT;
|
||||
|
||||
|
||||
/* actions */
|
||||
typedef enum {
|
||||
XW_ACT_NONE
|
||||
XWA_NONE
|
||||
|
||||
,XW_ACT_SEND_1ST_RSP
|
||||
,XW_ACT_SEND_1ST_RERSP
|
||||
,XWA_SEND_1ST_RSP
|
||||
,XWA_SEND_1ST_RERSP
|
||||
|
||||
,XW_ACT_CHKCOUNTS
|
||||
,XWA_CHKCOUNTS
|
||||
|
||||
,XW_ACT_REJECT
|
||||
,XWA_REJECT
|
||||
|
||||
,XW_ACT_SEND_RSP /* Send a connection response */
|
||||
,XW_ACT_SEND_RERSP
|
||||
,XWA_SEND_RSP /* Send a connection response */
|
||||
,XWA_SEND_RERSP
|
||||
|
||||
,XW_ACT_SENDALLHERE /* Let all devices know we're in business */
|
||||
,XW_ACT_2ND_SNDALLHERE
|
||||
,XWA_SENDALLHERE /* Let all devices know we're in business */
|
||||
,XWA_SNDALLHERE_2
|
||||
|
||||
,XW_ACT_FWD /* Forward a message */
|
||||
,XWA_FWD /* Forward a message */
|
||||
|
||||
,XW_ACT_NOTEHEART /* Record heartbeat received */
|
||||
,XWA_NOTEHEART /* Record heartbeat received */
|
||||
|
||||
,XW_ACT_DISCONNECTALL
|
||||
,XW_ACT_TIMERDISCONNECT /* disconnect all because of a timer */
|
||||
,XWA_DISCONNECTALL
|
||||
,XWA_TIMERDISCONN /* disconnect all because of a timer */
|
||||
|
||||
,XW_ACT_CHECKDEST /* check that a given hostID has a socket */
|
||||
,XWA_CHECKDEST /* check that a given hostID has a socket */
|
||||
|
||||
,XW_ACT_DISCONNECT
|
||||
,XWA_DISCONNECT
|
||||
|
||||
,XW_ACT_NOTIFYDISCON
|
||||
,XWA_NOTIFYDISCON
|
||||
|
||||
,XW_ACT_REMOVESOCKET
|
||||
,XWA_REMOVESOCKET
|
||||
|
||||
,XW_ACT_CHECK_CAN_LOCK /* check whether this message implies all
|
||||
,XWA_CHECK_CAN_LOCK /* check whether this message implies all
|
||||
expected players present */
|
||||
|
||||
,XW_ACT_HEARTDISCONNECT
|
||||
,XWA_HEARTDISCONN
|
||||
|
||||
} XW_RELAY_ACTION;
|
||||
|
||||
|
|
|
@ -48,7 +48,7 @@ void
|
|||
TimerMgr::setTimer( time_t inMillis, TimerProc proc, void* closure,
|
||||
int interval )
|
||||
{
|
||||
logf( "setTimer: now = %d", now() );
|
||||
logf( XW_LOGINFO, "setTimer: now = %d", now() );
|
||||
TimerInfo ti;
|
||||
ti.proc = proc;
|
||||
ti.closure = closure;
|
||||
|
@ -64,7 +64,7 @@ TimerMgr::setTimer( time_t inMillis, TimerProc proc, void* closure,
|
|||
m_timers.push_back( ti );
|
||||
|
||||
figureNextFire();
|
||||
logf( "setTimer done" );
|
||||
logf( XW_LOGINFO, "setTimer done" );
|
||||
}
|
||||
|
||||
time_t
|
||||
|
|
|
@ -54,7 +54,7 @@ XWThreadPool::XWThreadPool()
|
|||
|
||||
int fd[2];
|
||||
if ( pipe( fd ) ) {
|
||||
logf( "pipe failed" );
|
||||
logf( XW_LOGERROR, "pipe failed" );
|
||||
}
|
||||
m_pipeRead = fd[0];
|
||||
m_pipeWrite = fd[1];
|
||||
|
@ -83,7 +83,7 @@ XWThreadPool::Setup( int nThreads, packet_func pFunc )
|
|||
void
|
||||
XWThreadPool::AddSocket( int socket )
|
||||
{
|
||||
logf( "AddSocket(%d)", socket );
|
||||
logf( XW_LOGINFO, "AddSocket(%d)", socket );
|
||||
{
|
||||
RWWriteLock ml( &m_activeSocketsRWLock );
|
||||
m_activeSockets.push_back( socket );
|
||||
|
@ -162,9 +162,9 @@ XWThreadPool::get_process_packet( int socket )
|
|||
killSocket( socket, "nRead != packetSize" );
|
||||
return 0;
|
||||
}
|
||||
logf( "read %d bytes\n", nRead );
|
||||
logf( XW_LOGINFO, "read %d bytes\n", nRead );
|
||||
|
||||
logf( "calling m_pFunc" );
|
||||
logf( XW_LOGINFO, "calling m_pFunc" );
|
||||
(*m_pFunc)( buf, packetSize, socket );
|
||||
|
||||
return 1;
|
||||
|
@ -180,7 +180,7 @@ XWThreadPool::tpool_main( void* closure )
|
|||
void*
|
||||
XWThreadPool::real_tpool_main()
|
||||
{
|
||||
logf( "worker thread starting" );
|
||||
logf( XW_LOGINFO, "worker thread starting" );
|
||||
for ( ; ; ) {
|
||||
|
||||
pthread_mutex_lock( &m_queueMutex );
|
||||
|
@ -191,24 +191,24 @@ XWThreadPool::real_tpool_main()
|
|||
int socket = m_queue.front();
|
||||
m_queue.pop_front();
|
||||
pthread_mutex_unlock( &m_queueMutex );
|
||||
logf( "worker thread got socket %d from queue", socket );
|
||||
logf( XW_LOGINFO, "worker thread got socket %d from queue", socket );
|
||||
|
||||
if ( get_process_packet( socket ) ) {
|
||||
AddSocket( socket );
|
||||
} /* else drop it: error */
|
||||
}
|
||||
logf( "worker thread exiting" );
|
||||
logf( XW_LOGINFO, "worker thread exiting" );
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
XWThreadPool::interrupt_poll()
|
||||
{
|
||||
logf( "interrupt_poll" );
|
||||
logf( XW_LOGINFO, "interrupt_poll" );
|
||||
unsigned char byt = 0;
|
||||
int nSent = write( m_pipeWrite, &byt, 1 );
|
||||
if ( nSent != 1 ) {
|
||||
logf( "errno = %d", errno );
|
||||
logf( XW_LOGERROR, "errno = %d", errno );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -245,15 +245,15 @@ XWThreadPool::real_listener()
|
|||
int nMillis = tmgr->getPollTimeout();
|
||||
|
||||
int nEvents = poll( fds, nSockets, nMillis ); /* -1: infinite timeout */
|
||||
logf( "back from poll: %d", nEvents );
|
||||
logf( XW_LOGINFO, "back from poll: %d", nEvents );
|
||||
if ( nEvents == 0 ) {
|
||||
tmgr->fireElapsedTimers();
|
||||
} else if ( nEvents < 0 ) {
|
||||
logf( "errno: %d", errno );
|
||||
logf( XW_LOGERROR, "errno: %d", errno );
|
||||
}
|
||||
|
||||
if ( fds[0].revents != 0 ) {
|
||||
logf( "poll interrupted" );
|
||||
logf( XW_LOGINFO, "poll interrupted" );
|
||||
assert( fds[0].revents == POLLIN );
|
||||
unsigned char byt;
|
||||
read( fds[0].fd, &byt, 1 );
|
||||
|
@ -278,10 +278,10 @@ XWThreadPool::real_listener()
|
|||
|
||||
if ( curfd->revents == POLLIN
|
||||
|| curfd->revents == POLLPRI ) {
|
||||
logf( "enqueuing %d", socket );
|
||||
logf( XW_LOGINFO, "enqueuing %d", socket );
|
||||
enqueue( socket );
|
||||
} else {
|
||||
logf( "odd revents: %d", curfd->revents );
|
||||
logf( XW_LOGERROR, "odd revents: %d", curfd->revents );
|
||||
killSocket( socket, "error/hup in poll()" );
|
||||
}
|
||||
--nEvents;
|
||||
|
@ -310,7 +310,7 @@ XWThreadPool::enqueue( int socket )
|
|||
MutexLock ml( &m_queueMutex );
|
||||
m_queue.push_back( socket );
|
||||
|
||||
logf( "calling pthread_cond_signal" );
|
||||
logf( XW_LOGINFO, "calling pthread_cond_signal" );
|
||||
pthread_cond_signal( &m_queueCondVar );
|
||||
/* implicit unlock */
|
||||
}
|
||||
|
|
|
@ -22,4 +22,4 @@ CTLPORT=11000
|
|||
|
||||
# Need a unique name for every instance of the relay so they can
|
||||
# create game ids guaranteed to be unique
|
||||
#SERVERNAME=eehouse.org
|
||||
SERVERNAME=eehouse.org
|
||||
|
|
|
@ -63,7 +63,7 @@
|
|||
#define MILLIS 1000
|
||||
|
||||
void
|
||||
logf( const char* format, ... )
|
||||
logf( XW_LogLevel level, const char* format, ... )
|
||||
{
|
||||
FILE* where = stderr;
|
||||
struct tm* timp;
|
||||
|
@ -117,7 +117,7 @@ processHeartbeat( unsigned char* buf, int bufLen, int socket )
|
|||
|
||||
if ( getNetShort( &buf, end, &cookieID )
|
||||
&& getNetByte( &buf, end, &hostID ) ) {
|
||||
logf( "processHeartbeat: cookieID 0x%lx, hostID 0x%x",
|
||||
logf( XW_LOGINFO, "processHeartbeat: cookieID 0x%lx, hostID 0x%x",
|
||||
cookieID, hostID );
|
||||
|
||||
SafeCref scr( cookieID );
|
||||
|
@ -170,7 +170,7 @@ send_with_length_unsafe( int socket, unsigned char* buf, int bufLen )
|
|||
if ( nSent == 2 ) {
|
||||
nSent = send( socket, buf, bufLen, 0 );
|
||||
if ( nSent == bufLen ) {
|
||||
logf( "sent %d bytes on socket %d", nSent, socket );
|
||||
logf( XW_LOGINFO, "sent %d bytes on socket %d", nSent, socket );
|
||||
ok = 1;
|
||||
}
|
||||
}
|
||||
|
@ -196,7 +196,7 @@ processConnect( unsigned char* bufp, int bufLen, int socket )
|
|||
unsigned char* end = bufp + bufLen;
|
||||
int success = 0;
|
||||
|
||||
logf( "processConnect" );
|
||||
logf( XW_LOGINFO, "processConnect" );
|
||||
|
||||
cookie[0] = '\0';
|
||||
|
||||
|
@ -227,7 +227,7 @@ processReconnect( unsigned char* bufp, int bufLen, int socket )
|
|||
unsigned char* end = bufp + bufLen;
|
||||
int success = 0;
|
||||
|
||||
logf( "processReconnect" );
|
||||
logf( XW_LOGINFO, "processReconnect" );
|
||||
|
||||
connName[0] = '\0';
|
||||
|
||||
|
@ -264,18 +264,18 @@ processDisconnect( unsigned char* bufp, int bufLen, int socket )
|
|||
SafeCref scr( cookieID );
|
||||
scr.Disconnect( socket, hostID );
|
||||
} else {
|
||||
logf( "dropping XWRELAY_GAME_DISCONNECT; wrong length" );
|
||||
logf( XW_LOGERROR, "dropping XWRELAY_GAME_DISCONNECT; wrong length" );
|
||||
}
|
||||
} /* processDisconnect */
|
||||
|
||||
void
|
||||
killSocket( int socket, char* why )
|
||||
{
|
||||
logf( "killSocket(%d): %s", socket, why );
|
||||
logf( XW_LOGERROR, "killSocket(%d): %s", socket, why );
|
||||
CRefMgr::Get()->RemoveSocketRefs( socket );
|
||||
/* Might want to kill the thread it belongs to if we're not in it,
|
||||
e.g. when unable to write to another socket. */
|
||||
logf( "killSocket done" );
|
||||
logf( XW_LOGINFO, "killSocket done" );
|
||||
XWThreadPool::GetTPool()->CloseSocket( socket );
|
||||
}
|
||||
|
||||
|
@ -300,7 +300,7 @@ forwardMessage( unsigned char* buf, int buflen, int srcSocket )
|
|||
if ( getNetShort( &bufp, end, &cookieID )
|
||||
&& getNetByte( &bufp, end, &src )
|
||||
&& getNetByte( &bufp, end, &dest ) ) {
|
||||
logf( "cookieID = %d", cookieID );
|
||||
logf( XW_LOGINFO, "cookieID = %d", cookieID );
|
||||
|
||||
SafeCref scr( cookieID );
|
||||
success = scr.Forward( src, dest, buf, buflen );
|
||||
|
@ -314,28 +314,28 @@ processMessage( unsigned char* buf, int bufLen, int socket )
|
|||
XWRELAY_Cmd cmd = *buf;
|
||||
switch( cmd ) {
|
||||
case XWRELAY_GAME_CONNECT:
|
||||
logf( "processMessage got XWRELAY_CONNECT" );
|
||||
logf( XW_LOGINFO, "processMessage got XWRELAY_CONNECT" );
|
||||
processConnect( buf+1, bufLen-1, socket );
|
||||
break;
|
||||
case XWRELAY_GAME_RECONNECT:
|
||||
logf( "processMessage got XWRELAY_RECONNECT" );
|
||||
logf( XW_LOGINFO, "processMessage got XWRELAY_RECONNECT" );
|
||||
processReconnect( buf+1, bufLen-1, socket );
|
||||
break;
|
||||
case XWRELAY_GAME_DISCONNECT:
|
||||
processDisconnect( buf+1, bufLen-1, socket );
|
||||
break;
|
||||
case XWRELAY_HEARTBEAT:
|
||||
logf( "processMessage got XWRELAY_HEARTBEAT" );
|
||||
logf( XW_LOGINFO, "processMessage got XWRELAY_HEARTBEAT" );
|
||||
processHeartbeat( buf + 1, bufLen - 1, socket );
|
||||
break;
|
||||
case XWRELAY_MSG_TORELAY:
|
||||
logf( "processMessage got XWRELAY_MSG_TORELAY" );
|
||||
logf( XW_LOGINFO, "processMessage got XWRELAY_MSG_TORELAY" );
|
||||
if ( !forwardMessage( buf, bufLen, socket ) ) {
|
||||
killSocket( socket, "couldn't forward message" );
|
||||
}
|
||||
break;
|
||||
default:
|
||||
logf( "processMessage bad: %d", cmd );
|
||||
logf( XW_LOGINFO, "processMessage bad: %d", cmd );
|
||||
break;
|
||||
/* just drop it */
|
||||
}
|
||||
|
@ -353,15 +353,15 @@ make_socket( unsigned long addr, unsigned short port )
|
|||
|
||||
int result = bind( sock, (struct sockaddr*)&sockAddr, sizeof(sockAddr) );
|
||||
if ( result != 0 ) {
|
||||
logf( "exiting: unable to bind port %d: %d, errno = %d\n",
|
||||
logf( XW_LOGERROR, "exiting: unable to bind port %d: %d, errno = %d\n",
|
||||
port, result, errno );
|
||||
return -1;
|
||||
}
|
||||
logf( "bound socket %d on port %d", sock, port );
|
||||
logf( XW_LOGINFO, "bound socket %d on port %d", sock, port );
|
||||
|
||||
result = listen( sock, 5 );
|
||||
if ( result != 0 ) {
|
||||
logf( "exiting: unable to listen: %d, errno = %d\n", result, errno );
|
||||
logf( XW_LOGERROR, "exiting: unable to listen: %d, errno = %d\n", result, errno );
|
||||
return -1;
|
||||
}
|
||||
return sock;
|
||||
|
@ -533,7 +533,7 @@ int main( int argc, char** argv )
|
|||
int retval = select( highest, &rfds, NULL, NULL, NULL );
|
||||
if ( retval < 0 ) {
|
||||
if ( errno != 4 ) { /* 4's what we get when signal interrupts */
|
||||
logf( "errno: %d", errno );
|
||||
logf( XW_LOGINFO, "errno: %d", errno );
|
||||
}
|
||||
} else {
|
||||
if ( FD_ISSET( listener, &rfds ) ) {
|
||||
|
@ -541,7 +541,7 @@ int main( int argc, char** argv )
|
|||
socklen_t siz = sizeof(newaddr);
|
||||
int newSock = accept( listener, (sockaddr*)&newaddr, &siz );
|
||||
|
||||
logf( "accepting connection from %s",
|
||||
logf( XW_LOGINFO, "accepting connection from %s",
|
||||
inet_ntoa(newaddr.sin_addr) );
|
||||
|
||||
tPool->AddSocket( newSock );
|
||||
|
|
|
@ -7,7 +7,12 @@
|
|||
|
||||
typedef unsigned char HostID;
|
||||
|
||||
void logf( const char* format, ... );
|
||||
typedef enum {
|
||||
XW_LOGINFO
|
||||
,XW_LOGERROR
|
||||
} XW_LogLevel;
|
||||
|
||||
void logf( XW_LogLevel level, const char* format, ... );
|
||||
|
||||
void killSocket( int socket, char* why );
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue