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
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 );
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 );
if ( fd == sock ) {
g_io_channel_unref( data->channel );
XP_LOGF( "%s: unreffing channel %p", __func__, data->channel );
g_free( data );
globals->sources = g_list_remove_link( globals->sources, sources );
break;
@ -1047,7 +1050,11 @@ curses_stop_listening( CursesAppGlobals* globals, int sock )
static gboolean
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;
int fd = g_io_channel_unix_get_fd( source );
unsigned char buf[1024];
@ -1102,7 +1109,7 @@ data_socket_proc( GIOChannel* source, GIOCondition condition, gpointer data )
}
}
}
return TRUE;
return keep;
} /* data_socket_proc */
#endif
@ -1851,7 +1858,9 @@ cursesUDPSocketChanged( void* closure, int newSock, int XP_UNUSED(oldSock),
SourceData* sd = g_malloc( sizeof(*sd) );
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 );
sd->proc = proc;
sd->procClosure = procClosure;

View file

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

View file

@ -468,6 +468,11 @@ run_cmds() {
[ 0 -ge $COUNT ] && break
NOW=$(date '+%s')
[ $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))
KEYS=( ${!ARGS[*]} )
KEY=${KEYS[$INDX]}

View file

@ -604,16 +604,18 @@ SafeCref::SafeCref( const char* cookie, const AddrInfo* addr, int clientVers,
, m_isValid( false )
, m_seenSeed( false )
{
CidInfo* cinfo;
cinfo = m_mgr->getMakeCookieRef( cookie, nPlayersH, nPlayersS,
langCode, gameSeed, wantsPublic, makePublic,
&m_seenSeed );
if ( cinfo != NULL ) {
CookieRef* cref = cinfo->GetRef();
m_locked = cref->Lock();
m_cinfo = cinfo;
m_isValid = true;
if ( playerCountsOk( nPlayersH, nPlayersS ) ) {
CidInfo* cinfo;
cinfo = m_mgr->getMakeCookieRef( cookie, nPlayersH, nPlayersS,
langCode, gameSeed, wantsPublic, makePublic,
&m_seenSeed );
if ( cinfo != NULL ) {
CookieRef* cref = cinfo->GetRef();
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_isValid( false )
{
CidInfo* cinfo;
assert( hid <= 4 ); /* no more than 4 hosts */
if ( playerCountsOk( nPlayersH, nPlayersS ) && hid <= 4 ) {
CidInfo* cinfo;
bool isDead = false;
cinfo = m_mgr->getMakeCookieRef( connName, cookie, hid, nPlayersH,
nPlayersS, gameSeed, langCode,
wantsPublic || makePublic, &isDead );
bool isDead = false;
cinfo = m_mgr->getMakeCookieRef( connName, cookie, hid, nPlayersH,
nPlayersS, gameSeed, langCode,
wantsPublic || makePublic, &isDead );
/* If the reconnect doesn't check out, treat it as a connect */
if ( NULL == cinfo ) {
logf( XW_LOGINFO, "%s: taking a second crack", __func__ );
m_hid = HOST_ID_NONE;
cinfo = m_mgr->getMakeCookieRef( cookie, nPlayersH, nPlayersS,
langCode, gameSeed,
wantsPublic, makePublic, &m_seenSeed );
}
if ( cinfo != NULL ) {
assert( cinfo->GetCid() == cinfo->GetRef()->GetCid() );
m_locked = cinfo->GetRef()->Lock();
m_cinfo = cinfo;
m_isValid = true;
m_dead = isDead;
/* If the reconnect doesn't check out, treat it as a connect */
if ( NULL == cinfo ) {
logf( XW_LOGINFO, "%s: taking a second crack", __func__ );
m_hid = HOST_ID_NONE;
cinfo = m_mgr->getMakeCookieRef( cookie, nPlayersH, nPlayersS,
langCode, gameSeed,
wantsPublic, makePublic, &m_seenSeed );
}
if ( cinfo != NULL ) {
assert( cinfo->GetCid() == cinfo->GetRef()->GetCid() );
m_locked = cinfo->GetRef()->Lock();
m_cinfo = cinfo;
m_isValid = true;
m_dead = isDead;
}
}
}
@ -729,3 +732,16 @@ SafeCref::~SafeCref()
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; }
private:
bool playerCountsOk( int nPlayersH, int nPlayersS );
CidInfo* m_cinfo;
CRefMgr* m_mgr;
AddrInfo m_addr;

View file

@ -51,6 +51,7 @@
#include <syslog.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <glib.h>
#if defined(__FreeBSD__)
# if (OSVERSION > 500000)
@ -90,8 +91,8 @@ static int s_nSpawns = 0;
static int g_maxsocks = -1;
static int g_udpsock = -1;
void
logf( XW_LogLevel level, const char* format, ... )
static bool
willLog( XW_LogLevel level )
{
RelayConfigs* rc = RelayConfigs::GetConfigs();
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
char buf[256];
va_list ap;
@ -118,6 +125,7 @@ logf( XW_LogLevel level, const char* format, ... )
bool useFile;
char logFile[256];
RelayConfigs* rc = RelayConfigs::GetConfigs();
useFile = rc->GetValueFor( "LOGFILE_PATH", logFile, sizeof(logFile) );
if ( useFile ) {
@ -416,7 +424,7 @@ send_via_udp( int socket, const struct sockaddr *dest_addr,
{
uint32_t packetNum = UDPAckTrack::nextPacketID( cmd );
struct iovec vec[10];
int iocount = 0;
unsigned int iocount = 0;
unsigned char header[1 + 1 + sizeof(packetNum)];
header[0] = XWPDEV_PROTO_VERSION;
@ -434,6 +442,7 @@ send_via_udp( int socket, const struct sockaddr *dest_addr,
if ( !ptr ) {
break;
}
assert( iocount < VSIZE(vec) );
vec[iocount].iov_base = ptr;
vec[iocount].iov_len = va_arg(ap, int);
++iocount;
@ -450,7 +459,14 @@ send_via_udp( int socket, const struct sockaddr *dest_addr,
if ( 0 > nSent ) {
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;
}
@ -458,7 +474,7 @@ send_via_udp( int socket, const struct sockaddr *dest_addr,
* socket. */
bool
send_with_length_unsafe( const AddrInfo* addr, const unsigned char* buf,
size_t bufLen )
const size_t bufLen )
{
assert( !!addr );
bool ok = false;
@ -471,7 +487,8 @@ send_with_length_unsafe( const AddrInfo* addr, const unsigned char* buf,
if ( nSent == sizeof(len) ) {
nSent = send( socket, buf, bufLen, 0 );
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;
} else {
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 VSIZE(arr) (sizeof(arr)/sizeof(arr[0]))
#endif