Merge remote-tracking branch 'origin/android_branch' into android_branch

This commit is contained in:
Eric House 2013-07-16 08:07:41 -07:00
commit 4d9d65b67e
7 changed files with 111 additions and 58 deletions

View file

@ -987,7 +987,9 @@ cursesListenOnSocket( CursesAppGlobals* globals, int newSock
{ {
#ifdef USE_GLIBLOOP #ifdef USE_GLIBLOOP
GIOChannel* channel = g_io_channel_unix_new( newSock ); GIOChannel* channel = g_io_channel_unix_new( newSock );
guint watch = g_io_add_watch( channel, G_IO_IN | G_IO_ERR, XP_LOGF( "%s: created channel %p for socket %d", __func__, channel, newSock );
guint watch = g_io_add_watch( channel,
G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
func, globals ); func, globals );
SourceData* data = g_malloc( sizeof(*data) ); SourceData* data = g_malloc( sizeof(*data) );
@ -1018,6 +1020,7 @@ curses_stop_listening( CursesAppGlobals* globals, int sock )
gint fd = g_io_channel_unix_get_fd( data->channel ); gint fd = g_io_channel_unix_get_fd( data->channel );
if ( fd == sock ) { if ( fd == sock ) {
g_io_channel_unref( data->channel ); g_io_channel_unref( data->channel );
XP_LOGF( "%s: unreffing channel %p", __func__, data->channel );
g_free( data ); g_free( data );
globals->sources = g_list_remove_link( globals->sources, sources ); globals->sources = g_list_remove_link( globals->sources, sources );
break; break;
@ -1047,7 +1050,11 @@ curses_stop_listening( CursesAppGlobals* globals, int sock )
static gboolean static gboolean
data_socket_proc( GIOChannel* source, GIOCondition condition, gpointer data ) data_socket_proc( GIOChannel* source, GIOCondition condition, gpointer data )
{ {
if ( 0 != (G_IO_IN & condition) ) { gboolean keep = TRUE;
if ( 0 != ((G_IO_HUP|G_IO_ERR|G_IO_NVAL) & condition) ) {
XP_LOGF( "%s: got error condition; returning FALSE", __func__ );
keep = FALSE;
} else if ( 0 != (G_IO_IN & condition) ) {
CursesAppGlobals* globals = (CursesAppGlobals*)data; CursesAppGlobals* globals = (CursesAppGlobals*)data;
int fd = g_io_channel_unix_get_fd( source ); int fd = g_io_channel_unix_get_fd( source );
unsigned char buf[1024]; unsigned char buf[1024];
@ -1102,7 +1109,7 @@ data_socket_proc( GIOChannel* source, GIOCondition condition, gpointer data )
} }
} }
} }
return TRUE; return keep;
} /* data_socket_proc */ } /* data_socket_proc */
#endif #endif
@ -1851,7 +1858,9 @@ cursesUDPSocketChanged( void* closure, int newSock, int XP_UNUSED(oldSock),
SourceData* sd = g_malloc( sizeof(*sd) ); SourceData* sd = g_malloc( sizeof(*sd) );
sd->channel = g_io_channel_unix_new( newSock ); sd->channel = g_io_channel_unix_new( newSock );
sd->watch = g_io_add_watch( sd->channel, G_IO_IN | G_IO_ERR, XP_LOGF( "%s: created channel %p for socket %d", __func__, sd->channel, newSock );
sd->watch = g_io_add_watch( sd->channel,
G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
curses_app_socket_proc, globals ); curses_app_socket_proc, globals );
sd->proc = proc; sd->proc = proc;
sd->procClosure = procClosure; sd->procClosure = procClosure;

View file

@ -1170,26 +1170,29 @@ linux_close_socket( CommonGlobals* cGlobals )
static int static int
blocking_read( int fd, unsigned char* buf, const int len ) blocking_read( int fd, unsigned char* buf, const int len )
{ {
assert( -1 != fd ); int nRead = -1;
int nRead = 0; if ( 0 <= fd && 0 < len ) {
int tries; nRead = 0;
for ( tries = 5; nRead < len && tries > 0; --tries ) { int tries;
// XP_LOGF( "%s: blocking for %d bytes", __func__, len ); for ( tries = 5; nRead < len && tries > 0; --tries ) {
ssize_t nGot = read( fd, buf + nRead, len - nRead ); // XP_LOGF( "%s: blocking for %d bytes", __func__, len );
if ( nGot == 0 ) { ssize_t nGot = read( fd, buf + nRead, len - nRead );
XP_LOGF( "%s: read 0; let's try again (%d more times)", __func__, XP_LOGF( "%s: read(fd=%d, len=%d) => %d", __func__, fd, len - nRead, nGot );
tries ); if ( nGot == 0 ) {
usleep( 10000 ); XP_LOGF( "%s: read 0; let's try again (%d more times)", __func__,
} else if ( nGot < 0 ) { tries );
XP_LOGF( "read => %d (wanted %d), errno=%d (\"%s\")", nRead, usleep( 10000 );
len - nRead, errno, strerror(errno) ); } else if ( nGot < 0 ) {
break; XP_LOGF( "read => %d (wanted %d), errno=%d (\"%s\")", nRead,
len - nRead, errno, strerror(errno) );
break;
}
nRead += nGot;
} }
nRead += nGot;
}
if ( nRead < len ) { if ( nRead < len ) {
nRead = -1; nRead = -1;
}
} }
XP_LOGF( "%s(fd=%d, sought=%d) => %d", __func__, fd, len, nRead ); XP_LOGF( "%s(fd=%d, sought=%d) => %d", __func__, fd, len, nRead );

View file

@ -468,6 +468,11 @@ run_cmds() {
[ 0 -ge $COUNT ] && break [ 0 -ge $COUNT ] && break
NOW=$(date '+%s') NOW=$(date '+%s')
[ $NOW -ge $ENDTIME ] && break [ $NOW -ge $ENDTIME ] && break
if ls core.* >/dev/null 2>&1; then
echo "core file found; exiting..."
killall "$(basename $APP_NEW)"
break
fi
INDX=$(($RANDOM%COUNT)) INDX=$(($RANDOM%COUNT))
KEYS=( ${!ARGS[*]} ) KEYS=( ${!ARGS[*]} )
KEY=${KEYS[$INDX]} KEY=${KEYS[$INDX]}

View file

@ -604,16 +604,18 @@ SafeCref::SafeCref( const char* cookie, const AddrInfo* addr, int clientVers,
, m_isValid( false ) , m_isValid( false )
, m_seenSeed( false ) , m_seenSeed( false )
{ {
CidInfo* cinfo;
cinfo = m_mgr->getMakeCookieRef( cookie, nPlayersH, nPlayersS, if ( playerCountsOk( nPlayersH, nPlayersS ) ) {
langCode, gameSeed, wantsPublic, makePublic, CidInfo* cinfo;
&m_seenSeed ); cinfo = m_mgr->getMakeCookieRef( cookie, nPlayersH, nPlayersS,
if ( cinfo != NULL ) { langCode, gameSeed, wantsPublic, makePublic,
CookieRef* cref = cinfo->GetRef(); &m_seenSeed );
m_locked = cref->Lock(); if ( cinfo != NULL ) {
m_cinfo = cinfo; CookieRef* cref = cinfo->GetRef();
m_isValid = true; m_locked = cref->Lock();
m_cinfo = cinfo;
m_isValid = true;
}
} }
} }
@ -636,28 +638,29 @@ SafeCref::SafeCref( const char* connName, const char* cookie, HostID hid,
, m_hid( hid ) , m_hid( hid )
, m_isValid( false ) , m_isValid( false )
{ {
CidInfo* cinfo; if ( playerCountsOk( nPlayersH, nPlayersS ) && hid <= 4 ) {
assert( hid <= 4 ); /* no more than 4 hosts */ CidInfo* cinfo;
bool isDead = false; bool isDead = false;
cinfo = m_mgr->getMakeCookieRef( connName, cookie, hid, nPlayersH, cinfo = m_mgr->getMakeCookieRef( connName, cookie, hid, nPlayersH,
nPlayersS, gameSeed, langCode, nPlayersS, gameSeed, langCode,
wantsPublic || makePublic, &isDead ); wantsPublic || makePublic, &isDead );
/* If the reconnect doesn't check out, treat it as a connect */ /* If the reconnect doesn't check out, treat it as a connect */
if ( NULL == cinfo ) { if ( NULL == cinfo ) {
logf( XW_LOGINFO, "%s: taking a second crack", __func__ ); logf( XW_LOGINFO, "%s: taking a second crack", __func__ );
m_hid = HOST_ID_NONE; m_hid = HOST_ID_NONE;
cinfo = m_mgr->getMakeCookieRef( cookie, nPlayersH, nPlayersS, cinfo = m_mgr->getMakeCookieRef( cookie, nPlayersH, nPlayersS,
langCode, gameSeed, langCode, gameSeed,
wantsPublic, makePublic, &m_seenSeed ); wantsPublic, makePublic, &m_seenSeed );
} }
if ( cinfo != NULL ) { if ( cinfo != NULL ) {
assert( cinfo->GetCid() == cinfo->GetRef()->GetCid() ); assert( cinfo->GetCid() == cinfo->GetRef()->GetCid() );
m_locked = cinfo->GetRef()->Lock(); m_locked = cinfo->GetRef()->Lock();
m_cinfo = cinfo; m_cinfo = cinfo;
m_isValid = true; m_isValid = true;
m_dead = isDead; m_dead = isDead;
}
} }
} }
@ -729,3 +732,16 @@ SafeCref::~SafeCref()
m_mgr->m_cidlock->Relinquish( m_cinfo, recycle ); m_mgr->m_cidlock->Relinquish( m_cinfo, recycle );
} }
} }
bool
SafeCref::playerCountsOk( int nPlayersH, int nPlayersT )
{
bool result = ( 0 < nPlayersH && 4 >= nPlayersH
&& 0 < nPlayersT && 4 >= nPlayersT
&& nPlayersH < nPlayersT );
if ( !result ) {
logf( XW_LOGERROR, "%s: dropping with bad player counts: here: "
"%d; total: %d", __func__, nPlayersH, nPlayersT );
}
return result;
}

View file

@ -386,6 +386,7 @@ class SafeCref {
bool SeenSeed() { return m_seenSeed; } bool SeenSeed() { return m_seenSeed; }
private: private:
bool playerCountsOk( int nPlayersH, int nPlayersS );
CidInfo* m_cinfo; CidInfo* m_cinfo;
CRefMgr* m_mgr; CRefMgr* m_mgr;
AddrInfo m_addr; AddrInfo m_addr;

View file

@ -51,6 +51,7 @@
#include <syslog.h> #include <syslog.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/prctl.h> #include <sys/prctl.h>
#include <glib.h>
#if defined(__FreeBSD__) #if defined(__FreeBSD__)
# if (OSVERSION > 500000) # if (OSVERSION > 500000)
@ -90,8 +91,8 @@ static int s_nSpawns = 0;
static int g_maxsocks = -1; static int g_maxsocks = -1;
static int g_udpsock = -1; static int g_udpsock = -1;
void static bool
logf( XW_LogLevel level, const char* format, ... ) willLog( XW_LogLevel level )
{ {
RelayConfigs* rc = RelayConfigs::GetConfigs(); RelayConfigs* rc = RelayConfigs::GetConfigs();
int configLevel = level; int configLevel = level;
@ -103,7 +104,13 @@ logf( XW_LogLevel level, const char* format, ... )
} }
} }
if ( level <= configLevel ) { return level <= configLevel;
}
void
logf( XW_LogLevel level, const char* format, ... )
{
if ( willLog( level ) ) {
#ifdef USE_SYSLOG #ifdef USE_SYSLOG
char buf[256]; char buf[256];
va_list ap; va_list ap;
@ -118,6 +125,7 @@ logf( XW_LogLevel level, const char* format, ... )
bool useFile; bool useFile;
char logFile[256]; char logFile[256];
RelayConfigs* rc = RelayConfigs::GetConfigs();
useFile = rc->GetValueFor( "LOGFILE_PATH", logFile, sizeof(logFile) ); useFile = rc->GetValueFor( "LOGFILE_PATH", logFile, sizeof(logFile) );
if ( useFile ) { if ( useFile ) {
@ -416,7 +424,7 @@ send_via_udp( int socket, const struct sockaddr *dest_addr,
{ {
uint32_t packetNum = UDPAckTrack::nextPacketID( cmd ); uint32_t packetNum = UDPAckTrack::nextPacketID( cmd );
struct iovec vec[10]; struct iovec vec[10];
int iocount = 0; unsigned int iocount = 0;
unsigned char header[1 + 1 + sizeof(packetNum)]; unsigned char header[1 + 1 + sizeof(packetNum)];
header[0] = XWPDEV_PROTO_VERSION; header[0] = XWPDEV_PROTO_VERSION;
@ -434,6 +442,7 @@ send_via_udp( int socket, const struct sockaddr *dest_addr,
if ( !ptr ) { if ( !ptr ) {
break; break;
} }
assert( iocount < VSIZE(vec) );
vec[iocount].iov_base = ptr; vec[iocount].iov_base = ptr;
vec[iocount].iov_len = va_arg(ap, int); vec[iocount].iov_len = va_arg(ap, int);
++iocount; ++iocount;
@ -450,7 +459,14 @@ send_via_udp( int socket, const struct sockaddr *dest_addr,
if ( 0 > nSent ) { if ( 0 > nSent ) {
logf( XW_LOGERROR, "sendmsg->errno %d (%s)", errno, strerror(errno) ); logf( XW_LOGERROR, "sendmsg->errno %d (%s)", errno, strerror(errno) );
} }
logf( XW_LOGINFO, "%s()=>%d", __func__, nSent );
XW_LogLevel level = XW_LOGINFO;
if ( willLog( level ) ) {
gchar* b64 = g_base64_encode( (unsigned char*)dest_addr, sizeof(dest_addr) );
logf( level, "%s()=>%d; addr=%s", __func__, nSent, b64 );
g_free( b64 );
}
return nSent; return nSent;
} }
@ -458,7 +474,7 @@ send_via_udp( int socket, const struct sockaddr *dest_addr,
* socket. */ * socket. */
bool bool
send_with_length_unsafe( const AddrInfo* addr, const unsigned char* buf, send_with_length_unsafe( const AddrInfo* addr, const unsigned char* buf,
size_t bufLen ) const size_t bufLen )
{ {
assert( !!addr ); assert( !!addr );
bool ok = false; bool ok = false;
@ -471,7 +487,8 @@ send_with_length_unsafe( const AddrInfo* addr, const unsigned char* buf,
if ( nSent == sizeof(len) ) { if ( nSent == sizeof(len) ) {
nSent = send( socket, buf, bufLen, 0 ); nSent = send( socket, buf, bufLen, 0 );
if ( nSent == ssize_t(bufLen) ) { if ( nSent == ssize_t(bufLen) ) {
logf( XW_LOGINFO, "sent %d bytes on socket %d", nSent, socket ); logf( XW_LOGINFO, "%s: sent %d bytes on socket %d", __func__,
nSent, socket );
ok = true; ok = true;
} else { } else {
logf( XW_LOGERROR, "%s: send failed: %s (errno=%d)", __func__, logf( XW_LOGERROR, "%s: send failed: %s (errno=%d)", __func__,

View file

@ -259,4 +259,6 @@ typedef unsigned short CookieID;
#define COOKIE_ID_NONE 0 #define COOKIE_ID_NONE 0
#define VSIZE(arr) (sizeof(arr)/sizeof(arr[0]))
#endif #endif