determine upload/download rates the old way, which is not the most accurate way at a few points, but should have a positive overall impact

This commit is contained in:
Mitchell Livingston 2007-05-16 02:03:18 +00:00
parent 3af24be44e
commit 3f2861f0c7
6 changed files with 35 additions and 80 deletions

View File

@ -219,14 +219,16 @@ struct tr_torrent_s
struct tr_handle_s
{
int torrentCount;
tr_torrent_t * torrentList;
int torrentCount;
tr_torrent_t * torrentList;
char * tag;
int isPortSet;
int uploadLimit;
int downloadLimit;
tr_shared_t * shared;
char * tag;
int isPortSet;
tr_ratecontrol_t * upload;
tr_ratecontrol_t * download;
tr_shared_t * shared;
#define TR_ID_LEN 20
char id[TR_ID_LEN+1];

View File

@ -280,22 +280,11 @@ int tr_peerRead( tr_peer_t * peer )
{
if( tor )
{
if( tor->customDownloadLimit )
if( tor->customDownloadLimit
? !tr_rcCanTransfer( tor->download )
: !tr_rcCanTransfer( tor->handle->download ) )
{
if( !tr_rcCanTransfer( tor->download ) )
{
break;
}
}
else
{
tr_lockUnlock( &tor->lock );
if( !tr_rcCanGlobalTransfer( tor->handle, 0 ) )
{
tr_lockLock( &tor->lock );
break;
}
tr_lockLock( &tor->lock );
break;
}
}
@ -328,6 +317,11 @@ int tr_peerRead( tr_peer_t * peer )
{
tr_rcTransferred( peer->download, ret );
tr_rcTransferred( tor->download, ret );
if ( !tor->customDownloadLimit )
{
tr_rcTransferred( tor->handle->download, ret );
}
if( ( ret = parseBuf( tor, peer ) ) )
{
return ret;
@ -489,22 +483,11 @@ writeBegin:
/* Send pieces if we can */
while( ( p = blockPending( tor, peer, &size ) ) )
{
if( tor->customUploadLimit )
if( tor->customUploadLimit
? !tr_rcCanTransfer( tor->upload )
: !tr_rcCanTransfer( tor->handle->upload ) )
{
if( !tr_rcCanTransfer( tor->upload ) )
{
break;
}
}
else
{
tr_lockUnlock( &tor->lock );
if( !tr_rcCanGlobalTransfer( tor->handle, 1 ) )
{
tr_lockLock( &tor->lock );
break;
}
tr_lockLock( &tor->lock );
break;
}
ret = tr_netSend( peer->socket, p, size );
@ -520,6 +503,10 @@ writeBegin:
blockSent( peer, ret );
tr_rcTransferred( peer->upload, ret );
tr_rcTransferred( tor->upload, ret );
if ( !tor->customUploadLimit )
{
tr_rcTransferred( tor->handle->upload, ret );
}
tor->uploadedCur += ret;
peer->outTotal += ret;

View File

@ -82,42 +82,6 @@ tr_ratecontrol_t * tr_rcInit()
return r;
}
int tr_rcCanGlobalTransfer( tr_handle_t * h, int isUpload )
{
tr_torrent_t * tor;
tr_ratecontrol_t * r;
float rate = 0;
int limit = isUpload ? h->uploadLimit : h->downloadLimit;
if( limit <= 0 )
{
return limit < 0;
}
tr_sharedLock( h->shared );
for( tor = h->torrentList; tor; tor = tor->next )
{
if( isUpload ? tor->customUploadLimit : tor->customDownloadLimit )
{
continue;
}
r = isUpload ? tor->upload : tor->download;
tr_lockLock( &r->lock );
rate += rateForInterval( r, SHORT_INTERVAL );
tr_lockUnlock( &r->lock );
if( rate >= (float)limit )
{
tr_sharedUnlock( h->shared );
return 0;
}
}
tr_sharedUnlock( h->shared );
return 1;
}
void tr_rcSetLimit( tr_ratecontrol_t * r, int limit )
{
tr_lockLock( &r->lock );

View File

@ -25,7 +25,6 @@
typedef struct tr_ratecontrol_s tr_ratecontrol_t;
tr_ratecontrol_t * tr_rcInit();
int tr_rcCanGlobalTransfer( tr_handle_t * h, int isUpload );
void tr_rcSetLimit( tr_ratecontrol_t *, int );
int tr_rcCanTransfer( tr_ratecontrol_t * );
void tr_rcTransferred( tr_ratecontrol_t *, int );

View File

@ -240,7 +240,7 @@ char * tr_torrentGetFolder( tr_torrent_t * tor )
int tr_torrentDuplicateDownload( tr_torrent_t * tor )
{
tr_torrent_t * current, * next;
tr_torrent_t * current;
/* Check if a torrent with the same name and destination is already active */
for( current = tor->handle->torrentList; current; current = current->next )

View File

@ -79,9 +79,9 @@ tr_handle_t * tr_init( const char * tag )
signal( SIGPIPE, SIG_IGN );
/* Initialize rate and file descripts controls */
h->uploadLimit = -1;
h->downloadLimit = -1;
h->upload = tr_rcInit();
h->download = tr_rcInit();
tr_fdInit();
h->shared = tr_sharedInit( h );
@ -125,13 +125,13 @@ tr_handle_status_t * tr_handleStatus( tr_handle_t * h )
void tr_setGlobalUploadLimit( tr_handle_t * h, int limit )
{
h->uploadLimit = limit;
tr_rcSetLimit( h->upload, limit );
tr_sharedSetLimit( h->shared, limit );
}
void tr_setGlobalDownloadLimit( tr_handle_t * h, int limit )
{
h->downloadLimit = limit;
tr_rcSetLimit( h->download, limit );
}
void tr_torrentRates( tr_handle_t * h, float * dl, float * ul )
@ -170,6 +170,9 @@ void tr_torrentIterate( tr_handle_t * h, tr_callback_t func, void * d )
void tr_close( tr_handle_t * h )
{
tr_rcClose( h->upload );
tr_rcClose( h->download );
tr_sharedClose( h->shared );
tr_fdClose();
free( h );