Changeset 3269
- Timestamp:
- Oct 2, 2007, 2:35:02 PM (15 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/peer-io.c
r3267 r3269 451 451 { 452 452 case PEER_ENCRYPTION_NONE: 453 /*fprintf( stderr, "writing %d plaintext bytes to outbuf...\n", byteCount );*/454 453 evbuffer_add( outbuf, bytes, byteCount ); 455 454 break; 456 455 457 456 case PEER_ENCRYPTION_RC4: 458 /*fprintf( stderr, "encrypting and writing %d bytes to outbuf...\n", byteCount );*/459 457 tmp = tr_new( uint8_t, byteCount ); 460 458 tr_cryptoEncrypt( io->crypto, byteCount, bytes, tmp ); … … 505 503 { 506 504 case PEER_ENCRYPTION_NONE: 507 /*fprintf( stderr, "reading %d plaintext bytes from inbuf...\n", byteCount );*/508 505 evbuffer_remove( inbuf, bytes, byteCount ); 509 506 break; 510 507 511 508 case PEER_ENCRYPTION_RC4: 512 /*fprintf( stderr, "reading AND DECRYPTING %d bytes from inbuf...\n", byteCount );*/513 509 evbuffer_remove( inbuf, bytes, byteCount ); 514 510 tr_cryptoDecrypt( io->crypto, byteCount, bytes, bytes ); -
trunk/libtransmission/peer-mgr.c
r3268 r3269 16 16 #include <stdio.h> /* printf */ 17 17 #include <limits.h> /* INT_MAX */ 18 19 #include <event.h> 18 20 19 21 #include "transmission.h" … … 52 54 MIN_CHOKE_PERIOD_SEC = 10, 53 55 54 /* how soon is `soon' in the rechokeSoon, reconnecSoon funcs */55 SOON_MSEC = 1000,56 57 56 /* following the BT spec, we consider ourselves `snubbed' if 58 57 * we're we don't get piece data from a peer in this long */ … … 101 100 tr_timer * reconnectTimer; 102 101 tr_timer * rechokeTimer; 103 tr_timer * rechokeSoonTimer;104 102 tr_timer * refillTimer; 105 103 tr_torrent * tor; … … 125 123 126 124 static void 125 myDebug( const char * file, int line, const Torrent * t, const char * fmt, ... ) 126 { 127 FILE * fp = tr_getLog( ); 128 if( fp != NULL ) 129 { 130 va_list args; 131 struct evbuffer * buf = evbuffer_new( ); 132 evbuffer_add_printf( buf, "[%s:%d] %s ", file, line, t->tor->info.name ); 133 va_start( args, fmt ); 134 evbuffer_add_vprintf( buf, fmt, args ); 135 va_end( args ); 136 fprintf( fp, "%s\n", EVBUFFER_DATA(buf) ); 137 evbuffer_free( buf ); 138 } 139 } 140 141 #define tordbg(t, fmt...) myDebug(__FILE__, __LINE__, t, ##fmt ) 142 143 /** 144 *** 145 **/ 146 147 static void 127 148 managerLock( struct tr_peerMgr * manager ) 128 149 { … … 351 372 } 352 373 353 static void 354 freeTorrent( tr_peerMgr * manager, Torrent * t ) 374 /* torrent must have already been removed from manager->torrents */ 375 static void 376 freeTorrent( Torrent * t ) 355 377 { 356 378 uint8_t hash[SHA_DIGEST_LENGTH]; … … 359 381 assert( t->peers != NULL ); 360 382 assert( torrentIsLocked( t ) ); 361 assert( getExistingTorrent( manager, t->hash ) != NULL );362 383 363 384 memcpy( hash, t->hash, SHA_DIGEST_LENGTH ); … … 365 386 tr_timerFree( &t->reconnectTimer ); 366 387 tr_timerFree( &t->rechokeTimer ); 367 tr_timerFree( &t->rechokeSoonTimer );368 388 tr_timerFree( &t->refillTimer ); 369 389 … … 371 391 tr_ptrArrayFree( t->pool, (PtrArrayForeachFunc)tr_free ); 372 392 tr_ptrArrayFree( t->peers, (PtrArrayForeachFunc)freePeer ); 373 tr_ptrArrayRemoveSorted( manager->torrents, t, torrentCompare );374 393 tr_free( t ); 375 376 assert( getExistingTorrent( manager, hash ) == NULL );377 394 } 378 395 … … 456 473 managerLock( manager ); 457 474 458 tr_ptrArrayFree( manager->handshakes, (PtrArrayForeachFunc)tr_handshakeAbort );475 /* free the torrents. */ 459 476 tr_ptrArrayFree( manager->torrents, (PtrArrayForeachFunc)freeTorrent ); 477 478 /* free the handshakes. Abort invokes handshakeDoneCB(), which removes 479 * the item from manager->handshakes, so this is a little roundabout... */ 480 while( !tr_ptrArrayEmpty( manager->handshakes ) ) 481 tr_handshakeAbort( tr_ptrArrayNth( manager->handshakes, 0 ) ); 482 tr_ptrArrayFree( manager->handshakes, NULL ); 460 483 461 484 managerUnlock( manager ); … … 490 513 { 491 514 tr_priority_t priority; 515 uint16_t random; 492 516 uint32_t piece; 493 517 uint32_t peerCount; 494 518 uint32_t fastAllowed; 495 uint8_t random;496 519 }; 497 520 … … 515 538 516 539 /* otherwise go with our random seed */ 517 return tr_compareUint 8( a->random, b->random );540 return tr_compareUint16( a->random, b->random ); 518 541 } 519 542 … … 567 590 setme->peerCount = 0; 568 591 setme->fastAllowed = 0; 569 setme->random = tr_rand( UINT 8_MAX );592 setme->random = tr_rand( UINT16_MAX ); 570 593 /* FIXME */ 571 594 // setme->fastAllowed = tr_bitfieldHas( t->tor->allowedList, i); … … 586 609 } 587 610 588 #if 0589 fprintf (stderr, "new pool: ");590 for (i=0; i<15 && i<(int)poolSize; ++i ) fprintf (stderr, "%d, ", (int)pool[i] );591 fprintf (stderr, "\n");592 #endif593 611 tr_free( peers ); 594 612 … … 610 628 611 629 pieces = getPreferredPieces( t, &pieceCount ); 612 /*fprintf( stderr, "REFILL refillPulse for {%s} got %d of %d pieces\n", tor->info.name, (int)pieceCount, t->tor->info.pieceCount );*/613 630 614 631 req = tr_new( uint64_t, pieceCount * tor->blockCountInPiece ); … … 631 648 } 632 649 633 /*fprintf( stderr, "REFILL refillPulse for {%s} reqCount is %d, unreqCount is %d\n", tor->info.name, (int)reqCount, (int)unreqCount );*/634 650 ret = walk = tr_new( uint64_t, unreqCount + reqCount ); 635 651 memcpy( walk, unreq, sizeof(uint64_t) * unreqCount ); … … 667 683 blocks = getPreferredBlocks( t, &blockCount ); 668 684 peers = getConnectedPeers( t, &peerCount ); 669 670 /*fprintf( stderr, "REFILL refillPulse for {%s} got %d blocks\n", tor->info.name, (int)blockCount );*/671 685 672 686 for( i=0; peerCount && i<blockCount; ++i ) … … 698 712 699 713 case TR_ADDREQ_OK: 700 /*fprintf( stderr, "REFILL peer %p took the request for block %d\n", peers[j]->msgs, block );*/701 714 tr_bitfieldAdd( t->requested, block ); 702 715 j = peerCount; … … 774 787 if( t->isRunning ) 775 788 t->rechokeTimer = tr_timerNew( t->manager->handle, rechokePulse, t, RECHOKE_PERIOD_MSEC ); 776 }777 778 static void779 rechokeNow( Torrent * t )780 {781 rechokePulse( t );782 restartChokeTimer( t );783 }784 785 static int786 rechokeSoonCB( void * vt )787 {788 Torrent * t = vt;789 rechokeNow( t );790 t->rechokeSoonTimer = NULL;791 return FALSE;792 }793 794 static void795 rechokeSoon( Torrent * t )796 {797 if( t->rechokeSoonTimer == NULL )798 t->rechokeSoonTimer = tr_timerNew( t->manager->handle,799 rechokeSoonCB, t, SOON_MSEC );800 789 } 801 790 … … 859 848 a->from = from; 860 849 a->time = 0; 861 fprintf( stderr, "torrent [%s] getting a new atom: %s\n", t->tor->info.name, tr_peerIoAddrStr(&a->addr,a->port) );850 tordbg( t, "got a new atom: %s", tr_peerIoAddrStr(&a->addr,a->port) ); 862 851 tr_ptrArrayInsertSorted( t->pool, a, comparePeerAtoms ); 863 852 } … … 934 923 peer->client = peer_id ? tr_clientForId( peer_id ) : NULL; 935 924 peer->msgsTag = tr_peerMsgsSubscribe( peer->msgs, msgsCallbackFunc, t ); 936 rechokeSoon( t );937 925 } 938 926 } … … 1179 1167 assert( t != NULL ); 1180 1168 stopTorrent( t ); 1181 freeTorrent( manager, t ); 1169 tr_ptrArrayRemoveSorted( manager->torrents, t, torrentCompare ); 1170 freeTorrent( t ); 1182 1171 1183 1172 managerUnlock( manager ); … … 1563 1552 /* we don't need two connections to the same peer... */ 1564 1553 if( peerIsInUse( t, &atom->addr ) ) { 1565 fprintf( stderr, "RECONNECT peer %d (%s) is in use...\n", i, tr_peerIoAddrStr(&atom->addr,atom->port) );1554 tordbg( t, "RECONNECT peer %d (%s) is in use...\n", i, tr_peerIoAddrStr(&atom->addr,atom->port) ); 1566 1555 continue; 1567 1556 } … … 1569 1558 /* no need to connect if we're both seeds... */ 1570 1559 if( seed && (atom->flags & ADDED_F_SEED_FLAG) ) { 1571 fprintf( stderr, "RECONNECT peer %d (%s) is a seed and so are we...\n", i, tr_peerIoAddrStr(&atom->addr,atom->port) );1560 tordbg( t, "RECONNECT peer %d (%s) is a seed and so are we...\n", i, tr_peerIoAddrStr(&atom->addr,atom->port) ); 1572 1561 continue; 1573 1562 } … … 1575 1564 /* if we used this peer recently, give someone else a turn */ 1576 1565 if( ( now - atom->time ) < LAISSEZ_FAIRE_PERIOD_SECS ) { 1577 fprintf( stderr, "RECONNECT peer %d (%s) is in its grace period...\n", i, tr_peerIoAddrStr(&atom->addr,atom->port) );1566 tordbg( t, "RECONNECT peer %d (%s) is in its grace period...\n", i, tr_peerIoAddrStr(&atom->addr,atom->port) ); 1578 1567 continue; 1579 1568 } … … 1605 1594 const int peerCount = tr_ptrArraySize( t->peers ); 1606 1595 1607 fprintf( stderr, "RECONNECT pulse for [%s]: %d weak connections, %d connection candidates, %d atoms, max per pulse is %d\n",1596 tordbg( t, "RECONNECT pulse for [%s]: %d weak connections, %d connection candidates, %d atoms, max per pulse is %d\n", 1608 1597 t->tor->info.name, nConnections, nCandidates, tr_ptrArraySize(t->pool), (int)MAX_RECONNECTIONS_PER_PULSE ); 1609 1598 1610 1599 for( i=0; i<nConnections; ++i ) 1611 fprintf( stderr, "connection #%d: %s @ %.2f\n", i+1,1600 tordbg( t, "connection #%d: %s @ %.2f\n", i+1, 1612 1601 tr_peerIoAddrStr( &connections[i].peer->in_addr, connections[i].peer->port ), connections[i].throughput ); 1613 1602 … … 1616 1605 const double throughput = connections[i].throughput; 1617 1606 tr_peer * peer = connections[i].peer; 1618 fprintf( stderr, "RECONNECT culling peer %s, whose throughput was %f\n",1619 1607 tordbg( t, "RECONNECT culling peer %s, whose throughput was %f\n", 1608 tr_peerIoAddrStr(&peer->in_addr, peer->port), throughput ); 1620 1609 removePeer( t, peer ); 1621 1610 } … … 1626 1615 struct peer_atom * atom = candidates[i]; 1627 1616 tr_peerIo * io = tr_peerIoNewOutgoing( t->manager->handle, &atom->addr, atom->port, t->hash ); 1628 fprintf( stderr, "RECONNECT adding an outgoing connection...\n" );1617 tordbg( t, "RECONNECT adding an outgoing connection...\n" ); 1629 1618 initiateHandshake( t->manager, io ); 1630 1619 atom->time = time( NULL ); -
trunk/libtransmission/peer-msgs.c
r3265 r3269 320 320 tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) ); 321 321 tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, choke ? BT_CHOKE : BT_UNCHOKE ); 322 msgs->info->chokeChangedAt = time( NULL ); 322 323 } 323 324 } … … 564 565 tr_peerIoWriteBuf( msgs->io, outbuf ); 565 566 567 #if 0 566 568 dbgmsg( msgs, "here is the ltep handshake we sent:" ); 567 569 tr_bencPrint( &val ); 570 #endif 568 571 569 572 /* cleanup */ … … 588 591 } 589 592 593 #if 0 590 594 dbgmsg( msgs, "here is the ltep handshake we read:" ); 591 595 tr_bencPrint( &val ); 596 #endif 592 597 593 598 /* does the peer prefer encrypted connections? */ … … 788 793 break; 789 794 790 case BT_BITFIELD: 795 case BT_BITFIELD: { 796 const int clientIsSeed = tr_cpGetStatus( msgs->torrent->completion ) != TR_CP_INCOMPLETE; 791 797 dbgmsg( msgs, "w00t peer sent us a BT_BITFIELD" ); 792 798 assert( msglen == msgs->info->have->len ); 793 799 tr_peerIoReadBytes( msgs->io, inbuf, msgs->info->have->bits, msglen ); 794 800 msgs->info->progress = tr_bitfieldCountTrueBits( msgs->info->have ) / (float)msgs->torrent->info.pieceCount; 801 if( clientIsSeed && ( msgs->info->progress < 1.0 ) ) 802 tr_peerMsgsSetChoke( msgs, FALSE ); 795 803 dbgmsg( msgs, "after the HAVE message, peer progress is %f", msgs->info->progress ); 796 804 updateInterest( msgs ); … … 798 806 firePeerProgress( msgs ); 799 807 break; 808 } 800 809 801 810 case BT_REQUEST: { -
trunk/libtransmission/ptrarray.c
r3242 r3269 38 38 39 39 return p; 40 } 41 42 tr_ptrArray* 43 tr_ptrArrayDup( tr_ptrArray* in ) 44 { 45 tr_ptrArray * out; 46 47 out = tr_new( tr_ptrArray, 1 ); 48 out->n_items = in->n_items; 49 out->n_alloc = in->n_items; 50 out->items = tr_new( void*, out->n_alloc ); 51 memcpy( out->items, in->items, out->n_items * sizeof(void*) ); 52 53 return out; 40 54 } 41 55 -
trunk/libtransmission/ptrarray.h
r3242 r3269 22 22 23 23 tr_ptrArray * tr_ptrArrayNew ( void ); 24 tr_ptrArray * tr_ptrArrayDup ( tr_ptrArray* ); 24 25 void tr_ptrArrayForeach ( tr_ptrArray*, PtrArrayForeachFunc func ); 25 26 void tr_ptrArrayFree ( tr_ptrArray*, PtrArrayForeachFunc func );
Note: See TracChangeset
for help on using the changeset viewer.