Ignore:
Timestamp:
Nov 10, 2007, 4:06:00 PM (13 years ago)
Author:
charles
Message:

commit tiennou's fast-peers-final patch from ticket 149

File:
1 edited

Legend:

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

    r3777 r3785  
    7171    PEER_PULSE_INTERVAL     = (100),       /* msec between calls to pulse() */
    7272    RATE_PULSE_INTERVAL     = (333),       /* msec between calls to ratePulse() */
    73 
    74     /* number of pieces generated for allow-fast,
    75       threshold for fast-allowing others */
    76     MAX_ALLOWED_SET_COUNT   = 10
     73     
     74    /* Fast Peers Extension constants */
     75    MAX_FAST_ALLOWED_COUNT   = 10,          /* max. number of pieces we fast-allow to another peer */
     76    MAX_FAST_ALLOWED_THRESHOLD = 10,        /* max threshold for allowing fast-pieces requests */
     77
    7778};
    7879
     
    135136    tr_bitfield * clientAllowedPieces;
    136137    tr_bitfield * peerAllowedPieces;
     138   
     139    tr_bitfield * clientSuggestedPieces;
    137140   
    138141    uint8_t state;
     
    424427                 uint32_t      pieceIndex )
    425428{
    426     dbgmsg( msgs, "w00t SUGGESTing them piece #%d", pieceIndex );
    427     tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) + sizeof(uint32_t) );
    428     tr_peerIoWriteUint8( msgs->io, msgs->outMessages, BT_SUGGEST );
    429     tr_peerIoWriteUint32( msgs->io, msgs->outMessages, pieceIndex );
     429    assert( msgs != NULL );
    430430   
    431     updateInterest( msgs );
     431    if( tr_peerIoSupportsFEXT( msgs->io ) )
     432    {
     433        tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) + sizeof(uint32_t) );
     434        tr_peerIoWriteUint8( msgs->io, msgs->outMessages, BT_SUGGEST );
     435        tr_peerIoWriteUint32( msgs->io, msgs->outMessages, pieceIndex );
     436    }
    432437}
    433438#endif
     
    435440sendFastHave( tr_peermsgs * msgs, int all )
    436441{
    437     dbgmsg( msgs, "w00t telling them we have %s pieces", (all ? "ALL" : "NONE" ) );
    438     tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) );
    439     tr_peerIoWriteUint8( msgs->io, msgs->outMessages, ( all ? BT_HAVE_ALL
    440                                                             : BT_HAVE_NONE ) );
    441     updateInterest( msgs );
     442    assert( msgs != NULL );
     443   
     444    if( tr_peerIoSupportsFEXT( msgs->io ) )
     445    {
     446        tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) );
     447        tr_peerIoWriteUint8( msgs->io, msgs->outMessages, ( all ? BT_HAVE_ALL
     448                                                                : BT_HAVE_NONE ) );
     449        updateInterest( msgs );
     450    }
    442451}
    443452
     
    465474                 uint32_t      pieceIndex)
    466475{
    467     dbgmsg( msgs, "w00t telling them we ALLOW_FAST piece #%d", pieceIndex );
    468     tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) + sizeof(uint32_t) );
    469     tr_peerIoWriteUint8( msgs->io, msgs->outMessages, BT_ALLOWED_FAST );
    470     tr_peerIoWriteUint32( msgs->io, msgs->outMessages, pieceIndex );
     476    assert( msgs != NULL );
     477   
     478    if( tr_peerIoSupportsFEXT( msgs->io ) )
     479    {
     480        tr_peerIoWriteUint32( msgs->io, msgs->outMessages, sizeof(uint8_t) + sizeof(uint32_t) );
     481        tr_peerIoWriteUint8( msgs->io, msgs->outMessages, BT_ALLOWED_FAST );
     482        tr_peerIoWriteUint32( msgs->io, msgs->outMessages, pieceIndex );
     483    }
    471484}
    472485
     
    864877clientCanSendFastBlock( const tr_peermsgs * msgs UNUSED )
    865878{
    866     /* FIXME(tiennou): base this on how many blocks we've already sent this
    867      * peer, or maybe how many fast blocks per minute we've sent overall,
    868      * or maybe how much bandwidth we're already using up w/o fast peers.
    869      * I don't know what the Right Thing here is, but
    870      * the previous measurement of how many pieces we have is not enough. */
    871     return FALSE;
     879    /* We can't send a fast piece if a peer has more than MAX_FAST_ALLOWED_THRESHOLD pieces */
     880    if( tr_bitfieldCountTrueBits( msgs->info->have ) > MAX_FAST_ALLOWED_THRESHOLD )
     881        return FALSE;
     882   
     883    /* ...or if we don't have ourself enough pieces */
     884    if( tr_bitfieldCountTrueBits( tr_cpPieceBitfield( msgs->torrent->completion ) ) < MAX_FAST_ALLOWED_THRESHOLD )
     885        return FALSE;
     886
     887    /* Maybe a bandwidth limit ? */
     888    return TRUE;
    872889}
    873890
     
    10051022            msgs->info->peerIsInterested = 0;
    10061023            break;
    1007 
     1024           
    10081025        case BT_HAVE:
    10091026            tr_peerIoReadUint32( msgs->io, inbuf, &ui32 );
    10101027            dbgmsg( msgs, "got Have: %u", ui32 );
    10111028            tr_bitfieldAdd( msgs->info->have, ui32 );
     1029            /* If this is a fast-allowed piece for this peer, mark it as normal now */
     1030            if( msgs->clientAllowedPieces != NULL && tr_bitfieldHas( msgs->clientAllowedPieces, ui32 ) )
     1031                tr_bitfieldRem( msgs->clientAllowedPieces, ui32 );
    10121032            updatePeerProgress( msgs );
    10131033            tr_rcTransferred( msgs->torrent->swarmspeed, msgs->torrent->info.pieceSize );
     
    10581078            break;
    10591079        }
    1060 
     1080       
    10611081        case BT_PORT:
    10621082            dbgmsg( msgs, "Got a BT_PORT" );
    10631083            tr_peerIoReadUint16( msgs->io, inbuf, &msgs->info->port );
    10641084            break;
    1065 
    1066 #if 0
     1085           
    10671086        case BT_SUGGEST: {
    1068             /* FIXME(tiennou) */
    1069             uint32_t index;
    1070             tr_peerIoReadUint32( msgs->io, inbuf, &index );
     1087            dbgmsg( msgs, "Got a BT_SUGGEST" );
     1088            tr_peerIoReadUint32( msgs->io, inbuf, &ui32 );
     1089            const tr_bitfield * bt = tr_cpPieceBitfield( msgs->torrent->completion );
     1090            if( tr_bitfieldHas( bt, ui32 ) )
     1091                tr_bitfieldAdd( msgs->clientSuggestedPieces, ui32 );
    10711092            break;
    10721093        }
    1073 
     1094           
    10741095        case BT_HAVE_ALL:
    10751096            dbgmsg( msgs, "Got a BT_HAVE_ALL" );
     
    10771098            updatePeerProgress( msgs );
    10781099            break;
    1079 
     1100       
     1101       
    10801102        case BT_HAVE_NONE:
    10811103            dbgmsg( msgs, "Got a BT_HAVE_NONE" );
     
    10831105            updatePeerProgress( msgs );
    10841106            break;
    1085 
     1107       
    10861108        case BT_REJECT: {
    10871109            struct peer_request req;
     
    11001122            break;
    11011123        }
    1102 #endif
    11031124
    11041125        case BT_LTEP:
     
    16461667    m->peerAllowedPieces = NULL;
    16471668    m->clientAllowedPieces = NULL;
     1669    m->clientSuggestedPieces = NULL;
    16481670    *setme = tr_publisherSubscribe( m->publisher, func, userData );
    16491671   
     
    16551677            const struct in_addr *peerAddr = tr_peerIoGetAddress( m->io, NULL );
    16561678           
    1657             m->peerAllowedPieces = tr_peerMgrGenerateAllowedSet( MAX_ALLOWED_SET_COUNT,
     1679            m->peerAllowedPieces = tr_peerMgrGenerateAllowedSet( MAX_FAST_ALLOWED_COUNT,
    16581680                                                                 m->torrent->info.pieceCount,
    16591681                                                                 m->torrent->info.hash,
     
    16611683        }
    16621684        m->clientAllowedPieces = tr_bitfieldNew( m->torrent->info.pieceCount );
     1685       
     1686        m->clientSuggestedPieces = tr_bitfieldNew( m->torrent->info.pieceCount );
    16631687    }
    16641688   
     
    16861710        peerProgress = m->torrent->info.pieceCount * m->info->progress;
    16871711       
    1688         if ( peerProgress < MAX_ALLOWED_SET_COUNT )
     1712        if ( peerProgress < MAX_FAST_ALLOWED_COUNT )
    16891713            sendFastAllowedSet( m );
    16901714    }
     
    17301754
    17311755int
    1732 tr_peerMsgIsPieceFastAllowed( const tr_peermsgs * peer,
    1733                               uint32_t            index )
     1756tr_peerMsgsIsPieceFastAllowed( const tr_peermsgs * peer,
     1757                               uint32_t            index )
    17341758{
    17351759    return tr_bitfieldHas( peer->clientAllowedPieces, index );
    17361760}
     1761
     1762int
     1763tr_peerMsgsIsPieceSuggested( const tr_peermsgs * peer,
     1764                             uint32_t            index )
     1765{
     1766    return tr_bitfieldHas( peer->clientSuggestedPieces, index );
     1767}
Note: See TracChangeset for help on using the changeset viewer.