From 69f59b423ac18cfe33c83e63a1be498c629686be Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 19 Jan 2009 13:55:41 +0000 Subject: [PATCH] (trunk libT) possible fix for the `few peers' errors reported on OS X. denis_, merlyn3d, give this a spin... --- libtransmission/peer-io.c | 16 ++++++++++++---- libtransmission/peer-io.h | 5 +++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/libtransmission/peer-io.c b/libtransmission/peer-io.c index 1d1d6cc95..ad694de35 100644 --- a/libtransmission/peer-io.c +++ b/libtransmission/peer-io.c @@ -190,6 +190,8 @@ event_read_cb( int fd, short event UNUSED, void * vio ) assert( tr_isPeerIo( io ) ); + io->hasFinishedConnecting = TRUE; + curlen = EVBUFFER_LENGTH( io->inbuf ); howmuch = curlen >= max ? 0 : max - curlen; howmuch = tr_bandwidthClamp( &io->bandwidth, TR_DOWN, howmuch ); @@ -270,6 +272,8 @@ event_write_cb( int fd, short event UNUSED, void * vio ) assert( tr_isPeerIo( io ) ); + io->hasFinishedConnecting = TRUE; + dbgmsg( io, "libevent says this peer is ready to write" ); /* Write as much as possible, since the socket is non-blocking, write() will @@ -352,6 +356,7 @@ tr_peerIoNew( tr_session * session, io->port = port; io->socket = socket; io->isIncoming = isIncoming != 0; + io->hasFinishedConnecting = FALSE; io->timeCreated = time( NULL ); io->inbuf = evbuffer_new( ); io->outbuf = evbuffer_new( ); @@ -821,10 +826,13 @@ tr_peerIoFlush( tr_peerIo * io, tr_direction dir, size_t limit ) assert( tr_isPeerIo( io ) ); assert( tr_isDirection( dir ) ); - if( dir == TR_DOWN ) - bytesUsed = tr_peerIoTryRead( io, limit ); - else - bytesUsed = tr_peerIoTryWrite( io, limit ); + if( io->hasFinishedConnecting ) + { + if( dir == TR_DOWN ) + bytesUsed = tr_peerIoTryRead( io, limit ); + else + bytesUsed = tr_peerIoTryWrite( io, limit ); + } dbgmsg( io, "flushing peer-io, direction %d, limit %zu, bytesUsed %zd", (int)dir, limit, bytesUsed ); return bytesUsed; diff --git a/libtransmission/peer-io.h b/libtransmission/peer-io.h index 1441e40e6..eefeae47d 100644 --- a/libtransmission/peer-io.h +++ b/libtransmission/peer-io.h @@ -64,6 +64,11 @@ typedef struct tr_peerIo tr_bool extendedProtocolSupported; tr_bool fastExtensionSupported; + /* we create the socket in a nonblocking way, so this flag is initially + * false and then set to true when libevent says that the socket is ready + * for reading or writing */ + tr_bool hasFinishedConnecting; + int magicNumber; uint8_t encryptionMode;