Changeset 3061


Ignore:
Timestamp:
Sep 14, 2007, 4:09:52 AM (15 years ago)
Author:
charles
Message:

improving download speeds, cleaning up choke/unchoke bugs

Location:
branches/encryption/libtransmission
Files:
13 edited

Legend:

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

    r2713 r3061  
    4545}
    4646
    47 char * tr_clientForId( uint8_t * id )
     47char * tr_clientForId( const uint8_t * id )
    4848{
    4949    char * ret = NULL;
  • branches/encryption/libtransmission/clients.h

    r261 r3061  
    2323 *****************************************************************************/
    2424
    25 char * tr_clientForId( uint8_t * );
     25char * tr_clientForId( const uint8_t * );
  • branches/encryption/libtransmission/completion.c

    r3058 r3061  
    135135}
    136136
    137 int tr_cpPieceIsComplete( const tr_completion * cp, int piece )
     137int
     138tr_cpPieceIsComplete( const tr_completion * cp, int piece )
    138139{
    139140    return cp->completeBlocks[piece] >= tr_torPieceCountBlocks(cp->tor,piece);
  • branches/encryption/libtransmission/handshake.c

    r3056 r3061  
    9191    int crypto_select;
    9292    uint8_t myReq1[SHA_DIGEST_LENGTH];
     93    uint8_t peer_id[20];
     94    int have_peer_id;
    9395    handshakeDoneCB doneCB;
    9496    void * doneUserData;
     
    636638    uint8_t reserved[8];
    637639    uint8_t hash[SHA_DIGEST_LENGTH];
    638     uint8_t peer_id[20];
    639640    int bytesRead = 0;
    640641
     
    647648    pstrlen = EVBUFFER_DATA(inbuf)[0];
    648649    fprintf( stderr, "pstrlen 1 is %d [%c]\n", (int)pstrlen, pstrlen );
     650    fprintf( stderr, "the buf is [%c][%c][%c][%c]",
     651        EVBUFFER_DATA(inbuf)[0],
     652        EVBUFFER_DATA(inbuf)[1],
     653        EVBUFFER_DATA(inbuf)[2],
     654        EVBUFFER_DATA(inbuf)[3] );
    649655    isEncrypted = pstrlen != 19;
    650656    tr_peerIoSetEncryption( handshake->io, isEncrypted
     
    692698
    693699    /* peer id */
    694     tr_peerIoReadBytes( handshake->io, inbuf, peer_id, sizeof(peer_id) );
    695 //    fprintf( stderr, "peer_id: " );
    696 //    for( i=0; i<20; ++i ) fprintf( stderr, "[%c]", peer_id[i] );
    697 //    fprintf( stderr, "\n" );
    698     tr_peerIoSetPeersId( handshake->io, peer_id );
    699     bytesRead += sizeof(peer_id);
     700    tr_peerIoReadBytes( handshake->io, inbuf, handshake->peer_id, sizeof(handshake->peer_id) );
     701    tr_peerIoSetPeersId( handshake->io, handshake->peer_id );
     702    bytesRead += sizeof(handshake->peer_id);
     703    handshake->have_peer_id = TRUE;
    700704
    701705    assert( bytesRead == HANDSHAKE_SIZE );
     
    775779
    776780static void
    777 didWrite( struct bufferevent * evin UNUSED, void * arg )
    778 {
    779     tr_handshake * handshake = (tr_handshake *) arg;
    780     fprintf( stderr, "handshake %p, with a state of %s, got a didWrite event\n", handshake, getStateName(handshake->state) );
    781 #if 0
    782     abort ( );
    783 
    784     if( handshake->state == SENDING_LTEP_HANDHAKE )
    785     {
    786         fireDoneCB( handshake, TRUE );
    787     }
    788     else
    789     {
    790 cccccccccccccccccccccccccccccccccc
    791         int state = -1;
    792         switch( handshake->state )
    793         {
    794             //case SENDING_YA:                   state = AWAITING_YB; break;
    795             //case SENDING_YB:                   state = AWAITING_PAD_A; break;
    796             //case SENDING_CRYPTO_PROVIDE:       state = AWAITING_VC; break;
    797             //case SENDING_PLAINTEXT_HANDSHAKE:  state = AWAITING_HANDSHAKE; break;
    798         }
    799         assert( state != -1 );
    800         setState( handshake, state );
    801         tr_peerIoReadOrWait( handshake->io );
    802     }
    803 #endif
    804 }
    805 
    806 static void
    807781tr_handshakeFree( tr_handshake * handshake )
    808782{
     
    813787fireDoneCB( tr_handshake * handshake, int isConnected )
    814788{
     789    const uint8_t * peer_id = isConnected && handshake->have_peer_id
     790        ? handshake->peer_id
     791        : NULL;
    815792fprintf( stderr, "handshake %p: firing done.  connected==%d\n", handshake, isConnected );
    816     (*handshake->doneCB)(handshake, handshake->io, isConnected, handshake->doneUserData);
     793    (*handshake->doneCB)(handshake, handshake->io, isConnected, peer_id, handshake->doneUserData);
    817794    tr_handshakeFree( handshake );
    818795}
     
    867844
    868845    tr_peerIoSetIOMode( io, EV_READ|EV_WRITE, 0 );
    869     tr_peerIoSetIOFuncs( io, canRead, didWrite, gotError, handshake );
     846    tr_peerIoSetIOFuncs( io, canRead, NULL, gotError, handshake );
    870847
    871848fprintf( stderr, "handshake %p: new handshake for io %p\n", handshake, io );
     
    877854    else
    878855    {
    879         //handshake->encryptionPreference = HANDSHAKE_PLAINTEXT_PREFERRED; /* this line is just for testing */
    880856        sendHandshake( handshake );
    881857    }
  • branches/encryption/libtransmission/handshake.h

    r3049 r3061  
    2828                                struct tr_peerIo    * io,
    2929                                int                   isConnected,
     30                                const uint8_t       * peerId,
    3031                                void                * userData );
    3132
  • branches/encryption/libtransmission/peer-io.c

    r3049 r3061  
    8080        switch( ret ) {
    8181            case READ_AGAIN: if( EVBUFFER_LENGTH( e->input ) ) continue; /* note fall-through */
    82             case READ_MORE: //fprintf( stderr, "waiting for bytes from peer...\n" );
    83                             tr_peerIoSetIOMode( c, EV_READ, 0 ); return; break;
    84             case READ_DONE: return; fprintf( stderr, "READ_DONE\n"); break;
     82            case READ_MORE: tr_peerIoSetIOMode( c, EV_READ, 0 ); return; break;
     83            case READ_DONE: return;
    8584        }
    8685    }
     
    110109    tr_peerIo * c;
    111110    c = tr_new0( tr_peerIo, 1 );
    112 //fprintf( stderr, "peer-io: created %p; count is now %d\n", c, ++total_io );
    113111    c->crypto = tr_cryptoNew( torrentHash, isIncoming );
    114112    c->handle = handle;
     
    118116    c->rateToClient = tr_rcInit( );
    119117    c->isIncoming = isIncoming ? 1 : 0;
    120 fprintf( stderr, "io %p rates: peer %p client %p\n", c, c->rateToPeer, c->rateToClient );
    121118    c->bufev = bufferevent_new( c->socket,
    122119                                canReadWrapper,
     
    180177
    181178        tr_free( c );
    182 
    183 //fprintf( stderr, "peer-io: freeing %p; count is now %d\n", c, --total_io );
    184179    }
    185180}
     
    238233    assert( !tr_peerIoIsIncoming( io ) );
    239234
    240 fprintf( stderr, "tr_peerIoReconnect: io %p\n", io );
    241 
    242235    if( io->socket >= 0 )
    243236        tr_netClose( io->socket );
    244237
    245238    io->socket = tr_netOpenTCP( &io->in_addr, io->port, 0 );
    246 
    247 fprintf( stderr, "tr_peerIoReconnect: io->socket is %d\n", io->socket );
    248239 
    249240    if( io->socket >= 0 )
     
    401392    {
    402393        case PEER_ENCRYPTION_PLAINTEXT:
    403             fprintf( stderr, "writing %d plaintext bytes to outbuf...\n", byteCount );
     394            /*fprintf( stderr, "writing %d plaintext bytes to outbuf...\n", byteCount );*/
    404395            evbuffer_add( outbuf, bytes, byteCount );
    405396            break;
    406397
    407398        case PEER_ENCRYPTION_RC4:
    408             fprintf( stderr, "encrypting and writing %d bytes to outbuf...\n", byteCount );
     399            /*fprintf( stderr, "encrypting and writing %d bytes to outbuf...\n", byteCount );*/
    409400            tmp = tr_new( uint8_t, byteCount );
    410401            tr_cryptoEncrypt( io->crypto, byteCount, bytes, tmp );
     
    447438    {
    448439        case PEER_ENCRYPTION_PLAINTEXT:
    449             fprintf( stderr, "reading %d plaintext bytes from inbuf...\n", byteCount );
     440            /*fprintf( stderr, "reading %d plaintext bytes from inbuf...\n", byteCount );*/
    450441            evbuffer_remove(  inbuf, bytes, byteCount );
    451442            tr_rcTransferred( io->rateToClient, byteCount );
     
    453444
    454445        case PEER_ENCRYPTION_RC4:
    455             fprintf( stderr, "reading AND DECRYPTING %d bytes from inbuf...\n", byteCount );
     446            /*fprintf( stderr, "reading AND DECRYPTING %d bytes from inbuf...\n", byteCount );*/
    456447            evbuffer_remove(  inbuf, bytes, byteCount );
    457448            tr_cryptoDecrypt( io->crypto, byteCount, bytes, bytes );
  • branches/encryption/libtransmission/peer-mgr.c

    r3057 r3061  
    1717
    1818#include "transmission.h"
     19#include "clients.h"
     20#include "completion.h"
    1921#include "handshake.h"
    20 #include "completion.h"
    2122#include "net.h"
    2223#include "peer-io.h"
     
    2829#include "utils.h"
    2930
    30 #define MINUTES_TO_MSEC(N) ((N) * 60 * 1000)
    31 
    3231/* how frequently to change which peers are choked */
    33 #define RECHOKE_PERIOD_SECONDS (MINUTES_TO_MSEC(10))
     32#define RECHOKE_PERIOD_SECONDS (15 * 1000)
    3433
    3534/* how many downloaders to unchoke per-torrent.
    3635 * http://wiki.theory.org/BitTorrentSpecification#Choking_and_Optimistic_Unchoking */
    37 #define NUM_DOWNLOADERS_TO_UNCHOKE 4
    38 
    39 /* across all torrents, how many peers maximum do we want connected? */
    40 #define MAX_CONNECTED_PEERS 80
     36#define NUM_DOWNLOADERS_TO_UNCHOKE 6
    4137
    4238/**
     
    403399            {
    404400                const int val = tr_peerMsgsAddRequest( peers[j]->msgs, index, begin, length );
    405                 if( val == TR_ADDREQ_FULL ) {
    406                     fprintf( stderr, "peer %d of %d is full\n", (int)j, size );
     401                if( val==TR_ADDREQ_FULL || val==TR_ADDREQ_CLIENT_CHOKED ) {
     402                    fprintf( stderr, "peer %p (of %d) is full\n", peers[j]->msgs, size );
    407403                    peers[j] = peers[--size];
    408404                }
    409405                else if( val == TR_ADDREQ_MISSING ) {
    410                     fprintf( stderr, "peer doesn't have it\n" );
    411406                    ++j;
    412407                }
    413408                else if( val == TR_ADDREQ_OK ) {
    414                     fprintf( stderr, "peer %d took the request for block %d\n", j, i );
     409                    fprintf( stderr, "peer %p took the request for block %d\n", peers[j]->msgs, b );
    415410                    incrementReqCount( &t->blocks[i] );
    416411                    j = size;
     
    441436
    442437static void
     438broadcastHave( Torrent * t, uint32_t index )
     439{
     440    int i, size;
     441    tr_peer ** peers = getConnectedPeers( t, &size );
     442    for( i=0; i<size; ++i )
     443        tr_peerMsgsHave( peers[i]->msgs, index );
     444    tr_free( peers );
     445}
     446
     447static void
     448broadcastGotBlock( Torrent * t, uint32_t index, uint32_t offset, uint32_t length )
     449{
     450    int i, size;
     451    tr_peer ** peers = getConnectedPeers( t, &size );
     452    for( i=0; i<size; ++i )
     453        tr_peerMsgsCancel( peers[i]->msgs, index, offset, length );
     454    tr_free( peers );
     455}
     456
     457static void
    443458msgsCallbackFunc( void * source UNUSED, void * vevent, void * vt )
    444459{
     
    449464    {
    450465        case TR_PEERMSG_GOT_BITFIELD: {
    451             const uint32_t begin = 0;
    452             const uint32_t end = begin + t->tor->info.pieceCount;
    453             uint32_t i;
    454             for( i=begin; t->blocks!=NULL && i<end; ++i ) {
    455                 if( !tr_bitfieldHas( e->bitfield, i ) )
    456                     continue;
    457                 assert( t->blocks[i].block == i );
    458                 incrementScarcity( &t->blocks[i] );
     466            if( t->blocks!=NULL ) {
     467                int i;
     468                for( i=0; i<t->tor->info.pieceCount; ++i ) {
     469                    const uint32_t begin = tr_torPieceFirstBlock( t->tor, i );
     470                    const uint32_t end = begin + tr_torPieceCountBlocks( t->tor, i );
     471                    uint32_t j;
     472                    for( j=begin; t->blocks!=NULL && j<end; ++j ) {
     473                        assert( t->blocks[j].block == j );
     474                        incrementScarcity( &t->blocks[j] );
     475                    }
     476                }
    459477            }
    460478            break;
     
    469487                incrementScarcity( &t->blocks[i] );
    470488            }
     489            broadcastHave( t, e->pieceIndex );
    471490            break;
    472491        }
    473492
    474493        case TR_PEERMSG_GOT_BLOCK: {
    475             uint32_t i = e->blockIndex;
    476494            if( t->blocks != NULL ) {
     495                const uint32_t i = _tr_block( t->tor, e->pieceIndex, e->offset );
    477496                assert( t->blocks[i].block == i );
    478497                t->blocks[i].have = 1;
    479498            }
     499            broadcastGotBlock( t, e->pieceIndex, e->offset, e->length );
    480500            break;
    481501        }
     
    499519
    500520static void
    501 myHandshakeDoneCB( tr_handshake * handshake, tr_peerIo * io, int isConnected, void * vmanager )
     521myHandshakeDoneCB( tr_handshake    * handshake,
     522                   tr_peerIo       * io,
     523                   int               isConnected,
     524                   const uint8_t   * peer_id,
     525                   void            * vmanager )
    502526{
    503527    int ok = isConnected;
     
    559583            peer->io = io;
    560584            peer->msgs = tr_peerMsgsNew( t->tor, peer );
     585            peer->client = peer_id ? tr_clientForId( peer_id ) : NULL;
    561586            fprintf( stderr, "PUB sub peer %p to msgs %p\n", peer, peer->msgs );
    562587            peer->msgsTag = tr_peerMsgsSubscribe( peer->msgs, msgsCallbackFunc, t );
    563             chokePulse( t );
    564588        }
    565589    }
     
    643667    const uint8_t * walk = peerCompact;
    644668    Torrent * t = getExistingTorrent( manager, torrentHash );
    645     for( i=0; i<peerCount; ++i )
     669    for( i=0; t!=NULL && i<peerCount; ++i )
    646670    {
    647671        tr_peer * peer;
     
    662686
    663687int
    664 tr_peerMgrIsAcceptingConnections( const tr_peerMgr * manager )
    665 {
    666     return manager->connectionCount < MAX_CONNECTED_PEERS;
     688tr_peerMgrIsAcceptingConnections( const tr_peerMgr * manager UNUSED )
     689{
     690    return TRUE; /* manager->connectionCount < MAX_CONNECTED_PEERS; */
    667691}
    668692
     
    954978    ChokeData * data;
    955979    tr_peer ** peers = getConnectedPeers( t, &size );
    956 
    957 fprintf( stderr, "rechoking torrent %p, with %d peers\n", t, size );
     980    const time_t now = time( NULL );
     981
     982fprintf( stderr, "[%s] rechoking torrent %p, with %d peers\n", ctime(&now), t, size );
    958983
    959984    if( size < 1 )
  • branches/encryption/libtransmission/peer-msgs.c

    r3049 r3061  
    4444#define PEX_INTERVAL (MINUTES_TO_MSEC(1))
    4545
     46#define PEER_PULSE_INTERVAL_MSEC 50
     47
    4648/* the most requests we'll batch up for this peer */
    47 #define OUT_REQUESTS_MAX 6
     49#define OUT_REQUESTS_MAX 5
    4850
    4951/* when we get down to this many requests, we ask the manager for more */
    50 #define OUT_REQUESTS_LOW 2
     52#define OUT_REQUESTS_LOW 3
    5153
    5254enum
     
    132134**/
    133135
    134 static const tr_peermsgs_event blankEvent = { 0, 0, 0, NULL };
     136static const tr_peermsgs_event blankEvent = { 0, 0, 0, 0, NULL };
    135137
    136138static void
     
    167169
    168170static void
    169 fireGotBlock( tr_peermsgs * peer, uint32_t blockIndex )
     171fireGotBlock( tr_peermsgs * peer, uint32_t pieceIndex, uint32_t offset, uint32_t length )
    170172{
    171173    tr_peermsgs_event e = blankEvent;
    172174    e.eventType = TR_PEERMSG_GOT_BLOCK;
    173     e.pieceIndex = blockIndex;
     175    e.pieceIndex = pieceIndex;
     176    e.offset = offset;
     177    e.length = length;
    174178    tr_publisherPublish( peer->publisher, peer, &e );
    175179}
     
    263267        }
    264268
    265         fprintf( stderr, "peer %p: enqueuing a %s message\n", peer, (choke ? "choke" : "unchoke") );
     269        fprintf( stderr, "peer %p: sending a %s message\n", peer, (choke ? "CHOKE" : "UNCHOKE") );
    266270        tr_peerIoWriteUint32( peer->io, peer->outMessages, len );
    267271        tr_peerIoWriteBytes( peer->io, peer->outMessages, &bt_msgid, 1 );
    268272    }
     273}
     274
     275/**
     276***
     277**/
     278
     279void
     280tr_peerMsgsCancel( tr_peermsgs * msgs,
     281                   uint32_t      pieceIndex,
     282                   uint32_t      offset,
     283                   uint32_t      length )
     284{
     285    tr_list * node;
     286    struct peer_request tmp;
     287
     288    assert( msgs != NULL );
     289    assert( length > 0 );
     290
     291    tmp.index = pieceIndex;
     292    tmp.offset = offset;
     293    tmp.length = length;
     294
     295    node = tr_list_find( msgs->clientAskedFor, &tmp, peer_request_compare );
     296    if( node != NULL )
     297    {
     298        /* cancel the request */
     299        const uint8_t bt_msgid = BT_CANCEL;
     300        const uint32_t len = sizeof(uint8_t) + 3 * sizeof(uint32_t);
     301        fprintf( stderr, "w00t peer %p: cancelling req for piece %u, offset %u\n", msgs, (unsigned)pieceIndex, (unsigned)offset );
     302        tr_peerIoWriteUint32( msgs->io, msgs->outMessages, len );
     303        tr_peerIoWriteBytes( msgs->io, msgs->outMessages, &bt_msgid, 1 );
     304        tr_peerIoWriteUint32( msgs->io, msgs->outMessages, pieceIndex );
     305        tr_peerIoWriteUint32( msgs->io, msgs->outMessages, offset );
     306        tr_peerIoWriteUint32( msgs->io, msgs->outMessages, length );
     307
     308        /* remove it from our "requested" list */
     309        tr_list_remove_data( &msgs->peerAskedFor, node->data );
     310    }
     311}
     312
     313/**
     314***
     315**/
     316
     317void
     318tr_peerMsgsHave( tr_peermsgs * msgs,
     319                 uint32_t      pieceIndex )
     320{
     321    const uint8_t bt_msgid = BT_HAVE;
     322    const uint32_t len = sizeof(uint8_t) + sizeof(uint32_t);
     323    fprintf( stderr, "peer %p: telling them we HAVE piece #%d\n", msgs, pieceIndex );
     324    tr_peerIoWriteUint32( msgs->io, msgs->outMessages, len );
     325    tr_peerIoWriteBytes( msgs->io, msgs->outMessages, &bt_msgid, 1 );
     326    tr_peerIoWriteUint32( msgs->io, msgs->outMessages, pieceIndex );
    269327}
    270328
     
    283341    struct peer_request * req;
    284342
    285     if( tr_list_size(peer->clientAskedFor) >= OUT_REQUESTS_MAX )
    286         return TR_ADDREQ_FULL;
     343    if( peer->info->clientIsChoked )
     344        return TR_ADDREQ_CLIENT_CHOKED;
    287345
    288346    if( !tr_bitfieldHas( peer->info->have, index ) )
    289347        return TR_ADDREQ_MISSING;
     348
     349    if( tr_list_size( peer->clientAskedFor) >= OUT_REQUESTS_MAX )
     350        return TR_ADDREQ_FULL;
    290351
    291352    /* queue the request */
     
    295356    tr_peerIoWriteUint32( peer->io, peer->outMessages, offset );
    296357    tr_peerIoWriteUint32( peer->io, peer->outMessages, length );
    297     fprintf( stderr, "peer %p: requesting a block from piece %u, offset %u, length %u\n",
     358    fprintf( stderr, "w00t peer %p: requesting a block from piece %u, offset %u, length %u\n",
    298359             peer, (unsigned int)index, (unsigned int)offset, (unsigned int)length );
    299360
     
    303364    req->offset = offset;
    304365    req->length = length;
    305     tr_list_prepend( &peer->clientAskedFor, req );
    306     fprintf( stderr, "added a request; peer %p's clientAskedFor.size() is now %d\n", peer, tr_list_size(peer->clientAskedFor));
     366    tr_list_append( &peer->clientAskedFor, req );
     367    fprintf( stderr, "w00t added a request; peer %p's clientAskedFor.size() is now %d\n", peer, tr_list_size(peer->clientAskedFor));
    307368
    308369    return TR_ADDREQ_OK;
     
    497558            tr_bitfieldAdd( peer->info->have, ui32 );
    498559            peer->info->progress = tr_bitfieldCountTrueBits( peer->info->have ) / (float)peer->torrent->info.pieceCount;
     560            fprintf( stderr, "after the HAVE message, peer progress is %f\n", peer->info->progress );
     561            updateInterest( peer );
    499562            fireGotHave( peer, ui32 );
    500             updateInterest( peer );
    501563            break;
    502564
     
    506568            tr_peerIoReadBytes( peer->io, inbuf, peer->info->have->bits, msglen );
    507569            peer->info->progress = tr_bitfieldCountTrueBits( peer->info->have ) / (float)peer->torrent->info.pieceCount;
    508             fprintf( stderr, "peer progress is %f\n", peer->info->progress );
     570            fprintf( stderr, "after the BITFIELD peer progress is %f\n", peer->info->progress );
    509571            fireGotBitfield( peer, peer->info->have );
    510572            updateInterest( peer );
     
    579641
    580642static int
    581 canDownload( const tr_peermsgs * peer )
    582 {
     643canDownload( const tr_peermsgs * peer UNUSED )
     644{
     645#if 0
    583646    tr_torrent * tor = peer->torrent;
    584 
    585 #if 0
    586     /* FIXME: was swift worth it?  did anyone notice a difference? */
    587     if( SWIFT_ENABLED && !isSeeding && (peer->credit<0) )
    588         return FALSE;
    589 #endif
    590647
    591648    if( tor->downloadLimitMode == TR_SPEEDLIMIT_GLOBAL )
     
    594651    if( tor->downloadLimitMode == TR_SPEEDLIMIT_SINGLE )
    595652        return tr_rcCanTransfer( tor->download );
     653#endif
    596654
    597655    return TRUE;
     
    632690    }
    633691
    634     {
    635         uint64_t block = index;
    636         block *= tor->info.pieceSize;
    637         block += offset;
    638         block /= tor->blockSize;
    639         fireGotBlock( peer, (uint32_t)block );
    640     }
     692    fireGotBlock( peer, index, offset, length );
    641693
    642694    /* write to disk */
     
    682734        if( !peer->blockToUs.length )
    683735        {
    684 fprintf( stderr, "w00t\n" );
     736fprintf( stderr, "w00t -- index %u, offset %u\n", peer->blockToUs.index, peer->blockToUs.offset );
    685737            gotBlock( peer, peer->blockToUs.index,
    686738                            peer->blockToUs.offset,
     
    730782
    731783static int
    732 pulse( void * vpeer )
    733 {
    734     tr_peermsgs * peer = (tr_peermsgs *) vpeer;
     784pulse( void * vmsgs )
     785{
     786    tr_peermsgs * msgs = (tr_peermsgs *) vmsgs;
    735787    size_t len;
    736788
    737789    /* if we froze out a downloaded block because of speed limits,
    738790       start listening to the peer again */
    739     if( peer->notListening )
    740     {
    741         fprintf( stderr, "peer %p thawing out...\n", peer );
    742         peer->notListening = 0;
    743         tr_peerIoSetIOMode ( peer->io, EV_READ, 0 );
    744     }
    745 
    746     if(( len = EVBUFFER_LENGTH( peer->outBlock ) ))
    747     {
    748         if( canUpload( peer ) )
     791    if( msgs->notListening )
     792    {
     793        fprintf( stderr, "msgs %p thawing out...\n", msgs );
     794        msgs->notListening = 0;
     795        tr_peerIoSetIOMode ( msgs->io, EV_READ, 0 );
     796    }
     797
     798    /* if we're running low on requests, ask for ones */
     799    if( tr_list_size(msgs->clientAskedFor) <= OUT_REQUESTS_LOW )
     800        fireBlocksRunningLow( msgs );
     801
     802    if(( len = EVBUFFER_LENGTH( msgs->outBlock ) ))
     803    {
     804        while ( len && canUpload( msgs ) )
    749805        {
    750             const size_t outlen = MIN( len, 4096 );
    751             tr_peerIoWrite( peer->io, EVBUFFER_DATA(peer->outBlock), outlen );
    752             evbuffer_drain( peer->outBlock, outlen );
    753 
    754             peer->torrent->uploadedCur += outlen;
    755             tr_rcTransferred( peer->torrent->upload, outlen );
    756             tr_rcTransferred( peer->handle->upload, outlen );
     806            const size_t outlen = MIN( len, 1024 );
     807            tr_peerIoWrite( msgs->io, EVBUFFER_DATA(msgs->outBlock), outlen );
     808            evbuffer_drain( msgs->outBlock, outlen );
     809
     810            msgs->torrent->uploadedCur += outlen;
     811            tr_rcTransferred( msgs->torrent->upload, outlen );
     812            tr_rcTransferred( msgs->handle->upload, outlen );
     813
     814            len -= outlen;
    757815        }
    758816    }
    759     else if(( len = EVBUFFER_LENGTH( peer->outMessages ) ))
    760     {
    761         fprintf( stderr, "peer %p pulse is writing %d bytes worth of messages...\n", peer, (int)len );
    762         tr_peerIoWriteBuf( peer->io, peer->outMessages );
    763         evbuffer_drain( peer->outMessages, ~0 );
    764     }
    765     else if(( peer->peerAskedFor ))
    766     {
    767         struct peer_request * req = (struct peer_request*) peer->peerAskedFor->data;
     817    else if(( len = EVBUFFER_LENGTH( msgs->outMessages ) ))
     818    {
     819        tr_peerIoWriteBuf( msgs->io, msgs->outMessages );
     820        evbuffer_drain( msgs->outMessages, ~0 );
     821    }
     822    else if(( msgs->peerAskedFor ))
     823    {
     824        struct peer_request * req = (struct peer_request*) msgs->peerAskedFor->data;
    768825        uint8_t * tmp = tr_new( uint8_t, req->length );
    769826        const uint8_t msgid = BT_PIECE;
    770827        const uint32_t msglen = sizeof(uint8_t) + sizeof(uint32_t)*2 + req->length;
    771 fprintf( stderr, "peer %p starting to upload a block...\n", peer );
    772         tr_ioRead( peer->torrent, req->index, req->offset, req->length, tmp );
    773         tr_peerIoWriteUint32( peer->io, peer->outBlock, msglen );
    774         tr_peerIoWriteBytes ( peer->io, peer->outBlock, &msgid, 1 );
    775         tr_peerIoWriteUint32( peer->io, peer->outBlock, req->index );
    776         tr_peerIoWriteUint32( peer->io, peer->outBlock, req->offset );
    777         tr_peerIoWriteBytes ( peer->io, peer->outBlock, tmp, req->length );
     828        tr_ioRead( msgs->torrent, req->index, req->offset, req->length, tmp );
     829        tr_peerIoWriteUint32( msgs->io, msgs->outBlock, msglen );
     830        tr_peerIoWriteBytes ( msgs->io, msgs->outBlock, &msgid, 1 );
     831        tr_peerIoWriteUint32( msgs->io, msgs->outBlock, req->index );
     832        tr_peerIoWriteUint32( msgs->io, msgs->outBlock, req->offset );
     833        tr_peerIoWriteBytes ( msgs->io, msgs->outBlock, tmp, req->length );
    778834        tr_free( tmp );
    779835    }
    780 
    781     if( tr_list_size(peer->clientAskedFor) <= OUT_REQUESTS_LOW )
    782         fireBlocksRunningLow( peer );
    783836
    784837    return TRUE; /* loop forever */
     
    899952        peer->pexCount = diffs.elementCount;
    900953
    901        
    902954        /* build the pex payload */
    903955        tr_bencInit( &val, TYPE_DICT );
     
    9731025    peer->info->peerIsInterested = 0;
    9741026    peer->info->have = tr_bitfieldNew( torrent->info.pieceCount );
    975     peer->pulseTimer = tr_timerNew( peer->handle, pulse, peer, 500 );
    976 fprintf( stderr, "peer %p pulseTimer %p\n", peer, peer->pulseTimer );
     1027    peer->pulseTimer = tr_timerNew( peer->handle, pulse, peer, PEER_PULSE_INTERVAL_MSEC );
    9771028    peer->pexTimer = tr_timerNew( peer->handle, pexPulse, peer, PEX_INTERVAL );
    9781029    peer->outMessages = evbuffer_new( );
     
    9931044    if( p != NULL )
    9941045    {
    995 fprintf( stderr, "peer %p destroying its pulse tag\n", p );
    9961046        tr_timerFree( &p->pulseTimer );
    9971047        tr_timerFree( &p->pexTimer );
  • branches/encryption/libtransmission/peer-msgs.h

    r3021 r3061  
    2828void         tr_peerMsgsSetChoke( tr_peermsgs *, int doChoke );
    2929
     30void         tr_peerMsgsHave( tr_peermsgs * msgs,
     31                              uint32_t      pieceIndex );
     32
     33void         tr_peerMsgsCancel( tr_peermsgs * msgs,
     34                                uint32_t      pieceIndex,
     35                                uint32_t      offset,
     36                                uint32_t      length );
     37
    3038void         tr_peerMsgsFree( tr_peermsgs* );
    3139
    3240
    33 enum { TR_ADDREQ_OK=0, TR_ADDREQ_FULL, TR_ADDREQ_MISSING };
     41enum {
     42    TR_ADDREQ_OK=0,
     43    TR_ADDREQ_FULL,
     44    TR_ADDREQ_MISSING,
     45    TR_ADDREQ_CLIENT_CHOKED
     46};
    3447
    3548int          tr_peerMsgsAddRequest( tr_peermsgs * peer,
     
    5669{
    5770    PeerMsgsEventType eventType;
    58     uint32_t pieceIndex; /* for TR_PEERMSG_GOT_HAVE */
    59     uint32_t blockIndex; /* For TR_PEERMSG_GOT_BLOCK */
     71    uint32_t pieceIndex; /* for TR_PEERMSG_GOT_BLOCK, TR_PEERMSG_GOT_HAVE */
     72    uint32_t offset;     /* for TR_PEERMSG_GOT_BLOCK */
     73    uint32_t length;     /* for TR_PEERMSG_GOT_BLOCK */
    6074    const struct tr_bitfield * bitfield; /* for TR_PEERMSG_GOT_BITFIELD */
    6175}
  • branches/encryption/libtransmission/ptrarray.c

    r2969 r3061  
    136136                       int               * exact_match )
    137137{
    138     int c = -1;
    139138    int len = t->n_items;
    140139    int first = 0;
     
    144143        int half = len / 2;
    145144        int middle = first + half;
    146         c = compare( t->items[middle], ptr );
     145        const int c = compare( t->items[middle], ptr );
    147146        if( c < 0 ) {
    148147            first = middle + 1;
  • branches/encryption/libtransmission/ratecontrol.c

    r3025 r3061  
    3939{
    4040    uint64_t date;
    41     int      size;
     41    uint64_t size;
    4242}
    4343tr_transfer_t;
     
    134134
    135135void
    136 tr_rcTransferred( tr_ratecontrol * r, int size )
     136tr_rcTransferred( tr_ratecontrol * r, size_t size )
    137137{
    138138    uint64_t now;
  • branches/encryption/libtransmission/ratecontrol.h

    r2942 r3061  
    3232int              tr_rcGetLimit( const tr_ratecontrol * );
    3333int              tr_rcCanTransfer( const tr_ratecontrol * );
    34 void             tr_rcTransferred( tr_ratecontrol *, int );
     34void             tr_rcTransferred( tr_ratecontrol *, size_t );
    3535float            tr_rcRate( const tr_ratecontrol * );
    3636void             tr_rcReset( tr_ratecontrol * );
  • branches/encryption/libtransmission/shared.c

    r3049 r3061  
    8282    s->natpmp     = tr_natpmpInit();
    8383    s->upnp       = tr_upnpInit();
    84     s->pulseTimer   = tr_timerNew( h, SharedLoop, s, 20 );
     84    s->pulseTimer   = tr_timerNew( h, SharedLoop, s, 200 );
    8585
    8686    return s;
Note: See TracChangeset for help on using the changeset viewer.