Changeset 7168


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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/1.4x/libtransmission/peer-msgs.c

    r7065 r7168  
    19471947    if( msgs->peerSupportsPex && tr_torrentAllowsPex( msgs->torrent ) )
    19481948    {
    1949         int               i;
     1949        PexDiffs          diffs;
    19501950        tr_pex *          newPex = NULL;
    1951         const int         newCount = tr_peerMgrGetPeers(
    1952             msgs->session->peerMgr, msgs->torrent->info.hash, &newPex );
    1953         PexDiffs          diffs;
    1954         tr_benc           val;
    1955         uint8_t *         tmp, *walk;
    1956         char *            benc;
    1957         int               bencLen;
    1958         struct evbuffer * out = msgs->outMessages;
    1959 
     1951        const int         newCount = tr_peerMgrGetPeers( msgs->session->peerMgr,
     1952                                                         msgs->torrent->info.hash,
     1953                                                         &newPex );
    19601954        /* build the diffs */
    19611955        diffs.added = tr_new( tr_pex, newCount );
     
    19741968            msgs->pexCount, newCount, diffs.addedCount, diffs.droppedCount );
    19751969
    1976         /* update peer */
    1977         tr_free( msgs->pex );
    1978         msgs->pex = diffs.elements;
    1979         msgs->pexCount = diffs.elementCount;
    1980 
    1981         /* build the pex payload */
    1982         tr_bencInitDict( &val, 3 );
    1983 
    1984         /* "added" */
    1985         tmp = walk = tr_new( uint8_t, diffs.addedCount * 6 );
    1986         for( i = 0; i < diffs.addedCount; ++i )
     1970        if( diffs.addedCount || diffs.droppedCount )
    19871971        {
    1988             memcpy( walk, &diffs.added[i].in_addr, 4 ); walk += 4;
    1989             memcpy( walk, &diffs.added[i].port, 2 ); walk += 2;
     1972            int               i;
     1973            tr_benc           val;
     1974            uint8_t *         tmp, *walk;
     1975            char *            benc;
     1976            int               bencLen;
     1977            struct evbuffer * out = msgs->outMessages;
     1978
     1979            /* update peer */
     1980            tr_free( msgs->pex );
     1981            msgs->pex = diffs.elements;
     1982            msgs->pexCount = diffs.elementCount;
     1983
     1984            /* build the pex payload */
     1985            tr_bencInitDict( &val, 3 );
     1986
     1987            /* "added" */
     1988            tmp = walk = tr_new( uint8_t, diffs.addedCount * 6 );
     1989            for( i = 0; i < diffs.addedCount; ++i )
     1990            {
     1991                memcpy( walk, &diffs.added[i].in_addr, 4 ); walk += 4;
     1992                memcpy( walk, &diffs.added[i].port, 2 ); walk += 2;
     1993            }
     1994            assert( ( walk - tmp ) == diffs.addedCount * 6 );
     1995            tr_bencDictAddRaw( &val, "added", tmp, walk - tmp );
     1996            tr_free( tmp );
     1997
     1998            /* "added.f" */
     1999            tmp = walk = tr_new( uint8_t, diffs.addedCount );
     2000            for( i = 0; i < diffs.addedCount; ++i )
     2001                *walk++ = diffs.added[i].flags;
     2002            assert( ( walk - tmp ) == diffs.addedCount );
     2003            tr_bencDictAddRaw( &val, "added.f", tmp, walk - tmp );
     2004            tr_free( tmp );
     2005
     2006            /* "dropped" */
     2007            tmp = walk = tr_new( uint8_t, diffs.droppedCount * 6 );
     2008            for( i = 0; i < diffs.droppedCount; ++i )
     2009            {
     2010                memcpy( walk, &diffs.dropped[i].in_addr, 4 ); walk += 4;
     2011                memcpy( walk, &diffs.dropped[i].port, 2 ); walk += 2;
     2012            }
     2013            assert( ( walk - tmp ) == diffs.droppedCount * 6 );
     2014            tr_bencDictAddRaw( &val, "dropped", tmp, walk - tmp );
     2015            tr_free( tmp );
     2016
     2017            /* write the pex message */
     2018            benc = tr_bencSave( &val, &bencLen );
     2019            tr_peerIoWriteUint32( msgs->io, out, 2 * sizeof( uint8_t ) + bencLen );
     2020            tr_peerIoWriteUint8 ( msgs->io, out, BT_LTEP );
     2021            tr_peerIoWriteUint8 ( msgs->io, out, msgs->ut_pex_id );
     2022            tr_peerIoWriteBytes ( msgs->io, out, benc, bencLen );
     2023            pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
     2024            dbgmsg( msgs, "outMessage size is now %zu", EVBUFFER_LENGTH( out ) );
     2025
     2026            /* cleanup */
     2027            tr_free( benc );
     2028            tr_bencFree( &val );
    19902029        }
    1991         assert( ( walk - tmp ) == diffs.addedCount * 6 );
    1992         tr_bencDictAddRaw( &val, "added", tmp, walk - tmp );
    1993         tr_free( tmp );
    1994 
    1995         /* "added.f" */
    1996         tmp = walk = tr_new( uint8_t, diffs.addedCount );
    1997         for( i = 0; i < diffs.addedCount; ++i )
    1998             *walk++ = diffs.added[i].flags;
    1999         assert( ( walk - tmp ) == diffs.addedCount );
    2000         tr_bencDictAddRaw( &val, "added.f", tmp, walk - tmp );
    2001         tr_free( tmp );
    2002 
    2003         /* "dropped" */
    2004         tmp = walk = tr_new( uint8_t, diffs.droppedCount * 6 );
    2005         for( i = 0; i < diffs.droppedCount; ++i )
    2006         {
    2007             memcpy( walk, &diffs.dropped[i].in_addr, 4 ); walk += 4;
    2008             memcpy( walk, &diffs.dropped[i].port, 2 ); walk += 2;
    2009         }
    2010         assert( ( walk - tmp ) == diffs.droppedCount * 6 );
    2011         tr_bencDictAddRaw( &val, "dropped", tmp, walk - tmp );
    2012         tr_free( tmp );
    2013 
    2014         /* write the pex message */
    2015         benc = tr_bencSave( &val, &bencLen );
    2016         tr_peerIoWriteUint32( msgs->io, out,
    2017                               2 * sizeof( uint8_t ) + bencLen );
    2018         tr_peerIoWriteUint8 ( msgs->io, out, BT_LTEP );
    2019         tr_peerIoWriteUint8 ( msgs->io, out, msgs->ut_pex_id );
    2020         tr_peerIoWriteBytes ( msgs->io, out, benc, bencLen );
    2021         pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
    2022         dbgmsg( msgs, "outMessage size is now %d",
    2023                (int)EVBUFFER_LENGTH( out ) );
    2024 
    2025         /* cleanup */
    2026         tr_free( benc );
    2027         tr_bencFree( &val );
     2030
    20282031        tr_free( diffs.added );
    20292032        tr_free( diffs.dropped );
Note: See TracChangeset for help on using the changeset viewer.