Ignore:
Timestamp:
Feb 18, 2011, 12:39:33 AM (11 years ago)
Author:
jordan
Message:

implement bandwidth limiting for uploads to uTP peers

File:
1 edited

Legend:

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

    r11939 r11940  
    396396
    397397    rc = evbuffer_remove( io->outbuf, buf, buflen );
     398    assert( rc == (int)buflen ); /* if this fails, we've corrupted our bookkeeping somewhere */
    398399    if( rc < (long)buflen ) {
    399400        tr_nerr( "UTP", "Short write: %d < %ld", rc, (long)buflen);
     
    422423
    423424    if( state == UTP_STATE_CONNECT || state == UTP_STATE_WRITABLE ) {
    424         size_t count = evbuffer_get_length( io->outbuf );
    425         if( count > 0 )
    426             UTP_Write( io->utp_socket, count );
     425        /* noop */
    427426    } else if( state == UTP_STATE_EOF ) {
    428427        if( io->gotError )
     
    449448
    450449static void
    451 utp_on_overhead(void *closure, bool send, size_t count, int type)
     450utp_on_overhead(void *closure, bool send, size_t count, int type UNUSED)
    452451{
    453452    tr_peerIo *io = (tr_peerIo *)closure;
     
    10111010    evbuffer_add_buffer( io->outbuf, buf );
    10121011    addDatatype( io, byteCount, isPieceData );
    1013     if( io->utp_socket )
    1014         UTP_Write( io->utp_socket, byteCount );
    10151012}
    10161013
     
    11221119    if(( howmuch = tr_bandwidthClamp( &io->bandwidth, TR_DOWN, howmuch )))
    11231120    {
    1124         int e;
    1125 
    1126         EVUTIL_SET_SOCKET_ERROR( 0 );
    1127         res = evbuffer_read( io->inbuf, io->socket, (int)howmuch );
    1128         e = EVUTIL_SOCKET_ERROR( );
    1129 
    1130         dbgmsg( io, "read %d from peer (%s)", res, (res==-1?strerror(e):"") );
    1131 
    1132         if( evbuffer_get_length( io->inbuf ) )
    1133             canReadWrapper( io );
    1134 
    1135         if( ( res <= 0 ) && ( io->gotError ) && ( e != EAGAIN ) && ( e != EINTR ) && ( e != EINPROGRESS ) )
     1121        if( io->utp_socket != NULL ) /* utp peer connection */
    11361122        {
    1137             char errstr[512];
    1138             short what = BEV_EVENT_READING | BEV_EVENT_ERROR;
    1139             if( res == 0 )
    1140                 what |= BEV_EVENT_EOF;
    1141             tr_net_strerror( errstr, sizeof( errstr ), e );
    1142             dbgmsg( io, "tr_peerIoTryRead got an error. res is %d, what is %hd, errno is %d (%s)", res, what, e, errstr );
    1143             io->gotError( io, what, io->userData );
     1123            /* currently a noop. tr-utp's call to UTP_CheckTimeouts()
     1124             * keeps the pump primed, so no need to call UTP_RBDrained() */
     1125        }
     1126        else /* tcp peer connection */
     1127        {
     1128            int e;
     1129
     1130            EVUTIL_SET_SOCKET_ERROR( 0 );
     1131            res = evbuffer_read( io->inbuf, io->socket, (int)howmuch );
     1132            e = EVUTIL_SOCKET_ERROR( );
     1133
     1134            dbgmsg( io, "read %d from peer (%s)", res, (res==-1?strerror(e):"") );
     1135
     1136            if( evbuffer_get_length( io->inbuf ) )
     1137                canReadWrapper( io );
     1138
     1139            if( ( res <= 0 ) && ( io->gotError ) && ( e != EAGAIN ) && ( e != EINTR ) && ( e != EINPROGRESS ) )
     1140            {
     1141                char errstr[512];
     1142                short what = BEV_EVENT_READING | BEV_EVENT_ERROR;
     1143                if( res == 0 )
     1144                    what |= BEV_EVENT_EOF;
     1145                tr_net_strerror( errstr, sizeof( errstr ), e );
     1146                dbgmsg( io, "tr_peerIoTryRead got an error. res is %d, what is %hd, errno is %d (%s)", res, what, e, errstr );
     1147                io->gotError( io, what, io->userData );
     1148            }
    11441149        }
    11451150    }
     
    11551160    if(( howmuch = tr_bandwidthClamp( &io->bandwidth, TR_UP, howmuch )))
    11561161    {
    1157         int e;
    1158         EVUTIL_SET_SOCKET_ERROR( 0 );
    1159         n = tr_evbuffer_write( io, io->socket, howmuch );
    1160         e = EVUTIL_SOCKET_ERROR( );
    1161 
    1162         if( n > 0 )
    1163             didWriteWrapper( io, n );
    1164 
    1165         if( ( n < 0 ) && ( io->gotError ) && e && ( e != EPIPE ) && ( e != EAGAIN ) && ( e != EINTR ) && ( e != EINPROGRESS ) )
     1162        if( io->utp_socket != NULL ) /* utp peer connection */
    11661163        {
    1167             char errstr[512];
    1168             const short what = BEV_EVENT_WRITING | BEV_EVENT_ERROR;
    1169 
    1170             tr_net_strerror( errstr, sizeof( errstr ), e );
    1171             dbgmsg( io, "tr_peerIoTryWrite got an error. res is %d, what is %hd, errno is %d (%s)", n, what, e, errstr );
    1172 
    1173             if( io->gotError != NULL )
    1174                 io->gotError( io, what, io->userData );
     1164            const size_t old_len = evbuffer_get_length( io->outbuf );
     1165            UTP_Write( io->utp_socket, howmuch );
     1166            n = old_len - evbuffer_get_length( io->outbuf );
     1167        }
     1168        else
     1169        {
     1170            int e;
     1171
     1172            EVUTIL_SET_SOCKET_ERROR( 0 );
     1173            n = tr_evbuffer_write( io, io->socket, howmuch );
     1174            e = EVUTIL_SOCKET_ERROR( );
     1175
     1176            if( n > 0 )
     1177                didWriteWrapper( io, n );
     1178
     1179            if( ( n < 0 ) && ( io->gotError ) && e && ( e != EPIPE ) && ( e != EAGAIN ) && ( e != EINTR ) && ( e != EINPROGRESS ) )
     1180            {
     1181                char errstr[512];
     1182                const short what = BEV_EVENT_WRITING | BEV_EVENT_ERROR;
     1183
     1184                tr_net_strerror( errstr, sizeof( errstr ), e );
     1185                dbgmsg( io, "tr_peerIoTryWrite got an error. res is %d, what is %hd, errno is %d (%s)", n, what, e, errstr );
     1186
     1187                if( io->gotError != NULL )
     1188                    io->gotError( io, what, io->userData );
     1189            }
    11751190        }
    11761191    }
Note: See TracChangeset for help on using the changeset viewer.