Changeset 10749 for trunk/libtransmission/peer-mgr.c
- Timestamp:
- Jun 14, 2010, 3:42:54 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/peer-mgr.c
r10748 r10749 3273 3273 { 3274 3274 uint64_t score; 3275 int from;3276 uint8_t salt;3277 int priority;3278 int seedProbability;3279 int wasRecentlyStarted;3280 time_t lastConnectionAt;3281 time_t lastConnectionAttemptAt;3282 3275 tr_torrent * tor; 3283 3276 struct peer_atom * atom; 3284 3277 }; 3285 3278 3286 static int3287 compareSeedProbabilities( int a, int b )3288 {3289 /* 1. smaller numbers are better3290 2. prefer leechers to unknown3291 3. prefer unknown to seeds (FIXME: this is a simplistic test) */3292 if( a == 100 ) a = 101;3293 else if( a == -1 ) a = 100;3294 3295 if( b == 100 ) b = 101;3296 else if( b == -1 ) b = 100;3297 3298 if( a > b ) return 1;3299 if( a < b ) return -1;3300 return 0;3301 }3302 3303 3279 static tr_bool 3304 3280 torrentWasRecentlyStarted( const tr_torrent * tor ) … … 3307 3283 } 3308 3284 3309 static int loud = FALSE; 3310 3311 static inline uint64_t 3285 static uint64_t 3312 3286 addValToKey( uint64_t value, int width, uint64_t addme ) 3313 3287 { 3314 if(loud) fprintf( stderr, "old value %"PRIu64"... width %d, addme %d\n", value, width, (int)addme );3315 3288 value = (value << (uint64_t)width); 3316 if(loud) fprintf( stderr, "mid value %"PRIu64"\n", value );3317 3289 value |= addme; 3318 if(loud) fprintf( stderr, "new value %"PRIu64"\n", value );3319 3290 return value; 3320 3291 } … … 3365 3336 } 3366 3337 3367 static int3368 oldComparePeerCandidates( const void * va, const void * vb )3369 {3370 int i;3371 const struct peer_candidate * a = va;3372 const struct peer_candidate * b = vb;3373 3374 /* prefer peers we've connected to, or never tried, over peers we failed to connect to. */3375 const tr_bool a_failed = a->atom->lastConnectionAt < a->atom->lastConnectionAttemptAt;3376 const tr_bool b_failed = b->atom->lastConnectionAt < b->atom->lastConnectionAttemptAt;3377 if( a_failed != b_failed ) {3378 if(loud) fprintf( stderr, "old: a_failed %d\n", (int)a_failed );3379 if(loud) fprintf( stderr, "old: b_failed %d\n", (int)b_failed );3380 return a_failed ? 1 : -1;3381 }3382 3383 /* prefer the one we attempted least recently (to cycle through all peers) */3384 if( a->lastConnectionAttemptAt != b->lastConnectionAttemptAt ) {3385 if(loud) fprintf( stderr, "old: a->lastConnectionAttemptAt %u\n", (unsigned int)a->lastConnectionAttemptAt );3386 if(loud) fprintf( stderr, "old: b->lastConnectionAttemptAt %u\n", (unsigned int)b->lastConnectionAttemptAt );3387 return a->lastConnectionAttemptAt < b->lastConnectionAttemptAt ? -1 : 1;3388 }3389 3390 /* prefer peers belonging to a torrent of a higher priority */3391 if( a->priority != b->priority ) {3392 if(loud) fprintf( stderr, "old: a->priority %d\n", (int)a->priority );3393 if(loud) fprintf( stderr, "old: b->priority %d\n", (int)b->priority );3394 return a->priority > b->priority ? -1 : 1;3395 }3396 3397 /* prefer recently-started torrents */3398 if( a->wasRecentlyStarted != b->wasRecentlyStarted ) {3399 if(loud) fprintf( stderr, "old: a->wasRecentlyStarted %d\n", (int)a->wasRecentlyStarted );3400 if(loud) fprintf( stderr, "old: b->wasRecentlyStarted %d\n", (int)b->wasRecentlyStarted );3401 return a->wasRecentlyStarted ? -1 : 1;3402 }3403 3404 /* prefer peers that we might have a chance of uploading to */3405 if(( i = compareSeedProbabilities( a->seedProbability, b->seedProbability ))) {3406 if(loud) fprintf( stderr, "old: a->seedProbability %d\n", (int)a->seedProbability );3407 if(loud) fprintf( stderr, "old: b->seedProbability %d\n", (int)b->seedProbability );3408 return i;3409 }3410 3411 /* prefer peers that we got from more trusted sources */3412 if( a->from != b->from ) {3413 if(loud) fprintf( stderr, "old: a->from %d\n", (int)a->from );3414 if(loud) fprintf( stderr, "old: b->from %d\n", (int)b->from );3415 return a->from < b->from ? -1 : 1;3416 }3417 3418 /* salt */3419 if(loud) fprintf( stderr, "old: a->salt %d\n", (int)a->salt );3420 if(loud) fprintf( stderr, "old: b->salt %d\n", (int)b->salt );3421 if( a->salt != b->salt )3422 return a->salt < b->salt ? -1 : 1;3423 3424 return 0;3425 }3426 3427 3338 /* sort an array of peer candidates */ 3428 3339 static int 3429 3340 comparePeerCandidates( const void * va, const void * vb ) 3430 3341 { 3431 int ret;3432 int oldret;3433 3342 const struct peer_candidate * a = va; 3434 3343 const struct peer_candidate * b = vb; 3435 3344 3436 3345 if( a->score != b->score ) 3437 ret = a->score < b->score ? -1 : 1; 3438 else 3439 ret = 0; 3440 3441 oldret = oldComparePeerCandidates( va, vb ); 3442 3443 if( ret != oldret ) { 3444 loud = TRUE; 3445 oldret = oldComparePeerCandidates( va, vb ); 3446 fprintf( stderr, "ccc =========================\n" ); 3447 fprintf( stderr, "oldret %d\n", oldret ); 3448 fprintf( stderr, "ret %d\n", ret ); 3449 fprintf( stderr, "a score %"PRIu64"\n", a->score ); 3450 fprintf( stderr, "b score %"PRIu64"\n", b->score ); 3451 fprintf( stderr, "generating a score\n" ); 3452 getPeerCandidateScore( a->tor, a->atom, a->salt ); 3453 fprintf( stderr, "generating b bcore\n" ); 3454 getPeerCandidateScore( b->tor, b->atom, b->salt ); 3455 assert( 0 ); 3456 } 3457 3458 return ret; 3346 return a->score < b->score ? -1 : 1; 3347 3348 return 0; 3459 3349 } 3460 3350 … … 3514 3404 if( isPeerCandidate( tor, atom, now ) ) 3515 3405 { 3406 const uint8_t salt = tr_cryptoWeakRandInt( 1024 ); 3516 3407 walk->tor = tor; 3517 3408 walk->atom = atom; 3518 walk->from = atom->from; 3519 walk->salt = tr_cryptoWeakRandInt( 4096 ); 3520 walk->score = getPeerCandidateScore( tor, atom, walk->salt ); 3521 walk->priority = tr_torrentGetPriority( tor ); 3522 walk->seedProbability = atom->seedProbability; 3523 walk->wasRecentlyStarted = torrentWasRecentlyStarted( tor ); 3524 walk->lastConnectionAt = atom->lastConnectionAt; 3525 walk->lastConnectionAttemptAt = atom->lastConnectionAttemptAt; 3409 walk->score = getPeerCandidateScore( tor, atom, salt ); 3526 3410 ++walk; 3527 3411 }
Note: See TracChangeset
for help on using the changeset viewer.