Changeset 5178


Ignore:
Timestamp:
Mar 1, 2008, 2:09:18 PM (14 years ago)
Author:
charles
Message:

minor cleanups to peer-msgs

File:
1 edited

Legend:

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

    r5176 r5178  
    6161    TR_LTEP_PEX             = 1,
    6262
    63     MAX_REQUEST_BYTE_COUNT  = (16 * 1024), /* drop requests who want too much */
     63    /* drop requests who want too much */
     64    MAX_REQUEST_BYTE_COUNT  = (16 * 1024),
     65
     66    MIN_CHOKE_PERIOD_SEC    = (10),
    6467
    6568    /* idle seconds before we send a keepalive */
    6669    KEEPALIVE_INTERVAL_SECS = 90,
    6770
    68     PEX_INTERVAL            = (60 * 1000), /* msec between calls to sendPex() */
    69     PEER_PULSE_INTERVAL     = (100),       /* msec between calls to pulse() */
    70     RATE_PULSE_INTERVAL     = (250),       /* msec between calls to ratePulse() */
     71    PEX_INTERVAL            = (60 * 1000), /* msec between sendPex() calls */
     72    PEER_PULSE_INTERVAL     = (100),       /* msec between pulse() calls */
     73    RATE_PULSE_INTERVAL     = (250),       /* msec between ratePulse() calls */
    7174
    7275    MAX_QUEUE_SIZE          = (100),
    73 
    7476    MAX_OUTBUF_SIZE         = (1024),
    7577     
    76     /* Fast Peers Extension constants */
    77     MAX_FAST_ALLOWED_COUNT   = 10,          /* max. number of pieces we fast-allow to another peer */
    78     MAX_FAST_ALLOWED_THRESHOLD = 10,        /* max threshold for allowing fast-pieces requests */
     78    /* (fast peers) max number of pieces we fast-allow to another peer */
     79    MAX_FAST_ALLOWED_COUNT   = 10,
     80
     81    /* (fast peers) max threshold for allowing fast-pieces requests */
     82    MAX_FAST_ALLOWED_THRESHOLD = 10,
    7983
    8084    QUEUED_REQUEST_TTL_SECS = 20,
     
    8286    SENT_REQUEST_TTL_SECS = 90
    8387};
     88
     89/**
     90***  REQUEST MANAGEMENT
     91**/
    8492
    8593enum
     
    126134    {
    127135        list->max = max;
    128         list->requests = tr_renew( struct peer_request, list->requests, list->max );
     136        list->requests = tr_renew( struct peer_request,
     137                                   list->requests,
     138                                   list->max );
    129139    }
    130140}
     
    219229
    220230    reqListReserve( pruned, pruned->count + p );
    221     memcpy( pruned->requests + pruned->count, prune, sizeof(struct peer_request) * p );
     231    memcpy( pruned->requests + pruned->count,
     232            prune,
     233            sizeof(struct peer_request) * p );
    222234    pruned->count += p;
    223235}
     236
     237/**
     238***
     239**/
    224240
    225241/* this is raw, unchanged data from the peer regarding
     
    227243struct tr_incoming
    228244{
     245    uint8_t id;
    229246    uint32_t length; /* includes the +1 for id length */
    230     uint8_t id;
    231247    struct peer_request blockReq; /* metadata for incoming blocks */
    232248    struct evbuffer * block; /* piece data for incoming blocks */
     
    235251struct tr_peermsgs
    236252{
    237     tr_peer * info;
    238 
    239     tr_handle * handle;
    240     tr_torrent * torrent;
    241     tr_peerIo * io;
    242 
    243     tr_publisher_t * publisher;
    244 
    245     struct evbuffer * outBlock;    /* buffer of all the current piece message */
    246     struct evbuffer * outMessages; /* buffer of all the non-piece messages */
    247     struct request_list peerAskedFor;
    248     struct request_list peerAskedForFast;
    249     struct request_list clientAskedFor;
    250     struct request_list clientWillAskFor;
    251 
    252     tr_timer * rateTimer;
    253     tr_timer * pulseTimer;
    254     tr_timer * pexTimer;
    255 
    256     time_t lastReqAddedAt;
    257     time_t clientSentPexAt;
    258     time_t clientSentAnythingAt;
    259 
    260     time_t clientSentPieceDataAt;
    261     time_t peerSentPieceDataAt;
    262 
    263253    unsigned int peerSentBitfield         : 1;
    264254    unsigned int peerSupportsPex          : 1;
     
    267257    unsigned int sendingBlock             : 1;
    268258   
    269     tr_bitfield * clientAllowedPieces;
    270     tr_bitfield * peerAllowedPieces;
    271    
    272     tr_bitfield * clientSuggestedPieces;
    273    
    274259    uint8_t state;
    275260    uint8_t ut_pex_id;
     
    277262    uint32_t maxActiveRequests;
    278263    uint32_t minActiveRequests;
     264
     265    tr_peer * info;
     266
     267    tr_handle * handle;
     268    tr_torrent * torrent;
     269    tr_peerIo * io;
     270
     271    tr_publisher_t * publisher;
     272
     273    struct evbuffer * outBlock;    /* buffer of all the current piece message */
     274    struct evbuffer * outMessages; /* buffer of all the non-piece messages */
     275
     276    struct request_list peerAskedFor;
     277    struct request_list peerAskedForFast;
     278    struct request_list clientAskedFor;
     279    struct request_list clientWillAskFor;
     280
     281    tr_timer * rateTimer;
     282    tr_timer * pulseTimer;
     283    tr_timer * pexTimer;
     284
     285    time_t lastReqAddedAt;
     286    time_t clientSentPexAt;
     287    time_t clientSentAnythingAt;
     288    time_t clientSentPieceDataAt;
     289    time_t peerSentPieceDataAt;
     290   
     291    tr_bitfield * clientAllowedPieces;
     292    tr_bitfield * peerAllowedPieces;
     293    tr_bitfield * clientSuggestedPieces;
    279294
    280295    struct tr_incoming incoming;
     
    459474{
    460475    const tr_torrent * torrent = peer->torrent;
    461     if( torrent->info.pieces[piece].dnd ) /* we don't want it */
    462         return FALSE;
    463     if( tr_cpPieceIsComplete( torrent->completion, piece ) ) /* we have it */
    464         return FALSE;
    465     if( !tr_bitfieldHas( peer->info->have, piece ) ) /* peer doesn't have it */
    466         return FALSE;
    467     if( tr_bitfieldHas( peer->info->banned, piece ) ) /* peer is banned */
    468         return FALSE;
    469     return TRUE;
     476
     477    return ( ( !torrent->info.pieces[piece].dnd )               /* we want it */
     478          && ( !tr_cpPieceIsComplete( torrent->completion, piece ) ) /* !have */
     479          && ( tr_bitfieldHas( peer->info->have, piece ) )     /* peer has it */
     480          && ( !tr_bitfieldHas( peer->info->banned, piece ) ) );   /* !banned */
    470481}
    471482
     
    520531        fireNeedReq( msgs );
    521532}
    522 
    523 #define MIN_CHOKE_PERIOD_SEC 10
    524533
    525534static void
     
    718727pumpRequestQueue( tr_peermsgs * msgs )
    719728{
    720     struct peer_request req;
    721729    const int max = msgs->maxActiveRequests;
    722730    const int min = msgs->minActiveRequests;
    723731    const time_t now = time( NULL );
     732    int sent = 0;
    724733    int count = msgs->clientAskedFor.count;
    725     int sent = 0;
     734    struct peer_request req;
    726735
    727736    if( count > min )
     
    974983parseUtPex( tr_peermsgs * msgs, int msglen, struct evbuffer * inbuf )
    975984{
    976     int gotval = 0;
     985    int loaded = 0;
    977986    uint8_t * tmp = tr_new( uint8_t, msglen );
    978987    tr_benc val, *sub;
     
    980989
    981990    if( tr_torrentAllowsPex( msgs->torrent )
    982         && (( gotval = !tr_bencLoad( tmp, msglen, &val, NULL )))
     991        && (( loaded = !tr_bencLoad( tmp, msglen, &val, NULL )))
    983992        && (( sub = tr_bencDictFindType( &val, "added", TYPE_STR ))))
    984993    {
     
    9911000    }
    9921001
    993     if( gotval )
     1002    if( loaded )
    9941003        tr_bencFree( &val );
    9951004    tr_free( tmp );
     
    10801089updatePeerProgress( tr_peermsgs * msgs )
    10811090{
    1082     msgs->info->progress = tr_bitfieldCountTrueBits( msgs->info->have ) / (float)msgs->torrent->info.pieceCount;
     1091    msgs->info->progress = tr_bitfieldCountTrueBits( msgs->info->have )
     1092                         / (float)msgs->torrent->info.pieceCount;
    10831093    dbgmsg( msgs, "peer progress is %f", msgs->info->progress );
    10841094    updateInterest( msgs );
     
    10891099clientCanSendFastBlock( const tr_peermsgs * msgs UNUSED )
    10901100{
    1091     /* We can't send a fast piece if a peer has more than MAX_FAST_ALLOWED_THRESHOLD pieces */
     1101    /* don't send a fast piece if peer has MAX_FAST_ALLOWED_THRESHOLD pieces */
    10921102    if( tr_bitfieldCountTrueBits( msgs->info->have ) > MAX_FAST_ALLOWED_THRESHOLD )
    10931103        return FALSE;
     
    11851195clientGotBytes( tr_peermsgs * msgs, uint32_t byteCount )
    11861196{
     1197    const time_t now = time( NULL );
    11871198    tr_torrent * tor = msgs->torrent;
    11881199    tor->activityDate = tr_date( );
    11891200    tor->downloadedCur += byteCount;
    1190     msgs->peerSentPieceDataAt = time( NULL );
    1191     msgs->info->pieceDataActivityDate = time( NULL );
     1201    msgs->peerSentPieceDataAt = now;
     1202    msgs->info->pieceDataActivityDate = now;
    11921203    msgs->info->credit += (int)(byteCount * SWIFT_REPAYMENT_RATIO);
    11931204    tr_rcTransferred( msgs->info->rcToClient, byteCount );
     
    13231334
    13241335        case BT_REQUEST: {
    1325             struct peer_request req;
    1326             tr_peerIoReadUint32( msgs->io, inbuf, &req.index );
    1327             tr_peerIoReadUint32( msgs->io, inbuf, &req.offset );
    1328             tr_peerIoReadUint32( msgs->io, inbuf, &req.length );
    1329             dbgmsg( msgs, "got Request: %u:%u->%u", req.index, req.offset, req.length );
    1330             peerMadeRequest( msgs, &req );
     1336            struct peer_request r;
     1337            tr_peerIoReadUint32( msgs->io, inbuf, &r.index );
     1338            tr_peerIoReadUint32( msgs->io, inbuf, &r.offset );
     1339            tr_peerIoReadUint32( msgs->io, inbuf, &r.length );
     1340            dbgmsg( msgs, "got Request: %u:%u->%u", r.index, r.offset, r.length );
     1341            peerMadeRequest( msgs, &r );
    13311342            break;
    13321343        }
    13331344
    13341345        case BT_CANCEL: {
    1335             struct peer_request req;
    1336             tr_peerIoReadUint32( msgs->io, inbuf, &req.index );
    1337             tr_peerIoReadUint32( msgs->io, inbuf, &req.offset );
    1338             tr_peerIoReadUint32( msgs->io, inbuf, &req.length );
    1339             dbgmsg( msgs, "got a Cancel %u:%u->%u", req.index, req.offset, req.length );
    1340             reqListRemove( &msgs->peerAskedForFast, &req );
    1341             reqListRemove( &msgs->peerAskedFor, &req );
     1346            struct peer_request r;
     1347            tr_peerIoReadUint32( msgs->io, inbuf, &r.index );
     1348            tr_peerIoReadUint32( msgs->io, inbuf, &r.offset );
     1349            tr_peerIoReadUint32( msgs->io, inbuf, &r.length );
     1350            dbgmsg( msgs, "got a Cancel %u:%u->%u", r.index, r.offset, r.length );
     1351            reqListRemove( &msgs->peerAskedForFast, &r );
     1352            reqListRemove( &msgs->peerAskedFor, &r );
    13421353            break;
    13431354        }
    13441355
    13451356        case BT_PIECE:
    1346             assert( 0 ); /* should be handled elsewhere! */
     1357            assert( 0 ); /* handled elsewhere! */
    13471358            break;
    13481359       
     
    13531364           
    13541365        case BT_SUGGEST: {
    1355             const tr_bitfield * bt = tr_cpPieceBitfield( msgs->torrent->completion );
    13561366            dbgmsg( msgs, "Got a BT_SUGGEST" );
    13571367            tr_peerIoReadUint32( msgs->io, inbuf, &ui32 );
    1358             if( tr_bitfieldHas( bt, ui32 ) )
     1368            if( tr_cpPieceIsComplete( msgs->torrent->completion, ui32 ) )
    13591369                tr_bitfieldAdd( msgs->clientSuggestedPieces, ui32 );
    13601370            break;
     
    13771387       
    13781388        case BT_REJECT: {
    1379             struct peer_request req;
     1389            struct peer_request r;
    13801390            dbgmsg( msgs, "Got a BT_REJECT" );
    1381             tr_peerIoReadUint32( msgs->io, inbuf, &req.index );
    1382             tr_peerIoReadUint32( msgs->io, inbuf, &req.offset );
    1383             tr_peerIoReadUint32( msgs->io, inbuf, &req.length );
    1384             reqListRemove( &msgs->clientAskedFor, &req );
     1391            tr_peerIoReadUint32( msgs->io, inbuf, &r.index );
     1392            tr_peerIoReadUint32( msgs->io, inbuf, &r.offset );
     1393            tr_peerIoReadUint32( msgs->io, inbuf, &r.length );
     1394            reqListRemove( &msgs->clientAskedFor, &r );
    13851395            break;
    13861396        }
     
    14141424peerGotBytes( tr_peermsgs * msgs, uint32_t byteCount )
    14151425{
     1426    const time_t now = time( NULL );
    14161427    tr_torrent * tor = msgs->torrent;
    14171428    tor->activityDate = tr_date( );
    14181429    tor->uploadedCur += byteCount;
    1419     msgs->clientSentPieceDataAt = time( NULL );
    1420     msgs->info->pieceDataActivityDate = time( NULL );
     1430    msgs->clientSentPieceDataAt = now;
     1431    msgs->info->pieceDataActivityDate = now;
    14211432    msgs->info->credit -= byteCount;
    14221433    tr_rcTransferred( msgs->info->rcToPeer, byteCount );
     
    14521463
    14531464    /* decrement the `downloaded' field */
    1454     if( tor->downloadedCur >= byteCount )
    1455         tor->downloadedCur -= byteCount;
    1456     else
    1457         tor->downloadedCur = 0;
     1465    tor->downloadedCur -= MIN( tor->downloadedCur, byteCount );
    14581466}
    14591467
Note: See TracChangeset for help on using the changeset viewer.