mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-02-12 08:47:50 +01:00
don't try to read from socket if == -1
This commit is contained in:
parent
385243763a
commit
6418513a6d
1 changed files with 54 additions and 46 deletions
|
@ -915,6 +915,7 @@ linux_init_relay_socket( CommonGlobals* cGlobals, const CommsAddrRec* addrRec )
|
||||||
if ( 0 == connect( sock, (const struct sockaddr*)&to_sock,
|
if ( 0 == connect( sock, (const struct sockaddr*)&to_sock,
|
||||||
sizeof(to_sock) ) ) {
|
sizeof(to_sock) ) ) {
|
||||||
cGlobals->socket = sock;
|
cGlobals->socket = sock;
|
||||||
|
XP_LOGF( "%s: connected new socket %d to relay", __func__, sock );
|
||||||
|
|
||||||
struct timeval tv = {0};
|
struct timeval tv = {0};
|
||||||
tv.tv_sec = 15;
|
tv.tv_sec = 15;
|
||||||
|
@ -1157,6 +1158,7 @@ linux_send( const XP_U8* buf, XP_U16 buflen, const CommsAddrRec* addrRec,
|
||||||
void
|
void
|
||||||
linux_close_socket( CommonGlobals* cGlobals )
|
linux_close_socket( CommonGlobals* cGlobals )
|
||||||
{
|
{
|
||||||
|
LOG_FUNC();
|
||||||
int socket = cGlobals->socket;
|
int socket = cGlobals->socket;
|
||||||
|
|
||||||
(*cGlobals->socketChanged)( cGlobals->socketChangedClosure,
|
(*cGlobals->socketChanged)( cGlobals->socketChangedClosure,
|
||||||
|
@ -1177,7 +1179,8 @@ blocking_read( int fd, unsigned char* buf, const int len )
|
||||||
for ( tries = 5; nRead < len && tries > 0; --tries ) {
|
for ( tries = 5; nRead < len && tries > 0; --tries ) {
|
||||||
// XP_LOGF( "%s: blocking for %d bytes", __func__, len );
|
// XP_LOGF( "%s: blocking for %d bytes", __func__, len );
|
||||||
ssize_t nGot = read( fd, buf + nRead, len - nRead );
|
ssize_t nGot = read( fd, buf + nRead, len - nRead );
|
||||||
XP_LOGF( "%s: read(fd=%d, len=%d) => %d", __func__, fd, len - nRead, nGot );
|
XP_LOGF( "%s: read(fd=%d, len=%d) => %d", __func__, fd,
|
||||||
|
len - nRead, nGot );
|
||||||
if ( nGot == 0 ) {
|
if ( nGot == 0 ) {
|
||||||
XP_LOGF( "%s: read 0; let's try again (%d more times)", __func__,
|
XP_LOGF( "%s: read 0; let's try again (%d more times)", __func__,
|
||||||
tries );
|
tries );
|
||||||
|
@ -1204,58 +1207,63 @@ linux_relay_receive( CommonGlobals* cGlobals, unsigned char* buf, int bufSize )
|
||||||
{
|
{
|
||||||
LOG_FUNC();
|
LOG_FUNC();
|
||||||
int sock = cGlobals->socket;
|
int sock = cGlobals->socket;
|
||||||
unsigned short tmp;
|
ssize_t nRead = -1;
|
||||||
ssize_t nRead = blocking_read( sock, (unsigned char*)&tmp, sizeof(tmp) );
|
if ( 0 <= sock ) {
|
||||||
if ( nRead != 2 ) {
|
unsigned short tmp;
|
||||||
linux_close_socket( cGlobals );
|
nRead = blocking_read( sock, (unsigned char*)&tmp, sizeof(tmp) );
|
||||||
comms_transportFailed( cGlobals->game.comms );
|
if ( nRead != 2 ) {
|
||||||
nRead = -1;
|
linux_close_socket( cGlobals );
|
||||||
} else {
|
comms_transportFailed( cGlobals->game.comms );
|
||||||
unsigned short packetSize = ntohs( tmp );
|
nRead = -1;
|
||||||
XP_LOGF( "%s: got packet of size %d", __func__, packetSize );
|
} else {
|
||||||
assert( packetSize <= bufSize );
|
unsigned short packetSize = ntohs( tmp );
|
||||||
nRead = blocking_read( sock, buf, packetSize );
|
XP_LOGF( "%s: got packet of size %d", __func__, packetSize );
|
||||||
if ( nRead == packetSize ) {
|
assert( packetSize <= bufSize );
|
||||||
LaunchParams* params = cGlobals->params;
|
nRead = blocking_read( sock, buf, packetSize );
|
||||||
++params->nPacketsRcvd;
|
if ( nRead == packetSize ) {
|
||||||
if ( params->dropNthRcvd == 0 ) {
|
LaunchParams* params = cGlobals->params;
|
||||||
/* do nothing */
|
++params->nPacketsRcvd;
|
||||||
} else if ( params->dropNthRcvd > 0 ) {
|
if ( params->dropNthRcvd == 0 ) {
|
||||||
if ( params->nPacketsRcvd == params->dropNthRcvd ) {
|
/* do nothing */
|
||||||
XP_LOGF( "%s: dropping %dth packet per --drop-nth-packet",
|
} else if ( params->dropNthRcvd > 0 ) {
|
||||||
__func__, params->nPacketsRcvd );
|
if ( params->nPacketsRcvd == params->dropNthRcvd ) {
|
||||||
nRead = -1;
|
XP_LOGF( "%s: dropping %dth packet per "
|
||||||
}
|
"--drop-nth-packet",
|
||||||
} else {
|
__func__, params->nPacketsRcvd );
|
||||||
nRead = blocking_read( sock, buf, packetSize );
|
nRead = -1;
|
||||||
if ( nRead != packetSize ) {
|
}
|
||||||
nRead = -1;
|
|
||||||
} else {
|
} else {
|
||||||
LaunchParams* params = cGlobals->params;
|
nRead = blocking_read( sock, buf, packetSize );
|
||||||
++params->nPacketsRcvd;
|
if ( nRead != packetSize ) {
|
||||||
if ( params->dropNthRcvd == 0 ) {
|
nRead = -1;
|
||||||
/* do nothing */
|
|
||||||
} else if ( params->dropNthRcvd > 0 ) {
|
|
||||||
if ( params->nPacketsRcvd == params->dropNthRcvd ) {
|
|
||||||
XP_LOGF( "%s: dropping %dth packet per --drop-nth-packet",
|
|
||||||
__func__, params->nPacketsRcvd );
|
|
||||||
nRead = -1;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if ( 0 == XP_RANDOM() % -params->dropNthRcvd ) {
|
LaunchParams* params = cGlobals->params;
|
||||||
XP_LOGF( "%s: RANDOMLY dropping %dth packet "
|
++params->nPacketsRcvd;
|
||||||
"per --drop-nth-packet",
|
if ( params->dropNthRcvd == 0 ) {
|
||||||
__func__, params->nPacketsRcvd );
|
/* do nothing */
|
||||||
nRead = -1;
|
} else if ( params->dropNthRcvd > 0 ) {
|
||||||
|
if ( params->nPacketsRcvd == params->dropNthRcvd ) {
|
||||||
|
XP_LOGF( "%s: dropping %dth packet per "
|
||||||
|
"--drop-nth-packet",
|
||||||
|
__func__, params->nPacketsRcvd );
|
||||||
|
nRead = -1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ( 0 == XP_RANDOM() % -params->dropNthRcvd ) {
|
||||||
|
XP_LOGF( "%s: RANDOMLY dropping %dth packet "
|
||||||
|
"per --drop-nth-packet",
|
||||||
|
__func__, params->nPacketsRcvd );
|
||||||
|
nRead = -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ( -1 == nRead ) {
|
if ( -1 == nRead ) {
|
||||||
linux_close_socket( cGlobals );
|
linux_close_socket( cGlobals );
|
||||||
comms_transportFailed( cGlobals->game.comms );
|
comms_transportFailed( cGlobals->game.comms );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
XP_LOGF( "%s=>%d", __func__, nRead );
|
XP_LOGF( "%s=>%d", __func__, nRead );
|
||||||
|
|
Loading…
Add table
Reference in a new issue