Changeset 7375


Ignore:
Timestamp:
Dec 14, 2008, 2:44:00 AM (10 years ago)
Author:
charles
Message:

(1.4x libT) copy the bandwidth randomization code back from trunk

Location:
branches/1.4x/libtransmission
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/1.4x/libtransmission/bandwidth.c

    r7365 r7375  
    1818#include "transmission.h"
    1919#include "bandwidth.h"
     20#include "crypto.h"
    2021#include "iobuf.h"
    2122#include "ptrarray.h"
     
    245246#endif
    246247
    247 void
    248 tr_bandwidthAllocate( tr_bandwidth  * b,
    249                       tr_direction    dir,
    250                       int             period_msec )
     248static void
     249allocateBandwidth( tr_bandwidth  * b,
     250                   tr_direction    dir,
     251                   int             period_msec,
     252                   tr_ptrArray   * addme_buffers )
    251253{
    252254    assert( isBandwidth( b ) );
     
    256258    {
    257259        const double desiredSpeed = b->band[dir].desiredSpeed;
    258 #if 0
    259         const double currentSpeed = getSpeed( &b->band[dir].piece, HISTORY_MSEC - period_msec );
    260         const double pulseCount = ( HISTORY_MSEC - period_msec ) / (double)period_msec;
    261         const double nextPulseSpeed = desiredSpeed * ( pulseCount + 1 ) - ( currentSpeed * pulseCount );
    262 #else
    263260        const double nextPulseSpeed = desiredSpeed;
    264 #endif
    265261        b->band[dir].bytesLeft = MAX( 0.0, nextPulseSpeed * 1024.0 * period_msec / 1000.0 );
    266262
     
    275271    /* notify the io buffers that there's more bandwidth available */
    276272    {
    277         int i, n=0;
    278         short what = dir==TR_UP ? EV_WRITE : EV_READ;
    279         struct tr_iobuf ** iobufs = (struct tr_iobuf**) tr_ptrArrayPeek( b->iobufs, &n );
     273        int i;
     274        const int n = tr_ptrArraySize( b->iobufs );
     275        for( i=0; i<n; ++i )
     276            tr_ptrArrayAppend( addme_buffers, tr_ptrArrayNth( b->iobufs, i ) );
     277    }
     278
    280279#ifdef DEBUG_DIRECTION
    281280if( ( dir == DEBUG_DIRECTION ) && ( n > 1 ) )
    282281fprintf( stderr, "bandwidth %p has %d iobufs\n", b, n );
    283282#endif
    284         for( i=0; i<n; ++i )
    285             tr_iobuf_enable( iobufs[i], what );
    286     }
    287283
    288284    /* all children should reallocate too */
     
    295291}
    296292
     293void
     294tr_bandwidthAllocate( tr_bandwidth  * b,
     295                      tr_direction    dir,
     296                      int             period_msec )
     297{
     298    int n;
     299    tr_ptrArray * tmp;
     300    struct tr_iobuf ** buffers;
     301    const short what = dir==TR_UP ? EV_WRITE : EV_READ;
     302
     303    tmp = tr_ptrArrayNew( );
     304    allocateBandwidth( b, dir, period_msec, tmp );
     305    buffers = (struct tr_iobuf**) tr_ptrArrayPeek( tmp, &n );
     306
     307    /* notify the io buffers in a random order s.t. no
     308       particular peer gets to hog all the bandwidth */
     309    while( n > 0 ) {
     310        const int i = tr_cryptoRandInt( n );
     311        tr_iobuf_enable( buffers[i], what );
     312        buffers[i] = buffers[n-1];
     313        --n;
     314    }
     315
     316    tr_ptrArrayFree( tmp, NULL );
     317}
     318
    297319/***
    298320****
  • branches/1.4x/libtransmission/platform.c

    r7372 r7375  
    1111 */
    1212
     13#include <event.h>
     14
    1315#ifdef __BEOS__
    1416 #include <signal.h>
     
    103105    return tr_areThreadsEqual( tr_getCurrentThread( ), t->thread );
    104106}
    105 
    106 #include <event.h>
    107107
    108108#ifdef WIN32
Note: See TracChangeset for help on using the changeset viewer.