convert util_setTimer to take procptr; remove timer call on board; add

heartbeat to comms that uses util_setTimer with new timer type.
This commit is contained in:
ehouse 2005-06-23 04:16:53 +00:00
parent 5e02ca1c86
commit dfccb19667
5 changed files with 70 additions and 26 deletions

View file

@ -75,6 +75,8 @@ static void makeMiniWindowForText( BoardCtxt* board, XP_UCHAR* text,
static void invalTradeWindow( BoardCtxt* board, XP_S16 turn, XP_Bool redraw );
static void invalSelTradeWindow( BoardCtxt* board );
static void setTimerIf( BoardCtxt* board );
static void p_board_timerFired( void* closure, XWTimerReason why );
static XP_Bool replaceLastTile( BoardCtxt* board );
static XP_Bool setTrayVisState( BoardCtxt* board, XW_TrayVisState newState );
static XP_Bool advanceArrow( BoardCtxt* board );
@ -745,7 +747,8 @@ setTimerIf( BoardCtxt* board )
XP_Bool timerWanted = board->gi->timerEnabled && !board->gameOver;
if ( timerWanted && !board->timerPending ) {
util_setTimer( board->util, TIMER_TIMERTICK );
util_setTimer( board->util, TIMER_TIMERTICK, 0,
p_board_timerFired, board );
board->timerPending = XP_TRUE;
}
} /* setTimerIf */
@ -768,9 +771,10 @@ timerFiredForTimer( BoardCtxt* board )
setTimerIf( board );
} /* timerFiredForTimer */
void
board_timerFired( BoardCtxt* board, XWTimerReason why )
static void
p_board_timerFired( void* closure, XWTimerReason why )
{
BoardCtxt* board = (BoardCtxt*)closure;
if ( why == TIMER_PENDOWN ) {
timerFiredForPen( board );
} else {
@ -2136,7 +2140,7 @@ handlePenDownOnBoard( BoardCtxt* board, XP_U16 x, XP_U16 y )
if ( TRADE_IN_PROGRESS(board) && ptOnTradeWindow( board, x, y ) ) {
return XP_FALSE;
}
util_setTimer( board->util, TIMER_PENDOWN );
util_setTimer( board->util, TIMER_PENDOWN, 0, p_board_timerFired, board );
#ifdef XWFEATURE_SEARCHLIMIT
if ( board->gi->allowHintRect && board->trayVisState == TRAY_REVEALED ) {
result = startHintRegionDrag( board, x, y );
@ -2245,7 +2249,8 @@ board_handlePenDown( BoardCtxt* board, XP_U16 x, XP_U16 y, XP_Time when,
case OBJ_SCORE:
if ( figureScorePlayerTapped( board, x, y ) >= 0 ) {
util_setTimer( board->util, TIMER_PENDOWN );
util_setTimer( board->util, TIMER_PENDOWN, 0,
p_board_timerFired, board );
}
break;
default:

View file

@ -119,7 +119,6 @@ BoardObjectType board_getFocusOwner( BoardCtxt* board );
void board_hiliteCellAt( BoardCtxt* board, XP_U16 col, XP_U16 row );
void board_resetEngine( BoardCtxt* board );
void board_timerFired( BoardCtxt* board, XWTimerReason why );
XP_Bool board_commitTurn( BoardCtxt* board );

View file

@ -31,6 +31,7 @@
#include "strutils.h"
#define cEND 0x65454e44
#define HEARTBEAT_NONE 0
#ifndef XWFEATURE_STANDALONE_ONLY
@ -113,10 +114,11 @@ static AddressRecord* getRecordFor( CommsCtxt* comms,
static XP_S16 sendMsg( CommsCtxt* comms, MsgQueueElem* elem );
static void addToQueue( CommsCtxt* comms, MsgQueueElem* newMsgElem );
static XP_U16 countAddrRecs( CommsCtxt* comms );
static void comms_relayConnect( CommsCtxt* comms );
static XP_Bool comms_send_relay( CommsCtxt* comms, XWRELAY_Cmd cmd,
XWHostID destID, void* data, int dlen );
static void relayConnect( CommsCtxt* comms );
static XP_Bool send_via_relay( CommsCtxt* comms, XWRELAY_Cmd cmd,
XWHostID destID, void* data, int dlen );
static XWHostID getDestID( CommsCtxt* comms, XP_PlayerAddr channelNo );
static void setHeartbeatTimer( CommsCtxt* comms );
/****************************************************************************
* implementation
@ -325,7 +327,7 @@ comms_init( CommsCtxt* comms )
{
if ( comms->addr.conType == COMMS_CONN_RELAY ) {
comms->relayState = COMMS_RELAYSTATE_UNCONNECTED;
comms_relayConnect( comms );
relayConnect( comms );
}
}
@ -446,7 +448,7 @@ comms_setAddr( CommsCtxt* comms, CommsAddrRec* addr )
XP_MEMCPY( &comms->addr, addr, sizeof(comms->addr) );
/* We should now have a cookie so we can connect??? */
comms_relayConnect( comms );
relayConnect( comms );
} /* comms_setAddr */
CommsConnType
@ -618,8 +620,8 @@ sendMsg( CommsCtxt* comms, MsgQueueElem* elem )
if ( comms_getConType( comms ) == COMMS_CONN_RELAY ) {
if ( comms->relayState == COMMS_RELAYSTATE_CONNECTED ) {
XWHostID destID = getDestID( comms, channelNo );
result = comms_send_relay( comms, XWRELAY_MSG_TORELAY, destID,
elem->msg, elem->len );
result = send_via_relay( comms, XWRELAY_MSG_TORELAY, destID,
elem->msg, elem->len );
} else {
XP_LOGF( "skipping message: not connected" );
}
@ -676,6 +678,8 @@ relayPreProcess( CommsCtxt* comms, XWStreamCtxt* stream, XWHostID* senderID )
to be done later since we're inside the platform's socket read
proc now. */
comms_resendAll( comms );
setHeartbeatTimer( comms );
break;
case XWRELAY_MSG_FROMRELAY:
cookieID = stream_getU16( stream );
@ -792,6 +796,25 @@ comms_checkIncomingStream( CommsCtxt* comms, XWStreamCtxt* stream,
return validMessage;
} /* comms_checkIncomingStream */
static void
p_comms_timerFired( void* closure, XWTimerReason why )
{
CommsCtxt* comms = (CommsCtxt*)closure;
XP_ASSERT( why == TIMER_HEARTBEAT );
XP_LOGF( "comms_timerFired" );
if ( comms->heartbeat != HEARTBEAT_NONE ) {
send_via_relay( comms, XWRELAY_HEARTBEAT, HOST_ID_NONE, NULL, 0 );
setHeartbeatTimer( comms );
}
} /* comms_timerFired */
static void
setHeartbeatTimer( CommsCtxt* comms )
{
util_setTimer( comms->util, TIMER_HEARTBEAT, comms->heartbeat,
p_comms_timerFired, comms );
}
#ifdef DEBUG
void
comms_getStats( CommsCtxt* comms, XWStreamCtxt* stream )
@ -926,10 +949,10 @@ countAddrRecs( CommsCtxt* comms )
} /* countAddrRecs */
static XP_Bool
comms_send_relay( CommsCtxt* comms, XWRELAY_Cmd cmd, XWHostID destID,
void* data, int dlen )
send_via_relay( CommsCtxt* comms, XWRELAY_Cmd cmd, XWHostID destID,
void* data, int dlen )
{
XP_U16 result = 0;
XP_Bool success = XP_FALSE;
XP_U16 len = 0;
CommsAddrRec addr;
XWStreamCtxt* tmpStream;
@ -961,6 +984,11 @@ comms_send_relay( CommsCtxt* comms, XWRELAY_Cmd cmd, XWHostID destID,
stream_putU16( tmpStream, comms->cookieID );
comms->relayState = COMMS_RELAYSTATE_CONNECT_PENDING;
} else if ( cmd == XWRELAY_HEARTBEAT ) {
/* Add these for grins. Server can assert they match the IP
address it expects 'em on. */
stream_putU16( tmpStream, comms->cookieID );
stream_putU16( tmpStream, comms->myHostID );
}
len = stream_getSize( tmpStream );
@ -970,28 +998,34 @@ comms_send_relay( CommsCtxt* comms, XWRELAY_Cmd cmd, XWHostID destID,
}
stream_destroy( tmpStream );
if ( buf != NULL ) {
XP_U16 result;
XP_LOGF( "passing %d bytes to sendproc", len );
result = (*comms->sendproc)( buf, len, &addr, comms->sendClosure );
success = result == len;
if ( success ) {
setHeartbeatTimer( comms );
}
}
XP_FREE( comms->mpool, buf );
}
return result == len;
} /* comms_send_relay */
return success;
} /* send_via_relay */
/* Send a CONNECT message to the relay. This opens up a connection to the
* relay, and tells it our hostID and cookie so that it can associatate it
* with a socket. In the CONNECT_RESP we should get back what?
*/
static void
comms_relayConnect( CommsCtxt* comms )
relayConnect( CommsCtxt* comms )
{
XP_LOGF( "comms_relayConnect called" );
XP_LOGF( "relayConnect called" );
if ( !comms->connecting ) {
comms->connecting = XP_TRUE;
comms_send_relay( comms, XWRELAY_CONNECT, HOST_ID_NONE, NULL, 0 );
send_via_relay( comms, XWRELAY_CONNECT, HOST_ID_NONE, NULL, 0 );
comms->connecting = XP_FALSE;
}
} /* comms_relayConnect */
} /* relayConnect */
EXTERN_C_END

View file

@ -74,7 +74,10 @@ typedef XP_U16 XP_PlayerAddr;
typedef enum {
TIMER_PENDOWN = 1, /* ARM doesn't like ids of 0... */
TIMER_TIMERTICK
TIMER_TIMERTICK,
TIMER_HEARTBEAT,
TIMER_NUM_PLUS_ONE /* must be last */
} XWTimerReason;
#define MAX_NUM_PLAYERS 4

View file

@ -83,6 +83,8 @@ typedef struct BadWordInfo {
XP_UCHAR* words[MAX_TRAY_TILES+1]; /* can form in both directions */
} BadWordInfo;
typedef void (*TimerProc)( void* closure, XWTimerReason why );
/* Platform-specific utility functions that need to be
*/
typedef struct UtilVtable {
@ -120,7 +122,8 @@ typedef struct UtilVtable {
XP_Bool (*m_util_engineProgressCallback)( XW_UtilCtxt* uc );
void (*m_util_setTimer)( XW_UtilCtxt* uc, XWTimerReason why );
void (*m_util_setTimer)( XW_UtilCtxt* uc, XWTimerReason why, XP_U16 when,
TimerProc proc, void* closure );
void (*m_util_requestTime)( XW_UtilCtxt* uc );
@ -193,8 +196,8 @@ struct XW_UtilCtxt {
#define util_engineProgressCallback( uc ) \
(uc)->vtable->m_util_engineProgressCallback((uc))
#define util_setTimer( uc, why ) \
(uc)->vtable->m_util_setTimer((uc),(why))
#define util_setTimer( uc, why, when, proc, clos ) \
(uc)->vtable->m_util_setTimer((uc),(why),(when),(proc),(clos))
#define util_requestTime( uc ) \
(uc)->vtable->m_util_requestTime((uc))