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 "cursesask.h"
#include "cursesletterask.h"
#include "linuxbt.h"
#include "model.h"
#include "draw.h"
#include "board.h"
@ -595,8 +596,7 @@ SIGWINCH_handler( int signal )
} /* SIGWINCH_handler */
static void
cursesListenOnSocket( CursesAppGlobals* globals, int newSock,
XWStreamCtxt* XP_UNUSED(stream) )
cursesListenOnSocket( CursesAppGlobals* globals, int newSock )
{
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 );
}
if ( newSock != -1 ) {
cursesListenOnSocket( globals, newSock, NULL );
cursesListenOnSocket( globals, newSock );
}
} /* 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
figureTimeout( CursesAppGlobals* globals )
{
@ -704,48 +714,63 @@ blocking_gotEvent( CursesAppGlobals* globals, int* ch )
--numEvents;
nBytes = linux_relay_receive( &globals->cGlobals, buf,
sizeof(buf) );
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,
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 ) {
XWStreamCtxt* inboundS;
redraw = XP_FALSE;
if ( nBytes != -1 ) {
XWStreamCtxt* inboundS;
redraw = XP_FALSE;
XP_STATUSF( "linuxReceive=>%d", nBytes );
inboundS = stream_from_msgbuf( &globals->cGlobals,
buf, nBytes );
if ( !!inboundS ) {
CommsAddrRec addrRec;
XP_STATUSF( "linuxReceive=>%d", nBytes );
inboundS = stream_from_msgbuf( &globals->cGlobals,
buf, nBytes );
if ( !!inboundS ) {
CommsAddrRec addrRec;
XP_MEMSET( &addrRec, 0, sizeof(addrRec) );
addrRec.conType = COMMS_CONN_RELAY;
XP_MEMSET( &addrRec, 0, sizeof(addrRec) );
addrRec.conType = COMMS_CONN_RELAY;
addrRec.u.ip_relay.ipAddr =
ntohl(addr_sock.sin_addr.s_addr);
XP_LOGF( "captured incoming ip address: 0x%lx",
addrRec.u.ip_relay.ipAddr );
addrRec.u.ip_relay.ipAddr =
ntohl(addr_sock.sin_addr.s_addr);
XP_LOGF( "captured incoming ip address: 0x%lx",
addrRec.u.ip_relay.ipAddr );
if ( comms_checkIncomingStream(globals->cGlobals.game.comms,
inboundS,
&addrRec ) ) {
XP_LOGF( "comms read port: %d",
addrRec.u.ip_relay.port );
redraw =
server_receiveMessage( globals->cGlobals.game.server,
inboundS );
if ( comms_checkIncomingStream(
globals->cGlobals.game.comms,
inboundS, &addrRec ) ) {
XP_LOGF( "comms read port: %d",
addrRec.u.ip_relay.port );
redraw = server_receiveMessage(
globals->cGlobals.game.server, inboundS );
}
stream_destroy( inboundS );
}
stream_destroy( inboundS );
}
/* if there's something to draw resulting from the message, we
need to give the main loop time to reflect that on the
screen before giving the server another shot. So just call
the idle proc. */
if ( redraw ) {
curses_util_requestTime(globals->cGlobals.params->util);
/* if there's something to draw resulting from the
message, we need to give the main loop time to reflect
that on the screen before giving the server another
shot. So just call the idle proc. */
if ( redraw ) {
curses_util_requestTime(globals->cGlobals.params->util);
}
}
}
}
++fdIndex;
}
@ -928,6 +953,7 @@ cursesmain( XP_Bool isServer, LaunchParams* params )
globals.cGlobals.socketChanged = curses_socket_changed;
globals.cGlobals.socketChangedClosure = &globals;
globals.cGlobals.addAcceptor = curses_socket_acceptor;
globals.cp.showBoardArrow = XP_TRUE;
globals.cp.showRobotScores = params->showRobotScores;
@ -937,17 +963,17 @@ cursesmain( XP_Bool isServer, LaunchParams* params )
setupCursesUtilCallbacks( &globals, params->util );
if ( params->conType == COMMS_CONN_RELAY ) {
globals.cGlobals.u.relay.defaultServerName
globals.cGlobals.defaultServerName
= params->connInfo.relay.relayName;
}
cursesListenOnSocket( &globals, 0, NULL ); /* stdin */
cursesListenOnSocket( &globals, 0 ); /* stdin */
piperesult = pipe( globals.timepipe );
XP_ASSERT( piperesult == 0 );
/* reader pipe */
cursesListenOnSocket( &globals, globals.timepipe[0], NULL );
cursesListenOnSocket( &globals, globals.timepipe[0] );
signal( SIGWINCH, SIGWINCH_handler );
initCurses( &globals );
@ -960,13 +986,22 @@ cursesmain( XP_Bool isServer, LaunchParams* params )
if ( globals.cGlobals.game.comms ) {
CommsAddrRec addr;
addr.conType = COMMS_CONN_RELAY;
addr.u.ip_relay.ipAddr = 0; /* ??? */
addr.u.ip_relay.port = params->connInfo.relay.defaultSendPort;
XP_STRNCPY( addr.u.ip_relay.hostName, params->connInfo.relay.relayName,
sizeof(addr.u.ip_relay.hostName) - 1 );
XP_STRNCPY( addr.u.ip_relay.cookie, params->connInfo.relay.cookie,
sizeof(addr.u.ip_relay.cookie) - 1 );
if ( params->conType == COMMS_CONN_RELAY ) {
addr.conType = COMMS_CONN_RELAY;
addr.u.ip_relay.ipAddr = 0; /* ??? */
addr.u.ip_relay.port = params->connInfo.relay.defaultSendPort;
XP_STRNCPY( addr.u.ip_relay.hostName, params->connInfo.relay.relayName,
sizeof(addr.u.ip_relay.hostName) - 1 );
XP_STRNCPY( addr.u.ip_relay.cookie, params->connInfo.relay.cookie,
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 );
}

View file

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

View file

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

View file

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

View file

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