Changeset 3269


Ignore:
Timestamp:
Oct 2, 2007, 2:35:02 PM (15 years ago)
Author:
charles
Message:
  • simplify the the choking intervals
  • fix peer-mgr destructor bug
  • send debugging messagese to TR_DEBUG_FD instead of the console
Location:
trunk/libtransmission
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/peer-io.c

    r3267 r3269  
    451451    {
    452452        case PEER_ENCRYPTION_NONE:
    453             /*fprintf( stderr, "writing %d plaintext bytes to outbuf...\n", byteCount );*/
    454453            evbuffer_add( outbuf, bytes, byteCount );
    455454            break;
    456455
    457456        case PEER_ENCRYPTION_RC4:
    458             /*fprintf( stderr, "encrypting and writing %d bytes to outbuf...\n", byteCount );*/
    459457            tmp = tr_new( uint8_t, byteCount );
    460458            tr_cryptoEncrypt( io->crypto, byteCount, bytes, tmp );
     
    505503    {
    506504        case PEER_ENCRYPTION_NONE:
    507             /*fprintf( stderr, "reading %d plaintext bytes from inbuf...\n", byteCount );*/
    508505            evbuffer_remove(  inbuf, bytes, byteCount );
    509506            break;
    510507
    511508        case PEER_ENCRYPTION_RC4:
    512             /*fprintf( stderr, "reading AND DECRYPTING %d bytes from inbuf...\n", byteCount );*/
    513509            evbuffer_remove(  inbuf, bytes, byteCount );
    514510            tr_cryptoDecrypt( io->crypto, byteCount, bytes, bytes );
  • trunk/libtransmission/peer-mgr.c

    r3268 r3269  
    1616#include <stdio.h> /* printf */
    1717#include <limits.h> /* INT_MAX */
     18
     19#include <event.h>
    1820
    1921#include "transmission.h"
     
    5254    MIN_CHOKE_PERIOD_SEC = 10,
    5355
    54     /* how soon is `soon' in the rechokeSoon, reconnecSoon funcs */
    55     SOON_MSEC = 1000,
    56 
    5756    /* following the BT spec, we consider ourselves `snubbed' if
    5857     * we're we don't get piece data from a peer in this long */
     
    101100    tr_timer * reconnectTimer;
    102101    tr_timer * rechokeTimer;
    103     tr_timer * rechokeSoonTimer;
    104102    tr_timer * refillTimer;
    105103    tr_torrent * tor;
     
    125123
    126124static void
     125myDebug( 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
     147static void
    127148managerLock( struct tr_peerMgr * manager )
    128149{
     
    351372}
    352373
    353 static void
    354 freeTorrent( tr_peerMgr * manager, Torrent * t )
     374/* torrent must have already been removed from manager->torrents */
     375static void
     376freeTorrent( Torrent * t )
    355377{
    356378    uint8_t hash[SHA_DIGEST_LENGTH];
     
    359381    assert( t->peers != NULL );
    360382    assert( torrentIsLocked( t ) );
    361     assert( getExistingTorrent( manager, t->hash ) != NULL );
    362383
    363384    memcpy( hash, t->hash, SHA_DIGEST_LENGTH );
     
    365386    tr_timerFree( &t->reconnectTimer );
    366387    tr_timerFree( &t->rechokeTimer );
    367     tr_timerFree( &t->rechokeSoonTimer );
    368388    tr_timerFree( &t->refillTimer );
    369389
     
    371391    tr_ptrArrayFree( t->pool, (PtrArrayForeachFunc)tr_free );
    372392    tr_ptrArrayFree( t->peers, (PtrArrayForeachFunc)freePeer );
    373     tr_ptrArrayRemoveSorted( manager->torrents, t, torrentCompare );
    374393    tr_free( t );
    375 
    376     assert( getExistingTorrent( manager, hash ) == NULL );
    377394}
    378395
     
    456473    managerLock( manager );
    457474
    458     tr_ptrArrayFree( manager->handshakes, (PtrArrayForeachFunc)tr_handshakeAbort );
     475    /* free the torrents. */
    459476    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 );
    460483
    461484    managerUnlock( manager );
     
    490513{
    491514    tr_priority_t priority;
     515    uint16_t random;
    492516    uint32_t piece;
    493517    uint32_t peerCount;
    494518    uint32_t fastAllowed;
    495     uint8_t random;
    496519};
    497520
     
    515538
    516539    /* otherwise go with our random seed */
    517     return tr_compareUint8( a->random, b->random );
     540    return tr_compareUint16( a->random, b->random );
    518541}
    519542
     
    567590            setme->peerCount = 0;
    568591            setme->fastAllowed = 0;
    569             setme->random = tr_rand( UINT8_MAX );
     592            setme->random = tr_rand( UINT16_MAX );
    570593            /* FIXME */
    571594//            setme->fastAllowed = tr_bitfieldHas( t->tor->allowedList, i);
     
    586609    }
    587610
    588 #if 0
    589 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 #endif
    593611    tr_free( peers );
    594612
     
    610628
    611629    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 );*/
    613630
    614631    req = tr_new( uint64_t, pieceCount *  tor->blockCountInPiece );
     
    631648    }
    632649
    633 /*fprintf( stderr, "REFILL refillPulse for {%s} reqCount is %d, unreqCount is %d\n", tor->info.name, (int)reqCount, (int)unreqCount );*/
    634650    ret = walk = tr_new( uint64_t, unreqCount + reqCount );
    635651    memcpy( walk, unreq, sizeof(uint64_t) * unreqCount );
     
    667683    blocks = getPreferredBlocks( t, &blockCount );
    668684    peers = getConnectedPeers( t, &peerCount );
    669 
    670 /*fprintf( stderr, "REFILL refillPulse for {%s} got %d blocks\n", tor->info.name, (int)blockCount );*/
    671685
    672686    for( i=0; peerCount && i<blockCount; ++i )
     
    698712
    699713                case TR_ADDREQ_OK:
    700                     /*fprintf( stderr, "REFILL peer %p took the request for block %d\n", peers[j]->msgs, block );*/
    701714                    tr_bitfieldAdd( t->requested, block );
    702715                    j = peerCount;
     
    774787    if( t->isRunning )
    775788        t->rechokeTimer = tr_timerNew( t->manager->handle, rechokePulse, t, RECHOKE_PERIOD_MSEC );
    776 }
    777 
    778 static void
    779 rechokeNow( Torrent * t )
    780 {
    781     rechokePulse( t );
    782     restartChokeTimer( t );
    783 }
    784 
    785 static int
    786 rechokeSoonCB( void * vt )
    787 {
    788     Torrent * t = vt;
    789     rechokeNow( t );
    790     t->rechokeSoonTimer = NULL;
    791     return FALSE;
    792 }
    793 
    794 static void
    795 rechokeSoon( Torrent * t )
    796 {
    797     if( t->rechokeSoonTimer == NULL )
    798         t->rechokeSoonTimer = tr_timerNew( t->manager->handle,
    799                                            rechokeSoonCB, t, SOON_MSEC );
    800789}
    801790
     
    859848        a->from = from;
    860849        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) );
    862851        tr_ptrArrayInsertSorted( t->pool, a, comparePeerAtoms );
    863852    }
     
    934923            peer->client = peer_id ? tr_clientForId( peer_id ) : NULL;
    935924            peer->msgsTag = tr_peerMsgsSubscribe( peer->msgs, msgsCallbackFunc, t );
    936             rechokeSoon( t );
    937925        }
    938926    }
     
    11791167    assert( t != NULL );
    11801168    stopTorrent( t );
    1181     freeTorrent( manager, t );
     1169    tr_ptrArrayRemoveSorted( manager->torrents, t, torrentCompare );
     1170    freeTorrent( t );
    11821171
    11831172    managerUnlock( manager );
     
    15631552        /* we don't need two connections to the same peer... */
    15641553        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) );
    15661555            continue;
    15671556        }
     
    15691558        /* no need to connect if we're both seeds... */
    15701559        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) );
    15721561            continue;
    15731562        }
     
    15751564        /* if we used this peer recently, give someone else a turn */
    15761565        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) );
    15781567            continue;
    15791568        }
     
    16051594        const int peerCount = tr_ptrArraySize( t->peers );
    16061595
    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",
    16081597                 t->tor->info.name, nConnections, nCandidates, tr_ptrArraySize(t->pool), (int)MAX_RECONNECTIONS_PER_PULSE );
    16091598
    16101599        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,
    16121601                     tr_peerIoAddrStr( &connections[i].peer->in_addr, connections[i].peer->port ), connections[i].throughput );
    16131602
     
    16161605            const double throughput = connections[i].throughput;
    16171606            tr_peer * peer = connections[i].peer;
    1618             fprintf( stderr, "RECONNECT culling peer %s, whose throughput was %f\n",
    1619                              tr_peerIoAddrStr(&peer->in_addr, peer->port), throughput );
     1607            tordbg( t, "RECONNECT culling peer %s, whose throughput was %f\n",
     1608                       tr_peerIoAddrStr(&peer->in_addr, peer->port), throughput );
    16201609            removePeer( t, peer );
    16211610        }
     
    16261615            struct peer_atom * atom = candidates[i];
    16271616            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" );
    16291618            initiateHandshake( t->manager, io );
    16301619            atom->time = time( NULL );
  • trunk/libtransmission/peer-msgs.c

    r3265 r3269  
    320320        tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) );
    321321        tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, choke ? BT_CHOKE : BT_UNCHOKE );
     322        msgs->info->chokeChangedAt = time( NULL );
    322323    }
    323324}
     
    564565    tr_peerIoWriteBuf( msgs->io, outbuf );
    565566
     567#if 0
    566568    dbgmsg( msgs, "here is the ltep handshake we sent:" );
    567569    tr_bencPrint( &val );
     570#endif
    568571
    569572    /* cleanup */
     
    588591    }
    589592
     593#if 0
    590594    dbgmsg( msgs, "here is the ltep handshake we read:" );
    591595    tr_bencPrint( &val );
     596#endif
    592597
    593598    /* does the peer prefer encrypted connections? */
     
    788793            break;
    789794
    790         case BT_BITFIELD:
     795        case BT_BITFIELD: {
     796            const int clientIsSeed = tr_cpGetStatus( msgs->torrent->completion ) != TR_CP_INCOMPLETE;
    791797            dbgmsg( msgs, "w00t peer sent us a BT_BITFIELD" );
    792798            assert( msglen == msgs->info->have->len );
    793799            tr_peerIoReadBytes( msgs->io, inbuf, msgs->info->have->bits, msglen );
    794800            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 );
    795803            dbgmsg( msgs, "after the HAVE message, peer progress is %f", msgs->info->progress );
    796804            updateInterest( msgs );
     
    798806            firePeerProgress( msgs );
    799807            break;
     808        }
    800809
    801810        case BT_REQUEST: {
  • trunk/libtransmission/ptrarray.c

    r3242 r3269  
    3838
    3939    return p;
     40}
     41
     42tr_ptrArray*
     43tr_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;
    4054}
    4155
  • trunk/libtransmission/ptrarray.h

    r3242 r3269  
    2222
    2323tr_ptrArray * tr_ptrArrayNew     ( void );
     24tr_ptrArray * tr_ptrArrayDup     ( tr_ptrArray* );
    2425void    tr_ptrArrayForeach       ( tr_ptrArray*, PtrArrayForeachFunc func );
    2526void    tr_ptrArrayFree          ( tr_ptrArray*, PtrArrayForeachFunc func );
Note: See TracChangeset for help on using the changeset viewer.