Changeset 8782
- Timestamp:
- Jul 6, 2009, 12:27:24 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/peer-mgr.c
r8781 r8782 2159 2159 2160 2160 static int 2161 compareCandidates( const void * va, 2162 const void * vb ) 2161 compareCandidates( const void * va, const void * vb ) 2163 2162 { 2164 2163 const struct peer_atom * a = *(const struct peer_atom**) va; … … 2414 2413 { 2415 2414 tr_peer * peer; 2416 int speed;2415 void * clientData; 2417 2416 time_t pieceDataTime; 2418 2417 time_t time; 2419 void * clientData; 2418 int speed; 2419 tr_bool doPurge; 2420 2420 }; 2421 2421 … … 2426 2426 const struct peer_liveliness * b = vb; 2427 2427 2428 if( a->doPurge != b->doPurge ) 2429 return a->doPurge ? 1 : -1; 2430 2428 2431 if( a->speed != b->speed ) /* faster goes first */ 2429 2432 return a->speed > b->speed ? -1 : 1; 2430 2433 2431 if( a->pieceDataTime != b->pieceDataTime ) /* the one to give us data more recently goes first */ 2434 /* the one to give us data more recently goes first */ 2435 if( a->pieceDataTime != b->pieceDataTime ) 2432 2436 return a->pieceDataTime > b->pieceDataTime ? -1 : 1; 2433 2437 2434 if( a->time != b->time ) /* the one we connected to most recently goes first */ 2438 /* the one we connected to most recently goes first */ 2439 if( a->time != b->time ) 2435 2440 return a->time > b->time ? -1 : 1; 2436 2441 … … 2440 2445 /* FIXME: getPeersToClose() should use this */ 2441 2446 static void 2442 sortPeers FromBestToWorst( tr_peer ** peers, void** clientData, int n, uint64_t now )2447 sortPeersByLiveliness( tr_peer ** peers, void** clientData, int n, uint64_t now ) 2443 2448 { 2444 2449 int i; 2445 struct peer_liveliness * 2450 struct peer_liveliness *lives, *l; 2446 2451 2447 2452 /* build a sortable array of peer + extra info */ 2448 l = tr_new0( struct peer_liveliness, n );2449 for( i=0; i<n; ++i ) {2453 lives = l = tr_new0( struct peer_liveliness, n ); 2454 for( i=0; i<n; ++i, ++l ) { 2450 2455 tr_peer * p = peers[i]; 2451 l[i].peer = p; 2452 l[i].speed = 1024.0 * (tr_peerGetPieceSpeed( p, now, TR_UP ) + tr_peerGetPieceSpeed( p, now, TR_DOWN )); 2453 l[i].pieceDataTime = p->atom->piece_data_time; 2454 l[i].time = p->atom->time; 2456 l->peer = p; 2457 l->doPurge = p->doPurge; 2458 l->pieceDataTime = p->atom->piece_data_time; 2459 l->time = p->atom->time; 2460 l->speed = 1024.0 * ( tr_peerGetPieceSpeed( p, now, TR_UP ) 2461 + tr_peerGetPieceSpeed( p, now, TR_DOWN ) ); 2455 2462 if( clientData ) 2456 l [i].clientData = clientData[i];2463 l->clientData = clientData[i]; 2457 2464 } 2458 2465 2459 2466 /* sort 'em */ 2460 qsort( l, n, sizeof(struct peer_liveliness), comparePeerLiveliness ); 2467 assert( n == ( l - lives ) ); 2468 qsort( lives, n, sizeof( struct peer_liveliness ), comparePeerLiveliness ); 2461 2469 2462 2470 /* build the peer array */ 2463 for( i=0 ; i<n; ++i) {2464 peers[i] = l [i].peer;2471 for( i=0, l=lives; i<n; ++i, ++l ) { 2472 peers[i] = l->peer; 2465 2473 if( clientData ) 2466 clientData[i] = l[i].clientData; 2467 } 2474 clientData[i] = l->clientData; 2475 } 2476 assert( n == ( l - lives ) ); 2468 2477 2469 2478 /* cleanup */ 2470 tr_free( l );2479 tr_free( lives ); 2471 2480 } 2472 2481 … … 2478 2487 if( n > max ) 2479 2488 { 2480 tr_peer ** peers = tr_memdup( tr_ptrArrayBase( &t->peers ), n*sizeof(tr_peer*) ); 2481 sortPeersFromBestToWorst( peers, NULL, n, now ); 2489 void * base = tr_ptrArrayBase( &t->peers ); 2490 tr_peer ** peers = tr_memdup( base, n*sizeof( tr_peer* ) ); 2491 sortPeersByLiveliness( peers, NULL, n, now ); 2482 2492 while( n > max ) 2483 2493 closePeer( t, peers[--n] ); … … 2489 2499 enforceSessionPeerLimit( tr_session * session, uint64_t now ) 2490 2500 { 2491 int n ;2492 tr_torrent * tor ;2501 int n = 0; 2502 tr_torrent * tor = NULL; 2493 2503 const int max = tr_sessionGetPeerLimit( session ); 2494 2504 2495 2505 /* count the total number of peers */ 2496 n = 0;2497 tor = NULL;2498 2506 while(( tor = tr_torrentNext( session, tor ))) 2499 2507 n += tr_ptrArraySize( &tor->torrentPeers->peers ); … … 2502 2510 if( n > max ) 2503 2511 { 2504 int n = 0;2505 2512 tr_peer ** peers = tr_new( tr_peer*, n ); 2506 2513 Torrent ** torrents = tr_new( Torrent*, n ); 2507 2514 2508 2515 /* populate the peer array */ 2516 n = 0; 2509 2517 tor = NULL; 2510 2518 while(( tor = tr_torrentNext( session, tor ))) { … … 2519 2527 2520 2528 /* sort 'em */ 2521 sortPeers FromBestToWorst( peers, (void**)torrents, n, now );2529 sortPeersByLiveliness( peers, (void**)torrents, n, now ); 2522 2530 2523 2531 /* cull out the crappiest */
Note: See TracChangeset
for help on using the changeset viewer.