fix timeout error that caused us to drop connections prematurely

This commit is contained in:
Charles Kerr 2007-11-17 17:49:30 +00:00
parent bf191cec1b
commit 54a2bb978b
2 changed files with 52 additions and 43 deletions

View File

@ -50,6 +50,7 @@ struct tr_peerIo
int port;
int socket;
int encryptionMode;
int timeout;
struct bufferevent * bufev;
uint8_t peerId[20];
@ -130,12 +131,13 @@ tr_peerIoNew( struct tr_handle * handle,
c->port = port;
c->socket = socket;
c->isIncoming = isIncoming ? 1 : 0;
c->timeout = IO_TIMEOUT_SECS;
c->bufev = bufferevent_new( c->socket,
canReadWrapper,
NULL,
gotErrorWrapper,
c );
bufferevent_settimeout( c->bufev, IO_TIMEOUT_SECS, IO_TIMEOUT_SECS );
bufferevent_settimeout( c->bufev, c->timeout, c->timeout );
bufferevent_enable( c->bufev, EV_READ|EV_WRITE );
bufferevent_setwatermark( c->bufev, EV_READ, 0, TR_RDBUF );
@ -272,7 +274,7 @@ tr_peerIoReconnect( tr_peerIo * io )
io->bufev = bufferevent_new( io->socket,
canReadWrapper, NULL, gotErrorWrapper,
io );
bufferevent_settimeout( io->bufev, IO_TIMEOUT_SECS, IO_TIMEOUT_SECS );
bufferevent_settimeout( io->bufev, io->timeout, io->timeout );
bufferevent_enable( io->bufev, EV_READ|EV_WRITE );
bufferevent_setwatermark( io->bufev, EV_READ, 0, TR_RDBUF );
@ -285,7 +287,8 @@ tr_peerIoReconnect( tr_peerIo * io )
void
tr_peerIoSetTimeoutSecs( tr_peerIo * io, int secs )
{
bufferevent_settimeout( io->bufev, secs, secs );
io->timeout = secs;
bufferevent_settimeout( io->bufev, io->timeout, io->timeout );
}
/**
@ -434,6 +437,10 @@ tr_peerIoIsEncrypted( const tr_peerIo * io )
return io!=NULL && io->encryptionMode==PEER_ENCRYPTION_RC4;
}
/**
***
**/
void
tr_peerIoWriteBytes( tr_peerIo * io,
struct evbuffer * outbuf,
@ -460,6 +467,14 @@ tr_peerIoWriteBytes( tr_peerIo * io,
}
}
void
tr_peerIoWriteUint8( tr_peerIo * io,
struct evbuffer * outbuf,
uint8_t writeme )
{
tr_peerIoWriteBytes( io, outbuf, &writeme, sizeof(uint8_t) );
}
void
tr_peerIoWriteUint16( tr_peerIo * io,
struct evbuffer * outbuf,
@ -469,14 +484,6 @@ tr_peerIoWriteUint16( tr_peerIo * io,
tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof(uint16_t) );
}
void
tr_peerIoWriteUint8( tr_peerIo * io,
struct evbuffer * outbuf,
uint8_t writeme )
{
tr_peerIoWriteBytes( io, outbuf, &writeme, sizeof(uint8_t) );
}
void
tr_peerIoWriteUint32( tr_peerIo * io,
struct evbuffer * outbuf,
@ -486,6 +493,10 @@ tr_peerIoWriteUint32( tr_peerIo * io,
tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof(uint32_t) );
}
/***
****
***/
void
tr_peerIoReadBytes( tr_peerIo * io,
struct evbuffer * inbuf,
@ -510,6 +521,14 @@ tr_peerIoReadBytes( tr_peerIo * io,
}
}
void
tr_peerIoReadUint8( tr_peerIo * io,
struct evbuffer * inbuf,
uint8_t * setme )
{
tr_peerIoReadBytes( io, inbuf, setme, sizeof(uint8_t) );
}
void
tr_peerIoReadUint16( tr_peerIo * io,
struct evbuffer * inbuf,
@ -520,14 +539,6 @@ tr_peerIoReadUint16( tr_peerIo * io,
*setme = ntohs( tmp );
}
void
tr_peerIoReadUint8( tr_peerIo * io,
struct evbuffer * inbuf,
uint8_t * setme )
{
tr_peerIoReadBytes( io, inbuf, setme, sizeof(uint8_t) );
}
void
tr_peerIoReadUint32( tr_peerIo * io,
struct evbuffer * inbuf,

View File

@ -68,7 +68,7 @@ enum
KEEPALIVE_INTERVAL_SECS = 90,
PEX_INTERVAL = (60 * 1000), /* msec between calls to sendPex() */
PEER_PULSE_INTERVAL = (100), /* msec between calls to pulse() */
PEER_PULSE_INTERVAL = (250), /* msec between calls to pulse() */
RATE_PULSE_INTERVAL = (333), /* msec between calls to ratePulse() */
/* Fast Peers Extension constants */
@ -888,7 +888,6 @@ readBtLength( tr_peermsgs * msgs, struct evbuffer * inbuf, size_t inlen )
msgs->incoming.length = len;
msgs->state = AWAITING_BT_ID;
}
dbgmsg( msgs, "readBtLength: got a length of %d, msgs->state is now %d", (int)len, (int)msgs->state );
return READ_AGAIN;
}
@ -1085,14 +1084,10 @@ readBtMessage( tr_peermsgs * msgs, struct evbuffer * inbuf, size_t inlen )
const uint8_t id = msgs->incoming.id;
const size_t startBufLen = EVBUFFER_LENGTH( inbuf );
dbgmsg( msgs, "in readBtMessage" );
--msglen; // id length
if( inlen < msglen ) {
dbgmsg( msgs, " too short!!! " );
if( inlen < msglen )
return READ_MORE;
}
dbgmsg( msgs, "got BT id %d, len %d, buffer size is %d", (int)id, (int)msglen, (int)inlen );
@ -1230,8 +1225,6 @@ dbgmsg( msgs, "in readBtMessage" );
break;
}
dbgmsg( msgs, "startBufLen was %d, msglen was %d, current inbuf len is %d", (int)startBufLen, (int)(msglen+1), (int)EVBUFFER_LENGTH(inbuf) );
assert( msglen + 1 == msgs->incoming.length );
assert( EVBUFFER_LENGTH(inbuf) == startBufLen - msglen );
@ -1298,7 +1291,8 @@ reassignBytesToCorrupt( tr_peermsgs * msgs, uint32_t byteCount )
static void
gotBadPiece( tr_peermsgs * msgs, uint32_t pieceIndex )
{
const uint32_t byteCount = tr_torPieceCountBytes( msgs->torrent, (int)pieceIndex );
const uint32_t byteCount =
tr_torPieceCountBytes( msgs->torrent, (int)pieceIndex );
reassignBytesToCorrupt( msgs, byteCount );
}
@ -1317,7 +1311,9 @@ addPeerToBlamefield( tr_peermsgs * msgs, uint32_t index )
}
static int
clientGotBlock( tr_peermsgs * msgs, const uint8_t * data, const struct peer_request * req )
clientGotBlock( tr_peermsgs * msgs,
const uint8_t * data,
const struct peer_request * req )
{
int i;
tr_torrent * tor = msgs->torrent;
@ -1408,18 +1404,18 @@ canRead( struct bufferevent * evin, void * vmsgs )
tr_peermsgs * msgs = (tr_peermsgs *) vmsgs;
struct evbuffer * inbuf = EVBUFFER_INPUT ( evin );
const size_t buflen = EVBUFFER_LENGTH( inbuf );
const size_t inlen = MIN( buflen, getDownloadMax( msgs ) );
const size_t n = MIN( buflen, getDownloadMax( msgs ) );
if( !inlen )
if( !n )
{
ret = READ_DONE;
}
else switch( msgs->state )
{
case AWAITING_BT_LENGTH: ret = readBtLength ( msgs, inbuf, inlen ); break;
case AWAITING_BT_ID: ret = readBtId ( msgs, inbuf, inlen ); break;
case AWAITING_BT_MESSAGE: ret = readBtMessage( msgs, inbuf, inlen ); break;
case AWAITING_BT_PIECE: ret = readBtPiece ( msgs, inbuf, inlen ); break;
case AWAITING_BT_LENGTH: ret = readBtLength ( msgs, inbuf, n ); break;
case AWAITING_BT_ID: ret = readBtId ( msgs, inbuf, n ); break;
case AWAITING_BT_MESSAGE: ret = readBtMessage( msgs, inbuf, n ); break;
case AWAITING_BT_PIECE: ret = readBtPiece ( msgs, inbuf, n ); break;
default: assert( 0 );
}
@ -1441,10 +1437,7 @@ static int
canWrite( const tr_peermsgs * msgs )
{
/* don't let our outbuffer get too large */
if( tr_peerIoWriteBytesWaiting( msgs->io ) > 4096 )
return FALSE;
return TRUE;
return tr_peerIoWriteBytesWaiting( msgs->io ) < 4096;
}
static size_t
@ -1556,9 +1549,14 @@ pulse( void * vmsgs )
static void
gotError( struct bufferevent * evbuf UNUSED, short what, void * vmsgs )
{
dbgmsg( vmsgs, "libevent got an error! what=%d, errno=%d (%s)",
(int)what, errno, strerror(errno) );
fireGotError( vmsgs );
if( what & EVBUFFER_TIMEOUT )
dbgmsg( vmsgs, "libevent got a timeout, what=%hd", what );
if( what & ( EVBUFFER_EOF | EVBUFFER_ERROR ) ) {
dbgmsg( vmsgs, "libevent got an error! what=%hd, errno=%d (%s)",
what, errno, strerror(errno) );
fireGotError( vmsgs );
}
}
static void