From: Charles Kerr Date: Tue, 27 Oct 2009 20:06:55 +0000 (+0000) Subject: (trunk libT) use part of hexum's patch from . The part added sorts peers by their overall speed when deciding which ones to disconnect from first, and which to request from first. --- diff --git a/libtransmission/peer-mgr.c b/libtransmission/peer-mgr.c index e3932de03..6ffd32599 100644 --- a/libtransmission/peer-mgr.c +++ b/libtransmission/peer-mgr.c @@ -798,6 +798,9 @@ refillUpkeep( void * vmgr ) return TRUE; } +static void +sortPeersByLiveliness( tr_peer ** peers, void ** clientData, int n, uint64_t now ); + static void refillPulse( int fd UNUSED, short type UNUSED, void * vtorrent ) { @@ -822,6 +825,7 @@ refillPulse( int fd UNUSED, short type UNUSED, void * vtorrent ) t->refillQueue = blockIteratorNew( t ); peers = getPeersUploadingToClient( t, &peerCount ); + sortPeersByLiveliness( peers, NULL, peerCount, tr_date( ) ); webseedCount = tr_ptrArraySize( &t->webseeds ); webseeds = tr_memdup( tr_ptrArrayBase( &t->webseeds ), webseedCount * sizeof( tr_webseed* ) ); @@ -2166,6 +2170,8 @@ shouldPeerBeClosed( const Torrent * t, return TR_CAN_KEEP; } +static void sortPeersByLivelinessReverse( tr_peer ** peers, void ** clientData, int n, uint64_t now ); + static tr_peer ** getPeersToClose( Torrent * t, tr_close_type_t closeType, int * setmeSize ) { @@ -2179,6 +2185,8 @@ getPeersToClose( Torrent * t, tr_close_type_t closeType, int * setmeSize ) if( shouldPeerBeClosed( t, peers[i], peerCount ) == closeType ) ret[outsize++] = peers[i]; + sortPeersByLivelinessReverse ( ret, NULL, outsize, tr_date( ) ); + *setmeSize = outsize; return ret; } @@ -2468,16 +2476,26 @@ comparePeerLiveliness( const void * va, const void * vb ) return 0; } -/* FIXME: getPeersToClose() should use this */ +static int +comparePeerLivelinessReverse( const void * va, const void * vb ) +{ + return -comparePeerLiveliness (va, vb); +} + static void -sortPeersByLiveliness( tr_peer ** peers, void** clientData, int n, uint64_t now ) +sortPeersByLivelinessImpl( tr_peer ** peers, + void ** clientData, + int n, + uint64_t now, + int (*compare) ( const void *va, const void *vb ) ) { int i; struct peer_liveliness *lives, *l; /* build a sortable array of peer + extra info */ lives = l = tr_new0( struct peer_liveliness, n ); - for( i=0; ipeer = p; l->doPurge = p->doPurge; @@ -2491,7 +2509,7 @@ sortPeersByLiveliness( tr_peer ** peers, void** clientData, int n, uint64_t now /* sort 'em */ assert( n == ( l - lives ) ); - qsort( lives, n, sizeof( struct peer_liveliness ), comparePeerLiveliness ); + qsort( lives, n, sizeof( struct peer_liveliness ), compare ); /* build the peer array */ for( i=0, l=lives; i