Put relay-only fields into a struct for easier identification. No code change.

This commit is contained in:
ehouse 2006-10-02 14:26:56 +00:00
parent 9837d8cca7
commit 715d01b8a4

View file

@ -55,7 +55,9 @@ typedef struct AddressRecord {
MsgID nextMsgID; /* on a per-channel basis */ MsgID nextMsgID; /* on a per-channel basis */
MsgID lastMsgReceived; /* on a per-channel basis */ MsgID lastMsgReceived; /* on a per-channel basis */
XP_PlayerAddr channelNo; XP_PlayerAddr channelNo;
struct {
XWHostID hostID; /* used for relay case */ XWHostID hostID; /* used for relay case */
} r;
} AddressRecord; } AddressRecord;
#define ADDRESSRECORD_SIZE_68K 20 #define ADDRESSRECORD_SIZE_68K 20
@ -88,24 +90,26 @@ struct CommsCtxt {
CommsAddrRec addr; CommsAddrRec addr;
/* Stuff for relays */ /* Stuff for relays */
XWHostID myHostID; /* 0 if unset, 1 if acting as server, random for struct {
client */ XWHostID myHostID; /* 0 if unset, 1 if acting as server,
random for client */
CommsRelaystate relayState; /* not saved: starts at UNCONNECTED */ CommsRelaystate relayState; /* not saved: starts at UNCONNECTED */
CookieID cookieID; /* not saved; temp standin for cookie; set by CookieID cookieID; /* not saved; temp standin for cookie; set
relay */ by relay */
/* permanent globally unique name, set by relay and forever after /* permanent globally unique name, set by relay and forever after
associated with this game. Used to reconnect. */ associated with this game. Used to reconnect. */
XP_UCHAR connName[MAX_CONNNAME_LEN+1]; XP_UCHAR connName[MAX_CONNNAME_LEN+1];
/* heartbeat: for periodic pings if relay thinks the network the device is /* heartbeat: for periodic pings if relay thinks the network the
on requires them. Not saved since only valid when connected, and we device is on requires them. Not saved since only valid when
reconnect for every game and after restarting. */ connected, and we reconnect for every game and after restarting. */
XP_U16 heartbeat; XP_U16 heartbeat;
XP_U16 nPlayersHere; XP_U16 nPlayersHere;
XP_U16 nPlayersTotal; XP_U16 nPlayersTotal;
XP_Bool connecting;
} r;
XP_Bool isServer; XP_Bool isServer;
XP_Bool connecting;
#ifdef DEBUG #ifdef DEBUG
XP_U16 nUniqueBytes; XP_U16 nUniqueBytes;
#endif #endif
@ -157,12 +161,12 @@ comms_make( MPFORMAL XW_UtilCtxt* util, XP_Bool isServer,
result->util = util; result->util = util;
#ifdef BEYOND_IR #ifdef BEYOND_IR
result->myHostID = isServer? HOST_ID_SERVER: HOST_ID_NONE; result->r.myHostID = isServer? HOST_ID_SERVER: HOST_ID_NONE;
XP_LOGF( "set myHostID to %d", result->myHostID ); XP_LOGF( "set myHostID to %d", result->r.myHostID );
result->relayState = COMMS_RELAYSTATE_UNCONNECTED; result->r.relayState = COMMS_RELAYSTATE_UNCONNECTED;
result->nPlayersHere = nPlayersHere; result->r.nPlayersHere = nPlayersHere;
result->nPlayersTotal = nPlayersTotal; result->r.nPlayersTotal = nPlayersTotal;
#endif #endif
return result; return result;
} /* comms_make */ } /* comms_make */
@ -212,9 +216,9 @@ comms_reset( CommsCtxt* comms, XP_Bool isServer,
comms->connID = CONN_ID_NONE; comms->connID = CONN_ID_NONE;
#ifdef BEYOND_IR #ifdef BEYOND_IR
comms->cookieID = COOKIE_ID_NONE; comms->r.cookieID = COOKIE_ID_NONE;
comms->nPlayersHere = nPlayersHere; comms->r.nPlayersHere = nPlayersHere;
comms->nPlayersTotal = nPlayersTotal; comms->r.nPlayersTotal = nPlayersTotal;
relayConnect( comms ); relayConnect( comms );
#endif #endif
} /* comms_reset */ } /* comms_reset */
@ -297,8 +301,8 @@ comms_makeFromStream( MPFORMAL XWStreamCtxt* stream, XW_UtilCtxt* util,
comms->connID = stream_getU32( stream ); comms->connID = stream_getU32( stream );
comms->nextChannelNo = stream_getU16( stream ); comms->nextChannelNo = stream_getU16( stream );
comms->myHostID = stream_getU8( stream ); comms->r.myHostID = stream_getU8( stream );
stringFromStreamHere( stream, comms->connName, sizeof(comms->connName) ); stringFromStreamHere( stream, comms->r.connName, sizeof(comms->r.connName) );
#ifdef DEBUG #ifdef DEBUG
comms->nUniqueBytes = stream_getU16( stream ); comms->nUniqueBytes = stream_getU16( stream );
@ -318,7 +322,7 @@ comms_makeFromStream( MPFORMAL XWStreamCtxt* stream, XW_UtilCtxt* util,
rec->nextMsgID = stream_getU16( stream ); rec->nextMsgID = stream_getU16( stream );
rec->lastMsgReceived = stream_getU16( stream ); rec->lastMsgReceived = stream_getU16( stream );
rec->channelNo = stream_getU16( stream ); rec->channelNo = stream_getU16( stream );
rec->hostID = stream_getU8( stream ); /* unneeded unless RELAY */ rec->r.hostID = stream_getU8( stream ); /* unneeded unless RELAY */
#ifdef DEBUG #ifdef DEBUG
rec->lastACK = stream_getU16( stream ); rec->lastACK = stream_getU16( stream );
@ -360,7 +364,7 @@ comms_start( CommsCtxt* comms )
{ {
#ifdef BEYOND_IR #ifdef BEYOND_IR
if ( comms->addr.conType == COMMS_CONN_RELAY ) { if ( comms->addr.conType == COMMS_CONN_RELAY ) {
comms->relayState = COMMS_RELAYSTATE_UNCONNECTED; comms->r.relayState = COMMS_RELAYSTATE_UNCONNECTED;
relayConnect( comms ); relayConnect( comms );
#ifdef XWFEATURE_BLUETOOTH #ifdef XWFEATURE_BLUETOOTH
} else if ( comms->addr.conType == COMMS_CONN_BT ) { } else if ( comms->addr.conType == COMMS_CONN_BT ) {
@ -416,13 +420,13 @@ comms_writeToStream( CommsCtxt* comms, XWStreamCtxt* stream )
MsgQueueElem* msg; MsgQueueElem* msg;
stream_putU8( stream, (XP_U8)comms->isServer ); stream_putU8( stream, (XP_U8)comms->isServer );
stream_putBits( stream, 4, comms->nPlayersHere ); stream_putBits( stream, 4, comms->r.nPlayersHere );
stream_putBits( stream, 4, comms->nPlayersTotal ); stream_putBits( stream, 4, comms->r.nPlayersTotal );
stream_putU32( stream, comms->connID ); stream_putU32( stream, comms->connID );
stream_putU16( stream, comms->nextChannelNo ); stream_putU16( stream, comms->nextChannelNo );
stream_putU8( stream, comms->myHostID ); stream_putU8( stream, comms->r.myHostID );
stringToStream( stream, comms->connName ); stringToStream( stream, comms->r.connName );
#ifdef DEBUG #ifdef DEBUG
stream_putU16( stream, comms->nUniqueBytes ); stream_putU16( stream, comms->nUniqueBytes );
@ -442,7 +446,7 @@ comms_writeToStream( CommsCtxt* comms, XWStreamCtxt* stream )
stream_putU16( stream, (XP_U16)rec->nextMsgID ); stream_putU16( stream, (XP_U16)rec->nextMsgID );
stream_putU16( stream, (XP_U16)rec->lastMsgReceived ); stream_putU16( stream, (XP_U16)rec->lastMsgReceived );
stream_putU16( stream, rec->channelNo ); stream_putU16( stream, rec->channelNo );
stream_putU8( stream, rec->hostID ); /* unneeded unless RELAY */ stream_putU8( stream, rec->r.hostID ); /* unneeded unless RELAY */
#ifdef DEBUG #ifdef DEBUG
stream_putU16( stream, rec->lastACK ); stream_putU16( stream, rec->lastACK );
stream_putU16( stream, rec->nUniqueBytes ); stream_putU16( stream, rec->nUniqueBytes );
@ -683,7 +687,7 @@ sendMsg( CommsCtxt* comms, MsgQueueElem* elem )
if ( 0 ) { if ( 0 ) {
#ifdef BEYOND_IR #ifdef BEYOND_IR
} else if ( comms_getConType( comms ) == COMMS_CONN_RELAY ) { } else if ( comms_getConType( comms ) == COMMS_CONN_RELAY ) {
if ( comms->relayState == COMMS_RELAYSTATE_ALLCONNECTED ) { if ( comms->r.relayState == COMMS_RELAYSTATE_ALLCONNECTED ) {
XWHostID destID = getDestID( comms, channelNo ); XWHostID destID = getDestID( comms, channelNo );
result = send_via_relay( comms, XWRELAY_MSG_TORELAY, destID, result = send_via_relay( comms, XWRELAY_MSG_TORELAY, destID,
elem->msg, elem->len ); elem->msg, elem->len );
@ -740,25 +744,25 @@ relayPreProcess( CommsCtxt* comms, XWStreamCtxt* stream, XWHostID* senderID )
case XWRELAY_CONNECT_RESP: case XWRELAY_CONNECT_RESP:
case XWRELAY_RECONNECT_RESP: case XWRELAY_RECONNECT_RESP:
comms->relayState = COMMS_RELAYSTATE_CONNECTED; comms->r.relayState = COMMS_RELAYSTATE_CONNECTED;
comms->heartbeat = stream_getU16( stream ); comms->r.heartbeat = stream_getU16( stream );
comms->cookieID = stream_getU16( stream ); comms->r.cookieID = stream_getU16( stream );
comms->myHostID = (XWHostID)stream_getU8( stream ); comms->r.myHostID = (XWHostID)stream_getU8( stream );
XP_LOGF( "got XWRELAY_CONNECTRESP; set cookieID = %d; " XP_LOGF( "got XWRELAY_CONNECTRESP; set cookieID = %d; "
"set hostid: %x", "set hostid: %x",
comms->cookieID, comms->myHostID ); comms->r.cookieID, comms->r.myHostID );
setHeartbeatTimer( comms ); setHeartbeatTimer( comms );
break; break;
case XWRELAY_ALLHERE: case XWRELAY_ALLHERE:
comms->relayState = COMMS_RELAYSTATE_ALLCONNECTED; comms->r.relayState = COMMS_RELAYSTATE_ALLCONNECTED;
hasName = stream_getU8( stream ); hasName = stream_getU8( stream );
if ( hasName ) { if ( hasName ) {
stringFromStreamHere( stream, comms->connName, stringFromStreamHere( stream, comms->r.connName,
sizeof(comms->connName) ); sizeof(comms->r.connName) );
XP_LOGF( "read connName: %s", comms->connName ); XP_LOGF( "read connName: %s", comms->r.connName );
} else { } else {
XP_ASSERT( comms->connName[0] != '\0' ); XP_ASSERT( comms->r.connName[0] != '\0' );
} }
/* We're [re-]connected now. Send any pending messages. This may /* We're [re-]connected now. Send any pending messages. This may
@ -773,8 +777,8 @@ relayPreProcess( CommsCtxt* comms, XWStreamCtxt* stream, XWHostID* senderID )
XP_LOGF( "cookieID: %d; srcID: %x; destID: %x", XP_LOGF( "cookieID: %d; srcID: %x; destID: %x",
cookieID, srcID, destID ); cookieID, srcID, destID );
/* If these values don't check out, drop it */ /* If these values don't check out, drop it */
consumed = cookieID != comms->cookieID consumed = cookieID != comms->r.cookieID
|| destID != comms->myHostID; || destID != comms->r.myHostID;
if ( consumed ) { if ( consumed ) {
XP_LOGF( "rejecting data message" ); XP_LOGF( "rejecting data message" );
} else { } else {
@ -795,7 +799,7 @@ relayPreProcess( CommsCtxt* comms, XWStreamCtxt* stream, XWHostID* senderID )
XP_LOGF( "XWRELAY_DISCONNECT_YOU|XWRELAY_CONNECTDENIED" ); XP_LOGF( "XWRELAY_DISCONNECT_YOU|XWRELAY_CONNECTDENIED" );
relayErr = stream_getU8( stream ); relayErr = stream_getU8( stream );
util_userError( comms->util, ERR_RELAY_BASE + relayErr ); util_userError( comms->util, ERR_RELAY_BASE + relayErr );
comms->relayState = COMMS_RELAYSTATE_UNCONNECTED; comms->r.relayState = COMMS_RELAYSTATE_UNCONNECTED;
/* fallthru */ /* fallthru */
default: default:
XP_LOGF( "dropping relay msg with cmd %d", (XP_U16)cmd ); XP_LOGF( "dropping relay msg with cmd %d", (XP_U16)cmd );
@ -949,7 +953,7 @@ p_comms_timerFired( void* closure, XWTimerReason XP_UNUSED_DBG(why) )
CommsCtxt* comms = (CommsCtxt*)closure; CommsCtxt* comms = (CommsCtxt*)closure;
XP_ASSERT( why == TIMER_HEARTBEAT ); XP_ASSERT( why == TIMER_HEARTBEAT );
XP_LOGF( "comms_timerFired" ); XP_LOGF( "comms_timerFired" );
if ( comms->heartbeat != HEARTBEAT_NONE ) { if ( comms->r.heartbeat != HEARTBEAT_NONE ) {
send_via_relay( comms, XWRELAY_HEARTBEAT, HOST_ID_NONE, NULL, 0 ); send_via_relay( comms, XWRELAY_HEARTBEAT, HOST_ID_NONE, NULL, 0 );
/* No need to reset timer. send_via_relay does that. */ /* No need to reset timer. send_via_relay does that. */
} }
@ -958,7 +962,7 @@ p_comms_timerFired( void* closure, XWTimerReason XP_UNUSED_DBG(why) )
static void static void
setHeartbeatTimer( CommsCtxt* comms ) setHeartbeatTimer( CommsCtxt* comms )
{ {
util_setTimer( comms->util, TIMER_HEARTBEAT, comms->heartbeat, util_setTimer( comms->util, TIMER_HEARTBEAT, comms->r.heartbeat,
p_comms_timerFired, comms ); p_comms_timerFired, comms );
} }
#endif #endif
@ -1023,7 +1027,7 @@ rememberChannelAddress( CommsCtxt* comms, XP_PlayerAddr channelNo,
recs->nextMsgID = 0; recs->nextMsgID = 0;
recs->channelNo = channelNo; recs->channelNo = channelNo;
recs->hostID = hostID; recs->r.hostID = hostID;
#ifdef DEBUG #ifdef DEBUG
recs->nUniqueBytes = 0; recs->nUniqueBytes = 0;
#endif #endif
@ -1036,7 +1040,7 @@ rememberChannelAddress( CommsCtxt* comms, XP_PlayerAddr channelNo,
if ( !!recs ) { if ( !!recs ) {
if ( !!addr ) { if ( !!addr ) {
XP_MEMCPY( &recs->addr, addr, sizeof(recs->addr) ); XP_MEMCPY( &recs->addr, addr, sizeof(recs->addr) );
XP_ASSERT( recs->hostID == hostID ); XP_ASSERT( recs->r.hostID == hostID );
} else { } else {
XP_MEMSET( &recs->addr, 0, sizeof(recs->addr) ); XP_MEMSET( &recs->addr, 0, sizeof(recs->addr) );
recs->addr.conType = comms->addr.conType; recs->addr.conType = comms->addr.conType;
@ -1098,7 +1102,7 @@ getDestID( CommsCtxt* comms, XP_PlayerAddr channelNo )
AddressRecord* recs; AddressRecord* recs;
for ( recs = comms->recs; !!recs; recs = recs->next ) { for ( recs = comms->recs; !!recs; recs = recs->next ) {
if ( recs->channelNo == channelNo ) { if ( recs->channelNo == channelNo ) {
id = recs->hostID; id = recs->r.hostID;
} }
} }
} }
@ -1127,8 +1131,8 @@ send_via_relay( CommsCtxt* comms, XWRELAY_Cmd cmd, XWHostID destID,
switch ( cmd ) { switch ( cmd ) {
case XWRELAY_MSG_TORELAY: case XWRELAY_MSG_TORELAY:
stream_putU16( tmpStream, comms->cookieID ); stream_putU16( tmpStream, comms->r.cookieID );
stream_putU8( tmpStream, comms->myHostID ); stream_putU8( tmpStream, comms->r.myHostID );
stream_putU8( tmpStream, destID ); stream_putU8( tmpStream, destID );
if ( data != NULL && dlen > 0 ) { if ( data != NULL && dlen > 0 ) {
stream_putBytes( tmpStream, data, dlen ); stream_putBytes( tmpStream, data, dlen );
@ -1137,33 +1141,33 @@ send_via_relay( CommsCtxt* comms, XWRELAY_Cmd cmd, XWHostID destID,
case XWRELAY_GAME_CONNECT: case XWRELAY_GAME_CONNECT:
stream_putU8( tmpStream, XWRELAY_PROTO_VERSION ); stream_putU8( tmpStream, XWRELAY_PROTO_VERSION );
stringToStream( tmpStream, addr.u.ip_relay.cookie ); stringToStream( tmpStream, addr.u.ip_relay.cookie );
stream_putU8( tmpStream, comms->myHostID ); stream_putU8( tmpStream, comms->r.myHostID );
stream_putU8( tmpStream, comms->nPlayersHere ); stream_putU8( tmpStream, comms->r.nPlayersHere );
stream_putU8( tmpStream, comms->nPlayersTotal ); stream_putU8( tmpStream, comms->r.nPlayersTotal );
comms->relayState = COMMS_RELAYSTATE_CONNECT_PENDING; comms->r.relayState = COMMS_RELAYSTATE_CONNECT_PENDING;
break; break;
case XWRELAY_GAME_RECONNECT: case XWRELAY_GAME_RECONNECT:
stream_putU8( tmpStream, XWRELAY_PROTO_VERSION ); stream_putU8( tmpStream, XWRELAY_PROTO_VERSION );
stream_putU8( tmpStream, comms->myHostID ); stream_putU8( tmpStream, comms->r.myHostID );
stream_putU8( tmpStream, comms->nPlayersHere ); stream_putU8( tmpStream, comms->r.nPlayersHere );
stream_putU8( tmpStream, comms->nPlayersTotal ); stream_putU8( tmpStream, comms->r.nPlayersTotal );
stringToStream( tmpStream, comms->connName ); stringToStream( tmpStream, comms->r.connName );
comms->relayState = COMMS_RELAYSTATE_CONNECT_PENDING; comms->r.relayState = COMMS_RELAYSTATE_CONNECT_PENDING;
break; break;
case XWRELAY_GAME_DISCONNECT: case XWRELAY_GAME_DISCONNECT:
stream_putU16( tmpStream, comms->cookieID ); stream_putU16( tmpStream, comms->r.cookieID );
stream_putU8( tmpStream, comms->myHostID ); stream_putU8( tmpStream, comms->r.myHostID );
break; break;
case XWRELAY_HEARTBEAT: case XWRELAY_HEARTBEAT:
/* Add these for grins. Server can assert they match the IP /* Add these for grins. Server can assert they match the IP
address it expects 'em on. */ address it expects 'em on. */
stream_putU16( tmpStream, comms->cookieID ); stream_putU16( tmpStream, comms->r.cookieID );
stream_putU8( tmpStream, comms->myHostID ); stream_putU8( tmpStream, comms->r.myHostID );
break; break;
default: default:
@ -1198,13 +1202,13 @@ static void
relayConnect( CommsCtxt* comms ) relayConnect( CommsCtxt* comms )
{ {
XP_LOGF( "relayConnect called" ); XP_LOGF( "relayConnect called" );
if ( comms->addr.conType == COMMS_CONN_RELAY && !comms->connecting ) { if ( comms->addr.conType == COMMS_CONN_RELAY && !comms->r.connecting ) {
comms->connecting = XP_TRUE; comms->r.connecting = XP_TRUE;
send_via_relay( comms, send_via_relay( comms,
comms->connName[0] == '\0' ? comms->r.connName[0] == '\0' ?
XWRELAY_GAME_CONNECT:XWRELAY_GAME_RECONNECT, XWRELAY_GAME_CONNECT:XWRELAY_GAME_RECONNECT,
comms->myHostID, NULL, 0 ); comms->r.myHostID, NULL, 0 );
comms->connecting = XP_FALSE; comms->r.connecting = XP_FALSE;
} }
} /* relayConnect */ } /* relayConnect */
@ -1228,8 +1232,8 @@ relayDisconnect( CommsCtxt* comms )
#ifdef BEYOND_IR #ifdef BEYOND_IR
XP_LOGF( "relayDisconnect called" ); XP_LOGF( "relayDisconnect called" );
if ( comms->addr.conType == COMMS_CONN_RELAY ) { if ( comms->addr.conType == COMMS_CONN_RELAY ) {
if ( comms->relayState != COMMS_RELAYSTATE_UNCONNECTED ) { if ( comms->r.relayState != COMMS_RELAYSTATE_UNCONNECTED ) {
comms->relayState = COMMS_RELAYSTATE_UNCONNECTED; comms->r.relayState = COMMS_RELAYSTATE_UNCONNECTED;
send_via_relay( comms, XWRELAY_GAME_DISCONNECT, HOST_ID_NONE, send_via_relay( comms, XWRELAY_GAME_DISCONNECT, HOST_ID_NONE,
NULL, 0 ); NULL, 0 );
} }