diff --git a/xwords4/linux/cursesboard.c b/xwords4/linux/cursesboard.c index 4dc557c9b..600c441ac 100644 --- a/xwords4/linux/cursesboard.c +++ b/xwords4/linux/cursesboard.c @@ -52,8 +52,6 @@ struct CursesBoardGlobals { CursesMenuState* menuState; /* null if we're not using menus */ int refCount; - TransportProcs procs; - union { struct { XWStreamCtxt* stream; /* how we can reach the server */ @@ -224,7 +222,7 @@ cb_newFor( CursesBoardState* cbState, const NetLaunchInfo* nli, initCP( cGlobals ); if ( game_makeFromInvite( &cGlobals->game, NULL_XWE, nli, &selfAddr, cGlobals->util, (DrawCtx*)NULL, - &cGlobals->cp, &bGlobals->procs ) ) { + &cGlobals->cp, &cGlobals->procs ) ) { linuxSaveGame( cGlobals ); } else { XP_ASSERT( 0 ); @@ -363,6 +361,30 @@ curses_socket_acceptor( int listener, Acceptor func, CommonGlobals* cGlobals, } } +static void +initTProcsCurses( CommonGlobals* cGlobals ) +{ + cGlobals->procs.closure = cGlobals; + cGlobals->procs.sendMsg = linux_send; +#ifdef XWFEATURE_COMMS_INVITE + cGlobals->procs.sendInvt = linux_send_invt; +#endif +#ifdef COMMS_HEARTBEAT + cGlobals->procs.reset = linux_reset; +#endif +#ifdef XWFEATURE_RELAY + cGlobals->procs.rstatus = relay_status_curses; + cGlobals->procs.rconnd = relay_connd_curses; + cGlobals->procs.rerror = relay_error_curses; + cGlobals->procs.sendNoConn = relay_sendNoConn_curses; +#endif + cGlobals->procs.countChanged = curses_countChanged; + cGlobals->procs.getFlags = curses_getFlags; +# ifdef RELAY_VIA_HTTP + cGlobals->procs.requestJoin = relay_requestJoin_curses; +#endif +} + static CursesBoardGlobals* commonInit( CursesBoardState* cbState, sqlite3_int64 rowid, const CurGameInfo* gip ) @@ -386,26 +408,7 @@ commonInit( CursesBoardState* cbState, sqlite3_int64 rowid, cGlobals->onSaveClosure = bGlobals; cGlobals->addAcceptor = curses_socket_acceptor; - bGlobals->procs.closure = cGlobals; - bGlobals->procs.sendMsg = linux_send; -#ifdef XWFEATURE_COMMS_INVITE - bGlobals->procs.sendInvt = linux_send_invt; -#endif -#ifdef COMMS_HEARTBEAT - bGlobals->procs.reset = linux_reset; -#endif -#ifdef XWFEATURE_RELAY - bGlobals->procs.rstatus = relay_status_curses; - bGlobals->procs.rconnd = relay_connd_curses; - bGlobals->procs.rerror = relay_error_curses; - bGlobals->procs.sendNoConn = relay_sendNoConn_curses; -#endif - bGlobals->procs.countChanged = curses_countChanged; - bGlobals->procs.getFlags = curses_getFlags; -# ifdef RELAY_VIA_HTTP - bGlobals->procs.requestJoin = relay_requestJoin_curses; -#endif - + initTProcsCurses( cGlobals ); makeSelfAddress( &cGlobals->selfAddr, params ); setOneSecondTimer( cGlobals ); @@ -566,7 +569,7 @@ initNoDraw( CursesBoardState* cbState, sqlite3_int64 rowid, initCP( cGlobals ); - if ( linuxOpenGame( cGlobals, &result->procs ) ) { + if ( linuxOpenGame( cGlobals ) ) { result = ref( result ); } else { disposeBoard( result ); diff --git a/xwords4/linux/gtkboard.c b/xwords4/linux/gtkboard.c index ddd2ed1ac..cd290baeb 100644 --- a/xwords4/linux/gtkboard.c +++ b/xwords4/linux/gtkboard.c @@ -486,6 +486,7 @@ countChanged_gtk( XWEnv XP_UNUSED(xwe), void* closure, XP_U16 newCount ) static void setTransportProcs( TransportProcs* procs, GtkGameGlobals* globals ) { + XP_ASSERT( !procs->closure ); procs->closure = globals; procs->sendMsg = linux_send; #ifdef XWFEATURE_COMMS_INVITE @@ -618,10 +619,7 @@ createOrLoadObjects( GtkGameGlobals* globals ) cGlobals->draw = gtkDrawCtxtMake( globals->drawing_area, globals ); - TransportProcs procs; - setTransportProcs( &procs, globals ); - - if ( linuxOpenGame( cGlobals, &procs ) ) { + if ( linuxOpenGame( cGlobals ) ) { if ( !params->fileName && !!params->dbName ) { XP_UCHAR buf[64]; snprintf( buf, sizeof(buf), "%s / %lld", params->dbName, @@ -843,20 +841,10 @@ new_game_impl( GtkGameGlobals* globals, XP_Bool fireConnDlg ) if ( success ) { XP_Bool isClient = gi->serverRole == SERVER_ISCLIENT; XP_ASSERT( !isClient ); /* Doesn't make sense! Send invitation. */ - TransportProcs procs = { - .closure = globals, - .sendMsg = linux_send, -#ifdef XWFEATURE_COMMS_INVITE - .sendInvt = linux_send_invt, -#endif -#ifdef COMMS_HEARTBEAT - .reset = linux_reset, -#endif - }; (void)game_reset( MEMPOOL &cGlobals->game, NULL_XWE, gi, &cGlobals->selfAddr, NULL, cGlobals->util, - &cGlobals->cp, &procs ); + &cGlobals->cp, &cGlobals->procs ); (void)server_do( cGlobals->game.server, NULL_XWE ); /* assign tiles, etc. */ board_invalAll( cGlobals->game.board ); @@ -2426,6 +2414,8 @@ initGlobalsNoDraw( GtkGameGlobals* globals, LaunchParams* params, memset( globals, 0, sizeof(*globals) ); CommonGlobals* cGlobals = &globals->cGlobals; + setTransportProcs( &cGlobals->procs, globals ); + cGlobals->gi = &cGlobals->_gi; if ( !gi ) { gi = ¶ms->pgi; @@ -2667,9 +2657,6 @@ loadGameNoDraw( GtkGameGlobals* globals, LaunchParams* params, sqlite3* pDb = params->pDb; initGlobalsNoDraw( globals, params, NULL ); - TransportProcs procs; - setTransportProcs( &procs, globals ); - CommonGlobals* cGlobals = &globals->cGlobals; cGlobals->rowid = rowid; XWStreamCtxt* stream = mem_stream_make_raw( MPPARM(cGlobals->util->mpool) @@ -2678,7 +2665,8 @@ loadGameNoDraw( GtkGameGlobals* globals, LaunchParams* params, if ( loaded ) { loaded = game_makeFromStream( MEMPOOL NULL_XWE, stream, &cGlobals->game, cGlobals->gi, - cGlobals->util, (DrawCtx*)NULL, &cGlobals->cp, &procs ); + cGlobals->util, (DrawCtx*)NULL, + &cGlobals->cp, &cGlobals->procs ); if ( loaded ) { XP_LOGF( "%s: game loaded", __func__ ); #ifndef XWFEATURE_STANDALONE_ONLY diff --git a/xwords4/linux/gtkmain.c b/xwords4/linux/gtkmain.c index 94e72f2b6..e5e223be8 100644 --- a/xwords4/linux/gtkmain.c +++ b/xwords4/linux/gtkmain.c @@ -719,10 +719,9 @@ gameFromInvite( GtkAppGlobals* apg, const NetLaunchInfo* nli ) makeSelfAddress( &selfAddr, params ); CommonPrefs* cp = &cGlobals->cp; - TransportProcs* procs = NULL; game_makeFromInvite( &cGlobals->game, NULL_XWE, nli, &selfAddr, cGlobals->util, cGlobals->draw, - cp, procs ); + cp, &cGlobals->procs ); linuxSaveGame( cGlobals ); sqlite3_int64 rowid = cGlobals->rowid; diff --git a/xwords4/linux/linuxmain.c b/xwords4/linux/linuxmain.c index ee7126927..6d8148f42 100644 --- a/xwords4/linux/linuxmain.c +++ b/xwords4/linux/linuxmain.c @@ -160,7 +160,7 @@ send_msgs_idle( gpointer data ) } bool -linuxOpenGame( CommonGlobals* cGlobals, const TransportProcs* procs ) +linuxOpenGame( CommonGlobals* cGlobals ) { XWStreamCtxt* stream = NULL; XP_Bool opened = XP_FALSE; @@ -189,7 +189,7 @@ linuxOpenGame( CommonGlobals* cGlobals, const TransportProcs* procs ) opened = game_makeFromStream( MEMPOOL NULL_XWE, stream, &cGlobals->game, cGlobals->gi, cGlobals->util, cGlobals->draw, - &cGlobals->cp, procs ); + &cGlobals->cp, &cGlobals->procs ); XP_LOGFF( "loaded gi at %p", &cGlobals->gi ); stream_destroy( stream, NULL_XWE ); } @@ -202,7 +202,7 @@ linuxOpenGame( CommonGlobals* cGlobals, const TransportProcs* procs ) } game_makeNewGame( MEMPOOL NULL_XWE, &cGlobals->game, cGlobals->gi, &cGlobals->selfAddr, hostAddr, cGlobals->util, - cGlobals->draw, &cGlobals->cp, procs + cGlobals->draw, &cGlobals->cp, &cGlobals->procs #ifdef SET_GAMESEED , params->gameSeed #endif diff --git a/xwords4/linux/linuxmain.h b/xwords4/linux/linuxmain.h index 9ad7bf4c8..e0ba7bf33 100644 --- a/xwords4/linux/linuxmain.h +++ b/xwords4/linux/linuxmain.h @@ -114,7 +114,7 @@ XP_Bool parseSMSParams( LaunchParams* params, gchar** myPhone, XP_U16* myPort ); void makeSelfAddress( CommsAddrRec* selfAddr, const LaunchParams* params ); unsigned int makeRandomInt(); -bool linuxOpenGame( CommonGlobals* cGlobals, const TransportProcs* procs ); +bool linuxOpenGame( CommonGlobals* cGlobals ); void tryConnectToServer( CommonGlobals* cGlobals ); void ensureLocalPlayerNames( LaunchParams* params, CurGameInfo* gi ); void cancelTimers( CommonGlobals* cGlobals ); diff --git a/xwords4/linux/main.h b/xwords4/linux/main.h index 42f08e906..c338d98d3 100644 --- a/xwords4/linux/main.h +++ b/xwords4/linux/main.h @@ -224,6 +224,7 @@ typedef void (*OnSaveFunc)( void* closure, sqlite3_int64 rowid, struct CommonGlobals { CurGameInfo _gi; LaunchParams* params; + TransportProcs procs; CommonPrefs cp; XW_UtilCtxt* util;