mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-03 23:04:08 +01:00
cleanup; fix possible socket drop
Haven't seen it happen, but I think there was a bug that could have led to all the sockets coming back as ready from poll() being dropped. Fixed that and added/cleaned up some logging.
This commit is contained in:
parent
d5c4ecabce
commit
70dea02efc
2 changed files with 26 additions and 14 deletions
|
@ -400,35 +400,40 @@ XWThreadPool::real_listener()
|
||||||
curfd = 1;
|
curfd = 1;
|
||||||
|
|
||||||
int ii;
|
int ii;
|
||||||
for ( ii = 0; ii < nSockets && nEvents > 0; ++ii ) {
|
for ( ii = 0; ii < nSockets && nEvents > 0; ++ii, ++curfd ) {
|
||||||
|
|
||||||
if ( fds[curfd].revents != 0 ) {
|
if ( fds[curfd].revents != 0 ) {
|
||||||
// int socket = fds[curfd].fd;
|
// int socket = fds[curfd].fd;
|
||||||
SockInfo* sinfo = &sinfos[curfd];
|
SockInfo* sinfo = &sinfos[curfd];
|
||||||
const AddrInfo* addr = &sinfo->m_addr;
|
const AddrInfo* addr = &sinfo->m_addr;
|
||||||
|
|
||||||
assert( fds[curfd].fd == addr->getSocket() );
|
int sock = addr->getSocket();
|
||||||
|
assert( fds[curfd].fd == sock );
|
||||||
if ( !SocketFound( addr ) ) {
|
if ( !SocketFound( addr ) ) {
|
||||||
|
logf( XW_LOGINFO, "%s(): dropping socket %d: not found",
|
||||||
|
__func__, addr->getSocket() );
|
||||||
/* no further processing if it's been removed while
|
/* no further processing if it's been removed while
|
||||||
we've been sleeping in poll */
|
we've been sleeping in poll. BUT: shouldn't curfd
|
||||||
|
be incremented?? */
|
||||||
--nEvents;
|
--nEvents;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( 0 != (fds[curfd].revents & (POLLIN | POLLPRI)) ) {
|
if ( 0 != (fds[curfd].revents & (POLLIN | POLLPRI)) ) {
|
||||||
if ( !UdpQueue::get()->handle( addr, sinfo->m_proc ) ) {
|
if ( !UdpQueue::get()->handle( addr, sinfo->m_proc ) ) {
|
||||||
|
// This is likely wrong!!! return of 0 means
|
||||||
|
// remote closed, not error.
|
||||||
RemoveSocket( addr );
|
RemoveSocket( addr );
|
||||||
EnqueueKill( addr, "bad packet" );
|
EnqueueKill( addr, "got EOF" );
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logf( XW_LOGERROR, "odd revents: %x",
|
logf( XW_LOGERROR, "%s(): odd revents: %x; bad socket %d",
|
||||||
fds[curfd].revents );
|
__func__, fds[curfd].revents, sock );
|
||||||
RemoveSocket( addr );
|
RemoveSocket( addr );
|
||||||
EnqueueKill( addr, "error/hup in poll()" );
|
EnqueueKill( addr, "error/hup in poll()" );
|
||||||
}
|
}
|
||||||
--nEvents;
|
--nEvents;
|
||||||
}
|
}
|
||||||
++curfd;
|
|
||||||
}
|
}
|
||||||
assert( nEvents == 0 );
|
assert( nEvents == 0 );
|
||||||
}
|
}
|
||||||
|
|
|
@ -1303,14 +1303,16 @@ handleMsgsMsg( const AddrInfo* addr, bool sendFull,
|
||||||
const uint8_t* bufp, const uint8_t* end )
|
const uint8_t* bufp, const uint8_t* end )
|
||||||
{
|
{
|
||||||
unsigned short nameCount;
|
unsigned short nameCount;
|
||||||
int ii;
|
|
||||||
if ( getNetShort( &bufp, end, &nameCount ) ) {
|
if ( getNetShort( &bufp, end, &nameCount ) ) {
|
||||||
DBMgr* dbmgr = DBMgr::Get();
|
DBMgr* dbmgr = DBMgr::Get();
|
||||||
vector<uint8_t> out(4); /* space for len and n_msgs */
|
vector<uint8_t> out(4); /* space for len and n_msgs */
|
||||||
assert( out.size() == 4 );
|
assert( out.size() == 4 );
|
||||||
vector<int> msgIDs;
|
vector<int> msgIDs;
|
||||||
for ( ii = 0; ii < nameCount && bufp < end; ++ii ) {
|
for ( int ii = 0; ii < nameCount; ++ii ) {
|
||||||
|
if ( bufp >= end ) {
|
||||||
|
logf( XW_LOGERROR, "%s(): ran off the end", __func__ );
|
||||||
|
break;
|
||||||
|
}
|
||||||
// See NetUtils.java for reply format
|
// See NetUtils.java for reply format
|
||||||
// message-length: 2
|
// message-length: 2
|
||||||
// nameCount: 2
|
// nameCount: 2
|
||||||
|
@ -1344,9 +1346,14 @@ handleMsgsMsg( const AddrInfo* addr, bool sendFull,
|
||||||
memcpy( &out[0], &tmp, sizeof(tmp) );
|
memcpy( &out[0], &tmp, sizeof(tmp) );
|
||||||
tmp = htons( nameCount );
|
tmp = htons( nameCount );
|
||||||
memcpy( &out[2], &tmp, sizeof(tmp) );
|
memcpy( &out[2], &tmp, sizeof(tmp) );
|
||||||
ssize_t nwritten = write( addr->getSocket(), &out[0], out.size() );
|
int sock = addr->getSocket();
|
||||||
logf( XW_LOGVERBOSE0, "%s: wrote %d bytes", __func__, nwritten );
|
ssize_t nWritten = write( sock, &out[0], out.size() );
|
||||||
if ( sendFull && nwritten >= 0 && (size_t)nwritten == out.size() ) {
|
if ( nWritten < 0 ) {
|
||||||
|
logf( XW_LOGERROR, "%s(): write to socket %d failed: %d/%s", __func__,
|
||||||
|
sock, errno, strerror(errno) );
|
||||||
|
} else if ( sendFull && (size_t)nWritten == out.size() ) {
|
||||||
|
logf( XW_LOGVERBOSE0, "%s(): wrote %d bytes to socket %d", __func__,
|
||||||
|
nWritten, sock );
|
||||||
dbmgr->RecordSent( &msgIDs[0], msgIDs.size() );
|
dbmgr->RecordSent( &msgIDs[0], msgIDs.size() );
|
||||||
// This is wrong: should be removed when ACK returns and not
|
// This is wrong: should be removed when ACK returns and not
|
||||||
// before. But for some reason if I make that change apps wind up
|
// before. But for some reason if I make that change apps wind up
|
||||||
|
|
Loading…
Reference in a new issue