Largely untested changes to allow BT in ncurses mode, centered around

integrating sockets into poll fdset; move relay and bt globals out of
a union so can test if in use without knowing current connection method.
This commit is contained in:
ehouse 2006-09-19 03:39:08 +00:00
parent c5a92a617a
commit 1695cd044b
5 changed files with 97 additions and 65 deletions

View file

@ -41,6 +41,7 @@
#include "cursesmain.h" #include "cursesmain.h"
#include "cursesask.h" #include "cursesask.h"
#include "cursesletterask.h" #include "cursesletterask.h"
#include "linuxbt.h"
#include "model.h" #include "model.h"
#include "draw.h" #include "draw.h"
#include "board.h" #include "board.h"
@ -595,8 +596,7 @@ SIGWINCH_handler( int signal )
} /* SIGWINCH_handler */ } /* SIGWINCH_handler */
static void static void
cursesListenOnSocket( CursesAppGlobals* globals, int newSock, cursesListenOnSocket( CursesAppGlobals* globals, int newSock )
XWStreamCtxt* XP_UNUSED(stream) )
{ {
XP_ASSERT( globals->fdCount+1 < FD_MAX ); XP_ASSERT( globals->fdCount+1 < FD_MAX );
@ -635,10 +635,20 @@ curses_socket_changed( void* closure, int oldSock, int newSock )
curses_stop_listening( globals, oldSock ); curses_stop_listening( globals, oldSock );
} }
if ( newSock != -1 ) { if ( newSock != -1 ) {
cursesListenOnSocket( globals, newSock, NULL ); cursesListenOnSocket( globals, newSock );
} }
} /* curses_socket_changed */ } /* curses_socket_changed */
static void
curses_socket_acceptor( int listener, Acceptor func, CommonGlobals* cGlobals )
{
CursesAppGlobals* globals = (CursesAppGlobals*)cGlobals;
XP_ASSERT( !cGlobals->acceptor || (func == cGlobals->acceptor) );
cGlobals->acceptor = func;
globals->csInfo.server.serverSocket = listener;
cursesListenOnSocket( globals, listener );
}
static int static int
figureTimeout( CursesAppGlobals* globals ) figureTimeout( CursesAppGlobals* globals )
{ {
@ -704,8 +714,23 @@ blocking_gotEvent( CursesAppGlobals* globals, int* ch )
--numEvents; --numEvents;
if ( globals->fdArray[fdIndex].fd
== globals->csInfo.server.serverSocket ) {
/* It's the listening socket: call platform's accept()
wrapper */
(*globals->cGlobals.acceptor)( globals->fdArray[fdIndex].fd,
globals );
} else {
/* It's a normal data socket */
if ( globals->cGlobals.params->conType == COMMS_CONN_RELAY ) {
nBytes = linux_relay_receive( &globals->cGlobals, buf, nBytes = linux_relay_receive( &globals->cGlobals, buf,
sizeof(buf) ); sizeof(buf) );
} else if ( globals->cGlobals.params->conType == COMMS_CONN_BT ) {
nBytes = linux_bt_receive( globals->fdArray[fdIndex].fd,
buf, sizeof(buf) );
} else {
XP_ASSERT( 0 );
}
if ( nBytes != -1 ) { if ( nBytes != -1 ) {
XWStreamCtxt* inboundS; XWStreamCtxt* inboundS;
@ -725,27 +750,27 @@ blocking_gotEvent( CursesAppGlobals* globals, int* ch )
XP_LOGF( "captured incoming ip address: 0x%lx", XP_LOGF( "captured incoming ip address: 0x%lx",
addrRec.u.ip_relay.ipAddr ); addrRec.u.ip_relay.ipAddr );
if ( comms_checkIncomingStream(globals->cGlobals.game.comms, if ( comms_checkIncomingStream(
inboundS, globals->cGlobals.game.comms,
&addrRec ) ) { inboundS, &addrRec ) ) {
XP_LOGF( "comms read port: %d", XP_LOGF( "comms read port: %d",
addrRec.u.ip_relay.port ); addrRec.u.ip_relay.port );
redraw = redraw = server_receiveMessage(
server_receiveMessage( globals->cGlobals.game.server, globals->cGlobals.game.server, inboundS );
inboundS );
} }
stream_destroy( inboundS ); stream_destroy( inboundS );
} }
/* if there's something to draw resulting from the message, we /* if there's something to draw resulting from the
need to give the main loop time to reflect that on the message, we need to give the main loop time to reflect
screen before giving the server another shot. So just call that on the screen before giving the server another
the idle proc. */ shot. So just call the idle proc. */
if ( redraw ) { if ( redraw ) {
curses_util_requestTime(globals->cGlobals.params->util); curses_util_requestTime(globals->cGlobals.params->util);
} }
} }
}
++fdIndex; ++fdIndex;
} }
@ -928,6 +953,7 @@ cursesmain( XP_Bool isServer, LaunchParams* params )
globals.cGlobals.socketChanged = curses_socket_changed; globals.cGlobals.socketChanged = curses_socket_changed;
globals.cGlobals.socketChangedClosure = &globals; globals.cGlobals.socketChangedClosure = &globals;
globals.cGlobals.addAcceptor = curses_socket_acceptor;
globals.cp.showBoardArrow = XP_TRUE; globals.cp.showBoardArrow = XP_TRUE;
globals.cp.showRobotScores = params->showRobotScores; globals.cp.showRobotScores = params->showRobotScores;
@ -937,17 +963,17 @@ cursesmain( XP_Bool isServer, LaunchParams* params )
setupCursesUtilCallbacks( &globals, params->util ); setupCursesUtilCallbacks( &globals, params->util );
if ( params->conType == COMMS_CONN_RELAY ) { if ( params->conType == COMMS_CONN_RELAY ) {
globals.cGlobals.u.relay.defaultServerName globals.cGlobals.defaultServerName
= params->connInfo.relay.relayName; = params->connInfo.relay.relayName;
} }
cursesListenOnSocket( &globals, 0, NULL ); /* stdin */ cursesListenOnSocket( &globals, 0 ); /* stdin */
piperesult = pipe( globals.timepipe ); piperesult = pipe( globals.timepipe );
XP_ASSERT( piperesult == 0 ); XP_ASSERT( piperesult == 0 );
/* reader pipe */ /* reader pipe */
cursesListenOnSocket( &globals, globals.timepipe[0], NULL ); cursesListenOnSocket( &globals, globals.timepipe[0] );
signal( SIGWINCH, SIGWINCH_handler ); signal( SIGWINCH, SIGWINCH_handler );
initCurses( &globals ); initCurses( &globals );
@ -960,6 +986,8 @@ cursesmain( XP_Bool isServer, LaunchParams* params )
if ( globals.cGlobals.game.comms ) { if ( globals.cGlobals.game.comms ) {
CommsAddrRec addr; CommsAddrRec addr;
if ( params->conType == COMMS_CONN_RELAY ) {
addr.conType = COMMS_CONN_RELAY; addr.conType = COMMS_CONN_RELAY;
addr.u.ip_relay.ipAddr = 0; /* ??? */ addr.u.ip_relay.ipAddr = 0; /* ??? */
addr.u.ip_relay.port = params->connInfo.relay.defaultSendPort; addr.u.ip_relay.port = params->connInfo.relay.defaultSendPort;
@ -967,6 +995,13 @@ cursesmain( XP_Bool isServer, LaunchParams* params )
sizeof(addr.u.ip_relay.hostName) - 1 ); sizeof(addr.u.ip_relay.hostName) - 1 );
XP_STRNCPY( addr.u.ip_relay.cookie, params->connInfo.relay.cookie, XP_STRNCPY( addr.u.ip_relay.cookie, params->connInfo.relay.cookie,
sizeof(addr.u.ip_relay.cookie) - 1 ); sizeof(addr.u.ip_relay.cookie) - 1 );
} else if ( params->conType == COMMS_CONN_BT ) {
addr.conType = COMMS_CONN_BT;
XP_ASSERT( sizeof(addr.u.bt.btAddr)
>= sizeof(params->connInfo.bt.hostAddr));
XP_MEMCPY( &addr.u.bt.btAddr, &params->connInfo.bt.hostAddr,
sizeof(params->connInfo.bt.hostAddr) );
}
comms_setAddr( globals.cGlobals.game.comms, &addr ); comms_setAddr( globals.cGlobals.game.comms, &addr );
} }

View file

@ -262,7 +262,7 @@ createOrLoadObjects( GtkAppGlobals* globals )
if ( addr.conType == COMMS_CONN_RELAY ) { if ( addr.conType == COMMS_CONN_RELAY ) {
XP_ASSERT( !!params->connInfo.relay.relayName ); XP_ASSERT( !!params->connInfo.relay.relayName );
globals->cGlobals.u.relay.defaultServerName globals->cGlobals.defaultServerName
= params->connInfo.relay.relayName; = params->connInfo.relay.relayName;
} }

View file

@ -143,7 +143,7 @@ static XP_Bool
lbt_accept( int listener, void* ctxt ) lbt_accept( int listener, void* ctxt )
{ {
CommonGlobals* globals = (CommonGlobals*)ctxt; CommonGlobals* globals = (CommonGlobals*)ctxt;
LinBtStuff* btStuff = globals->u.bt.btStuff; LinBtStuff* btStuff = globals->btStuff;
int sock = -1; int sock = -1;
struct sockaddr_l2 inaddr; struct sockaddr_l2 inaddr;
socklen_t slen; socklen_t slen;
@ -171,7 +171,7 @@ lbt_accept( int listener, void* ctxt )
static void static void
lbt_listenerSetup( CommonGlobals* globals ) lbt_listenerSetup( CommonGlobals* globals )
{ {
LinBtStuff* btStuff = globals->u.bt.btStuff; LinBtStuff* btStuff = globals->btStuff;
struct sockaddr_l2 saddr; struct sockaddr_l2 saddr;
int listener; int listener;
@ -192,12 +192,12 @@ lbt_listenerSetup( CommonGlobals* globals )
void void
linux_bt_open( CommonGlobals* globals, XP_Bool amMaster ) linux_bt_open( CommonGlobals* globals, XP_Bool amMaster )
{ {
LinBtStuff* btStuff = globals->u.bt.btStuff; LinBtStuff* btStuff = globals->btStuff;
if ( !btStuff ) { if ( !btStuff ) {
btStuff = globals->u.bt.btStuff btStuff = globals->btStuff
= lbt_make( MPPARM(globals->params->util->mpool) amMaster ); = lbt_make( MPPARM(globals->params->util->mpool) amMaster );
btStuff->globals = globals; btStuff->globals = globals;
globals->u.bt.btStuff = btStuff; globals->btStuff = btStuff;
if ( amMaster ) { if ( amMaster ) {
lbt_listenerSetup( globals ); lbt_listenerSetup( globals );
@ -214,14 +214,14 @@ linux_bt_open( CommonGlobals* globals, XP_Bool amMaster )
void void
linux_bt_close( CommonGlobals* globals ) linux_bt_close( CommonGlobals* globals )
{ {
LinBtStuff* btStuff = globals->u.bt.btStuff; LinBtStuff* btStuff = globals->btStuff;
if ( !!btStuff ) { if ( !!btStuff ) {
if ( btStuff->amMaster ) { if ( btStuff->amMaster ) {
close( btStuff->u.master.listener ); close( btStuff->u.master.listener );
} }
XP_FREE( globals->params->util->mpool, btStuff ); XP_FREE( globals->params->util->mpool, btStuff );
globals->u.bt.btStuff = NULL; globals->btStuff = NULL;
} }
} /* linux_bt_close */ } /* linux_bt_close */
@ -235,7 +235,7 @@ linux_bt_send( const XP_U8* buf, XP_U16 buflen,
XP_LOGF( "%s(len=%d)", __FUNCTION__, buflen ); XP_LOGF( "%s(len=%d)", __FUNCTION__, buflen );
btStuff = globals->u.bt.btStuff; btStuff = globals->btStuff;
if ( !!btStuff ) { if ( !!btStuff ) {
CommsAddrRec addr; CommsAddrRec addr;
if ( !addrP ) { if ( !addrP ) {
@ -282,7 +282,7 @@ linux_bt_receive( int sock, XP_U8* buf, XP_U16 buflen )
void void
linux_bt_socketclosed( CommonGlobals* globals, int sock ) linux_bt_socketclosed( CommonGlobals* globals, int sock )
{ {
LinBtStuff* btStuff = globals->u.bt.btStuff; LinBtStuff* btStuff = globals->btStuff;
if ( btStuff->amMaster ) { if ( btStuff->amMaster ) {
lbt_removeSock( btStuff, sock ); lbt_removeSock( btStuff, sock );
} }

View file

@ -359,7 +359,7 @@ linux_init_relay_socket( CommonGlobals* cGlobals )
return -1; return -1;
} else { } else {
XP_WARNF( "gethostbyname for %s worked", XP_WARNF( "gethostbyname for %s worked",
cGlobals->u.relay.defaultServerName ); cGlobals->defaultServerName );
} }
memcpy( &(to_sock.sin_addr.s_addr), host->h_addr_list[0], memcpy( &(to_sock.sin_addr.s_addr), host->h_addr_list[0],
sizeof(struct in_addr)); sizeof(struct in_addr));

View file

@ -104,14 +104,11 @@ struct CommonGlobals {
int socket; /* either relay or bt */ int socket; /* either relay or bt */
union { /* Used only for relay case */
struct {
char* defaultServerName; char* defaultServerName;
} relay;
struct { /* Used only for bluetooth case */
struct LinBtStuff* btStuff; struct LinBtStuff* btStuff;
} bt;
} u;
TimerProc timerProcs[NUM_TIMERS_PLUS_ONE]; TimerProc timerProcs[NUM_TIMERS_PLUS_ONE];
void* timerClosures[NUM_TIMERS_PLUS_ONE]; void* timerClosures[NUM_TIMERS_PLUS_ONE];