on shutdown, close the most active torrents first. That way if we can't get everyting closed in a reasonable amount of time, at least we get the ones that affect stats first.

This commit is contained in:
Charles Kerr 2008-05-30 17:22:26 +00:00
parent f3db37ab1b
commit e391ef6e1a
3 changed files with 33 additions and 2 deletions

View File

@ -397,18 +397,40 @@ tr_sessionCountTorrents( const tr_handle * h )
return h->torrentCount;
}
/* close the biggest torrents first */
static int
compareTorrentByCur( const void * va, const void * vb )
{
const tr_torrent * a = *(const tr_torrent**)va;
const tr_torrent * b = *(const tr_torrent**)vb;
return -tr_compareUint64( a->downloadedCur + a->uploadedCur,
b->downloadedCur + b->uploadedCur );
}
static void
tr_closeAllConnections( void * vh )
{
tr_handle * h = vh;
tr_torrent * tor;
int i, n;
tr_torrent ** torrents;
tr_sharedShuttingDown( h->shared );
tr_trackerShuttingDown( h );
tr_rpcClose( &h->rpcServer );
while(( tor = tr_torrentNext( h, NULL )))
tr_torrentFree( tor );
/* close the torrents. get the most active ones first so that
* if we can't get them all closed in a reasonable amount of time,
* at least we get the most important ones first. */
tor = NULL;
n = h->torrentCount;
torrents = tr_new( tr_torrent*, h->torrentCount );
for( i=0; i<n; ++i )
torrents[i] = tr_torrentNext( h, tor );
qsort( torrents, n, sizeof(tr_torrent*), compareTorrentByCur );
for( i=0; i<n; ++i )
tr_torrentFree( torrents[i] );
tr_free( torrents );
tr_peerMgrFree( h->peerMgr );

View File

@ -354,6 +354,14 @@ tr_compareUint32( uint32_t a, uint32_t b )
return 0;
}
int
tr_compareUint64( uint64_t a, uint64_t b )
{
if( a < b ) return -1;
if( a > b ) return 1;
return 0;
}
/**
***
**/

View File

@ -217,6 +217,7 @@ void tr_set_compare( const void * a, size_t aCount,
int tr_compareUint16( uint16_t a, uint16_t b );
int tr_compareUint32( uint32_t a, uint32_t b );
int tr_compareUint64( uint64_t a, uint64_t b );
void tr_sha1_to_hex( char * out, const uint8_t * sha1 );