Changeset 3278


Ignore:
Timestamp:
Oct 3, 2007, 4:42:43 PM (15 years ago)
Author:
charles
Message:

minor source code cleanup...

Location:
trunk/libtransmission
Files:
3 edited

Legend:

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

    r3277 r3278  
    4646
    4747    /* how frequently to refill peers' request lists */
    48     REFILL_PERIOD_MSEC = 1000,
     48    REFILL_PERIOD_MSEC = 2000,
    4949
    5050    /* how many peers to unchoke per-torrent. */
     
    233233torrentCompareToHash( const void * va, const void * vb )
    234234{
    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;
    237237    return memcmp( a->hash, b_hash, SHA_DIGEST_LENGTH );
    238238}
     
    249249peerCompare( const void * va, const void * vb )
    250250{
    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;
    253253    return compareAddresses( &a->in_addr, &b->in_addr );
    254254}
     
    257257peerCompareToAddr( const void * va, const void * vb )
    258258{
    259     const tr_peer * a = (const tr_peer *) va;
     259    const tr_peer * a = va;
    260260    return compareAddresses( &a->in_addr, vb );
    261261}
     
    292292    assert( torrentIsLocked ( t ) );
    293293
    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
     299static tr_peer*
     300peerConstructor( 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;
    297308}
    298309
     
    306317    peer = getExistingPeer( torrent, in_addr );
    307318
    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 );
    314321        tr_ptrArrayInsertSorted( torrent->peers, peer, peerCompare );
    315322    }
     
    344351
    345352static void
    346 freePeer( tr_peer * peer )
     353peerDestructor( tr_peer * peer )
    347354{
    348355    disconnectPeer( peer );
     
    367374    removed = tr_ptrArrayRemoveSorted  ( t->peers, peer, peerCompare );
    368375    assert( removed == peer );
    369     freePeer( removed );
     376    peerDestructor( removed );
    370377}
    371378
     
    377384}
    378385
    379 /* torrent must have already been removed from manager->torrents */
    380386static void
    381387torrentDestructor( Torrent * t )
     
    574580        return 0;
    575581
    576     if( tr_cpPieceIsComplete( tor->completion, piece ) ) /* we already have it */
     582    if( tr_cpPieceIsComplete( tor->completion, piece ) ) /* we have it */
    577583        return 0;
    578584
     
    11121118
    11131119    /* disconnect the peers. */
    1114     tr_ptrArrayForeach( t->peers, (PtrArrayForeachFunc)freePeer );
     1120    tr_ptrArrayForeach( t->peers, (PtrArrayForeachFunc)peerDestructor );
    11151121    tr_ptrArrayClear( t->peers );
    11161122
     
    13211327**/
    13221328
    1323 typedef struct
     1329struct ChokeData
    13241330{
    13251331    tr_peer * peer;
     
    13281334    int preferred;
    13291335    int doUnchoke;
    1330 }
    1331 ChokeData;
     1336};
    13321337
    13331338static int
    13341339compareChoke( const void * va, const void * vb )
    13351340{
    1336     const ChokeData * a = va;
    1337     const ChokeData * b = vb;
     1341    const struct ChokeData * a = va;
     1342    const struct ChokeData * b = vb;
    13381343
    13391344    if( a->preferred != b->preferred )
     
    13651370
    13661371static void
    1367 rechokeLeech( Torrent * t )
     1372rechoke( Torrent * t )
    13681373{
    13691374    int i, peerCount, size=0, unchoked=0;
    13701375    const time_t ignorePeersNewerThan = time(NULL) - MIN_CHOKE_PERIOD_SEC;
    13711376    tr_peer ** peers = getConnectedPeers( t, &peerCount );
    1372     ChokeData * choke = tr_new0( ChokeData, peerCount );
     1377    struct ChokeData * choke = tr_new0( struct ChokeData, peerCount );
    13731378
    13741379    assert( torrentIsLocked( t ) );
     
    13781383    {
    13791384        tr_peer * peer = peers[i];
    1380         ChokeData * node;
     1385        struct ChokeData * node;
    13811386        if( peer->chokeChangedAt > ignorePeersNewerThan )
    13821387            continue;
     
    13901395    }
    13911396
    1392     qsort( choke, size, sizeof(ChokeData), compareChoke );
     1397    qsort( choke, size, sizeof(struct ChokeData), compareChoke );
    13931398
    13941399    for( i=0; i<size && i<NUM_UNCHOKED_PEERS_PER_TORRENT; ++i ) {
     
    14121417}
    14131418
    1414 static void
    1415 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 
    14311419static int
    14321420rechokePulse( void * vtorrent )
     
    14341422    Torrent * t = vtorrent;
    14351423    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 );
    14431425    torrentUnlock( t );
    14441426    return TRUE;
     
    14901472            isWeak = FALSE;
    14911473        else
    1492             isWeak = now - peer->pieceDataActivityDate > 180;
     1474            isWeak = ( now - peer->pieceDataActivityDate ) > 180;
    14931475
    14941476        if( isWeak )
  • trunk/libtransmission/peer-msgs.c

    r3277 r3278  
    146146
    147147static void
    148 myDebug( const char * file, int line, const struct tr_peermsgs * msgs, const char * fmt, ... )
     148myDebug( const char * file, int line,
     149         const struct tr_peermsgs * msgs,
     150         const char * fmt, ... )
    149151{
    150152    FILE * fp = tr_getLog( );
     
    233235    if( torrent->info.pieces[piece].dnd ) /* we don't want it */
    234236        return FALSE;
    235     if( tr_cpPieceIsComplete( torrent->completion, piece ) ) /* we already have it */
     237    if( tr_cpPieceIsComplete( torrent->completion, piece ) ) /* we have it */
    236238        return FALSE;
    237239    if( !tr_bitfieldHas( peer->info->have, piece ) ) /* peer doesn't have it */
    238240        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 */
    240242        return FALSE;
    241243    return TRUE;
    242244}
    243245
    244 /* "interested" means we'll ask for piece data from the peer if they unchoke us */
     246/* "interested" means we'll ask for piece data if they unchoke us */
    245247static int
    246248isPeerInteresting( const tr_peermsgs * msgs )
     
    249251    const tr_torrent * torrent;
    250252    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;
    252255
    253256    if( clientIsSeed )
     
    257260    bitfield = tr_cpPieceBitfield( torrent->completion );
    258261
    259     if( !msgs->info->have ) /* We don't know what this peer has... what should this be? */
     262    if( !msgs->info->have )
    260263        return TRUE;
    261264
     
    275278
    276279    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");
    278282
    279283    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 );
    281286}
    282287
     
    307312            {
    308313                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 */
    310315                struct peer_request *req = walk->data;
    311316                if ( !tr_bitfieldHas( msgs->peerAllowedPieces, req->index ) )
     
    319324        dbgmsg( msgs, "sending a %s message", (choke ? "CHOKE" : "UNCHOKE") );
    320325        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 );
    322328        msgs->info->chokeChangedAt = time( NULL );
    323329    }
     
    367373    dbgmsg( msgs, "w00t telling them we HAVE piece #%d", pieceIndex );
    368374
    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) );
    370377    tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, BT_HAVE );
    371378    tr_peerIoWriteUint32( msgs->io, msgs->outMessages, pieceIndex );
     
    614621    }
    615622
    616 #if 0
    617     /* 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 #endif
    629 
    630623    /* get peer's listening port */
    631624    sub = tr_bencDictFind( &val, "p" );
     
    722715
    723716    return READ_AGAIN;
     717}
     718
     719static void
     720updatePeerProgress( 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 );
    724726}
    725727
     
    787789            tr_peerIoReadUint32( msgs->io, inbuf, &ui32 );
    788790            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 );
    793792            break;
    794793
     
    798797            assert( msglen == msgs->info->have->len );
    799798            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 );
    801800            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 );
    804801            fireNeedReq( msgs );
    805             firePeerProgress( msgs );
    806802            break;
    807803        }
     
    906902            dbgmsg( msgs, "peer sent us a BT_HAVE_ALL" );
    907903            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 );
    912905            break;
    913906        }
     
    917910            dbgmsg( msgs, "peer sent us a BT_HAVE_NONE" );
    918911            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 );
    923913            break;
    924914        }
     
    10941084    **/
    10951085
    1096     if( tr_ioWrite( tor, index, offset, length, EVBUFFER_DATA( inbuf ))) {
     1086    if( tr_ioWrite( tor, index, offset, length, EVBUFFER_DATA( inbuf )))
    10971087        return;
    1098     }
    10991088
    11001089    tr_cpBlockAdd( tor->completion, block );
     
    12991288{
    13001289    const tr_bitfield * bitfield = tr_cpPieceBitfield( msgs->torrent->completion );
     1290    struct evbuffer * out = msgs->outMessages;
    13011291
    13021292    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 );
    13061296}
    13071297
  • trunk/libtransmission/peer-msgs.h

    r3277 r3278  
    1111 */
    1212
    13 #ifndef TR_P_H
    14 #define TR_P_H
     13#ifndef TR_PEER_MSGS_H
     14#define TR_PEER_MSGS_H
    1515
    1616#include <inttypes.h>
Note: See TracChangeset for help on using the changeset viewer.