Changeset 7169


Ignore:
Timestamp:
Nov 28, 2008, 5:48:17 AM (13 years ago)
Author:
charles
Message:

(libT) #1527: don't send pex messages if we don't have any new information to send

File:
1 edited

Legend:

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

    r7154 r7169  
    19531953    if( msgs->peerSupportsPex && tr_torrentAllowsPex( msgs->torrent ) )
    19541954    {
    1955         int               i;
    1956         tr_pex *          newPex = NULL;
    1957         const int         newCount = tr_peerMgrGetPeers(
    1958             msgs->session->peerMgr, msgs->torrent->info.hash, &newPex );
    1959         PexDiffs          diffs;
    1960         tr_benc           val;
    1961         uint8_t *         tmp, *walk;
    1962         char *            benc;
    1963         int               bencLen;
    1964         struct evbuffer * out = msgs->outMessages;
     1955        PexDiffs diffs;
     1956        tr_pex * newPex = NULL;
     1957        const int newCount = tr_peerMgrGetPeers( msgs->session->peerMgr,
     1958                                                 msgs->torrent->info.hash,
     1959                                                 &newPex );
    19651960
    19661961        /* build the diffs */
     
    19801975            msgs->pexCount, newCount, diffs.addedCount, diffs.droppedCount );
    19811976
    1982         /* update peer */
    1983         tr_free( msgs->pex );
    1984         msgs->pex = diffs.elements;
    1985         msgs->pexCount = diffs.elementCount;
    1986 
    1987         /* build the pex payload */
    1988         tr_bencInitDict( &val, 3 );
    1989 
    1990         /* "added" */
    1991         tmp = walk = tr_new( uint8_t, diffs.addedCount * 6 );
    1992         for( i = 0; i < diffs.addedCount; ++i )
     1977        if( diffs.addedCount || diffs.droppedCount )
    19931978        {
    1994             memcpy( walk, &diffs.added[i].in_addr, 4 ); walk += 4;
    1995             memcpy( walk, &diffs.added[i].port, 2 ); walk += 2;
     1979            int  i;
     1980            tr_benc val;
     1981            char * benc;
     1982            int bencLen;
     1983            uint8_t * tmp, *walk;
     1984            struct evbuffer * out = msgs->outMessages;
     1985
     1986            /* update peer */
     1987            tr_free( msgs->pex );
     1988            msgs->pex = diffs.elements;
     1989            msgs->pexCount = diffs.elementCount;
     1990
     1991            /* build the pex payload */
     1992            tr_bencInitDict( &val, 3 );
     1993
     1994            /* "added" */
     1995            tmp = walk = tr_new( uint8_t, diffs.addedCount * 6 );
     1996            for( i = 0; i < diffs.addedCount; ++i ) {
     1997                memcpy( walk, &diffs.added[i].in_addr, 4 ); walk += 4;
     1998                memcpy( walk, &diffs.added[i].port, 2 ); walk += 2;
     1999            }
     2000            assert( ( walk - tmp ) == diffs.addedCount * 6 );
     2001            tr_bencDictAddRaw( &val, "added", tmp, walk - tmp );
     2002            tr_free( tmp );
     2003
     2004            /* "added.f" */
     2005            tmp = walk = tr_new( uint8_t, diffs.addedCount );
     2006            for( i = 0; i < diffs.addedCount; ++i )
     2007                *walk++ = diffs.added[i].flags;
     2008            assert( ( walk - tmp ) == diffs.addedCount );
     2009            tr_bencDictAddRaw( &val, "added.f", tmp, walk - tmp );
     2010            tr_free( tmp );
     2011
     2012            /* "dropped" */
     2013            tmp = walk = tr_new( uint8_t, diffs.droppedCount * 6 );
     2014            for( i = 0; i < diffs.droppedCount; ++i ) {
     2015                memcpy( walk, &diffs.dropped[i].in_addr, 4 ); walk += 4;
     2016                memcpy( walk, &diffs.dropped[i].port, 2 ); walk += 2;
     2017            }
     2018            assert( ( walk - tmp ) == diffs.droppedCount * 6 );
     2019            tr_bencDictAddRaw( &val, "dropped", tmp, walk - tmp );
     2020            tr_free( tmp );
     2021
     2022            /* write the pex message */
     2023            benc = tr_bencSave( &val, &bencLen );
     2024            tr_peerIoWriteUint32( msgs->io, out, 2 * sizeof( uint8_t ) + bencLen );
     2025            tr_peerIoWriteUint8 ( msgs->io, out, BT_LTEP );
     2026            tr_peerIoWriteUint8 ( msgs->io, out, msgs->ut_pex_id );
     2027            tr_peerIoWriteBytes ( msgs->io, out, benc, bencLen );
     2028            pokeBatchPeriod( msgs, HIGH_PRIORITY_INTERVAL_SECS );
     2029            dbgmsg( msgs, "sending a pex message; outMessage size is now %zu", EVBUFFER_LENGTH( out ) );
     2030
     2031            tr_free( benc );
     2032            tr_bencFree( &val );
    19962033        }
    1997         assert( ( walk - tmp ) == diffs.addedCount * 6 );
    1998         tr_bencDictAddRaw( &val, "added", tmp, walk - tmp );
    1999         tr_free( tmp );
    2000 
    2001         /* "added.f" */
    2002         tmp = walk = tr_new( uint8_t, diffs.addedCount );
    2003         for( i = 0; i < diffs.addedCount; ++i )
    2004             *walk++ = diffs.added[i].flags;
    2005         assert( ( walk - tmp ) == diffs.addedCount );
    2006         tr_bencDictAddRaw( &val, "added.f", tmp, walk - tmp );
    2007         tr_free( tmp );
    2008 
    2009         /* "dropped" */
    2010         tmp = walk = tr_new( uint8_t, diffs.droppedCount * 6 );
    2011         for( i = 0; i < diffs.droppedCount; ++i )
    2012         {
    2013             memcpy( walk, &diffs.dropped[i].in_addr, 4 ); walk += 4;
    2014             memcpy( walk, &diffs.dropped[i].port, 2 ); walk += 2;
    2015         }
    2016         assert( ( walk - tmp ) == diffs.droppedCount * 6 );
    2017         tr_bencDictAddRaw( &val, "dropped", tmp, walk - tmp );
    2018         tr_free( tmp );
    2019 
    2020         /* write the pex message */
    2021         benc = tr_bencSave( &val, &bencLen );
    2022         tr_peerIoWriteUint32( msgs->io, out,
    2023                               2 * sizeof( uint8_t ) + bencLen );
    2024         tr_peerIoWriteUint8 ( msgs->io, out, BT_LTEP );
    2025         tr_peerIoWriteUint8 ( msgs->io, out, msgs->ut_pex_id );
    2026         tr_peerIoWriteBytes ( msgs->io, out, benc, bencLen );
    2027         pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
    2028         dbgmsg( msgs, "sending a pex message; outMessage size is now %zu", EVBUFFER_LENGTH( out ) );
    20292034
    20302035        /* cleanup */
    2031         tr_free( benc );
    2032         tr_bencFree( &val );
    20332036        tr_free( diffs.added );
    20342037        tr_free( diffs.dropped );
Note: See TracChangeset for help on using the changeset viewer.