mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-29 08:34:37 +01:00
deal with address change, esp of hostname, and separate name
resolution from socket opening.
This commit is contained in:
parent
45621dffdb
commit
f5320a46bd
2 changed files with 102 additions and 83 deletions
|
@ -34,17 +34,23 @@ palm_ip_setup( PalmAppGlobals* globals )
|
||||||
globals->nlStuff.netLibRef = 0; /* probably unnecessary */
|
globals->nlStuff.netLibRef = 0; /* probably unnecessary */
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static void
|
||||||
palm_ip_close( PalmAppGlobals* globals )
|
close_socket( PalmAppGlobals* globals )
|
||||||
{
|
{
|
||||||
if ( globals->nlStuff.netLibRef != 0 ) {
|
|
||||||
|
|
||||||
if ( globals->nlStuff.socket != -1 ) {
|
if ( globals->nlStuff.socket != -1 ) {
|
||||||
Err ignore;
|
Err ignore;
|
||||||
NetLibSocketClose( globals->nlStuff.netLibRef,
|
NetLibSocketClose( globals->nlStuff.netLibRef,
|
||||||
globals->nlStuff.socket, 0, &ignore);
|
globals->nlStuff.socket, 0, &ignore);
|
||||||
globals->nlStuff.socket = -1;
|
globals->nlStuff.socket = -1;
|
||||||
}
|
}
|
||||||
|
} /* close_socket */
|
||||||
|
|
||||||
|
void
|
||||||
|
palm_ip_close( PalmAppGlobals* globals )
|
||||||
|
{
|
||||||
|
if ( globals->nlStuff.netLibRef != 0 ) {
|
||||||
|
|
||||||
|
close_socket( globals );
|
||||||
|
|
||||||
NetLibClose(globals->nlStuff.netLibRef, 0);
|
NetLibClose(globals->nlStuff.netLibRef, 0);
|
||||||
|
|
||||||
|
@ -79,46 +85,7 @@ openNetLibIfNot( PalmAppGlobals* globals )
|
||||||
} /* openNetLibIfNot */
|
} /* openNetLibIfNot */
|
||||||
|
|
||||||
static XP_Bool
|
static XP_Bool
|
||||||
openSocketIfNot( PalmAppGlobals* globals )
|
connectSocket( PalmAppGlobals* globals, const CommsAddrRec* addr )
|
||||||
{
|
|
||||||
XP_Bool open = globals->nlStuff.socket != -1;
|
|
||||||
|
|
||||||
if ( !open ) {
|
|
||||||
Err err;
|
|
||||||
NetSocketRef socket;
|
|
||||||
|
|
||||||
XP_ASSERT( globals->nlStuff.netLibRef != 0 );
|
|
||||||
|
|
||||||
socket = NetLibSocketOpen( globals->nlStuff.netLibRef,
|
|
||||||
netSocketAddrINET,
|
|
||||||
netSocketTypeStream,
|
|
||||||
0, /* protocol (ignored) */
|
|
||||||
NETLIB_TIMEOUT, &err );
|
|
||||||
if ( err == errNone ) {
|
|
||||||
NetSocketLingerType lt;
|
|
||||||
|
|
||||||
open = XP_TRUE;
|
|
||||||
globals->nlStuff.socket = socket;
|
|
||||||
XP_LOGF( "Opened socket %d", socket );
|
|
||||||
|
|
||||||
/* Just for grins, turn linger off; suggested by
|
|
||||||
* http://tomfrauen.blogspot.com/2005/01/some-palm-os-network-programming.html
|
|
||||||
*/
|
|
||||||
lt.onOff = true;
|
|
||||||
lt.time = 0;
|
|
||||||
NetLibSocketOptionSet( globals->nlStuff.netLibRef, socket,
|
|
||||||
netSocketOptLevelSocket,
|
|
||||||
netSocketOptSockLinger, <, sizeof(lt),
|
|
||||||
NETLIB_TIMEOUT, &err );
|
|
||||||
} else {
|
|
||||||
XP_LOGF( "Failed to open socket: %d", err );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return open;
|
|
||||||
} /* openSocketIfNot */
|
|
||||||
|
|
||||||
static XP_Bool
|
|
||||||
connectSocket( PalmAppGlobals* globals, CommsAddrRec* addr )
|
|
||||||
{
|
{
|
||||||
XP_Bool success;
|
XP_Bool success;
|
||||||
Err err;
|
Err err;
|
||||||
|
@ -139,15 +106,56 @@ connectSocket( PalmAppGlobals* globals, CommsAddrRec* addr )
|
||||||
return success;
|
return success;
|
||||||
} /* connectSocket */
|
} /* connectSocket */
|
||||||
|
|
||||||
/* Only returns true if the name resolves and the machine on the other end is
|
static XP_Bool
|
||||||
* accepting connections on our port. Might be better to break the process
|
openSocketIfNot( PalmAppGlobals* globals, const CommsAddrRec* addr )
|
||||||
* up.
|
{
|
||||||
|
XP_Bool open = globals->nlStuff.socket != -1;
|
||||||
|
|
||||||
|
if ( !open ) {
|
||||||
|
Err err;
|
||||||
|
NetSocketRef socket;
|
||||||
|
|
||||||
|
XP_ASSERT( globals->nlStuff.netLibRef != 0 );
|
||||||
|
|
||||||
|
socket = NetLibSocketOpen( globals->nlStuff.netLibRef,
|
||||||
|
netSocketAddrINET,
|
||||||
|
netSocketTypeStream,
|
||||||
|
0, /* protocol (ignored) */
|
||||||
|
NETLIB_TIMEOUT, &err );
|
||||||
|
if ( err == errNone ) {
|
||||||
|
NetSocketLingerType lt;
|
||||||
|
|
||||||
|
XP_LOGF( "Opened socket %d", socket );
|
||||||
|
|
||||||
|
/* Just for grins, turn linger off; suggested by
|
||||||
|
* http://tomfrauen.blogspot.com/2005/01/some-palm-os-network-programming.html
|
||||||
*/
|
*/
|
||||||
|
lt.onOff = true;
|
||||||
|
lt.time = 0;
|
||||||
|
NetLibSocketOptionSet( globals->nlStuff.netLibRef, socket,
|
||||||
|
netSocketOptLevelSocket,
|
||||||
|
netSocketOptSockLinger, <, sizeof(lt),
|
||||||
|
NETLIB_TIMEOUT, &err );
|
||||||
|
|
||||||
|
globals->nlStuff.socket = socket;
|
||||||
|
open = connectSocket( globals, addr );
|
||||||
|
if ( !open ) {
|
||||||
|
close_socket( globals );
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
XP_LOGF( "Failed to open socket: %d", err );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return open;
|
||||||
|
} /* openSocketIfNot */
|
||||||
|
|
||||||
static XP_Bool
|
static XP_Bool
|
||||||
resolveAddressIfNot( PalmAppGlobals* globals, CommsAddrRec* addr,
|
resolveAddressIfNot( PalmAppGlobals* globals, CommsAddrRec* addr,
|
||||||
XP_Bool* resolvedP )
|
XP_Bool* resolvedP )
|
||||||
{
|
{
|
||||||
XP_Bool resolved = addr->u.ip_relay.ipAddr != 0;
|
XP_Bool resolved = addr->u.ip_relay.ipAddr != 0
|
||||||
|
&& !globals->nlStuff.ipAddrInval;
|
||||||
*resolvedP = XP_FALSE;
|
*resolvedP = XP_FALSE;
|
||||||
|
|
||||||
if ( !resolved ) {
|
if ( !resolved ) {
|
||||||
|
@ -161,7 +169,6 @@ resolveAddressIfNot( PalmAppGlobals* globals, CommsAddrRec* addr,
|
||||||
if ( result == NULL ) {
|
if ( result == NULL ) {
|
||||||
XP_LOGF( "NetLibGetHostByName => %d", err );
|
XP_LOGF( "NetLibGetHostByName => %d", err );
|
||||||
} else {
|
} else {
|
||||||
if ( openSocketIfNot( globals ) ) {
|
|
||||||
|
|
||||||
XP_ASSERT( result->addrLen == sizeof(addr->u.ip_relay.ipAddr) );
|
XP_ASSERT( result->addrLen == sizeof(addr->u.ip_relay.ipAddr) );
|
||||||
/* Addresses are in host byte order. So just copy. */
|
/* Addresses are in host byte order. So just copy. */
|
||||||
|
@ -170,15 +177,29 @@ resolveAddressIfNot( PalmAppGlobals* globals, CommsAddrRec* addr,
|
||||||
XP_LOGF( "got address 0x%lx for %s", addr->u.ip_relay.ipAddr,
|
XP_LOGF( "got address 0x%lx for %s", addr->u.ip_relay.ipAddr,
|
||||||
addr->u.ip_relay.hostName );
|
addr->u.ip_relay.hostName );
|
||||||
|
|
||||||
if ( connectSocket( globals, addr ) ) {
|
|
||||||
*resolvedP = resolved = XP_TRUE;
|
*resolvedP = resolved = XP_TRUE;
|
||||||
}
|
globals->nlStuff.ipAddrInval = XP_FALSE;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return resolved;
|
return resolved;
|
||||||
} /* resolveAddressIfNot */
|
} /* resolveAddressIfNot */
|
||||||
|
|
||||||
|
void
|
||||||
|
ip_addr_change( PalmAppGlobals* globals, const CommsAddrRec* oldAddr,
|
||||||
|
const CommsAddrRec* newAddr )
|
||||||
|
{
|
||||||
|
/* If host name changing, close any open socket and set up to inval
|
||||||
|
the cached ip address. */
|
||||||
|
if ( 0 != XP_STRNCMP( oldAddr->u.ip_relay.hostName,
|
||||||
|
newAddr->u.ip_relay.hostName,
|
||||||
|
sizeof( oldAddr->u.ip_relay.hostName ) ) ) {
|
||||||
|
|
||||||
|
close_socket( globals );
|
||||||
|
globals->nlStuff.ipAddrInval = XP_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
} /* ip_addr_change */
|
||||||
|
|
||||||
/* Deal with NetLibSend's willingness to send less than the full buffer */
|
/* Deal with NetLibSend's willingness to send less than the full buffer */
|
||||||
static XP_Bool
|
static XP_Bool
|
||||||
sendLoop( PalmAppGlobals* globals, XP_U8* buf, XP_U16 len )
|
sendLoop( PalmAppGlobals* globals, XP_U8* buf, XP_U16 len )
|
||||||
|
@ -233,12 +254,13 @@ palm_ip_send( XP_U8* buf, XP_U16 len, CommsAddrRec* addr,
|
||||||
|
|
||||||
if ( openNetLibIfNot( globals ) ) {
|
if ( openNetLibIfNot( globals ) ) {
|
||||||
if ( resolveAddressIfNot( globals, addr, &resolved ) ) {
|
if ( resolveAddressIfNot( globals, addr, &resolved ) ) {
|
||||||
XP_U16 netlen;
|
|
||||||
|
|
||||||
if ( resolved ) {
|
if ( resolved ) {
|
||||||
comms_setAddr( globals->game.comms, addr );
|
comms_setAddr( globals->game.comms, addr );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( openSocketIfNot( globals, addr ) ) {
|
||||||
|
XP_U16 netlen;
|
||||||
|
|
||||||
/* Send the length */
|
/* Send the length */
|
||||||
netlen = XP_HTONS( len );
|
netlen = XP_HTONS( len );
|
||||||
if ( sendLoop( globals, (XP_U8*)&netlen, sizeof(netlen) )
|
if ( sendLoop( globals, (XP_U8*)&netlen, sizeof(netlen) )
|
||||||
|
@ -247,6 +269,7 @@ palm_ip_send( XP_U8* buf, XP_U16 len, CommsAddrRec* addr,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return nSent;
|
return nSent;
|
||||||
} /* palm_ip_send */
|
} /* palm_ip_send */
|
||||||
|
|
||||||
|
@ -295,7 +318,7 @@ recvLoop( PalmAppGlobals* globals, XP_U8* buf, XP_U16 lenSought )
|
||||||
} /* recvLoop */
|
} /* recvLoop */
|
||||||
|
|
||||||
static XWStreamCtxt*
|
static XWStreamCtxt*
|
||||||
packetToStream( PalmAppGlobals* globals, CommsAddrRec* retAddr )
|
packetToStream( PalmAppGlobals* globals )
|
||||||
{
|
{
|
||||||
XP_U8 buf[MAX_MSG_LEN];
|
XP_U8 buf[MAX_MSG_LEN];
|
||||||
XWStreamCtxt* result = NULL;
|
XWStreamCtxt* result = NULL;
|
||||||
|
@ -335,24 +358,18 @@ checkHandleNetEvents( PalmAppGlobals* globals )
|
||||||
netFDSet( sysFileDescStdIn, &readFDs );
|
netFDSet( sysFileDescStdIn, &readFDs );
|
||||||
width = XP_MAX( globals->nlStuff.socket, sysFileDescStdIn );
|
width = XP_MAX( globals->nlStuff.socket, sysFileDescStdIn );
|
||||||
|
|
||||||
|
XP_ASSERT( globals->nlStuff.netLibRef != 0 );
|
||||||
nSockets = NetLibSelect( globals->nlStuff.netLibRef,
|
nSockets = NetLibSelect( globals->nlStuff.netLibRef,
|
||||||
width + 1,
|
width + 1,
|
||||||
&readFDs, &writeFDs, &ignoreFDs,
|
&readFDs, &writeFDs, &ignoreFDs,
|
||||||
NETLIB_TIMEOUT, /* timeout */
|
globals->runningOnPOSE? 0 : NETLIB_TIMEOUT,
|
||||||
&err );
|
&err );
|
||||||
|
|
||||||
if ( nSockets > 0 && err == errNone ) {
|
if ( nSockets > 0 && netFDIsSet(globals->nlStuff.socket, &readFDs) ) {
|
||||||
if ( netFDIsSet( globals->nlStuff.socket, &readFDs ) ) {
|
|
||||||
|
|
||||||
XWStreamCtxt* instream;
|
XWStreamCtxt* instream = packetToStream( globals );
|
||||||
CommsAddrRec raddr;
|
|
||||||
|
|
||||||
XP_MEMSET( &raddr, 0, sizeof(raddr) );
|
|
||||||
|
|
||||||
instream = packetToStream( globals, &raddr );
|
|
||||||
if ( !!instream ) {
|
if ( !!instream ) {
|
||||||
checkAndDeliver( globals, instream, &raddr );
|
checkAndDeliver( globals, instream );
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,8 @@ void palm_ip_setup( PalmAppGlobals* globals );
|
||||||
void palm_ip_close( PalmAppGlobals* globals );
|
void palm_ip_close( PalmAppGlobals* globals );
|
||||||
XP_S16 palm_ip_send( XP_U8* buf, XP_U16 len, CommsAddrRec* addr,
|
XP_S16 palm_ip_send( XP_U8* buf, XP_U16 len, CommsAddrRec* addr,
|
||||||
PalmAppGlobals* globals );
|
PalmAppGlobals* globals );
|
||||||
|
void ip_addr_change( PalmAppGlobals* globals, const CommsAddrRec* oldAddr,
|
||||||
|
const CommsAddrRec* newAddr );
|
||||||
void checkHandleNetEvents( PalmAppGlobals* globals );
|
void checkHandleNetEvents( PalmAppGlobals* globals );
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Reference in a new issue