Changeset 11548


Ignore:
Timestamp:
Dec 20, 2010, 2:07:51 AM (12 years ago)
Author:
charles
Message:

(trunk) #3836 "libevent2 support" -- bump libevent2 requirement to 2.0.10. This will break the Mac build for a minute intil BMW applies his Mac patch

Location:
trunk
Files:
23 edited

Legend:

Unmodified
Added
Removed
  • trunk/configure.ac

    r11505 r11548  
    4141CURL_MINIMUM=7.15.4
    4242AC_SUBST(CURL_MINIMUM)
    43 LIBEVENT_MINIMUM=1.4.9
     43LIBEVENT_MINIMUM=2.0.10
    4444AC_SUBST(LIBEVENT_MINIUM)
    4545OPENSSL_MINIMUM=0.9.4
     
    8686if test "x$GCC" = "xyes" ; then
    8787
    88     CFLAGS="$CFLAGS -std=gnu99 -ggdb3 -Wall -W -Wpointer-arith -Wformat-security -Wcast-align -Wundef -Wcast-align -Wstrict-prototypes -Wmissing-declarations -Wmissing-format-attribute -Wredundant-decls -Wnested-externs -Wunused-parameter -Wwrite-strings -Waggregate-return -Winline -Wfloat-equal"
     88    CFLAGS="$CFLAGS -std=gnu99 -ggdb3 -Wall -W -Wpointer-arith -Wformat-security -Wcast-align -Wundef -Wcast-align -Wstrict-prototypes -Wmissing-declarations -Wmissing-format-attribute -Wredundant-decls -Wnested-externs -Wunused-parameter -Wwrite-strings -Winline -Wfloat-equal"
    8989
    9090    dnl figure out gcc version
     
    123123PKG_CHECK_MODULES(OPENSSL, [openssl >= $OPENSSL_MINIMUM], , [CHECK_SSL()])
    124124PKG_CHECK_MODULES(LIBCURL, [libcurl >= $CURL_MINIMUM])
     125PKG_CHECK_MODULES(LIBEVENT, [libevent >= $LIBEVENT_MINIMUM])
    125126AC_PATH_ZLIB
    126127
     
    204205
    205206
    206 dnl libevent
    207 dnl if the user specified LIBEVENT_LIBS or
    208 if test -n "$LIBEVENT_LIBS"; then
    209     user_specified_libevent=yes
    210 elif test -n "$LIBEVENT_CFLAGS"; then
    211     user_specified_libevent=yes
    212 fi
    213 if test "x$user_specified_libevent" = "xyes"; then
    214     AC_MSG_NOTICE([Using user-specified LIBEVENT_LIBS and LIBEVENT_CFLAGS])
    215 else
    216     AC_CHECK_LIB([event],[evutil_vsnprintf],
    217                  [],
    218                  [AC_MSG_ERROR(libevent $LIBEVENT_MINIMUM or higher not found!)],
    219                  [$libevent_extra_libs])
    220     AC_CHECK_HEADER([event-config.h],[],
    221                     [AC_MSG_ERROR(event-config.h not found!)])
    222     LIBEVENT_CFLAGS=""
    223     LIBEVENT_LIBS="-levent $libevent_extra_libs"
    224 fi
    225 AC_ARG_VAR([LIBEVENT_CFLAGS], [C compiler flags for LIBEVENT, overriding pkg-config])dnl
    226 AC_ARG_VAR([LIBEVENT_LIBS], [linker flags for LIBEVENT, overriding pkg-config])dnl
    227 
    228 
    229 
    230207dnl ----------------------------------------------------------------------------
    231208dnl
  • trunk/gtk/tr-core.c

    r11546 r11548  
    3434#endif
    3535
     36#include <event2/buffer.h>
     37
    3638#include <libtransmission/transmission.h>
    3739#include <libtransmission/bencode.h>
     
    15981600    tr_benc top;
    15991601    int64_t intVal;
    1600     GByteArray * response = vresponse;
    1601 
    1602     tr_jsonParse( NULL, response->data, response->len, &top, NULL );
     1602    struct evbuffer * response = vresponse;
     1603
     1604    tr_jsonParse( NULL, evbuffer_pullup( response, -1 ), evbuffer_get_length( response ), &top, NULL );
    16031605
    16041606    if( tr_bencDictFindInt( &top, "tag", &intVal ) )
     
    16141616
    16151617    tr_bencFree( &top );
    1616     g_byte_array_free( response, TRUE );
     1618    evbuffer_free( response );
    16171619    return FALSE;
    16181620}
     
    16201622static void
    16211623readResponse( tr_session  * session UNUSED,
    1622               const char  * response,
    1623               size_t        response_len,
     1624              struct evbuffer * response,
    16241625              void        * unused UNUSED )
    16251626{
    1626     GByteArray * bytes = g_byte_array_new( );
    1627 #ifdef DEBUG_RPC
    1628     g_message( "response: [%*.*s]", (int)response_len, (int)response_len, response );
    1629 #endif
    1630     g_byte_array_append( bytes, (const uint8_t*)response, response_len );
    1631     gtr_idle_add( readResponseIdle, bytes );
     1627    struct evbuffer * buf = evbuffer_new( );
     1628    evbuffer_add_buffer( buf, response );
     1629    gtr_idle_add( readResponseIdle, buf );
    16321630}
    16331631
  • trunk/libtransmission/announcer.c

    r11451 r11548  
    1414#include <limits.h>
    1515
    16 #include <event.h>
    17 #include <evhttp.h> /* for HTTP_OK */
     16#include <event2/buffer.h>
     17#include <event2/event.h>
     18#include <event2/http.h> /* for HTTP_OK */
    1819
    1920#include "transmission.h"
     
    264265    a->slotsAvailable = MAX_CONCURRENT_TASKS;
    265266    a->lpdHouseKeepingAt = relaxUntil;
    266     a->upkeepTimer = tr_new0( struct event, 1 );
    267     evtimer_set( a->upkeepTimer, onUpkeepTimer, a );
     267    a->upkeepTimer = evtimer_new( NULL, onUpkeepTimer, a );
    268268    tr_timerAdd( a->upkeepTimer, UPKEEP_INTERVAL_SECS, 0 );
    269269
     
    280280    flushCloseMessages( announcer );
    281281
    282     evtimer_del( announcer->upkeepTimer );
    283     tr_free( announcer->upkeepTimer );
     282    event_free( announcer->upkeepTimer );
    284283    announcer->upkeepTimer = NULL;
    285284
     
    725724    const char * ann = tracker->announce;
    726725    struct evbuffer * buf = evbuffer_new( );
    727     char * ret;
    728726    const char * str;
    729727    const unsigned char * ipv6;
     
    785783    }
    786784
    787     ret = tr_strndup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
    788     dbgmsg( tier, "announce URL is \"%s\"", ret );
    789     evbuffer_free( buf );
    790     return ret;
     785    return evbuffer_free_to_str( buf );
    791786}
    792787
  • trunk/libtransmission/bencode-test.c

    r10760 r11548  
    44#include <string.h>
    55
    6 #include <event.h>
     6#include <event2/buffer.h>
    77
    88#include "transmission.h"
     
    326326    tr_bencLoad( benc_str, strlen( benc_str ), &top, NULL );
    327327    tr_bencToBuf( &top, TR_FMT_JSON, buf );
    328     serialized = (char*) EVBUFFER_DATA( buf );
     328    serialized = (char*) evbuffer_pullup( buf, -1 );
    329329    stripWhitespace( serialized );
    330330#if 0
  • trunk/libtransmission/bencode.c

    r11544 r11548  
    3030#include <unistd.h> /* stat() */
    3131
    32 #include <event.h> /* struct evbuffer */
     32#include <event2/buffer.h>
    3333
    3434#include "ConvertUTF.h"
     
    15871587tr_bencToBuf( const tr_benc * top, tr_fmt_mode mode, struct evbuffer * buf )
    15881588{
    1589     evbuffer_drain( buf, EVBUFFER_LENGTH( buf ) );
     1589    evbuffer_drain( buf, evbuffer_get_length( buf ) );
    15901590    evbuffer_expand( buf, 4096 ); /* alloc a little memory to start off with */
    15911591
     
    16031603            data.parents = NULL;
    16041604            bencWalk( top, &jsonWalkFuncs, &data );
    1605             if( EVBUFFER_LENGTH( buf ) )
     1605            if( evbuffer_get_length( buf ) )
    16061606                evbuffer_add_printf( buf, "\n" );
    16071607            break;
     
    16151615    char * ret;
    16161616    struct evbuffer * buf = evbuffer_new( );
     1617    size_t n;
    16171618    tr_bencToBuf( top, mode, buf );
    1618     ret = tr_strndup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
     1619    n = evbuffer_get_length( buf );
     1620    ret = evbuffer_free_to_str( buf );
    16191621    if( len != NULL )
    1620         *len = (int) EVBUFFER_LENGTH( buf );
    1621     evbuffer_free( buf );
     1622        *len = (int) n;
    16221623    return ret;
    16231624}
     
    16671668    if( fd >= 0 )
    16681669    {
    1669         int len;
    1670         char * str = tr_bencToStr( top, mode, &len );
    1671 
    1672         if( write( fd, str, len ) == (ssize_t)len )
     1670        int nleft;
     1671
     1672        /* save the benc to a temporary file */
     1673        {
     1674            struct evbuffer * buffer = evbuffer_new( );
     1675            tr_bencToBuf( top, mode, buffer );
     1676            nleft = evbuffer_get_length( buffer );
     1677            while( nleft > 0 ) {
     1678                const int n = evbuffer_write( buffer, fd );
     1679                if( n < nleft )
     1680                    fprintf( stderr, "wrote %d of %d to %s\n", n, nleft, tmp );
     1681                if( n >= 0 )
     1682                    nleft -= n;
     1683                else if( errno != EAGAIN ) {
     1684                    err = errno;
     1685                    break;
     1686                }
     1687            }
     1688            evbuffer_free( buffer );
     1689        }
     1690
     1691        if( nleft > 0 )
     1692        {
     1693            tr_err( _( "Couldn't save temporary file \"%1$s\": %2$s" ), tmp, tr_strerror( err ) );
     1694            tr_close_file( fd );
     1695            unlink( tmp );
     1696        }
     1697        else
    16731698        {
    16741699            struct stat sb;
     
    16981723            }
    16991724        }
    1700         else
    1701         {
    1702             err = errno;
    1703             tr_err( _( "Couldn't save temporary file \"%1$s\": %2$s" ), tmp, tr_strerror( err ) );
    1704             tr_close_file( fd );
    1705             unlink( tmp );
    1706         }
    1707 
    1708         tr_free( str );
    17091725    }
    17101726    else
  • trunk/libtransmission/handshake.c

    r10912 r11548  
    1818#include <stdio.h>
    1919
    20 #include <event.h>
     20#include <event2/event.h>
    2121
    2222#include "transmission.h"
     
    121121    handshakeDoneCB       doneCB;
    122122    void *                doneUserData;
    123     struct event          timeout_timer;
     123    struct event        * timeout_timer;
    124124};
    125125
     
    266266
    267267    dbgmsg( handshake, "payload: need %d, got %zu",
    268             (int)HANDSHAKE_SIZE, EVBUFFER_LENGTH( inbuf ) );
    269 
    270     if( EVBUFFER_LENGTH( inbuf ) < HANDSHAKE_SIZE )
     268            (int)HANDSHAKE_SIZE, evbuffer_get_length( inbuf ) );
     269
     270    if( evbuffer_get_length( inbuf ) < HANDSHAKE_SIZE )
    271271        return READ_LATER;
    272272
     
    347347    /* send it */
    348348    setReadState( handshake, AWAITING_YB );
    349     tr_peerIoWrite( handshake->io, outbuf, walk - outbuf, FALSE );
     349    tr_peerIoWriteBytes( handshake->io, outbuf, walk - outbuf, FALSE );
    350350}
    351351
     
    402402
    403403static int
    404 readYb( tr_handshake *    handshake,
    405         struct evbuffer * inbuf )
     404readYb( tr_handshake * handshake, struct evbuffer * inbuf )
    406405{
    407406    int               isEncrypted;
     
    411410    size_t            needlen = HANDSHAKE_NAME_LEN;
    412411
    413     if( EVBUFFER_LENGTH( inbuf ) < needlen )
     412    if( evbuffer_get_length( inbuf ) < needlen )
    414413        return READ_LATER;
    415414
    416     isEncrypted = memcmp( EVBUFFER_DATA( inbuf ), HANDSHAKE_NAME, HANDSHAKE_NAME_LEN );
     415    isEncrypted = memcmp( evbuffer_pullup( inbuf, HANDSHAKE_NAME_LEN ), HANDSHAKE_NAME, HANDSHAKE_NAME_LEN );
    417416    if( isEncrypted )
    418417    {
    419418        needlen = KEY_LEN;
    420         if( EVBUFFER_LENGTH( inbuf ) < needlen )
     419        if( evbuffer_get_length( inbuf ) < needlen )
    421420            return READ_LATER;
    422421    }
     
    476475        tr_peerIoSetEncryption( handshake->io, PEER_ENCRYPTION_RC4 );
    477476
    478         tr_peerIoWriteBytes( handshake->io, outbuf, vc, VC_LENGTH );
    479         tr_peerIoWriteUint32( handshake->io, outbuf,
    480                              getCryptoProvide( handshake ) );
    481         tr_peerIoWriteUint16( handshake->io, outbuf, 0 );
     477        evbuffer_add        ( outbuf, vc, VC_LENGTH );
     478        evbuffer_add_uint32 ( outbuf, getCryptoProvide( handshake ) );
     479        evbuffer_add_uint16 ( outbuf, 0 );
    482480    }
    483481
     
    487485        buildHandshakeMessage( handshake, msg );
    488486
    489         tr_peerIoWriteUint16( handshake->io, outbuf, sizeof( msg ) );
    490         tr_peerIoWriteBytes( handshake->io, outbuf, msg, sizeof( msg ) );
     487        evbuffer_add_uint16 ( outbuf, sizeof( msg ) );
     488        evbuffer_add        ( outbuf, msg, sizeof( msg ) );
    491489
    492490        handshake->haveSentBitTorrentHandshake = 1;
     
    516514    for( ; ; )
    517515    {
    518         if( EVBUFFER_LENGTH( inbuf ) < VC_LENGTH )
     516        if( evbuffer_get_length( inbuf ) < VC_LENGTH )
    519517        {
    520518            dbgmsg( handshake, "not enough bytes... returning read_more" );
     
    522520        }
    523521
    524         memcpy( tmp, EVBUFFER_DATA( inbuf ), key_len );
     522        memcpy( tmp, evbuffer_pullup( inbuf, key_len ), key_len );
    525523        tr_cryptoDecryptInit( handshake->crypto );
    526524        tr_cryptoDecrypt( handshake->crypto, key_len, tmp, tmp );
     
    545543    const size_t needlen = sizeof( uint32_t ) + sizeof( uint16_t );
    546544
    547     if( EVBUFFER_LENGTH( inbuf ) < needlen )
     545    if( evbuffer_get_length( inbuf ) < needlen )
    548546        return READ_LATER;
    549547
     
    581579
    582580    dbgmsg( handshake, "pad d: need %zu, got %zu",
    583             needlen, EVBUFFER_LENGTH( inbuf ) );
    584     if( EVBUFFER_LENGTH( inbuf ) < needlen )
     581            needlen, evbuffer_get_length( inbuf ) );
     582    if( evbuffer_get_length( inbuf ) < needlen )
    585583        return READ_LATER;
    586584
     
    611609
    612610    dbgmsg( handshake, "payload: need %d, got %zu",
    613             (int)INCOMING_HANDSHAKE_LEN, EVBUFFER_LENGTH( inbuf ) );
    614 
    615     if( EVBUFFER_LENGTH( inbuf ) < INCOMING_HANDSHAKE_LEN )
     611            (int)INCOMING_HANDSHAKE_LEN, evbuffer_get_length( inbuf ) );
     612
     613    if( evbuffer_get_length( inbuf ) < INCOMING_HANDSHAKE_LEN )
    616614        return READ_LATER;
    617615
    618616    handshake->haveReadAnythingFromPeer = TRUE;
    619617
    620     pstrlen = EVBUFFER_DATA( inbuf )[0]; /* peek, don't read.  We may be
    621                                           handing inbuf to AWAITING_YA */
     618    pstrlen = evbuffer_pullup( inbuf, 1 )[0]; /* peek, don't read.  We may be
     619                                                 handing inbuf to AWAITING_YA */
    622620
    623621    if( pstrlen == 19 ) /* unencrypted */
     
    713711        uint8_t msg[HANDSHAKE_SIZE];
    714712        buildHandshakeMessage( handshake, msg );
    715         tr_peerIoWrite( handshake->io, msg, sizeof( msg ), FALSE );
     713        tr_peerIoWriteBytes( handshake->io, msg, sizeof( msg ), FALSE );
    716714        handshake->haveSentBitTorrentHandshake = 1;
    717715    }
     
    731729    uint8_t peer_id[PEER_ID_LEN];
    732730
    733     if( EVBUFFER_LENGTH( inbuf ) < PEER_ID_LEN )
     731    if( evbuffer_get_length( inbuf ) < PEER_ID_LEN )
    734732        return READ_LATER;
    735733
     
    760758
    761759    dbgmsg( handshake, "in readYa... need %d, have %zu",
    762             (int)KEY_LEN, EVBUFFER_LENGTH( inbuf ) );
    763     if( EVBUFFER_LENGTH( inbuf ) < KEY_LEN )
     760            (int)KEY_LEN, evbuffer_get_length( inbuf ) );
     761    if( evbuffer_get_length( inbuf ) < KEY_LEN )
    764762        return READ_LATER;
    765763
     
    781779
    782780    setReadState( handshake, AWAITING_PAD_A );
    783     tr_peerIoWrite( handshake->io, outbuf, walk - outbuf, FALSE );
     781    tr_peerIoWriteBytes( handshake->io, outbuf, walk - outbuf, FALSE );
    784782    return READ_NOW;
    785783}
    786784
    787785static int
    788 readPadA( tr_handshake *    handshake,
    789           struct evbuffer * inbuf )
    790 {
    791     uint8_t * pch;
    792 
    793     dbgmsg( handshake, "looking to get past pad a... & resync on hash('req',S) ... have %zu bytes",
    794             EVBUFFER_LENGTH( inbuf ) );
    795     /**
    796     *** Resynchronizing on HASH('req1',S)
    797     **/
    798 
    799     pch = memchr( EVBUFFER_DATA( inbuf ),
    800                  handshake->myReq1[0],
    801                  EVBUFFER_LENGTH( inbuf ) );
    802     if( pch == NULL )
    803     {
    804         dbgmsg( handshake, "no luck so far.. draining %zu bytes",
    805                 EVBUFFER_LENGTH( inbuf ) );
    806         evbuffer_drain( inbuf, EVBUFFER_LENGTH( inbuf ) );
     786readPadA( tr_handshake * handshake, struct evbuffer * inbuf )
     787{
     788    /* resynchronizing on HASH('req1',S) */
     789    struct evbuffer_ptr ptr = evbuffer_search( inbuf, (const char*)handshake->myReq1, SHA_DIGEST_LENGTH, NULL );
     790
     791    if( ptr.pos != -1 ) /* match */
     792    {
     793        evbuffer_drain( inbuf, ptr.pos );
     794        dbgmsg( handshake, "found it... looking setting to awaiting_crypto_provide" );
     795        setState( handshake, AWAITING_CRYPTO_PROVIDE );
     796        return READ_NOW;
     797    }
     798    else
     799    {
     800        const size_t len = evbuffer_get_length( inbuf );
     801        if( len > SHA_DIGEST_LENGTH )
     802            evbuffer_drain( inbuf, len - SHA_DIGEST_LENGTH );
    807803        return READ_LATER;
    808804    }
    809     dbgmsg( handshake, "looking for hash('req',S) ... draining %d bytes",
    810            (int)( pch - EVBUFFER_DATA( inbuf ) ) );
    811     evbuffer_drain( inbuf, pch - EVBUFFER_DATA( inbuf ) );
    812     if( EVBUFFER_LENGTH( inbuf ) < SHA_DIGEST_LENGTH )
    813         return READ_LATER;
    814     if( memcmp( EVBUFFER_DATA( inbuf ), handshake->myReq1,
    815                 SHA_DIGEST_LENGTH ) )
    816     {
    817         dbgmsg( handshake, "draining one more byte" );
    818         evbuffer_drain( inbuf, 1 );
    819         return READ_NOW;
    820     }
    821 
    822     dbgmsg( handshake,
    823             "found it... looking setting to awaiting_crypto_provide" );
    824     setState( handshake, AWAITING_CRYPTO_PROVIDE );
    825     return READ_NOW;
    826805}
    827806
     
    848827    tr_torrent * tor = NULL;
    849828
    850     if( EVBUFFER_LENGTH( inbuf ) < needlen )
     829    if( evbuffer_get_length( inbuf ) < needlen )
    851830        return READ_LATER;
    852831
     
    906885    const size_t needlen = handshake->pad_c_len + sizeof( uint16_t );
    907886
    908     if( EVBUFFER_LENGTH( inbuf ) < needlen )
     887    if( evbuffer_get_length( inbuf ) < needlen )
    909888        return READ_LATER;
    910889
     
    927906
    928907    dbgmsg( handshake, "reading IA... have %zu, need %zu",
    929             EVBUFFER_LENGTH( inbuf ), needlen );
    930     if( EVBUFFER_LENGTH( inbuf ) < needlen )
     908            evbuffer_get_length( inbuf ), needlen );
     909    if( evbuffer_get_length( inbuf ) < needlen )
    931910        return READ_LATER;
    932911
     
    943922        uint8_t vc[VC_LENGTH];
    944923        memset( vc, 0, VC_LENGTH );
    945         tr_peerIoWriteBytes( handshake->io, outbuf, vc, VC_LENGTH );
     924        evbuffer_add( outbuf, vc, VC_LENGTH );
    946925    }
    947926
     
    951930    {
    952931        dbgmsg( handshake, "selecting crypto mode '%d'", (int)crypto_select );
    953         tr_peerIoWriteUint32( handshake->io, outbuf, crypto_select );
     932        evbuffer_add_uint32( outbuf, crypto_select );
    954933    }
    955934    else
     
    966945    {
    967946        const uint16_t len = 0;
    968         tr_peerIoWriteUint16( handshake->io, outbuf, len );
     947        evbuffer_add_uint16( outbuf, len );
    969948    }
    970949
     
    982961        buildHandshakeMessage( handshake, msg );
    983962
    984         tr_peerIoWriteBytes( handshake->io, outbuf, msg, sizeof( msg ) );
     963        evbuffer_add( outbuf, msg, sizeof( msg ) );
    985964        handshake->haveSentBitTorrentHandshake = 1;
    986965    }
     
    1003982
    1004983    dbgmsg( handshake, "reading payload stream... have %zu, need %zu",
    1005             EVBUFFER_LENGTH( inbuf ), needlen );
    1006     if( EVBUFFER_LENGTH( inbuf ) < needlen )
     984            evbuffer_get_length( inbuf ), needlen );
     985    if( evbuffer_get_length( inbuf ) < needlen )
    1007986        return READ_LATER;
    1008987
     
    10861065            readyForMore = FALSE;
    10871066        else if( handshake->state == AWAITING_PAD_C )
    1088             readyForMore = EVBUFFER_LENGTH( inbuf ) >= handshake->pad_c_len;
     1067            readyForMore = evbuffer_get_length( inbuf ) >= handshake->pad_c_len;
    10891068        else if( handshake->state == AWAITING_PAD_D )
    1090             readyForMore = EVBUFFER_LENGTH( inbuf ) >= handshake->pad_d_len;
     1069            readyForMore = evbuffer_get_length( inbuf ) >= handshake->pad_d_len;
    10911070        else if( handshake->state == AWAITING_IA )
    1092             readyForMore = EVBUFFER_LENGTH( inbuf ) >= handshake->ia_len;
     1071            readyForMore = evbuffer_get_length( inbuf ) >= handshake->ia_len;
    10931072    }
    10941073
     
    11181097        tr_peerIoUnref( handshake->io ); /* balanced by the ref in tr_handshakeNew */
    11191098
    1120     evtimer_del( &handshake->timeout_timer );
    1121 
     1099    event_free( handshake->timeout_timer );
    11221100    tr_free( handshake );
    11231101}
     
    11671145        handshake->haveSentBitTorrentHandshake = 1;
    11681146        setReadState( handshake, AWAITING_HANDSHAKE );
    1169         tr_peerIoWrite( handshake->io, msg, sizeof( msg ), FALSE );
     1147        tr_peerIoWriteBytes( handshake->io, msg, sizeof( msg ), FALSE );
    11701148    }
    11711149    else
     
    12021180    handshake->doneUserData = doneUserData;
    12031181    handshake->session = tr_peerIoGetSession( io );
    1204 
    1205     evtimer_set( &handshake->timeout_timer, handshakeTimeout, handshake );
    1206     tr_timerAdd( &handshake->timeout_timer, HANDSHAKE_TIMEOUT_SEC, 0 );
     1182    handshake->timeout_timer = evtimer_new( NULL, handshakeTimeout, handshake );
     1183    tr_timerAdd( handshake->timeout_timer, HANDSHAKE_TIMEOUT_SEC, 0 );
    12071184
    12081185    tr_peerIoRef( io ); /* balanced by the unref in tr_handshakeFree */
     
    12211198        handshake->haveSentBitTorrentHandshake = 1;
    12221199        setReadState( handshake, AWAITING_HANDSHAKE );
    1223         tr_peerIoWrite( handshake->io, msg, sizeof( msg ), FALSE );
     1200        tr_peerIoWriteBytes( handshake->io, msg, sizeof( msg ), FALSE );
    12241201    }
    12251202
  • trunk/libtransmission/metainfo.c

    r11398 r11548  
    2020#include <unistd.h> /* unlink, stat */
    2121
    22 #include <event.h> /* struct evbuffer */
     22#include <event2/buffer.h>
    2323
    2424#include "transmission.h"
     
    155155    {
    156156        int i;
     157        char * tmp;
    157158        const int n = tr_bencListSize( path );
    158159        struct evbuffer * buf = evbuffer_new( );
     
    169170        }
    170171
    171         *setme = tr_utf8clean( (char*)EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
     172        tmp = evbuffer_free_to_str( buf );
     173        *setme = tr_utf8clean( tmp, -1 );
     174        tr_free( tmp );
    172175        /* fprintf( stderr, "[%s]\n", *setme ); */
    173         evbuffer_free( buf );
    174176        success = TRUE;
    175177    }
  • trunk/libtransmission/peer-io.c

    r11398 r11548  
    2424#endif
    2525
    26 #include <event.h>
     26#include <event2/event.h>
     27#include <event2/bufferevent.h>
    2728
    2829#include "transmission.h"
     
    144145        {
    145146            size_t piece = 0;
    146             const size_t oldLen = EVBUFFER_LENGTH( io->inbuf );
     147            const size_t oldLen = evbuffer_get_length( io->inbuf );
    147148            const int ret = io->canRead( io, io->userData, &piece );
    148149
    149             const size_t used = oldLen - EVBUFFER_LENGTH( io->inbuf );
     150            const size_t used = oldLen - evbuffer_get_length( io->inbuf );
    150151
    151152            assert( tr_isPeerIo( io ) );
     
    165166            {
    166167                case READ_NOW:
    167                     if( EVBUFFER_LENGTH( io->inbuf ) )
     168                    if( evbuffer_get_length( io->inbuf ) )
    168169                        continue;
    169170                    done = 1;
     
    183184
    184185        tr_sessionUnlock( session );
    185     }
    186 
    187     /* keep the iobuf's excess capacity from growing too large */
    188     if( EVBUFFER_LENGTH( io->inbuf ) == 0 ) {
    189         evbuffer_free( io->inbuf );
    190         io->inbuf = evbuffer_new( );
    191186    }
    192187
     
    223218    io->pendingEvents &= ~EV_READ;
    224219
    225     curlen = EVBUFFER_LENGTH( io->inbuf );
     220    curlen = evbuffer_get_length( io->inbuf );
    226221    howmuch = curlen >= max ? 0 : max - curlen;
    227222    howmuch = tr_bandwidthClamp( &io->bandwidth, TR_DOWN, howmuch );
     
    249244    {
    250245        char errstr[512];
    251         short what = EVBUFFER_READ;
     246        short what = BEV_EVENT_READING;
    252247
    253248        if( res == 0 ) /* EOF */
    254             what |= EVBUFFER_EOF;
     249            what |= BEV_EVENT_EOF;
    255250        else if( res == -1 ) {
    256251            if( e == EAGAIN || e == EINTR ) {
     
    258253                return;
    259254            }
    260             what |= EVBUFFER_ERROR;
     255            what |= BEV_EVENT_ERROR;
    261256        }
    262257
     
    275270    int n;
    276271    char errstr[256];
    277     struct evbuffer * buffer = io->outbuf;
    278 
    279     howmuch = MIN( EVBUFFER_LENGTH( buffer ), howmuch );
     272    const size_t len = evbuffer_get_length( io->outbuf );
     273    const int vecCount = evbuffer_peek( io->outbuf, len, NULL, NULL, 0 );
    280274
    281275    EVUTIL_SET_SOCKET_ERROR( 0 );
    282 #ifdef WIN32
    283     n = (int) send(fd, buffer->buffer, howmuch,  0 );
    284 #else
    285     n = (int) write(fd, buffer->buffer, howmuch );
    286 #endif
     276    n = evbuffer_write_atmost( io->outbuf, fd, howmuch );
    287277    e = EVUTIL_SOCKET_ERROR( );
    288278    dbgmsg( io, "wrote %d to peer (%s)", n, (n==-1?tr_net_strerror(errstr,sizeof(errstr),e):"") );
    289279
    290     if( n > 0 )
    291         evbuffer_drain( buffer, (size_t)n );
    292 
    293     /* keep the iobuf's excess capacity from growing too large */
    294     if( EVBUFFER_LENGTH( io->outbuf ) == 0 ) {
    295         evbuffer_free( io->outbuf );
    296         io->outbuf = evbuffer_new( );
    297     }
    298 
    299280    return n;
    300281}
     
    305286    int res = 0;
    306287    int e;
    307     short what = EVBUFFER_WRITE;
     288    short what = BEV_EVENT_WRITING;
    308289    tr_peerIo * io = vio;
    309290    size_t howmuch;
     
    319300    /* Write as much as possible, since the socket is non-blocking, write() will
    320301     * return if it can't write any more data without blocking */
    321     howmuch = tr_bandwidthClamp( &io->bandwidth, dir, EVBUFFER_LENGTH( io->outbuf ) );
     302    howmuch = tr_bandwidthClamp( &io->bandwidth, dir, evbuffer_get_length( io->outbuf ) );
    322303
    323304    /* if we don't have any bandwidth left, stop writing */
     
    332313
    333314    if (res == -1) {
    334         if (e == EAGAIN || e == EINTR || e == EINPROGRESS)
     315        if (!e || e == EAGAIN || e == EINTR || e == EINPROGRESS)
    335316            goto reschedule;
    336317        /* error case */
    337         what |= EVBUFFER_ERROR;
     318        what |= BEV_EVENT_ERROR;
    338319    } else if (res == 0) {
    339320        /* eof case */
    340         what |= EVBUFFER_EOF;
     321        what |= BEV_EVENT_EOF;
    341322    }
    342323    if (res <= 0)
    343324        goto error;
    344325
    345     if( EVBUFFER_LENGTH( io->outbuf ) )
     326    if( evbuffer_get_length( io->outbuf ) )
    346327        tr_peerIoSetEnabled( io, dir, TRUE );
    347328
     
    350331
    351332 reschedule:
    352     if( EVBUFFER_LENGTH( io->outbuf ) )
     333    if( evbuffer_get_length( io->outbuf ) )
    353334        tr_peerIoSetEnabled( io, dir, TRUE );
    354335    return;
     
    416397    io->inbuf = evbuffer_new( );
    417398    io->outbuf = evbuffer_new( );
     399    io->event_read = event_new( NULL, io->socket, EV_READ, event_read_cb, io );
     400    io->event_write = event_new( NULL, io->socket, EV_WRITE, event_write_cb, io );
    418401    tr_bandwidthConstruct( &io->bandwidth, session, parent );
    419402    tr_bandwidthSetPeer( &io->bandwidth, io );
    420403    dbgmsg( io, "bandwidth is %p; its parent is %p", &io->bandwidth, parent );
    421 
    422     event_set( &io->event_read, io->socket, EV_READ, event_read_cb, io );
    423     event_set( &io->event_write, io->socket, EV_WRITE, event_write_cb, io );
    424404
    425405    return io;
     
    471451    assert( io->session != NULL );
    472452    assert( io->session->events != NULL );
    473     assert( event_initialized( &io->event_read ) );
    474     assert( event_initialized( &io->event_write ) );
     453    assert( event_initialized( io->event_read ) );
     454    assert( event_initialized( io->event_write ) );
    475455
    476456    if( io->socket < 0 )
     
    480460    {
    481461        dbgmsg( io, "enabling libevent ready-to-read polling" );
    482         event_add( &io->event_read, NULL );
     462        event_add( io->event_read, NULL );
    483463        io->pendingEvents |= EV_READ;
    484464    }
     
    487467    {
    488468        dbgmsg( io, "enabling libevent ready-to-write polling" );
    489         event_add( &io->event_write, NULL );
     469        event_add( io->event_write, NULL );
    490470        io->pendingEvents |= EV_WRITE;
    491471    }
     
    498478    assert( io->session != NULL );
    499479    assert( io->session->events != NULL );
    500     assert( event_initialized( &io->event_read ) );
    501     assert( event_initialized( &io->event_write ) );
     480    assert( event_initialized( io->event_read ) );
     481    assert( event_initialized( io->event_write ) );
    502482
    503483    if( ( event & EV_READ ) && ( io->pendingEvents & EV_READ ) )
    504484    {
    505485        dbgmsg( io, "disabling libevent ready-to-read polling" );
    506         event_del( &io->event_read );
     486        event_del( io->event_read );
    507487        io->pendingEvents &= ~EV_READ;
    508488    }
     
    511491    {
    512492        dbgmsg( io, "disabling libevent ready-to-write polling" );
    513         event_del( &io->event_write );
     493        event_del( io->event_write );
    514494        io->pendingEvents &= ~EV_WRITE;
    515495    }
     
    549529    dbgmsg( io, "in tr_peerIo destructor" );
    550530    event_disable( io, EV_READ | EV_WRITE );
     531    event_free( io->event_read );
     532    event_free( io->event_write );
    551533    tr_bandwidthDestruct( &io->bandwidth );
    552534    evbuffer_free( io->outbuf );
     
    662644        tr_netClose( session, io->socket );
    663645
     646    event_del( io->event_read );
     647    event_del( io->event_write );
    664648    io->socket = tr_netOpenPeerSocket( session, &io->addr, io->port, io->isSeed );
    665     event_set( &io->event_read, io->socket, EV_READ, event_read_cb, io );
    666     event_set( &io->event_write, io->socket, EV_WRITE, event_write_cb, io );
     649    io->event_read = event_new( NULL, io->socket, EV_READ, event_read_cb, io );
     650    io->event_write = event_new( NULL, io->socket, EV_WRITE, event_write_cb, io );
    667651
    668652    if( io->socket >= 0 )
     
    746730{
    747731    const size_t desiredLen = getDesiredOutputBufferSize( io, now );
    748     const size_t currentLen = EVBUFFER_LENGTH( io->outbuf );
     732    const size_t currentLen = evbuffer_get_length( io->outbuf );
    749733    size_t freeSpace = 0;
    750734
     
    773757**/
    774758
    775 void
    776 tr_peerIoWrite( tr_peerIo   * io,
    777                 const void  * bytes,
    778                 size_t        byteCount,
    779                 tr_bool       isPieceData )
    780 {
    781     /* FIXME(libevent2): this implementation snould be moved to tr_peerIoWriteBuf.   This function should be implemented as evbuffer_new() + evbuffer_add_reference() + a call to tr_peerIoWriteBuf() + evbuffer_free() */
    782     struct tr_datatype * datatype;
    783 
    784     assert( tr_amInEventThread( io->session ) );
    785     dbgmsg( io, "adding %zu bytes into io->output", byteCount );
    786 
    787     datatype = tr_new( struct tr_datatype, 1 );
    788     datatype->isPieceData = isPieceData != 0;
    789     datatype->length = byteCount;
    790     tr_list_append( &io->outbuf_datatypes, datatype );
    791 
    792     switch( io->encryptionMode )
    793     {
    794         case PEER_ENCRYPTION_RC4:
    795         {
    796             /* FIXME(libevent2): use evbuffer_reserve_space() and evbuffer_commit_space() instead of tmp */
    797             void * tmp = tr_sessionGetBuffer( io->session );
    798             const size_t tmplen = SESSION_BUFFER_SIZE;
    799             const uint8_t * walk = bytes;
    800             evbuffer_expand( io->outbuf, byteCount );
    801             while( byteCount > 0 )
    802             {
    803                 const size_t thisPass = MIN( byteCount, tmplen );
    804                 tr_cryptoEncrypt( io->crypto, thisPass, walk, tmp );
    805                 evbuffer_add( io->outbuf, tmp, thisPass );
    806                 walk += thisPass;
    807                 byteCount -= thisPass;
    808             }
    809             tr_sessionReleaseBuffer( io->session );
    810             break;
    811         }
    812 
    813         case PEER_ENCRYPTION_NONE:
    814             evbuffer_add( io->outbuf, bytes, byteCount );
    815             break;
    816 
    817         default:
    818             assert( 0 );
    819             break;
    820     }
    821 }
    822 
    823 void
    824 tr_peerIoWriteBuf( tr_peerIo         * io,
    825                    struct evbuffer   * buf,
    826                    tr_bool             isPieceData )
    827 {
    828     /* FIXME(libevent2): loop through calls to evbuffer_get_contiguous_space() + evbuffer_drain() */
    829     const size_t n = EVBUFFER_LENGTH( buf );
    830     tr_peerIoWrite( io, EVBUFFER_DATA( buf ), n, isPieceData );
    831     evbuffer_drain( buf, n );
    832 }
    833 
    834 void
    835 tr_peerIoWriteUint16( tr_peerIo        * io,
    836                       struct evbuffer  * outbuf,
    837                       uint16_t           writeme )
    838 {
    839     const uint16_t tmp = htons( writeme );
    840     tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint16_t ) );
    841 }
    842 
    843 void
    844 tr_peerIoWriteUint32( tr_peerIo        * io,
    845                       struct evbuffer  * outbuf,
    846                       uint32_t           writeme )
    847 {
    848     const uint32_t tmp = htonl( writeme );
    849     tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint32_t ) );
     759static void
     760addDatatype( tr_peerIo * io, size_t byteCount, tr_bool isPieceData )
     761{
     762    struct tr_datatype * d;
     763
     764    d = tr_new( struct tr_datatype, 1 );
     765    d->isPieceData = isPieceData != 0;
     766    d->length = byteCount;
     767    tr_list_append( &io->outbuf_datatypes, d );
     768}
     769
     770static struct evbuffer_iovec *
     771evbuffer_peek_all( struct evbuffer * buf, size_t * setme_vecCount )
     772{
     773    const size_t byteCount = evbuffer_get_length( buf );
     774    const int vecCount = evbuffer_peek( buf, byteCount, NULL, NULL, 0 );
     775    struct evbuffer_iovec * iovec = tr_new0( struct evbuffer_iovec, vecCount );
     776    const int n = evbuffer_peek( buf, byteCount, NULL, iovec, vecCount );
     777    assert( vecCount == n );
     778    *setme_vecCount = vecCount;
     779    return iovec;
     780}
     781
     782static void
     783maybeEncryptBuffer( tr_peerIo * io, struct evbuffer * buf )
     784{
     785    if( io->encryptionMode == PEER_ENCRYPTION_RC4 )
     786    {
     787        size_t i, n;
     788        struct evbuffer_iovec * iovec = evbuffer_peek_all( buf, &n );
     789
     790        for( i=0; i<n; ++i )
     791            tr_cryptoEncrypt( io->crypto, iovec[i].iov_len, iovec[i].iov_base, iovec[i].iov_base );
     792
     793        tr_free( iovec );
     794    }
     795}
     796
     797void
     798tr_peerIoWriteBuf( tr_peerIo * io, struct evbuffer * buf, tr_bool isPieceData )
     799{
     800    const size_t byteCount = evbuffer_get_length( buf );
     801    maybeEncryptBuffer( io, buf );
     802    evbuffer_add_buffer( io->outbuf, buf );
     803    addDatatype( io, byteCount, isPieceData );
     804}
     805
     806void
     807tr_peerIoWriteBytes( tr_peerIo * io, const void * bytes, size_t byteCount, tr_bool isPieceData )
     808{
     809    struct evbuffer * buf = evbuffer_new( );
     810    evbuffer_add( buf, bytes, byteCount );
     811    tr_peerIoWriteBuf( io, buf, isPieceData );
     812    evbuffer_free( buf );
    850813}
    851814
     
    855818
    856819void
    857 tr_peerIoReadBytes( tr_peerIo       * io,
    858                     struct evbuffer * inbuf,
    859                     void            * bytes,
    860                     size_t            byteCount )
    861 {
    862     assert( tr_isPeerIo( io ) );
    863     /* FIXME(libevent2): use evbuffer_get_length() */
    864     assert( EVBUFFER_LENGTH( inbuf ) >= byteCount );
     820evbuffer_add_uint16( struct evbuffer * outbuf, uint16_t addme_hs )
     821{
     822    const uint16_t ns = htons( addme_hs );
     823    evbuffer_add( outbuf, &ns, sizeof( ns ) );
     824}
     825
     826void
     827evbuffer_add_uint32( struct evbuffer * outbuf, uint32_t addme_hl )
     828{
     829    const uint32_t nl = htonl( addme_hl );
     830    evbuffer_add( outbuf, &nl, sizeof( nl ) );
     831}
     832
     833/***
     834****
     835***/
     836
     837void
     838tr_peerIoReadBytes( tr_peerIo * io, struct evbuffer * inbuf, void * bytes, size_t byteCount )
     839{
     840    assert( tr_isPeerIo( io ) );
     841    assert( evbuffer_get_length( inbuf )  >= byteCount );
    865842
    866843    switch( io->encryptionMode )
     
    871848
    872849        case PEER_ENCRYPTION_RC4:
    873             /* FIXME(libevent2): loop through calls to evbuffer_get_contiguous_space() + evbuffer_drain() */
    874             tr_cryptoDecrypt( io->crypto, byteCount, EVBUFFER_DATA(inbuf), bytes );
    875             evbuffer_drain(inbuf, byteCount );
     850            evbuffer_remove( inbuf, bytes, byteCount );
     851            tr_cryptoDecrypt( io->crypto, byteCount, bytes, bytes );
    876852            break;
    877853
     
    937913        dbgmsg( io, "read %d from peer (%s)", res, (res==-1?strerror(e):"") );
    938914
    939         if( EVBUFFER_LENGTH( io->inbuf ) )
     915        if( evbuffer_get_length( io->inbuf ) )
    940916            canReadWrapper( io );
    941917
     
    943919        {
    944920            char errstr[512];
    945             short what = EVBUFFER_READ | EVBUFFER_ERROR;
     921            short what = BEV_EVENT_READING | BEV_EVENT_ERROR;
    946922            if( res == 0 )
    947                 what |= EVBUFFER_EOF;
     923                what |= BEV_EVENT_EOF;
    948924            tr_net_strerror( errstr, sizeof( errstr ), e );
    949925            dbgmsg( io, "tr_peerIoTryRead got an error. res is %d, what is %hd, errno is %d (%s)", res, what, e, errstr );
     
    970946            didWriteWrapper( io, n );
    971947
    972         if( ( n < 0 ) && ( io->gotError ) && ( e != EPIPE ) && ( e != EAGAIN ) && ( e != EINTR ) && ( e != EINPROGRESS ) )
     948        if( ( n < 0 ) && ( io->gotError ) && e && ( e != EPIPE ) && ( e != EAGAIN ) && ( e != EINTR ) && ( e != EINPROGRESS ) )
    973949        {
    974950            char errstr[512];
    975             const short what = EVBUFFER_WRITE | EVBUFFER_ERROR;
     951            const short what = BEV_EVENT_WRITING | BEV_EVENT_ERROR;
    976952
    977953            tr_net_strerror( errstr, sizeof( errstr ), e );
  • trunk/libtransmission/peer-io.h

    r11299 r11548  
    2424#include <assert.h>
    2525
    26 #include <event.h>
     26#include <event2/buffer.h>
     27#include <event2/event.h>
    2728
    2829#include "transmission.h"
     
    110111    struct tr_list      * outbuf_datatypes; /* struct tr_datatype */
    111112
    112     struct event          event_read;
    113     struct event          event_write;
     113    struct event        * event_read;
     114    struct event        * event_write;
    114115}
    115116tr_peerIo;
     
    249250**/
    250251
    251 void    tr_peerIoWrite          ( tr_peerIo         * io,
     252void    tr_peerIoWriteBytes     ( tr_peerIo         * io,
    252253                                  const void        * writeme,
    253254                                  size_t              writemeLen,
     
    283284}
    284285
    285 static inline void tr_peerIoWriteBytes( tr_peerIo        * io UNUSED,
    286                                            struct evbuffer  * outbuf,
    287                                            const void       * bytes,
    288                                            size_t             byteCount )
    289 {
    290     evbuffer_add( outbuf, bytes, byteCount );
    291 }
    292 
    293 static inline void  tr_peerIoWriteUint8( tr_peerIo        * io,
    294                                             struct evbuffer  * outbuf,
    295                                             uint8_t            writeme )
    296 {
    297     tr_peerIoWriteBytes( io, outbuf, &writeme, sizeof( uint8_t ) );
    298 }
    299 
    300 void tr_peerIoWriteUint16( tr_peerIo        * io,
    301                            struct evbuffer  * outbuf,
    302                            uint16_t           writeme );
    303 
    304 void tr_peerIoWriteUint32( tr_peerIo        * io,
    305                            struct evbuffer  * outbuf,
    306                            uint32_t           writeme );
     286static inline void
     287evbuffer_add_uint8( struct evbuffer * outbuf, uint8_t byte )
     288{
     289    evbuffer_add( outbuf, &byte, 1 );
     290}
     291
     292void evbuffer_add_uint16( struct evbuffer * outbuf, uint16_t hs );
     293void evbuffer_add_uint32( struct evbuffer * outbuf, uint32_t hl );
    307294
    308295void tr_peerIoReadBytes( tr_peerIo        * io,
  • trunk/libtransmission/peer-msgs.c

    r11509 r11548  
    1919#include <string.h>
    2020
    21 #include <event.h>
     21#include <event2/bufferevent.h>
     22#include <event2/event.h>
    2223
    2324#include "transmission.h"
     
    228229    int64_t               reqq;
    229230
    230     struct event          pexTimer;
     231    struct event        * pexTimer;
    231232};
    232233
     
    278279        va_end( args );
    279280        evbuffer_add_printf( buf, " (%s:%d)\n", base, line );
    280         /* FIXME(libevent2) tr_getLog() should return an fd, then use evbuffer_write() here */
    281         fwrite( EVBUFFER_DATA( buf ), 1, EVBUFFER_LENGTH( buf ), fp );
     281        fwrite( evbuffer_pullup( buf, -1 ), 1, evbuffer_get_length( buf ), fp );
    282282
    283283        tr_free( base );
     
    310310dbgOutMessageLen( tr_peermsgs * msgs )
    311311{
    312     dbgmsg( msgs, "outMessage size is now %zu", EVBUFFER_LENGTH( msgs->outMessages ) );
     312    dbgmsg( msgs, "outMessage size is now %zu", evbuffer_get_length( msgs->outMessages ) );
    313313}
    314314
     
    316316protocolSendReject( tr_peermsgs * msgs, const struct peer_request * req )
    317317{
    318     tr_peerIo       * io  = msgs->peer->io;
    319318    struct evbuffer * out = msgs->outMessages;
    320319
    321320    assert( tr_peerIoSupportsFEXT( msgs->peer->io ) );
    322321
    323     tr_peerIoWriteUint32( io, out, sizeof( uint8_t ) + 3 * sizeof( uint32_t ) );
    324     tr_peerIoWriteUint8 ( io, out, BT_FEXT_REJECT );
    325     tr_peerIoWriteUint32( io, out, req->index );
    326     tr_peerIoWriteUint32( io, out, req->offset );
    327     tr_peerIoWriteUint32( io, out, req->length );
     322    evbuffer_add_uint32( out, sizeof( uint8_t ) + 3 * sizeof( uint32_t ) );
     323    evbuffer_add_uint8 ( out, BT_FEXT_REJECT );
     324    evbuffer_add_uint32( out, req->index );
     325    evbuffer_add_uint32( out, req->offset );
     326    evbuffer_add_uint32( out, req->length );
    328327
    329328    dbgmsg( msgs, "rejecting %u:%u->%u...", req->index, req->offset, req->length );
     
    332331
    333332static void
    334 protocolSendRequest( tr_peermsgs               * msgs,
    335                      const struct peer_request * req )
    336 {
    337     tr_peerIo       * io  = msgs->peer->io;
     333protocolSendRequest( tr_peermsgs * msgs, const struct peer_request * req )
     334{
    338335    struct evbuffer * out = msgs->outMessages;
    339336
    340     tr_peerIoWriteUint32( io, out, sizeof( uint8_t ) + 3 * sizeof( uint32_t ) );
    341     tr_peerIoWriteUint8 ( io, out, BT_REQUEST );
    342     tr_peerIoWriteUint32( io, out, req->index );
    343     tr_peerIoWriteUint32( io, out, req->offset );
    344     tr_peerIoWriteUint32( io, out, req->length );
     337    evbuffer_add_uint32( out, sizeof( uint8_t ) + 3 * sizeof( uint32_t ) );
     338    evbuffer_add_uint8 ( out, BT_REQUEST );
     339    evbuffer_add_uint32( out, req->index );
     340    evbuffer_add_uint32( out, req->offset );
     341    evbuffer_add_uint32( out, req->length );
    345342
    346343    dbgmsg( msgs, "requesting %u:%u->%u...", req->index, req->offset, req->length );
     
    350347
    351348static void
    352 protocolSendCancel( tr_peermsgs               * msgs,
    353                     const struct peer_request * req )
    354 {
    355     tr_peerIo       * io  = msgs->peer->io;
     349protocolSendCancel( tr_peermsgs * msgs, const struct peer_request * req )
     350{
    356351    struct evbuffer * out = msgs->outMessages;
    357352
    358     tr_peerIoWriteUint32( io, out, sizeof( uint8_t ) + 3 * sizeof( uint32_t ) );
    359     tr_peerIoWriteUint8 ( io, out, BT_CANCEL );
    360     tr_peerIoWriteUint32( io, out, req->index );
    361     tr_peerIoWriteUint32( io, out, req->offset );
    362     tr_peerIoWriteUint32( io, out, req->length );
     353    evbuffer_add_uint32( out, sizeof( uint8_t ) + 3 * sizeof( uint32_t ) );
     354    evbuffer_add_uint8 ( out, BT_CANCEL );
     355    evbuffer_add_uint32( out, req->index );
     356    evbuffer_add_uint32( out, req->offset );
     357    evbuffer_add_uint32( out, req->length );
    363358
    364359    dbgmsg( msgs, "cancelling %u:%u->%u...", req->index, req->offset, req->length );
     
    370365protocolSendPort(tr_peermsgs *msgs, uint16_t port)
    371366{
    372     tr_peerIo       * io  = msgs->peer->io;
    373367    struct evbuffer * out = msgs->outMessages;
    374368
    375369    dbgmsg( msgs, "sending Port %u", port);
    376     tr_peerIoWriteUint32( io, out, 3 );
    377     tr_peerIoWriteUint8 ( io, out, BT_PORT );
    378     tr_peerIoWriteUint16( io, out, port);
    379 }
    380 
    381 static void
    382 protocolSendHave( tr_peermsgs * msgs,
    383                   uint32_t      index )
    384 {
    385     tr_peerIo       * io  = msgs->peer->io;
     370    evbuffer_add_uint32( out, 3 );
     371    evbuffer_add_uint8 ( out, BT_PORT );
     372    evbuffer_add_uint16( out, port);
     373}
     374
     375static void
     376protocolSendHave( tr_peermsgs * msgs, uint32_t index )
     377{
    386378    struct evbuffer * out = msgs->outMessages;
    387379
    388     tr_peerIoWriteUint32( io, out, sizeof(uint8_t) + sizeof(uint32_t) );
    389     tr_peerIoWriteUint8 ( io, out, BT_HAVE );
    390     tr_peerIoWriteUint32( io, out, index );
     380    evbuffer_add_uint32( out, sizeof(uint8_t) + sizeof(uint32_t) );
     381    evbuffer_add_uint8 ( out, BT_HAVE );
     382    evbuffer_add_uint32( out, index );
    391383
    392384    dbgmsg( msgs, "sending Have %u", index );
     
    404396    assert( tr_peerIoSupportsFEXT( msgs->peer->io ) );
    405397
    406     tr_peerIoWriteUint32( io, out, sizeof(uint8_t) + sizeof(uint32_t) );
    407     tr_peerIoWriteUint8 ( io, out, BT_FEXT_ALLOWED_FAST );
    408     tr_peerIoWriteUint32( io, out, pieceIndex );
     398    evbuffer_add_uint32( io, out, sizeof(uint8_t) + sizeof(uint32_t) );
     399    evbuffer_add_uint8 ( io, out, BT_FEXT_ALLOWED_FAST );
     400    evbuffer_add_uint32( io, out, pieceIndex );
    409401
    410402    dbgmsg( msgs, "sending Allowed Fast %u...", pieceIndex );
     
    414406
    415407static void
    416 protocolSendChoke( tr_peermsgs * msgs,
    417                    int           choke )
    418 {
    419     tr_peerIo       * io  = msgs->peer->io;
     408protocolSendChoke( tr_peermsgs * msgs, int choke )
     409{
    420410    struct evbuffer * out = msgs->outMessages;
    421411
    422     tr_peerIoWriteUint32( io, out, sizeof( uint8_t ) );
    423     tr_peerIoWriteUint8 ( io, out, choke ? BT_CHOKE : BT_UNCHOKE );
     412    evbuffer_add_uint32( out, sizeof( uint8_t ) );
     413    evbuffer_add_uint8 ( out, choke ? BT_CHOKE : BT_UNCHOKE );
    424414
    425415    dbgmsg( msgs, "sending %s...", choke ? "Choke" : "Unchoke" );
     
    431421protocolSendHaveAll( tr_peermsgs * msgs )
    432422{
    433     tr_peerIo       * io  = msgs->peer->io;
    434423    struct evbuffer * out = msgs->outMessages;
    435424
    436425    assert( tr_peerIoSupportsFEXT( msgs->peer->io ) );
    437426
    438     tr_peerIoWriteUint32( io, out, sizeof( uint8_t ) );
    439     tr_peerIoWriteUint8 ( io, out, BT_FEXT_HAVE_ALL );
     427    evbuffer_add_uint32( out, sizeof( uint8_t ) );
     428    evbuffer_add_uint8 ( out, BT_FEXT_HAVE_ALL );
    440429
    441430    dbgmsg( msgs, "sending HAVE_ALL..." );
     
    447436protocolSendHaveNone( tr_peermsgs * msgs )
    448437{
    449     tr_peerIo       * io  = msgs->peer->io;
    450438    struct evbuffer * out = msgs->outMessages;
    451439
    452440    assert( tr_peerIoSupportsFEXT( msgs->peer->io ) );
    453441
    454     tr_peerIoWriteUint32( io, out, sizeof( uint8_t ) );
    455     tr_peerIoWriteUint8 ( io, out, BT_FEXT_HAVE_NONE );
     442    evbuffer_add_uint32( out, sizeof( uint8_t ) );
     443    evbuffer_add_uint8 ( out, BT_FEXT_HAVE_NONE );
    456444
    457445    dbgmsg( msgs, "sending HAVE_NONE..." );
     
    676664    msgs->peer->clientIsInterested = clientIsInterested;
    677665    dbgmsg( msgs, "Sending %s", clientIsInterested ? "Interested" : "Not Interested" );
    678     tr_peerIoWriteUint32( msgs->peer->io, out, sizeof( uint8_t ) );
    679     tr_peerIoWriteUint8 ( msgs->peer->io, out, clientIsInterested ? BT_INTERESTED : BT_NOT_INTERESTED );
     666    evbuffer_add_uint32( out, sizeof( uint8_t ) );
     667    evbuffer_add_uint8 ( out, clientIsInterested ? BT_INTERESTED : BT_NOT_INTERESTED );
    680668
    681669    pokeBatchPeriod( msgs, HIGH_PRIORITY_INTERVAL_SECS );
     
    858846    buf = tr_bencToStr( &val, TR_FMT_BENC, &len );
    859847
    860     tr_peerIoWriteUint32( msgs->peer->io, out, 2 * sizeof( uint8_t ) + len );
    861     tr_peerIoWriteUint8 ( msgs->peer->io, out, BT_LTEP );
    862     tr_peerIoWriteUint8 ( msgs->peer->io, out, LTEP_HANDSHAKE );
    863     tr_peerIoWriteBytes ( msgs->peer->io, out, buf, len );
     848    evbuffer_add_uint32( out, 2 * sizeof( uint8_t ) + len );
     849    evbuffer_add_uint8 ( out, BT_LTEP );
     850    evbuffer_add_uint8 ( out, LTEP_HANDSHAKE );
     851    evbuffer_add       ( out, buf, len );
    864852    pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
    865853    dbgOutMessageLen( msgs );
     
    10161004            int payloadLen;
    10171005            char * payload;
    1018             tr_peerIo  * io  = msgs->peer->io;
    10191006            struct evbuffer * out = msgs->outMessages;
    10201007
     
    10271014
    10281015            /* write it out as a LTEP message to our outMessages buffer */
    1029             tr_peerIoWriteUint32( io, out, 2 * sizeof( uint8_t ) + payloadLen );
    1030             tr_peerIoWriteUint8 ( io, out, BT_LTEP );
    1031             tr_peerIoWriteUint8 ( io, out, msgs->ut_metadata_id );
    1032             tr_peerIoWriteBytes ( io, out, payload, payloadLen );
     1016            evbuffer_add_uint32( out, 2 * sizeof( uint8_t ) + payloadLen );
     1017            evbuffer_add_uint8 ( out, BT_LTEP );
     1018            evbuffer_add_uint8 ( out, msgs->ut_metadata_id );
     1019            evbuffer_add       ( out, payload, payloadLen );
    10331020            pokeBatchPeriod( msgs, HIGH_PRIORITY_INTERVAL_SECS );
    10341021            dbgOutMessageLen( msgs );
     
    13041291    struct peer_request * req = &msgs->incoming.blockReq;
    13051292
    1306     assert( EVBUFFER_LENGTH( inbuf ) >= inlen );
     1293    assert( evbuffer_get_length( inbuf ) >= inlen );
    13071294    dbgmsg( msgs, "In readBtPiece" );
    13081295
     
    13231310
    13241311        /* read in another chunk of data */
    1325         const size_t nLeft = req->length - EVBUFFER_LENGTH( msgs->incoming.block );
     1312        const size_t nLeft = req->length - evbuffer_get_length( msgs->incoming.block );
    13261313        size_t n = MIN( nLeft, inlen );
    13271314        size_t i = n;
     
    13441331        dbgmsg( msgs, "got %zu bytes for block %u:%u->%u ... %d remain",
    13451332               n, req->index, req->offset, req->length,
    1346                (int)( req->length - EVBUFFER_LENGTH( msgs->incoming.block ) ) );
    1347         if( EVBUFFER_LENGTH( msgs->incoming.block ) < req->length )
     1333               (int)( req->length - evbuffer_get_length( msgs->incoming.block ) ) );
     1334        if( evbuffer_get_length( msgs->incoming.block ) < req->length )
    13481335            return READ_LATER;
    13491336
    13501337        /* we've got the whole block ... process it */
    1351         err = clientGotBlock( msgs, EVBUFFER_DATA( msgs->incoming.block ), req );
     1338        err = clientGotBlock( msgs, evbuffer_pullup( msgs->incoming.block, -1 ), req );
    13521339
    13531340        /* cleanup */
     
    13691356    const uint8_t id = msgs->incoming.id;
    13701357#ifndef NDEBUG
    1371     const size_t  startBufLen = EVBUFFER_LENGTH( inbuf );
     1358    const size_t  startBufLen = evbuffer_get_length( inbuf );
    13721359#endif
    13731360    const tr_bool fext = tr_peerIoSupportsFEXT( msgs->peer->io );
     
    15591546
    15601547    assert( msglen + 1 == msgs->incoming.length );
    1561     assert( EVBUFFER_LENGTH( inbuf ) == startBufLen - msglen );
     1548    assert( evbuffer_get_length( inbuf ) == startBufLen - msglen );
    15621549
    15631550    msgs->state = AWAITING_BT_LENGTH;
     
    16331620    tr_peermsgs *     msgs = vmsgs;
    16341621    struct evbuffer * in = tr_peerIoGetReadBuffer( io );
    1635     const size_t      inlen = EVBUFFER_LENGTH( in );
     1622    const size_t      inlen = evbuffer_get_length( in );
    16361623
    16371624    dbgmsg( msgs, "canRead: inlen is %zu, msgs->state is %d", inlen, msgs->state );
     
    16641651
    16651652    /* log the raw data that was read */
    1666     if( ( ret != READ_ERR ) && ( EVBUFFER_LENGTH( in ) != inlen ) )
    1667         fireClientGotData( msgs, inlen - EVBUFFER_LENGTH( in ), FALSE );
     1653    if( ( ret != READ_ERR ) && ( evbuffer_get_length( in ) != inlen ) )
     1654        fireClientGotData( msgs, inlen - evbuffer_get_length( in ), FALSE );
    16681655
    16691656    return ret;
     
    17451732        int payloadLen;
    17461733        char * payload;
    1747         tr_peerIo  * io  = msgs->peer->io;
    17481734        struct evbuffer * out = msgs->outMessages;
    17491735
     
    17581744
    17591745        /* write it out as a LTEP message to our outMessages buffer */
    1760         tr_peerIoWriteUint32( io, out, 2 * sizeof( uint8_t ) + payloadLen );
    1761         tr_peerIoWriteUint8 ( io, out, BT_LTEP );
    1762         tr_peerIoWriteUint8 ( io, out, msgs->ut_metadata_id );
    1763         tr_peerIoWriteBytes ( io, out, payload, payloadLen );
     1746        evbuffer_add_uint32( out, 2 * sizeof( uint8_t ) + payloadLen );
     1747        evbuffer_add_uint8 ( out, BT_LTEP );
     1748        evbuffer_add_uint8 ( out, msgs->ut_metadata_id );
     1749        evbuffer_add       ( out, payload, payloadLen );
    17641750        pokeBatchPeriod( msgs, HIGH_PRIORITY_INTERVAL_SECS );
    17651751        dbgOutMessageLen( msgs );
     
    18001786    size_t bytesWritten = 0;
    18011787    struct peer_request req;
    1802     const tr_bool haveMessages = EVBUFFER_LENGTH( msgs->outMessages ) != 0;
     1788    const tr_bool haveMessages = evbuffer_get_length( msgs->outMessages ) != 0;
    18031789    const tr_bool fext = tr_peerIoSupportsFEXT( msgs->peer->io );
    18041790
     
    18091795    if( haveMessages && !msgs->outMessagesBatchedAt ) /* fresh batch */
    18101796    {
    1811         dbgmsg( msgs, "started an outMessages batch (length is %zu)", EVBUFFER_LENGTH( msgs->outMessages ) );
     1797        dbgmsg( msgs, "started an outMessages batch (length is %zu)", evbuffer_get_length( msgs->outMessages ) );
    18121798        msgs->outMessagesBatchedAt = now;
    18131799    }
    18141800    else if( haveMessages && ( ( now - msgs->outMessagesBatchedAt ) >= msgs->outMessagesBatchPeriod ) )
    18151801    {
    1816         const size_t len = EVBUFFER_LENGTH( msgs->outMessages );
     1802        const size_t len = evbuffer_get_length( msgs->outMessages );
    18171803        /* flush the protocol messages */
    18181804        dbgmsg( msgs, "flushing outMessages... to %p (length is %zu)", msgs->peer->io, len );
     
    18411827            int payloadLen;
    18421828            char * payload;
    1843             tr_peerIo  * io  = msgs->peer->io;
    18441829            struct evbuffer * out = msgs->outMessages;
    18451830
     
    18531838
    18541839            /* write it out as a LTEP message to our outMessages buffer */
    1855             tr_peerIoWriteUint32( io, out, 2 * sizeof( uint8_t ) + payloadLen + dataLen );
    1856             tr_peerIoWriteUint8 ( io, out, BT_LTEP );
    1857             tr_peerIoWriteUint8 ( io, out, msgs->ut_metadata_id );
    1858             tr_peerIoWriteBytes ( io, out, payload, payloadLen );
    1859             tr_peerIoWriteBytes ( io, out, data, dataLen );
     1840            evbuffer_add_uint32( out, 2 * sizeof( uint8_t ) + payloadLen + dataLen );
     1841            evbuffer_add_uint8 ( out, BT_LTEP );
     1842            evbuffer_add_uint8 ( out, msgs->ut_metadata_id );
     1843            evbuffer_add       ( out, payload, payloadLen );
     1844            evbuffer_add       ( out, data, dataLen );
    18601845            pokeBatchPeriod( msgs, HIGH_PRIORITY_INTERVAL_SECS );
    18611846            dbgOutMessageLen( msgs );
     
    18721857            int payloadLen;
    18731858            char * payload;
    1874             tr_peerIo  * io  = msgs->peer->io;
    18751859            struct evbuffer * out = msgs->outMessages;
    18761860
     
    18831867
    18841868            /* write it out as a LTEP message to our outMessages buffer */
    1885             tr_peerIoWriteUint32( io, out, 2 * sizeof( uint8_t ) + payloadLen );
    1886             tr_peerIoWriteUint8 ( io, out, BT_LTEP );
    1887             tr_peerIoWriteUint8 ( io, out, msgs->ut_metadata_id );
    1888             tr_peerIoWriteBytes ( io, out, payload, payloadLen );
     1869            evbuffer_add_uint32( out, 2 * sizeof( uint8_t ) + payloadLen );
     1870            evbuffer_add_uint8 ( out, BT_LTEP );
     1871            evbuffer_add_uint8 ( out, msgs->ut_metadata_id );
     1872            evbuffer_add       ( out, payload, payloadLen );
    18891873            pokeBatchPeriod( msgs, HIGH_PRIORITY_INTERVAL_SECS );
    18901874            dbgOutMessageLen( msgs );
     
    19061890            && tr_cpPieceIsComplete( &msgs->torrent->completion, req.index ) )
    19071891        {
    1908             /* FIXME(libevent2) use evbuffer_reserve_space() + evbuffer_commit_space() */
    19091892            int err;
    19101893            const uint32_t msglen = 4 + 1 + 4 + 4 + req.length;
    19111894            struct evbuffer * out;
    1912             tr_peerIo * io = msgs->peer->io;
     1895            struct evbuffer_iovec iovec[1];
    19131896
    19141897            out = evbuffer_new( );
    19151898            evbuffer_expand( out, msglen );
    19161899
    1917             tr_peerIoWriteUint32( io, out, sizeof( uint8_t ) + 2 * sizeof( uint32_t ) + req.length );
    1918             tr_peerIoWriteUint8 ( io, out, BT_PIECE );
    1919             tr_peerIoWriteUint32( io, out, req.index );
    1920             tr_peerIoWriteUint32( io, out, req.offset );
    1921 
    1922             err = tr_cacheReadBlock( getSession(msgs)->cache, msgs->torrent, req.index, req.offset, req.length, EVBUFFER_DATA(out)+EVBUFFER_LENGTH(out) );
     1900            evbuffer_add_uint32( out, sizeof( uint8_t ) + 2 * sizeof( uint32_t ) + req.length );
     1901            evbuffer_add_uint8 ( out, BT_PIECE );
     1902            evbuffer_add_uint32( out, req.index );
     1903            evbuffer_add_uint32( out, req.offset );
     1904
     1905            evbuffer_reserve_space( out, req.length, iovec, 1 );
     1906            err = tr_cacheReadBlock( getSession(msgs)->cache, msgs->torrent, req.index, req.offset, req.length, iovec[0].iov_base );
     1907            iovec[0].iov_len = req.length;
     1908            evbuffer_commit_space( out, iovec, 1 );
    19231909
    19241910            /* check the piece if it needs checking... */
     
    19341920            else
    19351921            {
     1922                const size_t n = evbuffer_get_length( out );
    19361923                dbgmsg( msgs, "sending block %u:%u->%u", req.index, req.offset, req.length );
    1937                 EVBUFFER_LENGTH(out) += req.length;
    1938                 assert( EVBUFFER_LENGTH( out ) == msglen );
    1939                 tr_peerIoWriteBuf( io, out, TRUE );
    1940                 bytesWritten += EVBUFFER_LENGTH( out );
     1924                assert( n == msglen );
     1925                tr_peerIoWriteBuf( msgs->peer->io, out, TRUE );
     1926                bytesWritten += n;
    19411927                msgs->clientSentAnythingAt = now;
    19421928                tr_historyAdd( msgs->peer->blocksSentToPeer, tr_time( ), 1 );
     
    19691955    {
    19701956        dbgmsg( msgs, "sending a keepalive message" );
    1971         tr_peerIoWriteUint32( msgs->peer->io, msgs->outMessages, 0 );
     1957        evbuffer_add_uint32( msgs->outMessages, 0 );
    19721958        pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
    19731959    }
     
    20071993          void       * vmsgs )
    20081994{
    2009     if( what & EVBUFFER_TIMEOUT )
     1995    if( what & BEV_EVENT_TIMEOUT )
    20101996        dbgmsg( vmsgs, "libevent got a timeout, what=%hd", what );
    2011     if( what & ( EVBUFFER_EOF | EVBUFFER_ERROR ) )
     1997    if( what & ( BEV_EVENT_EOF | BEV_EVENT_ERROR ) )
    20121998        dbgmsg( vmsgs, "libevent got an error! what=%hd, errno=%d (%s)",
    20131999               what, errno, tr_strerror( errno ) );
     
    20542040    }
    20552041
    2056     tr_peerIoWriteUint32( msgs->peer->io, out,
    2057                           sizeof( uint8_t ) + field->byteCount );
    2058     tr_peerIoWriteUint8 ( msgs->peer->io, out, BT_BITFIELD );
    2059     /* FIXME(libevent2): use evbuffer_add_reference() */
    2060     tr_peerIoWriteBytes ( msgs->peer->io, out, field->bits, field->byteCount );
     2042    evbuffer_add_uint32( out, sizeof( uint8_t ) + field->byteCount );
     2043    evbuffer_add_uint8 ( out, BT_BITFIELD );
     2044    evbuffer_add       ( out, field->bits, field->byteCount );
    20612045    dbgmsg( msgs, "sending bitfield... outMessage size is now %zu",
    2062             EVBUFFER_LENGTH( out ) );
     2046            evbuffer_get_length( out ) );
    20632047    pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
    20642048
     
    22002184            int bencLen;
    22012185            uint8_t * tmp, *walk;
    2202             tr_peerIo       * io  = msgs->peer->io;
    22032186            struct evbuffer * out = msgs->outMessages;
    22042187
     
    22892272            /* write the pex message */
    22902273            benc = tr_bencToStr( &val, TR_FMT_BENC, &bencLen );
    2291             tr_peerIoWriteUint32( io, out, 2 * sizeof( uint8_t ) + bencLen );
    2292             tr_peerIoWriteUint8 ( io, out, BT_LTEP );
    2293             tr_peerIoWriteUint8 ( io, out, msgs->ut_pex_id );
    2294             tr_peerIoWriteBytes ( io, out, benc, bencLen );
     2274            evbuffer_add_uint32( out, 2 * sizeof( uint8_t ) + bencLen );
     2275            evbuffer_add_uint8 ( out, BT_LTEP );
     2276            evbuffer_add_uint8 ( out, msgs->ut_pex_id );
     2277            evbuffer_add       ( out, benc, bencLen );
    22952278            pokeBatchPeriod( msgs, HIGH_PRIORITY_INTERVAL_SECS );
    2296             dbgmsg( msgs, "sending a pex message; outMessage size is now %zu", EVBUFFER_LENGTH( out ) );
     2279            dbgmsg( msgs, "sending a pex message; outMessage size is now %zu", evbuffer_get_length( out ) );
    22972280            dbgOutMessageLen( msgs );
    22982281
     
    23202303    sendPex( msgs );
    23212304
    2322     tr_timerAdd( &msgs->pexTimer, PEX_INTERVAL_SECS, 0 );
     2305    tr_timerAdd( msgs->pexTimer, PEX_INTERVAL_SECS, 0 );
    23232306}
    23242307
     
    23522335    m->outMessagesBatchPeriod = LOW_PRIORITY_INTERVAL_SECS;
    23532336    m->incoming.block = evbuffer_new( );
    2354     evtimer_set( &m->pexTimer, pexPulse, m );
    2355     tr_timerAdd( &m->pexTimer, PEX_INTERVAL_SECS, 0 );
     2337    m->pexTimer = evtimer_new( NULL, pexPulse, m );
    23562338    peer->msgs = m;
     2339    tr_timerAdd( m->pexTimer, PEX_INTERVAL_SECS, 0 );
    23572340
    23582341    if( tr_peerIoSupportsLTEP( peer->io ) )
     
    23812364    if( msgs )
    23822365    {
    2383         evtimer_del( &msgs->pexTimer );
     2366        event_free( msgs->pexTimer );
    23842367
    23852368        evbuffer_free( msgs->incoming.block );
  • trunk/libtransmission/rpc-server.c

    r11524 r11548  
    3131#include "bencode.h"
    3232#include "crypto.h"
     33#include "fdlimit.h"
    3334#include "list.h"
    3435#include "net.h"
     
    156157static void
    157158extract_parts_from_multipart( const struct evkeyvalq * headers,
    158                               const struct evbuffer * body,
     159                              struct evbuffer * body,
    159160                              tr_ptrArray * setme_parts )
    160161{
    161162    const char * content_type = evhttp_find_header( headers, "Content-Type" );
    162163    const char * in = (const char*) EVBUFFER_DATA( body );
    163     size_t inlen = EVBUFFER_LENGTH( body );
     164    size_t inlen = evbuffer_get_length( body );
    164165
    165166    const char * boundary_key = "boundary=";
     
    278279                tr_rpc_request_exec_json( server->session,
    279280                                          EVBUFFER_DATA( json ),
    280                                           EVBUFFER_LENGTH( json ),
     281                                          evbuffer_get_length( json ),
    281282                                          NULL, NULL );
    282283                evbuffer_free( json );
     
    328329
    329330static void
    330 add_response( struct evhttp_request * req,
    331               struct tr_rpc_server *  server,
    332               struct evbuffer *       out,
    333               const void *            content,
    334               size_t                  content_len )
     331add_response( struct evhttp_request * req, struct tr_rpc_server * server,
     332              struct evbuffer * out, struct evbuffer * content )
    335333{
    336334#ifndef HAVE_ZLIB
    337     evbuffer_add( out, content, content_len );
     335    evbuffer_add_buffer( out, content );
    338336#else
    339337    const char * key = "Accept-Encoding";
     
    343341    if( !do_compress )
    344342    {
    345         evbuffer_add( out, content, content_len );
     343        evbuffer_add_buffer( out, content );
    346344    }
    347345    else
    348346    {
    349347        int state;
    350 
    351         /* FIXME(libevent2): this won't compile under libevent2.
    352            but we can use evbuffer_reserve_space() + evbuffer_commit_space() instead */
     348        struct evbuffer_iovec iovec[1];
     349        void * content_ptr = evbuffer_pullup( content, -1 );
     350        const size_t content_len = evbuffer_get_length( content );
    353351
    354352        if( !server->isStreamInitialized )
     
    371369        }
    372370
    373         server->stream.next_in = (Bytef*) content;
     371        server->stream.next_in = content_ptr;
    374372        server->stream.avail_in = content_len;
    375373
     
    377375         * we won't use the deflated data if it's longer than the raw data,
    378376         * so it's okay to let deflate() run out of output buffer space */
    379         evbuffer_expand( out, content_len );
    380         server->stream.next_out = EVBUFFER_DATA( out );
    381         server->stream.avail_out = content_len;
    382 
     377        evbuffer_reserve_space( out, content_len, iovec, 1 );
     378        server->stream.next_out = iovec[0].iov_base;
     379        server->stream.avail_out = iovec[0].iov_len;
    383380        state = deflate( &server->stream, Z_FINISH );
    384381
    385382        if( state == Z_STREAM_END )
    386383        {
    387             EVBUFFER_LENGTH( out ) = content_len - server->stream.avail_out;
     384            iovec[0].iov_len -= server->stream.avail_out;
    388385
    389386#if 0
    390387            fprintf( stderr, "compressed response is %.2f of original (raw==%zu bytes; compressed==%zu)\n",
    391                              (double)EVBUFFER_LENGTH(out)/content_len,
    392                              content_len, EVBUFFER_LENGTH(out) );
     388                             (double)evbuffer_get_length(out)/content_len,
     389                             content_len, evbuffer_get_length(out) );
    393390#endif
    394391            evhttp_add_header( req->output_headers,
     
    397394        else
    398395        {
    399             evbuffer_drain( out, EVBUFFER_LENGTH( out ) );
    400             evbuffer_add( out, content, content_len );
    401         }
    402 
     396            memcpy( iovec[0].iov_base, content_ptr, content_len );
     397            iovec[0].iov_len = content_len;
     398        }
     399
     400        evbuffer_commit_space( out, iovec, 1 );
    403401        deflateReset( &server->stream );
    404402    }
     
    429427    else
    430428    {
    431         size_t content_len;
    432         uint8_t * content;
     429        void * file;
     430        size_t file_len;
     431        struct evbuffer * content = evbuffer_new( );
    433432        const int error = errno;
    434433
    435434        errno = 0;
    436         content_len = 0;
    437         content = tr_loadFile( filename, &content_len );
     435        file_len = 0;
     436        file = tr_loadFile( filename, &file_len );
     437        content = evbuffer_new( );
     438        evbuffer_add_reference( content, file, file_len, evbuffer_ref_cleanup_tr_free, file );
    438439
    439440        if( errno )
     
    453454            add_time_header( req->output_headers, "Date", now );
    454455            add_time_header( req->output_headers, "Expires", now+(24*60*60) );
    455             add_response( req, server, out, content, content_len );
     456            add_response( req, server, out, content );
    456457            evhttp_send_reply( req, HTTP_OK, "OK", out );
    457458
    458459            evbuffer_free( out );
    459             tr_free( content );
    460         }
     460        }
     461
     462        evbuffer_free( content );
    461463    }
    462464}
     
    513515};
    514516
    515 /* FIXME(libevent2): make "response" an evbuffer and remove response_len */
    516517static void
    517518rpc_response_func( tr_session      * session UNUSED,
    518                    const char      * response,
    519                    size_t            response_len,
     519                   struct evbuffer * response,
    520520                   void            * user_data )
    521521{
     
    523523    struct evbuffer * buf = evbuffer_new( );
    524524
    525     add_response( data->req, data->server, buf, response, response_len );
     525    add_response( data->req, data->server, buf, response );
    526526    evhttp_add_header( data->req->output_headers,
    527527                           "Content-Type", "application/json; charset=UTF-8" );
     
    552552        tr_rpc_request_exec_json( server->session,
    553553                                  EVBUFFER_DATA( req->input_buffer ),
    554                                   EVBUFFER_LENGTH( req->input_buffer ),
     554                                  evbuffer_get_length( req->input_buffer ),
    555555                                  rpc_response_func, data );
    556556    }
  • trunk/libtransmission/rpcimpl.c

    r11538 r11548  
    103103
    104104    tr_bencToBuf( data->response, TR_FMT_JSON_LEAN, buf );
    105     (*data->callback)( data->session, (const char*)EVBUFFER_DATA(buf),
    106                        EVBUFFER_LENGTH(buf), data->callback_user_data );
     105    (*data->callback)( data->session, buf, data->callback_user_data );
    107106
    108107    evbuffer_free( buf );
     
    16481647
    16491648static void
    1650 noop_response_callback( tr_session * session UNUSED,
    1651                         const char * response UNUSED,
    1652                         size_t       response_len UNUSED,
    1653                         void       * user_data UNUSED )
     1649noop_response_callback( tr_session       * session UNUSED,
     1650                        struct evbuffer  * response UNUSED,
     1651                        void             * user_data UNUSED )
    16541652{
    16551653}
     
    16941692            tr_bencDictAddInt( &response, "tag", tag );
    16951693        tr_bencToBuf( &response, TR_FMT_JSON_LEAN, buf );
    1696         (*callback)( session, (const char*)EVBUFFER_DATA(buf),
    1697                      EVBUFFER_LENGTH( buf ), callback_user_data );
     1694        (*callback)( session, buf, callback_user_data );
    16981695
    16991696        evbuffer_free( buf );
     
    17161713            tr_bencDictAddInt( &response, "tag", tag );
    17171714        tr_bencToBuf( &response, TR_FMT_JSON_LEAN, buf );
    1718         (*callback)( session, (const char*)EVBUFFER_DATA(buf),
    1719                      EVBUFFER_LENGTH(buf), callback_user_data );
     1715        (*callback)( session, buf, callback_user_data );
    17201716
    17211717        evbuffer_free( buf );
  • trunk/libtransmission/rpcimpl.h

    r9868 r11548  
    2323
    2424struct tr_benc;
     25struct evbuffer;
    2526
    2627/* FIXME(libevent2): make "response" an evbuffer and remove response_len */
    2728typedef void( *tr_rpc_response_func )( tr_session      * session,
    28                                        const char      * response,
    29                                        size_t            response_len,
     29                                       struct evbuffer * response,
    3030                                       void            * user_data );
    3131/* http://www.json.org/ */
  • trunk/libtransmission/torrent-magnet.c

    r11301 r11548  
    1212
    1313#include <assert.h>
    14 #include <event.h> /* struct evbuffer */
    1514#include <stdio.h> /* remove() */
     15
     16#include <event2/buffer.h>
    1617
    1718#include "transmission.h"
     
    371372{
    372373    int i;
    373     char * ret;
    374374    const char * name;
    375375    struct evbuffer * s;
     
    391391    }
    392392
    393     ret = tr_strndup( EVBUFFER_DATA( s ), EVBUFFER_LENGTH( s ) );
    394     evbuffer_free( s );
    395     return ret;
    396 }
     393    return evbuffer_free_to_str( s );
     394}
  • trunk/libtransmission/tr-dht.c

    r11359 r11548  
    4444
    4545/* third party */
    46 #include <event.h>
     46#include <event2/event.h>
    4747#include <dht/dht.h>
    4848
     
    6161
    6262static int dht_socket = -1, dht6_socket = -1;
    63 static struct event dht_event, dht6_event;
     63static struct event * dht_event = NULL;
     64static struct event * dht6_event = NULL;
    6465static tr_port dht_port;
    6566static unsigned char myid[20];
     
    400401    tr_threadNew( dht_bootstrap, cl );
    401402
    402     event_set( &dht_event, dht_socket, EV_READ, event_callback, NULL );
    403     tr_timerAdd( &dht_event, 0, tr_cryptoWeakRandInt( 1000000 ) );
     403    dht_event = event_new( NULL, dht_socket, EV_READ, event_callback, NULL );
     404    tr_timerAdd( dht_event, 0, tr_cryptoWeakRandInt( 1000000 ) );
    404405
    405406    if( dht6_socket >= 0 )
    406407    {
    407         event_set( &dht6_event, dht6_socket, EV_READ, event_callback, NULL );
    408         tr_timerAdd( &dht6_event, 0, tr_cryptoWeakRandInt( 1000000 ) );
     408        dht6_event = event_new( NULL, dht6_socket, EV_READ, event_callback, NULL );
     409        tr_timerAdd( dht6_event, 0, tr_cryptoWeakRandInt( 1000000 ) );
    409410    }
    410411
     
    436437    tr_ndbg( "DHT", "Uninitializing DHT" );
    437438
    438     event_del( &dht_event );
    439 
    440     if( dht6_socket >= 0 )
    441         event_del( &dht6_event );
     439    event_free( dht_event );
     440    dht_event = NULL;
     441
     442    if( dht6_event ) {
     443        event_free( dht6_event );
     444        dht6_event = NULL;
     445    }
    442446
    443447    /* Since we only save known good nodes, avoid erasing older data if we
     
    692696event_callback(int s, short type, void *ignore UNUSED )
    693697{
    694     struct event *event = (s == dht_socket) ? &dht_event : &dht6_event;
     698    struct event *event = (s == dht_socket) ? dht_event : dht6_event;
    695699    time_t tosleep;
    696700    static int count = 0;
  • trunk/libtransmission/tr-lpd.c

    r11357 r11548  
    4343
    4444/* third party */
    45 #include <event.h>
    46 #include <evutil.h>
     45#include <event2/event.h>
     46#include <event2/util.h>
    4747
    4848/* libT */
     
    7171static int lpd_socket; /**<separate multicast receive socket */
    7272static int lpd_socket2; /**<and multicast send socket */
    73 static struct event lpd_event;
     73static struct event * lpd_event = NULL;
    7474static tr_port lpd_port;
    7575
     
    339339     * any announcement received during the initial interval will be discarded. */
    340340
    341     event_set( &lpd_event, lpd_socket, EV_READ | EV_PERSIST, event_callback, NULL );
    342     event_add( &lpd_event, NULL );
     341    lpd_event = event_new( NULL, lpd_socket, EV_READ | EV_PERSIST, event_callback, NULL );
     342    event_add( lpd_event, NULL );
    343343
    344344    tr_ndbg( "LPD", "Local Peer Discovery initialised" );
     
    368368    tr_ndbg( "LPD", "Uninitialising Local Peer Discovery" );
    369369
    370     event_del( &lpd_event );
     370    event_free( lpd_event );
     371    lpd_event = NULL;
    371372
    372373    /* just shut down, we won't remember any former nodes */
  • trunk/libtransmission/trevent.c

    r10913 r11548  
    127127#include <unistd.h>
    128128
    129 #include <event.h>
    130 
    131129#include "transmission.h"
    132130#include "platform.h"
  • trunk/libtransmission/utils.c

    r11490 r11548  
    4343#include <unistd.h> /* stat(), getcwd(), getpagesize() */
    4444
    45 #include "event.h"
     45#include <event2/buffer.h>
     46#include <event2/event.h>
    4647
    4748#ifdef WIN32
     
    248249        evbuffer_add_printf( buf, " (%s:%d)\n", base, line );
    249250        /* FIXME(libevent2) ifdef this out for nonwindows platforms */
    250         OutputDebugString( EVBUFFER_DATA( buf ) );
     251        OutputDebugString( evbuffer_pullup( buf, -1 ) );
    251252        if(fp) /* FIXME(libevent2) tr_getLog() should return an fd, then use evbuffer_write() here ) */
    252             (void) fwrite( EVBUFFER_DATA( buf ), 1, EVBUFFER_LENGTH( buf ), fp );
     253            (void) fwrite( evbuffer_pullup( buf, -1 ), 1, evbuffer_get_length( buf ), fp );
    253254
    254255        tr_free( base );
     
    693694
    694695char*
     696evbuffer_free_to_str( struct evbuffer * buf )
     697{
     698    const size_t n = evbuffer_get_length( buf );
     699    char * ret = tr_new( char, n + 1 );
     700    evbuffer_copyout( buf, ret, n );
     701    evbuffer_free( buf );
     702    ret[n] = '\0';
     703    return ret;
     704}
     705
     706char*
    695707tr_strdup( const void * in )
    696708{
     
    12501262strip_non_utf8( const char * in, size_t inlen )
    12511263{
    1252     char * ret;
    12531264    const char * end;
    12541265    const char zero = '\0';
     
    12671278    evbuffer_add( buf, in, inlen );
    12681279    evbuffer_add( buf, &zero, 1 );
    1269     ret = tr_memdup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
    1270     evbuffer_free( buf );
    1271     return ret;
     1280    return evbuffer_free_to_str( buf );
    12721281}
    12731282
  • trunk/libtransmission/utils.h

    r11490 r11548  
    304304void tr_free( void * p );
    305305
     306static inline
     307void evbuffer_ref_cleanup_tr_free( const void * data UNUSED, size_t datalen UNUSED, void * extra )
     308{
     309    tr_free( extra );
     310}
     311
    306312/**
    307313 * @brief make a newly-allocated copy of a chunk of memory
     
    337343 */
    338344char* tr_strdup( const void * in );
     345
     346
     347struct evbuffer;
     348
     349char* evbuffer_free_to_str( struct evbuffer * buf );
    339350
    340351/** @brief similar to bsearch() but returns the index of the lower bound */
  • trunk/libtransmission/web.c

    r11512 r11548  
    1818
    1919#include <curl/curl.h>
    20 #include <event.h>
     20
     21#include <event2/buffer.h>
    2122
    2223#include "transmission.h"
     
    193194        task->done_func( task->session,
    194195                         task->code,
    195                          EVBUFFER_DATA( task->response ),
    196                          EVBUFFER_LENGTH( task->response ),
     196                         evbuffer_pullup( task->response, -1 ),
     197                         evbuffer_get_length( task->response ),
    197198                         task->done_func_user_data );
    198199
     
    351352                curl_multi_remove_handle( multi, e );
    352353                curl_easy_cleanup( e );
    353 /*fprintf( stderr, "removing a completed task.. taskCount is now %d (response code: %d, response len: %d)\n", taskCount, (int)task->code, (int)EVBUFFER_LENGTH(task->response) );*/
     354/*fprintf( stderr, "removing a completed task.. taskCount is now %d (response code: %d, response len: %d)\n", taskCount, (int)task->code, (int)evbuffer_get_length(task->response) );*/
    354355                tr_runInEventThread( task->session, task_finish_func, task );
    355356                --taskCount;
  • trunk/libtransmission/webseed.c

    r10931 r11548  
    1313#include <string.h> /* strlen */
    1414
    15 #include <event.h>
     15#include <event2/buffer.h>
    1616
    1717#include "transmission.h"
     
    9898         const tr_file * file )
    9999{
    100     char *            ret;
    101100    struct evbuffer * out = evbuffer_new( );
    102101    const char *      url = w->url;
     
    109108        tr_http_escape( out, file->name, strlen(file->name), FALSE );
    110109
    111     ret = tr_strndup( EVBUFFER_DATA( out ), EVBUFFER_LENGTH( out ) );
    112     evbuffer_free( out );
    113     return ret;
     110    return evbuffer_free_to_str( out );
    114111}
    115112
     
    142139        }
    143140
    144         if( EVBUFFER_LENGTH( w->content ) < w->byteCount )
     141        if( evbuffer_get_length( w->content ) < w->byteCount )
    145142            requestNextChunk( w );
    146143        else {
    147             tr_ioWrite( tor, w->pieceIndex, w->pieceOffset, w->byteCount, EVBUFFER_DATA(w->content) );
    148             evbuffer_drain( w->content, EVBUFFER_LENGTH( w->content ) );
     144            tr_ioWrite( tor, w->pieceIndex, w->pieceOffset, w->byteCount, evbuffer_pullup( w->content, -1 ) );
     145            evbuffer_drain( w->content, evbuffer_get_length( w->content ) );
    149146            w->busy = 0;
    150147            if( w->dead )
     
    166163    {
    167164        const tr_info * inf = tr_torrentInfo( tor );
    168         const uint32_t have = EVBUFFER_LENGTH( w->content );
     165        const uint32_t have = evbuffer_get_length( w->content );
    169166        const uint32_t left = w->byteCount - have;
    170167        const uint32_t pieceOffset = w->pieceOffset + have;
     
    207204        w->pieceOffset = pieceOffset;
    208205        w->byteCount = byteCount;
    209         evbuffer_drain( w->content, EVBUFFER_LENGTH( w->content ) );
     206        evbuffer_drain( w->content, evbuffer_get_length( w->content ) );
    210207        requestNextChunk( w );
    211208        ret = TR_ADDREQ_OK;
  • trunk/utils/edit.c

    r11522 r11548  
    1313#include <stdio.h>
    1414
    15 #include <event.h> /* evbuffer */
     15#include <event2/buffer.h>
    1616
    1717#include <libtransmission/transmission.h>
     
    157157    }
    158158
    159     walk = tr_strndup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
    160     evbuffer_free( buf );
    161     return walk;
     159    return evbuffer_free_to_str( buf );
    162160}
    163161
  • trunk/utils/show.c

    r11522 r11548  
    1717#include <curl/curl.h>
    1818
    19 #include <event.h> /* struct evbuffer */
     19#include <event2/buffer.h>
    2020
    2121#include <libtransmission/transmission.h>
     
    222222                tr_benc * files;
    223223                tr_bool matched = FALSE;
    224                 const char * begin = (const char*) EVBUFFER_DATA( buf );
    225                 const char * end = begin + EVBUFFER_LENGTH( buf );
     224                const char * begin = (const char*) evbuffer_pullup( buf, -1 );
     225                const char * end = begin + evbuffer_get_length( buf );
    226226
    227227                if( !tr_bencParse( begin, end, &top, NULL ) )
Note: See TracChangeset for help on using the changeset viewer.