Changeset 3278
- Timestamp:
- Oct 3, 2007, 4:42:43 PM (15 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/peer-mgr.c
r3277 r3278 46 46 47 47 /* how frequently to refill peers' request lists */ 48 REFILL_PERIOD_MSEC = 1000,48 REFILL_PERIOD_MSEC = 2000, 49 49 50 50 /* how many peers to unchoke per-torrent. */ … … 233 233 torrentCompareToHash( const void * va, const void * vb ) 234 234 { 235 const Torrent * a = (const Torrent*)va;236 const uint8_t * b_hash = (const uint8_t*)vb;235 const Torrent * a = va; 236 const uint8_t * b_hash = vb; 237 237 return memcmp( a->hash, b_hash, SHA_DIGEST_LENGTH ); 238 238 } … … 249 249 peerCompare( const void * va, const void * vb ) 250 250 { 251 const tr_peer * a = (const tr_peer *)va;252 const tr_peer * b = (const tr_peer *)vb;251 const tr_peer * a = va; 252 const tr_peer * b = vb; 253 253 return compareAddresses( &a->in_addr, &b->in_addr ); 254 254 } … … 257 257 peerCompareToAddr( const void * va, const void * vb ) 258 258 { 259 const tr_peer * a = (const tr_peer *)va;259 const tr_peer * a = va; 260 260 return compareAddresses( &a->in_addr, vb ); 261 261 } … … 292 292 assert( torrentIsLocked ( t ) ); 293 293 294 return ( getExistingPeer( t, addr ) != NULL ) 295 || ( getExistingHandshake( t->outgoingHandshakes, addr ) != NULL ) 296 || ( getExistingHandshake( t->manager->incomingHandshakes, addr ) != NULL ); 294 return getExistingPeer( t, addr ) 295 || getExistingHandshake( t->outgoingHandshakes, addr ) 296 || getExistingHandshake( t->manager->incomingHandshakes, addr ); 297 } 298 299 static tr_peer* 300 peerConstructor( const struct in_addr * in_addr ) 301 { 302 tr_peer * p; 303 p = tr_new0( tr_peer, 1 ); 304 p->rateToClient = tr_rcInit( ); 305 p->rateToPeer = tr_rcInit( ); 306 memcpy( &p->in_addr, in_addr, sizeof(struct in_addr) ); 307 return p; 297 308 } 298 309 … … 306 317 peer = getExistingPeer( torrent, in_addr ); 307 318 308 if( peer == NULL ) 309 { 310 peer = tr_new0( tr_peer, 1 ); 311 peer->rateToClient = tr_rcInit( ); 312 peer->rateToPeer = tr_rcInit( ); 313 memcpy( &peer->in_addr, in_addr, sizeof(struct in_addr) ); 319 if( peer == NULL ) { 320 peer = peerConstructor( in_addr ); 314 321 tr_ptrArrayInsertSorted( torrent->peers, peer, peerCompare ); 315 322 } … … 344 351 345 352 static void 346 freePeer( tr_peer * peer )353 peerDestructor( tr_peer * peer ) 347 354 { 348 355 disconnectPeer( peer ); … … 367 374 removed = tr_ptrArrayRemoveSorted ( t->peers, peer, peerCompare ); 368 375 assert( removed == peer ); 369 freePeer( removed );376 peerDestructor( removed ); 370 377 } 371 378 … … 377 384 } 378 385 379 /* torrent must have already been removed from manager->torrents */380 386 static void 381 387 torrentDestructor( Torrent * t ) … … 574 580 return 0; 575 581 576 if( tr_cpPieceIsComplete( tor->completion, piece ) ) /* we alreadyhave it */582 if( tr_cpPieceIsComplete( tor->completion, piece ) ) /* we have it */ 577 583 return 0; 578 584 … … 1112 1118 1113 1119 /* disconnect the peers. */ 1114 tr_ptrArrayForeach( t->peers, (PtrArrayForeachFunc) freePeer );1120 tr_ptrArrayForeach( t->peers, (PtrArrayForeachFunc)peerDestructor ); 1115 1121 tr_ptrArrayClear( t->peers ); 1116 1122 … … 1321 1327 **/ 1322 1328 1323 typedef struct 1329 struct ChokeData 1324 1330 { 1325 1331 tr_peer * peer; … … 1328 1334 int preferred; 1329 1335 int doUnchoke; 1330 } 1331 ChokeData; 1336 }; 1332 1337 1333 1338 static int 1334 1339 compareChoke( const void * va, const void * vb ) 1335 1340 { 1336 const ChokeData * a = va;1337 const ChokeData * b = vb;1341 const struct ChokeData * a = va; 1342 const struct ChokeData * b = vb; 1338 1343 1339 1344 if( a->preferred != b->preferred ) … … 1365 1370 1366 1371 static void 1367 rechoke Leech( Torrent * t )1372 rechoke( Torrent * t ) 1368 1373 { 1369 1374 int i, peerCount, size=0, unchoked=0; 1370 1375 const time_t ignorePeersNewerThan = time(NULL) - MIN_CHOKE_PERIOD_SEC; 1371 1376 tr_peer ** peers = getConnectedPeers( t, &peerCount ); 1372 ChokeData * choke = tr_new0(ChokeData, peerCount );1377 struct ChokeData * choke = tr_new0( struct ChokeData, peerCount ); 1373 1378 1374 1379 assert( torrentIsLocked( t ) ); … … 1378 1383 { 1379 1384 tr_peer * peer = peers[i]; 1380 ChokeData * node;1385 struct ChokeData * node; 1381 1386 if( peer->chokeChangedAt > ignorePeersNewerThan ) 1382 1387 continue; … … 1390 1395 } 1391 1396 1392 qsort( choke, size, sizeof( ChokeData), compareChoke );1397 qsort( choke, size, sizeof(struct ChokeData), compareChoke ); 1393 1398 1394 1399 for( i=0; i<size && i<NUM_UNCHOKED_PEERS_PER_TORRENT; ++i ) { … … 1412 1417 } 1413 1418 1414 static void1415 rechokeSeed( Torrent * t )1416 {1417 int i, size;1418 tr_peer ** peers;1419 1420 assert( torrentIsLocked( t ) );1421 1422 peers = getConnectedPeers( t, &size );1423 1424 /* FIXME */1425 for( i=0; i<size; ++i )1426 tr_peerMsgsSetChoke( peers[i]->msgs, FALSE );1427 1428 tr_free( peers );1429 }1430 1431 1419 static int 1432 1420 rechokePulse( void * vtorrent ) … … 1434 1422 Torrent * t = vtorrent; 1435 1423 torrentLock( t ); 1436 1437 const int done = tr_cpGetStatus( t->tor->completion ) != TR_CP_INCOMPLETE; 1438 if( done ) 1439 rechokeLeech( vtorrent ); 1440 else 1441 rechokeSeed( vtorrent ); 1442 1424 rechoke( t ); 1443 1425 torrentUnlock( t ); 1444 1426 return TRUE; … … 1490 1472 isWeak = FALSE; 1491 1473 else 1492 isWeak = now - peer->pieceDataActivityDate> 180;1474 isWeak = ( now - peer->pieceDataActivityDate ) > 180; 1493 1475 1494 1476 if( isWeak ) -
trunk/libtransmission/peer-msgs.c
r3277 r3278 146 146 147 147 static void 148 myDebug( const char * file, int line, const struct tr_peermsgs * msgs, const char * fmt, ... ) 148 myDebug( const char * file, int line, 149 const struct tr_peermsgs * msgs, 150 const char * fmt, ... ) 149 151 { 150 152 FILE * fp = tr_getLog( ); … … 233 235 if( torrent->info.pieces[piece].dnd ) /* we don't want it */ 234 236 return FALSE; 235 if( tr_cpPieceIsComplete( torrent->completion, piece ) ) /* we alreadyhave it */237 if( tr_cpPieceIsComplete( torrent->completion, piece ) ) /* we have it */ 236 238 return FALSE; 237 239 if( !tr_bitfieldHas( peer->info->have, piece ) ) /* peer doesn't have it */ 238 240 return FALSE; 239 if( tr_bitfieldHas( peer->info->banned, piece ) ) /* peer is banned for it*/241 if( tr_bitfieldHas( peer->info->banned, piece ) ) /* peer is banned */ 240 242 return FALSE; 241 243 return TRUE; 242 244 } 243 245 244 /* "interested" means we'll ask for piece data from the peerif they unchoke us */246 /* "interested" means we'll ask for piece data if they unchoke us */ 245 247 static int 246 248 isPeerInteresting( const tr_peermsgs * msgs ) … … 249 251 const tr_torrent * torrent; 250 252 const tr_bitfield * bitfield; 251 const int clientIsSeed = tr_cpGetStatus( msgs->torrent->completion ) != TR_CP_INCOMPLETE; 253 const int clientIsSeed = 254 tr_cpGetStatus( msgs->torrent->completion ) != TR_CP_INCOMPLETE; 252 255 253 256 if( clientIsSeed ) … … 257 260 bitfield = tr_cpPieceBitfield( torrent->completion ); 258 261 259 if( !msgs->info->have ) /* We don't know what this peer has... what should this be? */262 if( !msgs->info->have ) 260 263 return TRUE; 261 264 … … 275 278 276 279 msgs->info->clientIsInterested = weAreInterested; 277 dbgmsg( msgs, ": sending an %s message", (weAreInterested ? "INTERESTED" : "NOT_INTERESTED") ); 280 dbgmsg( msgs, ": sending an %s message", 281 weAreInterested ? "INTERESTED" : "NOT_INTERESTED"); 278 282 279 283 tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) ); 280 tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, weAreInterested ? BT_INTERESTED : BT_NOT_INTERESTED ); 284 tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, 285 weAreInterested ? BT_INTERESTED : BT_NOT_INTERESTED ); 281 286 } 282 287 … … 307 312 { 308 313 tr_list * next = walk->next; 309 /* We shouldn't reject a peer's fast allowed requests at choke */314 /* don't reject a peer's fast allowed requests at choke */ 310 315 struct peer_request *req = walk->data; 311 316 if ( !tr_bitfieldHas( msgs->peerAllowedPieces, req->index ) ) … … 319 324 dbgmsg( msgs, "sending a %s message", (choke ? "CHOKE" : "UNCHOKE") ); 320 325 tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) ); 321 tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, choke ? BT_CHOKE : BT_UNCHOKE ); 326 tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, 327 choke ? BT_CHOKE : BT_UNCHOKE ); 322 328 msgs->info->chokeChangedAt = time( NULL ); 323 329 } … … 367 373 dbgmsg( msgs, "w00t telling them we HAVE piece #%d", pieceIndex ); 368 374 369 tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) + sizeof(uint32_t) ); 375 tr_peerIoWriteUint32( msgs->io, msgs->outMessages, 376 sizeof(uint8_t) + sizeof(uint32_t) ); 370 377 tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, BT_HAVE ); 371 378 tr_peerIoWriteUint32( msgs->io, msgs->outMessages, pieceIndex ); … … 614 621 } 615 622 616 #if 0617 /* get peer's client name */618 sub = tr_bencDictFind( &val, "v" );619 if( tr_bencIsStr( sub ) ) {620 int i;621 tr_free( msgs->info->client );622 fprintf( stderr, "dictionary says client is [%s]\n", sub->val.s.s );623 msgs->info->client = tr_strndup( sub->val.s.s, sub->val.s.i );624 for( i=0; i<sub->val.s.i; ++i ) { fprintf( stderr, "[%c] (%d)\n", sub->val.s.s[i], (int)sub->val.s.s[i] );625 if( (int)msgs->info->client[i]==-75 ) msgs->info->client[i]='u'; }626 fprintf( stderr, "msgs->client is now [%s]\n", msgs->info->client );627 }628 #endif629 630 623 /* get peer's listening port */ 631 624 sub = tr_bencDictFind( &val, "p" ); … … 722 715 723 716 return READ_AGAIN; 717 } 718 719 static void 720 updatePeerProgress( tr_peermsgs * msgs ) 721 { 722 msgs->info->progress = tr_bitfieldCountTrueBits( msgs->info->have ) / (float)msgs->torrent->info.pieceCount; 723 dbgmsg( msgs, "peer progress is %f", msgs->info->progress ); 724 updateInterest( msgs ); 725 firePeerProgress( msgs ); 724 726 } 725 727 … … 787 789 tr_peerIoReadUint32( msgs->io, inbuf, &ui32 ); 788 790 tr_bitfieldAdd( msgs->info->have, ui32 ); 789 msgs->info->progress = tr_bitfieldCountTrueBits( msgs->info->have ) / (float)msgs->torrent->info.pieceCount; 790 dbgmsg( msgs, "after the HAVE message, peer progress is %f", msgs->info->progress ); 791 updateInterest( msgs ); 792 firePeerProgress( msgs ); 791 updatePeerProgress( msgs ); 793 792 break; 794 793 … … 798 797 assert( msglen == msgs->info->have->len ); 799 798 tr_peerIoReadBytes( msgs->io, inbuf, msgs->info->have->bits, msglen ); 800 msgs->info->progress = tr_bitfieldCountTrueBits( msgs->info->have ) / (float)msgs->torrent->info.pieceCount;799 updatePeerProgress( msgs ); 801 800 tr_peerMsgsSetChoke( msgs, !clientIsSeed || (msgs->info->progress<1.0) ); 802 dbgmsg( msgs, "after the HAVE message, peer progress is %f", msgs->info->progress );803 updateInterest( msgs );804 801 fireNeedReq( msgs ); 805 firePeerProgress( msgs );806 802 break; 807 803 } … … 906 902 dbgmsg( msgs, "peer sent us a BT_HAVE_ALL" ); 907 903 memset( msgs->info->have->bits, 1, msgs->info->have->len ); 908 msgs->info->progress = tr_bitfieldCountTrueBits( msgs->info->have ) / (float)msgs->torrent->info.pieceCount; 909 dbgmsg( msgs, "after the HAVE_ALL message, peer progress is %f", msgs->info->progress ); 910 updateInterest( msgs ); 911 firePeerProgress( msgs ); 904 updatePeerProgress( msgs ); 912 905 break; 913 906 } … … 917 910 dbgmsg( msgs, "peer sent us a BT_HAVE_NONE" ); 918 911 memset( msgs->info->have->bits, 1, msgs->info->have->len ); 919 msgs->info->progress = tr_bitfieldCountTrueBits( msgs->info->have ) / (float)msgs->torrent->info.pieceCount; 920 dbgmsg( msgs, "after the HAVE_NONE message, peer progress is %f", msgs->info->progress ); 921 updateInterest( msgs ); 922 firePeerProgress( msgs ); 912 updatePeerProgress( msgs ); 923 913 break; 924 914 } … … 1094 1084 **/ 1095 1085 1096 if( tr_ioWrite( tor, index, offset, length, EVBUFFER_DATA( inbuf ))) {1086 if( tr_ioWrite( tor, index, offset, length, EVBUFFER_DATA( inbuf ))) 1097 1087 return; 1098 }1099 1088 1100 1089 tr_cpBlockAdd( tor->completion, block ); … … 1299 1288 { 1300 1289 const tr_bitfield * bitfield = tr_cpPieceBitfield( msgs->torrent->completion ); 1290 struct evbuffer * out = msgs->outMessages; 1301 1291 1302 1292 dbgmsg( msgs, "sending peer a bitfield message" ); 1303 tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) + bitfield->len );1304 tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, BT_BITFIELD );1305 tr_peerIoWriteBytes ( msgs->io, msgs->outMessages, bitfield->bits, bitfield->len );1293 tr_peerIoWriteUint32( msgs->io, out, sizeof(uint8_t) + bitfield->len ); 1294 tr_peerIoWriteUint8 ( msgs->io, out, BT_BITFIELD ); 1295 tr_peerIoWriteBytes ( msgs->io, out, bitfield->bits, bitfield->len ); 1306 1296 } 1307 1297 -
trunk/libtransmission/peer-msgs.h
r3277 r3278 11 11 */ 12 12 13 #ifndef TR_P _H14 #define TR_P _H13 #ifndef TR_PEER_MSGS_H 14 #define TR_PEER_MSGS_H 15 15 16 16 #include <inttypes.h>
Note: See TracChangeset
for help on using the changeset viewer.