(trunk libT) minor tweaks: make bencode's int parser code more consistent; make some comments more readable

This commit is contained in:
Charles Kerr 2009-12-02 15:16:29 +00:00
parent 6eec59008c
commit 6d008257c8
3 changed files with 22 additions and 24 deletions

View File

@ -130,6 +130,7 @@ compareHostToName( const void * va, const void * vb )
return strcmp( a->name, vb );
}
/* format: hostname + ':' + port */
static char *
getHostName( const char * url )
{
@ -425,6 +426,7 @@ tierFree( void * vtier )
static void
tierIncrementTracker( tr_tier * tier )
{
/* move our index to the next tracker in the tier */
const int i = ( tier->currentTrackerIndex + 1 )
% tr_ptrArraySize( &tier->trackers );
tier->currentTracker = tr_ptrArrayNth( &tier->trackers, i );
@ -1456,11 +1458,11 @@ onScrapeDone( tr_session * session,
size_t responseLen,
void * vdata )
{
tr_bool success = FALSE;
tr_announcer * announcer = session->announcer;
struct announce_data * data = vdata;
tr_tier * tier = getTier( announcer, data->torrentId, data->tierId );
const time_t now = tr_time( );
tr_bool success = FALSE;
if( announcer )
++announcer->scrapeSlotsAvailable;
@ -1532,8 +1534,8 @@ onScrapeDone( tr_session * session,
static void
tierScrape( tr_announcer * announcer, tr_tier * tier )
{
char * url;
const char * scrape;
struct evbuffer * buf;
struct announce_data * data;
const time_t now = tr_time( );
@ -1548,19 +1550,18 @@ tierScrape( tr_announcer * announcer, tr_tier * tier )
scrape = tier->currentTracker->scrape;
buf = evbuffer_new( );
evbuffer_add_printf( buf, "%s%cinfo_hash=%s",
scrape,
strchr( scrape, '?' ) ? '&' : '?',
tier->tor->info.hashEscaped );
url = tr_strdup_printf( "%s%cinfo_hash=%s",
scrape,
strchr( scrape, '?' ) ? '&' : '?',
tier->tor->info.hashEscaped );
tier->isScraping = TRUE;
tier->lastScrapeStartTime = now;
--announcer->scrapeSlotsAvailable;
dbgmsg( tier, "scraping \"%s\"", (const char*)EVBUFFER_DATA(buf) );
tr_webRun( announcer->session, (const char*)EVBUFFER_DATA(buf), NULL, onScrapeDone, data );
dbgmsg( tier, "scraping \"%s\"", url );
tr_webRun( announcer->session, url, NULL, onScrapeDone, data );
evbuffer_free( buf );
tr_free( url );
}
static void

View File

@ -83,7 +83,6 @@ tr_bencParseInt( const uint8_t * buf,
const uint8_t ** setme_end,
int64_t * setme_val )
{
int err = 0;
char * endptr;
const void * begin;
const void * end;
@ -102,16 +101,13 @@ tr_bencParseInt( const uint8_t * buf,
errno = 0;
val = evutil_strtoll( begin, &endptr, 10 );
if( errno || ( endptr != end ) ) /* incomplete parse */
err = EILSEQ;
else if( val && *(const char*)begin == '0' ) /* no leading zeroes! */
err = EILSEQ;
else
{
*setme_end = (const uint8_t*)end + 1;
*setme_val = val;
}
return EILSEQ;
if( val && *(const char*)begin == '0' ) /* no leading zeroes! */
return EILSEQ;
return err;
*setme_end = (const uint8_t*)end + 1;
*setme_val = val;
return 0;
}
/**

View File

@ -955,9 +955,10 @@ tr_peerMgrGetNextRequests( tr_torrent * tor,
}
}
/* We almost always change only a handful of pieces in the array.
* In these cases, it's cheaper to sort those changed pieces and merge,
* than qsort()ing the whole array again */
/* In most cases we've just changed the weights of a small number of pieces.
* So rather than qsort()ing the entire array, it's faster to sort just the
* changed ones, then do a standard merge-two-sorted-arrays pass on the
* changed and unchanged pieces. */
if( got > 0 )
{
struct weighted_piece * p;
@ -967,7 +968,7 @@ tr_peerMgrGetNextRequests( tr_torrent * tor,
struct weighted_piece * b = a_end;
struct weighted_piece * b_end = t->pieces + t->pieceCount;
/* rescore the pieces that we changed */
/* resort the pieces that we changed */
weightTorrent = t->tor;
qsort( a, a_end-a, sizeof( struct weighted_piece ), comparePieceByWeight );