Ignore:
Timestamp:
Nov 17, 2008, 4:00:57 AM (14 years ago)
Author:
charles
Message:

(libT) better possible fix for #1468: Speed display is very jumpy

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/peer-io.c

    r7069 r7125  
    2828#include "transmission.h"
    2929#include "crypto.h"
     30#include "list.h"
    3031#include "net.h"
    3132#include "peer-io.h"
     
    7778};
    7879
     80struct tr_datatype
     81{
     82    unsigned int    isPieceData : 1;
     83    size_t          length;
     84};
     85
    7986struct tr_peerIo
    8087{
     
    93100    time_t                 timeCreated;
    94101
    95     tr_session *          session;
     102    tr_session           * session;
    96103
    97104    struct in_addr         in_addr;
    98     struct bufferevent *   bufev;
    99     struct evbuffer *      output;
     105    struct bufferevent   * bufev;
     106    struct evbuffer      * output;
     107    tr_list              * output_datatypes; /* struct tr_datatype */
    100108
    101109    tr_can_read_cb         canRead;
     
    200208    if( len < io->bufferSize[TR_UP] )
    201209    {
    202         const size_t payload = io->bufferSize[TR_UP] - len;
    203         const size_t n = addPacketOverhead( payload );
    204         struct tr_bandwidth * b = &io->bandwidth[TR_UP];
    205         b->bytesLeft -= MIN( b->bytesLeft, (size_t)n );
    206         b->bytesUsed += n;
    207         tr_rcTransferred( io->session->rawSpeed[TR_UP], n );
    208         dbgmsg( io,
    209                 "wrote %zu bytes to peer... upload bytesLeft is now %zu",
    210                 n,
    211                 b->bytesLeft );
     210        size_t payload = io->bufferSize[TR_UP] - len;
     211
     212        while( payload )
     213        {
     214            struct tr_datatype * next = io->output_datatypes->data;
     215            const size_t chunk_length = MIN( next->length, payload );
     216            const size_t n = addPacketOverhead( chunk_length );
     217
     218            if( next->isPieceData )
     219            {
     220                struct tr_bandwidth * b = &io->bandwidth[TR_UP];
     221                b->bytesLeft -= MIN( b->bytesLeft, n );
     222                b->bytesUsed += n;
     223            }
     224
     225            if( io->didWrite )
     226                io->didWrite( io, n, next->isPieceData, io->userData );
     227
     228            payload -= chunk_length;
     229            next->length -= chunk_length;
     230            if( !next->length )
     231                tr_free( tr_list_pop_front( &io->output_datatypes ) );
     232        }
    212233    }
    213234
    214235    adjustOutputBuffer( io );
    215236
    216     if( io->didWrite )
    217         io->didWrite( e, io->userData );
    218237}
    219238
     
    238257        b->bytesLeft -= MIN( b->bytesLeft, (size_t)n );
    239258        b->bytesUsed += n;
    240         tr_rcTransferred( io->session->rawSpeed[TR_DOWN], n );
    241         dbgmsg( io,
    242                 "%zu new input bytes. bytesUsed is %zu, bytesLeft is %zu",
    243                 n, b->bytesUsed,
    244                 b->bytesLeft );
     259        dbgmsg( io, "%zu new input bytes. bytesUsed is %zu, bytesLeft is %zu", n, b->bytesUsed, b->bytesLeft );
    245260
    246261        adjustInputBuffer( io );
     
    387402    tr_netClose( io->socket );
    388403    tr_cryptoFree( io->crypto );
     404    tr_list_free( &io->output_datatypes, tr_free );
    389405    tr_free( io );
    390406}
     
    718734
    719735void
    720 tr_peerIoWrite( tr_peerIo *  io,
    721                 const void * writeme,
    722                 size_t       writemeLen )
    723 {
     736tr_peerIoWrite( tr_peerIo   * io,
     737                const void  * writeme,
     738                size_t        writemeLen,
     739                int           isPieceData )
     740{
     741    struct tr_datatype * datatype;
    724742    assert( tr_amInEventThread( io->session ) );
    725743    dbgmsg( io, "adding %zu bytes into io->output", writemeLen );
     
    730748        evbuffer_add( io->output, writeme, writemeLen );
    731749
     750    datatype = tr_new( struct tr_datatype, 1 );
     751    datatype->isPieceData = isPieceData != 0;
     752    datatype->length = writemeLen;
     753    tr_list_append( &io->output_datatypes, datatype );
     754
    732755    adjustOutputBuffer( io );
    733756}
    734757
    735758void
    736 tr_peerIoWriteBuf( tr_peerIo *       io,
    737                    struct evbuffer * buf )
     759tr_peerIoWriteBuf( tr_peerIo         * io,
     760                   struct evbuffer   * buf,
     761                   int                 isPieceData )
    738762{
    739763    const size_t n = EVBUFFER_LENGTH( buf );
    740764
    741     tr_peerIoWrite( io, EVBUFFER_DATA( buf ), n );
     765    tr_peerIoWrite( io, EVBUFFER_DATA( buf ), n, isPieceData );
    742766    evbuffer_drain( buf, n );
    743767}
Note: See TracChangeset for help on using the changeset viewer.