Changeset 2978


Ignore:
Timestamp:
Sep 6, 2007, 12:33:33 AM (14 years ago)
Author:
charles
Message:

still way too buggy, but I can now seed at full speed using < 2% of the CPU. =)

Location:
branches/encryption/libtransmission
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • branches/encryption/libtransmission/fastresume.c

    r2954 r2978  
    7575
    7676    /* IPs and ports of connectable peers */
    77     FR_ID_PEERS = 4,
     77    FR_ID_PEERS_OLD = 4,
    7878
    7979    /* progress data:
     
    101101
    102102    /* number of corrupt bytes downloaded */
    103     FR_ID_CORRUPT = 10
     103    FR_ID_CORRUPT = 10,
     104
     105    /* IPs and ports of connectable peers */
     106    FR_ID_PEERS = 11
    104107};
    105108
     
    284287
    285288    /* Write download and upload totals */
     289
    286290    total = tor->downloadedCur + tor->downloadedPrev;
    287291    fastResumeWriteData( FR_ID_DOWNLOADED, &total, 8, 1, file );
     292
    288293    total = tor->uploadedCur + tor->uploadedPrev;
    289294    fastResumeWriteData( FR_ID_UPLOADED, &total, 8, 1, file );
     295
    290296    total = tor->corruptCur + tor->corruptPrev;
    291297    fastResumeWriteData( FR_ID_CORRUPT, &total, 8, 1, file );
     
    293299    if( !( TR_FLAG_PRIVATE & tor->info.flags ) )
    294300    {
    295         /* Write IPs and ports of connectable peers, if any */
    296         uint8_t * buf = NULL;
    297         const int size = tr_peerMgrGetPeers( tor->handle->peerMgr,
    298                                              tor->info.hash, &buf );
    299         if( size > 0 )
    300         {
    301             fastResumeWriteData( FR_ID_PEERS, buf, size, 1, file );
    302             free( buf );
    303         }
     301        tr_pex * pex;
     302        const int count = tr_peerMgrGetPeers( tor->handle->peerMgr,
     303                                              tor->info.hash,
     304                                              &pex );
     305        if( count > 0 )
     306            fastResumeWriteData( FR_ID_PEERS, pex, sizeof(tr_pex), count, file );
     307        tr_free( pex );
    304308    }
    305309
     
    638642                break;
    639643
    640             case FR_ID_PEERS:
     644            case FR_ID_PEERS_OLD:
    641645                if( !( TR_FLAG_PRIVATE & tor->info.flags ) )
    642646                {
     
    658662                    ret |= TR_FR_PEERS;
    659663                }
     664
     665            case FR_ID_PEERS:
     666                if( !( TR_FLAG_PRIVATE & tor->info.flags ) )
     667                {
     668                    const int count = len / sizeof(tr_pex);
     669                    tr_pex * pex = tr_new0( tr_pex, count );
     670                    if( 1 != fread( pex, sizeof(tr_pex), count, file ) )
     671                    {
     672                        free( pex );
     673                        fclose( file );
     674                        return ret;
     675                    }
     676
     677                    tr_peerMgrAddPex( tor->handle->peerMgr,
     678                                      tor->info.hash,
     679                                      TR_PEER_FROM_CACHE,
     680                                      pex, count );
     681
     682                    tr_dbg( "found %i peers in resume file", len/6 );
     683                    free( pex );
     684                    ret |= TR_FR_PEERS;
     685                }
    660686                continue;
    661687
  • branches/encryption/libtransmission/handshake.c

    r2972 r2978  
    1212
    1313#include <assert.h>
     14#include <errno.h>
    1415#include <inttypes.h>
    1516#include <limits.h> /* UCHAR_MAX */
     
    3233
    3334/* enable Azureus messaging protocol */
    34 #define ENABLE_AZMP
     35//#define ENABLE_AZMP
    3536
    3637/***
     
    433434    int isEncrypted;
    434435    const uint8_t * secret;
    435     const size_t needlen = KEY_LEN;
    436436    uint8_t yb[KEY_LEN];
    437437    struct evbuffer * outbuf;
     438    size_t needlen = HANDSHAKE_NAME_LEN;
    438439
    439440    if( EVBUFFER_LENGTH(inbuf) < needlen )
    440441        return READ_MORE;
     442fprintf( stderr, "%*.*s\n", HANDSHAKE_NAME_LEN, HANDSHAKE_NAME_LEN, EVBUFFER_DATA(inbuf) );
    441443
    442444    isEncrypted = memcmp( EVBUFFER_DATA(inbuf), HANDSHAKE_NAME, HANDSHAKE_NAME_LEN );
     445    if( isEncrypted ) {
     446        needlen = KEY_LEN;
     447        if( EVBUFFER_LENGTH(inbuf) < needlen )
     448            return READ_MORE;
     449    }
     450
    443451    fprintf( stderr, "got a %s handshake\n", (isEncrypted ? "encrypted" : "plaintext") );
    444452    tr_peerIoSetEncryption( handshake->io, isEncrypted
     
    671679    /* peer id */
    672680    tr_peerIoReadBytes( handshake->io, inbuf, peer_id, sizeof(peer_id) );
    673     fprintf( stderr, "peer_id: " );
    674     for( i=0; i<20; ++i ) fprintf( stderr, "[%c]", peer_id[i] );
    675     fprintf( stderr, "\n" );
     681//    fprintf( stderr, "peer_id: " );
     682//    for( i=0; i<20; ++i ) fprintf( stderr, "[%c]", peer_id[i] );
     683//    fprintf( stderr, "\n" );
    676684    tr_peerIoSetPeersId( handshake->io, peer_id );
    677685    bytesRead += sizeof(peer_id);
     
    801809{
    802810    tr_handshake * handshake = (tr_handshake *) arg;
    803 fprintf( stderr, "handshake %p: got error; what==%hd\n", handshake, what );
     811fprintf( stderr, "handshake %p: got error [%s]; what==%hd... state was [%s]\n", handshake, strerror(errno), what, getStateName(handshake->state) );
    804812
    805813
     
    807815     * have encountered a peer that doesn't do encryption... reconnect and
    808816     * try a plaintext handshake */
    809     if(    ( handshake->state == AWAITING_YB )
     817    if(    ( ( handshake->state == AWAITING_YB ) || ( handshake->state == AWAITING_VC ) )
    810818        && ( handshake->encryptionPreference != HANDSHAKE_ENCRYPTION_REQUIRED )
    811819        && ( !tr_peerIoReconnect( handshake->io ) ) )
    812820    {
     821fprintf( stderr, "handshake %p trying again in plaintext...\n", handshake );
    813822        handshake->encryptionPreference = HANDSHAKE_PLAINTEXT_REQUIRED;
    814823        sendPlaintextHandshake( handshake );
  • branches/encryption/libtransmission/peer-io.c

    r2971 r2978  
    226226    assert( !tr_peerIoIsIncoming( io ) );
    227227
     228fprintf( stderr, "tr_peerIoReconnect: io %p\n", io );
     229
    228230    if( io->socket >= 0 )
    229231        tr_netClose( io->socket );
    230232
    231     io->socket = tr_netOpenTCP( &io->in_addr,
    232                                         io->port, 0 );
    233 
    234     return io->socket >= 0 ? 0 : -1;
     233    io->socket = tr_netOpenTCP( &io->in_addr, io->port, 0 );
     234
     235fprintf( stderr, "tr_peerIoReconnect: io->socket is %d\n", io->socket );
     236 
     237    if( io->socket >= 0 )
     238    {
     239        bufferevent_free( io->bufev );
     240
     241        io->bufev = bufferevent_new( io->socket,
     242                                     canReadWrapper,
     243                                     didWriteWrapper,
     244                                     gotErrorWrapper,
     245                                     io );
     246        bufferevent_enable( io->bufev, EV_READ|EV_WRITE );
     247
     248        return 0;
     249    }
     250 
     251    return -1;
    235252}
    236253
  • branches/encryption/libtransmission/peer-mgr-private.h

    r2968 r2978  
    1616#include <inttypes.h> /* uint16_t */
    1717#include <arpa/inet.h> /* struct in_addr */
     18
    1819struct tr_bitfield;
    1920struct tr_peerIo;
     
    4445
    4546    struct tr_peermsgs * msgs;
     47
     48    struct tr_pex * lastPex;
     49    int lastPexCount;
    4650}
    4751tr_peer;
  • branches/encryption/libtransmission/peer-mgr.c

    r2971 r2978  
    226226        peer->port = port;
    227227        peer->msgs = tr_peerMsgsNew( t->tor, peer );
     228        chokePulse( t );
    228229    }
    229230}
     
    240241                     myHandshakeDoneCB,
    241242                     manager );
     243}
     244
     245static void
     246maybeConnect( tr_peerMgr * manager, Torrent * t, tr_peer * peer )
     247{
     248    if( tr_peerMgrIsAcceptingConnections( manager ) )
     249    {
     250        fprintf( stderr, "peer-mgr: torrent [%s] is handshaking with a new peer %08x:%04x\n",
     251                 t->tor->info.name,
     252                 (uint32_t) peer->in_addr.s_addr, peer->port );
     253
     254        peer->io = tr_peerIoNewOutgoing( manager->handle, &peer->in_addr, peer->port, t->hash );
     255
     256        tr_handshakeAdd( peer->io, HANDSHAKE_ENCRYPTION_PREFERRED,
     257                         myHandshakeDoneCB, manager );
     258    }
     259}
     260
     261void
     262tr_peerMgrAddPex( tr_peerMgr     * manager,
     263                  const uint8_t  * torrentHash,
     264                  int              from,
     265                  const tr_pex   * pex,
     266                  int              pexCount )
     267{
     268    int i;
     269    const tr_pex * walk = pex;
     270    Torrent * t = getExistingTorrent( manager, torrentHash );
     271    for( i=0; i<pexCount; ++i )
     272    {
     273        tr_peer * peer = getPeer( t, &walk->in_addr );
     274        peer->port = walk->port;
     275        peer->from = from;
     276        maybeConnect( manager, t, peer );
     277    }
    242278}
    243279
     
    252288    const uint8_t * walk = peerCompact;
    253289    Torrent * t = getExistingTorrent( manager, torrentHash );
    254 
    255290    for( i=0; i<peerCount; ++i )
    256291    {
     
    258293        struct in_addr addr;
    259294        uint16_t port;
    260 
    261295        memcpy( &addr, walk, 4 ); walk += 4;
    262296        memcpy( &port, walk, 2 ); walk += 2;
    263 
    264297        peer = getPeer( t, &addr );
    265298        peer->port = port;
    266299        peer->from = from;
    267 
    268         if( tr_peerMgrIsAcceptingConnections( manager ) )
    269         {
    270             fprintf( stderr, "peer-mgr: torrent [%s] is handshaking with a new peer...\n", t->tor->info.name );
    271 
    272             peer->io = tr_peerIoNewOutgoing( manager->handle, &addr, port, t->hash );
    273 
    274             tr_handshakeAdd( peer->io, HANDSHAKE_ENCRYPTION_PREFERRED,
    275                              myHandshakeDoneCB, manager );
    276         }
     300        maybeConnect( manager, t, peer );
    277301    }
    278302}
     
    298322
    299323int
     324tr_pexCompare( const void * va, const void * vb )
     325{
     326    const tr_pex * a = (const tr_pex *) va;
     327    const tr_pex * b = (const tr_pex *) vb;
     328    int i = memcmp( &a->in_addr, &b->in_addr, sizeof(struct in_addr) );
     329    if( i ) return i;
     330    if( a->port < b->port ) return -1;
     331    if( a->port > b->port ) return 1;
     332    return 0;
     333}
     334
     335int tr_pexCompare( const void * a, const void * b );
     336
     337
     338int
    300339tr_peerMgrGetPeers( tr_peerMgr      * manager,
    301340                    const uint8_t   * torrentHash,
    302                     uint8_t        ** setme_compact )
     341                    tr_pex         ** setme_pex )
    303342{
    304343    const Torrent * t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash );
    305344    int i, peerCount;
    306345    const tr_peer ** peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount );
    307     uint8_t * ret = tr_new( uint8_t, peerCount * 6 );
    308     uint8_t * walk = ret;
    309 
    310     for( i=0; i<peerCount; ++i )
    311     {
    312         memcpy( walk, &peers[i]->in_addr, sizeof( struct in_addr ) );
    313         walk += sizeof( struct in_addr );
    314         memcpy( walk, &peers[i]->port, sizeof( uint16_t ) );
    315         walk += sizeof( uint16_t );
    316     }
    317 
    318     assert( ( walk - ret ) == peerCount * 6 );
    319     *setme_compact = ret;
     346    tr_pex * pex = tr_new( tr_pex, peerCount );
     347    tr_pex * walk = pex;
     348
     349    for( i=0; i<peerCount; ++i, ++walk )
     350    {
     351        walk->in_addr = peers[i]->in_addr;
     352        walk->port = peers[i]->port;
     353        walk->flags = '\0'; /* FIXME */
     354    }
     355
     356    assert( ( walk - pex ) == peerCount );
     357    qsort( pex, peerCount, sizeof(tr_pex), tr_pexCompare );
     358    *setme_pex = pex;
    320359    return peerCount;
    321360}
     
    524563
    525564static int
    526 chokePulse( void * vtorrent UNUSED )
     565chokePulse( void * vtorrent )
    527566{
    528567    Torrent * t = (Torrent *) vtorrent;
    529     int i, size, optimistic;
     568    int i, size, unchoked;
    530569    const int done = tr_cpGetStatus( t->tor->completion ) != TR_CP_INCOMPLETE;
    531570    tr_peer ** peers = (tr_peer **) tr_ptrArrayPeek( t->peers, &size );
     
    549588    qsort( data, size, sizeof(ChokeData), compareChokeByDownloader );
    550589    bestDownloaderRate = data[0].rate;
    551     for( i=0; i<size && i<NUM_DOWNLOADERS_TO_UNCHOKE; ++i )
    552         tr_peerMsgsSetChoke( data[i].peer->msgs, FALSE );
     590    for( i=unchoked=0; i<size && unchoked<NUM_DOWNLOADERS_TO_UNCHOKE; ++i ) {
     591        if( data[i].peer->msgs != NULL ) {
     592            tr_peerMsgsSetChoke( data[i].peer->msgs, FALSE );
     593            ++unchoked;
     594        }
     595    }
    553596    memmove( data, data+i, sizeof(ChokeData)*(size-i) );
    554597    size -= i;
     
    562605
    563606    /* of those remaining, optimistically unchoke one; choke the rest */
    564     optimistic = tr_rand( size );
    565     for( i=0; i<size; ++i )
    566         tr_peerMsgsSetChoke( data[i].peer->msgs, i!=optimistic );
     607    if( size > 0 ) {
     608        const int optimistic = tr_rand( size );
     609        for( i=0; i<size; ++i )
     610            tr_peerMsgsSetChoke( data[i].peer->msgs, i!=optimistic );
     611    }
    567612
    568613    /* cleanup */
  • branches/encryption/libtransmission/peer-mgr.h

    r2969 r2978  
    1414#define TR_PEER_MGR_H
    1515
     16#include <inttypes.h> /* uint16_t */
     17#include <arpa/inet.h> /* struct in_addr */
     18
    1619struct in_addr;
    1720struct tr_handle;
     
    1922struct tr_torrent;
    2023typedef struct tr_peerMgr tr_peerMgr;
     24
     25typedef struct tr_pex
     26{
     27    struct in_addr in_addr;
     28    uint16_t port;
     29    uint8_t flags;
     30}
     31tr_pex;
     32
     33int tr_pexCompare( const void * a, const void * b );
    2134
    2235tr_peerMgr* tr_peerMgrNew( struct tr_handle * );
     
    3649                         int              peerCount );
    3750
     51void tr_peerMgrAddPex( tr_peerMgr     * manager,
     52                       const uint8_t  * torrentHash,
     53                       int              from,
     54                       const tr_pex   * pex,
     55                       int              pexCount );
     56
    3857void tr_peerMgrSetBlame( tr_peerMgr     * manager,
    3958                         const uint8_t  * torrentHash,
     
    4362int tr_peerMgrGetPeers( tr_peerMgr      * manager,
    4463                        const uint8_t   * torrentHash,
    45                         uint8_t        ** setme_compact );
     64                        tr_pex         ** setme_pex );
    4665
    4766void tr_peerMgrStartTorrent( tr_peerMgr     * manager,
  • branches/encryption/libtransmission/peer-msgs.c

    r2972 r2978  
    3939**/
    4040
     41#define MINUTES_TO_MSEC(N) ((N) * 60 * 1000)
     42
     43/* pex attempts are made this frequently */
     44#define PEX_INTERVAL (MINUTES_TO_MSEC(1))
     45
    4146enum
    4247{
     
    110115
    111116    tr_timer_tag pulseTag;
     117    tr_timer_tag pexTag;
    112118
    113119    unsigned int  notListening        : 1;
     
    121127    uint64_t gotKeepAliveTime;
    122128
    123     uint16_t ut_pex;
     129    uint8_t ut_pex;
    124130    uint16_t listeningPort;
     131
     132    tr_pex * pex;
     133    int pexCount;
    125134};
    126135
     
    186195tr_peerMsgsSetChoke( tr_peermsgs * peer, int choke )
    187196{
     197    assert( peer != NULL );
     198
    188199    if( peer->info->peerIsChoked != !!choke )
    189200    {
     
    228239        sub = tr_bencDictFind( sub, "ut_pex" );
    229240        if( tr_bencIsInt( sub ) ) {
    230             peer->ut_pex = sub->val.i;
     241            peer->ut_pex = (uint8_t) sub->val.i;
    231242            fprintf( stderr, "peer->ut_pex is %d\n", peer->ut_pex );
    232243        }
     
    598609    size_t len;
    599610
     611fprintf( stderr, "peer %p pulse... notlistening %d, outblock size: %d, outmessages size %d, peerAskedFor %p\n",
     612         vpeer,
     613         (int)peer->notListening,
     614         (int)EVBUFFER_LENGTH( peer->outBlock ),
     615         (int)EVBUFFER_LENGTH( peer->outMessages ),
     616         peer->peerAskedFor );
     617
    600618    /* if we froze out a downloaded block because of speed limits,
    601619       start listening to the peer again */
     
    609627    if(( len = EVBUFFER_LENGTH( peer->outBlock ) ))
    610628    {
     629fprintf( stderr, "peer %p needing to upload... canUpload %d\n", peer, canUpload(peer) );
    611630        if( canUpload( peer ) )
    612631        {
    613632            const size_t outlen = MIN( len, 2048 );
     633fprintf( stderr, "peer %p writing %d bytes...\n", peer, (int)outlen );
    614634            tr_peerIoWrite( peer->io, EVBUFFER_DATA(peer->outBlock), outlen );
    615635            evbuffer_drain( peer->outBlock, outlen );
     636
     637            peer->torrent->uploadedCur += outlen;
     638            tr_rcTransferred( peer->torrent->upload, outlen );
     639            tr_rcTransferred( peer->handle->upload, outlen );
    616640        }
    617641    }
     
    628652        const uint8_t msgid = BT_PIECE;
    629653        const uint32_t msglen = sizeof(uint8_t) + sizeof(uint32_t)*2 + req->length;
     654fprintf( stderr, "peer %p starting to upload a block...\n", peer );
    630655        tr_ioRead( peer->torrent, req->index, req->offset, req->length, tmp );
    631656        tr_peerIoWriteUint32( peer->io, peer->outBlock, msglen );
     
    667692    tr_peerIoWriteBytes( peer->io, peer->outMessages, bitfield->bits, bitfield->len );
    668693}
     694
     695/**
     696***
     697**/
     698
     699#define MAX_DIFFS 50
     700
     701typedef struct
     702{
     703    tr_pex * added;
     704    tr_pex * dropped;
     705    tr_pex * elements;
     706    int addedCount;
     707    int droppedCount;
     708    int elementCount;
     709    int diffCount;
     710}
     711PexDiffs;
     712
     713static void pexAddedCb( void * vpex, void * userData )
     714{
     715    PexDiffs * diffs = (PexDiffs *) userData;
     716    tr_pex * pex = (tr_pex *) vpex;
     717    if( diffs->diffCount < MAX_DIFFS )
     718    {
     719        diffs->diffCount++;
     720        diffs->added[diffs->addedCount++] = *pex;
     721        diffs->elements[diffs->elementCount++] = *pex;
     722    }
     723}
     724
     725static void pexRemovedCb( void * vpex, void * userData )
     726{
     727    PexDiffs * diffs = (PexDiffs *) userData;
     728    tr_pex * pex = (tr_pex *) vpex;
     729    if( diffs->diffCount < MAX_DIFFS )
     730    {
     731        diffs->diffCount++;
     732        diffs->dropped[diffs->droppedCount++] = *pex;
     733    }
     734}
     735
     736static void pexElementCb( void * vpex, void * userData )
     737{
     738    PexDiffs * diffs = (PexDiffs *) userData;
     739    tr_pex * pex = (tr_pex *) vpex;
     740    if( diffs->diffCount < MAX_DIFFS )
     741    {
     742        diffs->diffCount++;
     743        diffs->elements[diffs->elementCount++] = *pex;
     744    }
     745}
     746
     747static int
     748pexPulse( void * vpeer )
     749{
     750    tr_peermsgs * peer = (tr_peermsgs *) vpeer;
     751
     752    if( peer->info->pexEnabled )
     753    {
     754        int i;
     755        tr_pex * newPex = NULL;
     756        const int newCount = tr_peerMgrGetPeers( peer->handle->peerMgr, peer->torrent->info.hash, &newPex );
     757        PexDiffs diffs;
     758        benc_val_t val, *added, *dropped, *flags;
     759        uint8_t *tmp, *walk;
     760        char * benc;
     761        int bencLen;
     762        const uint8_t bt_msgid = BT_LTEP;
     763        const uint8_t ltep_msgid = peer->ut_pex;
     764
     765        /* build the diffs */
     766        diffs.added = tr_new( tr_pex, newCount );
     767        diffs.addedCount = 0;
     768        diffs.dropped = tr_new( tr_pex, peer->pexCount );
     769        diffs.droppedCount = 0;
     770        diffs.elements = tr_new( tr_pex, newCount + peer->pexCount );
     771        diffs.elementCount = 0;
     772        diffs.diffCount = 0;
     773        tr_set_compare( peer->pex, peer->pexCount,
     774                        newPex, newCount,
     775                        tr_pexCompare, sizeof(tr_pex),
     776                        pexRemovedCb, pexAddedCb, pexElementCb, &diffs );
     777        fprintf( stderr, "pex: old peer count %d, new peer count %d, added %d, removed %d\n", peer->pexCount, newCount, diffs.addedCount, diffs.droppedCount );
     778
     779        /* update peer */
     780        tr_free( peer->pex );
     781        peer->pex = diffs.elements;
     782        peer->pexCount = diffs.elementCount;
     783
     784       
     785        /* build the pex payload */
     786        tr_bencInit( &val, TYPE_DICT );
     787        tr_bencDictReserve( &val, 3 );
     788
     789        /* "added" */
     790        added = tr_bencDictAdd( &val, "added" );
     791        tmp = walk = tr_new( uint8_t, diffs.addedCount * 6 );
     792        for( i=0; i<diffs.addedCount; ++i ) {
     793            memcpy( walk, &diffs.added[i].in_addr, 4 ); walk += 4;
     794            memcpy( walk, &diffs.added[i].port, 2 ); walk += 2;
     795        }
     796        assert( ( walk - tmp ) == diffs.addedCount * 6 );
     797        tr_bencInitStr( added, tmp, walk-tmp, FALSE );
     798
     799        /* "added.f" */
     800        flags = tr_bencDictAdd( &val, "added.f" );
     801        tmp = walk = tr_new( uint8_t, diffs.addedCount );
     802        for( i=0; i<diffs.addedCount; ++i )
     803            *walk++ = diffs.added[i].flags;
     804        assert( ( walk - tmp ) == diffs.addedCount );
     805        tr_bencInitStr( flags, tmp, walk-tmp, FALSE );
     806
     807        /* "dropped" */
     808        dropped = tr_bencDictAdd( &val, "dropped" );
     809        tmp = walk = tr_new( uint8_t, diffs.droppedCount * 6 );
     810        for( i=0; i<diffs.droppedCount; ++i ) {
     811            memcpy( walk, &diffs.dropped[i].in_addr, 4 ); walk += 4;
     812            memcpy( walk, &diffs.dropped[i].port, 2 ); walk += 2;
     813        }
     814        assert( ( walk - tmp ) == diffs.droppedCount * 6 );
     815        tr_bencInitStr( dropped, tmp, walk-tmp, FALSE );
     816
     817        /* write the pex message */
     818        benc = tr_bencSaveMalloc( &val, &bencLen );
     819        tr_peerIoWriteUint32( peer->io, peer->outBlock, 1 + 1 + bencLen );
     820        tr_peerIoWriteBytes ( peer->io, peer->outBlock, &bt_msgid, 1 );
     821        tr_peerIoWriteBytes ( peer->io, peer->outBlock, &ltep_msgid, 1 );
     822        tr_peerIoWriteBytes ( peer->io, peer->outBlock, benc, bencLen );
     823
     824        /* cleanup */
     825        tr_free( benc );
     826        tr_bencFree( &val );
     827        tr_free( diffs.added );
     828        tr_free( diffs.dropped );
     829        tr_free( newPex );
     830    }
     831
     832    return TRUE;
     833}
     834
     835/**
     836***
     837**/
    669838
    670839tr_peermsgs*
     
    686855    peer->info->peerIsInterested = 0;
    687856    peer->info->have = tr_bitfieldNew( torrent->info.pieceCount );
    688     peer->pulseTag = tr_timerNew( peer->handle, pulse, peer, NULL, 200 );
     857    peer->pulseTag = tr_timerNew( peer->handle, pulse, peer, NULL, 500 );
     858fprintf( stderr, "peer %p pulseTag %p\n", peer, peer->pulseTag );
     859    peer->pexTag = tr_timerNew( peer->handle, pexPulse, peer, NULL, PEX_INTERVAL );
    689860    peer->outMessages = evbuffer_new( );
    690861    peer->outBlock = evbuffer_new( );
     
    704875    if( p != NULL )
    705876    {
     877fprintf( stderr, "peer %p destroying its pulse tag\n", p );
    706878        tr_timerFree( &p->pulseTag );
     879        tr_timerFree( &p->pexTag );
    707880        evbuffer_free( p->outMessages );
    708881        evbuffer_free( p->outBlock );
  • branches/encryption/libtransmission/utils.c

    r2941 r2978  
    186186}
    187187
     188/***
     189****
     190***/
     191
     192void
     193tr_set_compare( const void * va, size_t aCount,
     194                const void * vb, size_t bCount,
     195                int compare( const void * a, const void * b ),
     196                size_t elementSize,
     197                tr_set_func in_a_cb,
     198                tr_set_func in_b_cb,
     199                tr_set_func in_both_cb,
     200                void * userData )
     201{
     202    size_t ai, bi;
     203    const uint8_t * a = (const uint8_t *) va;
     204    const uint8_t * b = (const uint8_t *) vb;
     205
     206    for( ai=bi=0; ai<aCount && bi<bCount; )
     207    {
     208        if( ai==aCount )
     209        {
     210            (*in_b_cb)( (void*)b, userData );
     211            b += elementSize;
     212        }
     213        else if ( bi==bCount )
     214        {
     215            (*in_a_cb)( (void*)a, userData );
     216            a += elementSize;
     217        }
     218        else
     219        {
     220            const int val = (*compare)( a, b );
     221
     222            if( !val )
     223            {
     224                (*in_both_cb)( (void*)a, userData );
     225                a += elementSize;
     226                b += elementSize;
     227            }
     228            else if( val < 0 )
     229            {
     230                (*in_a_cb)( (void*)a, userData );
     231                a += elementSize;
     232            }
     233            else if( val > 0 )
     234            {
     235                (*in_b_cb)( (void*)b, userData );
     236                b += elementSize;
     237            }
     238        }
     239    }
     240}
     241
     242/***
     243****
     244***/
     245
    188246
    189247#if 0
  • branches/encryption/libtransmission/utils.h

    r2941 r2978  
    139139***/
    140140
     141typedef void (tr_set_func)(void * element, void * userData );
     142
     143void tr_set_compare( const void * a, size_t aCount,
     144                     const void * b, size_t bCount,
     145                     int compare( const void * a, const void * b ),
     146                     size_t elementSize,
     147                     tr_set_func in_a_cb,
     148                     tr_set_func in_b_cb,
     149                     tr_set_func in_both_cb,
     150                     void * userData );
     151                   
     152
     153/***
     154****
     155***/
     156
    141157struct tr_bitfield
    142158{
Note: See TracChangeset for help on using the changeset viewer.