From 12ffdb7d8709c579ef5131f9edd7c08be4fdbb35 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 Dec 2009 01:02:54 +0000 Subject: [PATCH] (trunk libT) #2632 "Add streaming capability to libtransmission (but not the Transmission GUI clients)" -- implemented --- libtransmission/peer-mgr.c | 14 +++++++++----- libtransmission/resume.c | 11 ++++++++++- libtransmission/resume.h | 3 ++- libtransmission/torrent.c | 22 ++++++++++++++++++++++ libtransmission/torrent.h | 1 + libtransmission/transmission.h | 9 ++++++--- 6 files changed, 50 insertions(+), 10 deletions(-) diff --git a/libtransmission/peer-mgr.c b/libtransmission/peer-mgr.c index 34ff6ef63..1dc31b980 100644 --- a/libtransmission/peer-mgr.c +++ b/libtransmission/peer-mgr.c @@ -11,10 +11,8 @@ */ #include -#include #include /* memcpy, memcmp, strstr */ #include /* qsort */ -#include /* INT_MAX */ #include @@ -154,7 +152,7 @@ struct block_request struct weighted_piece { tr_piece_index_t index; - int16_t salt; + tr_piece_index_t salt; int16_t requestCount; }; @@ -778,7 +776,11 @@ comparePieceByWeight( const void * va, const void * vb ) if( ia < ib ) return 1; /* tertiary key: random */ - return a->salt - b->salt; + if( a->salt < b->salt ) return -1; + if( a->salt > b->salt ) return 1; + + /* okay, they're equal */ + return 0; } static int @@ -857,6 +859,7 @@ pieceListRebuild( Torrent * t ) tr_piece_index_t poolCount = 0; const tr_torrent * tor = t->tor; const tr_info * inf = tr_torrentInfo( tor ); + const tr_bool isStreaming = tr_torrentIsStreaming( tor ); struct weighted_piece * pieces; int pieceCount; @@ -872,7 +875,8 @@ pieceListRebuild( Torrent * t ) struct weighted_piece * piece = pieces + i; piece->index = pool[i]; piece->requestCount = 0; - piece->salt = tr_cryptoWeakRandInt( 255 ); + piece->salt = isStreaming ? piece->index + : (tr_piece_index_t)tr_cryptoWeakRandInt( 4096 ); } /* if we already had a list of pieces, merge it into diff --git a/libtransmission/resume.c b/libtransmission/resume.c index cef4a3d38..31334b8c0 100644 --- a/libtransmission/resume.c +++ b/libtransmission/resume.c @@ -42,6 +42,7 @@ #define KEY_SPEEDLIMIT_OLD "speed-limit" #define KEY_SPEEDLIMIT_UP "speed-limit-up" #define KEY_SPEEDLIMIT_DOWN "speed-limit-down" +#define KEY_STREAMING "streaming" #define KEY_RATIOLIMIT "ratio-limit" #define KEY_UPLOADED "uploaded" @@ -503,7 +504,7 @@ tr_torrentSaveResume( const tr_torrent * tor ) tr_tordbg( tor, "Saving .resume file for \"%s\"", tr_torrentName( tor ) ); - tr_bencInitDict( &top, 33 ); /* arbitrary "big enough" number */ + tr_bencInitDict( &top, 34 ); /* arbitrary "big enough" number */ tr_bencDictAddInt( &top, KEY_ACTIVITY_DATE, tor->activityDate ); tr_bencDictAddInt( &top, KEY_ADDED_DATE, tor->addedDate ); tr_bencDictAddInt( &top, KEY_CORRUPT, tor->corruptPrev + tor->corruptCur ); @@ -516,6 +517,7 @@ tr_torrentSaveResume( const tr_torrent * tor ) tr_bencDictAddInt( &top, KEY_MAX_PEERS, tor->maxConnectedPeers ); tr_bencDictAddInt( &top, KEY_BANDWIDTH_PRIORITY, tr_torrentGetPriority( tor ) ); tr_bencDictAddBool( &top, KEY_PAUSED, !tor->isRunning ); + tr_bencDictAddBool( &top, KEY_STREAMING, tr_torrentIsStreaming( tor ) ); savePeers( &top, tor ); saveFilePriorities( &top, tor ); saveDND( &top, tor ); @@ -609,6 +611,13 @@ loadFromFile( tr_torrent * tor, fieldsLoaded |= TR_FR_RUN; } + if( ( fieldsToLoad & TR_FR_STREAMING ) + && tr_bencDictFindBool( &top, KEY_STREAMING, &boolVal ) ) + { + tor->isStreaming = boolVal; + fieldsLoaded |= TR_FR_STREAMING; + } + if( ( fieldsToLoad & TR_FR_ADDED_DATE ) && tr_bencDictFindInt( &top, KEY_ADDED_DATE, &i ) ) { diff --git a/libtransmission/resume.h b/libtransmission/resume.h index 573e1c6b0..870bae6be 100644 --- a/libtransmission/resume.h +++ b/libtransmission/resume.h @@ -35,7 +35,8 @@ enum TR_FR_ADDED_DATE = ( 1 << 13 ), TR_FR_DONE_DATE = ( 1 << 14 ), TR_FR_ACTIVITY_DATE = ( 1 << 15 ), - TR_FR_RATIOLIMIT = ( 1 << 16 ) + TR_FR_RATIOLIMIT = ( 1 << 16 ), + TR_FR_STREAMING = ( 1 << 17 ) }; /** diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index cdf68a64d..9311bbe77 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -1890,6 +1890,28 @@ tr_torrentSetPriority( tr_torrent * tor, tr_priority_t priority ) **** ***/ +tr_bool +tr_torrentIsStreaming( const tr_torrent * tor ) +{ + assert( tr_isTorrent( tor ) ); + assert( tr_isBool( tor->isStreaming ) ); + + return tor->isStreaming; +} + +void +tr_torrentSetStreaming( tr_torrent * tor, tr_bool isStreaming ) +{ + assert( tr_isTorrent( tor ) ); + assert( tr_isBool( isStreaming ) ); + + tor->isStreaming = isStreaming; +} + +/*** +**** +***/ + void tr_torrentSetPeerLimit( tr_torrent * tor, uint16_t maxConnectedPeers ) diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index ae984c058..6b1b4d41d 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -226,6 +226,7 @@ struct tr_torrent tr_torrent_ratio_limit_hit_func * ratio_limit_hit_func; void * ratio_limit_hit_func_user_data; + tr_bool isStreaming; tr_bool isRunning; tr_bool isDeleting; tr_bool needsSeedRatioCheck; diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index 686370e91..1f52856f6 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -707,9 +707,6 @@ uint16_t tr_sessionGetPeerLimit( const tr_session * ); void tr_sessionSetPeerLimitPerTorrent( tr_session *, uint16_t maxGlobalPeers ); uint16_t tr_sessionGetPeerLimitPerTorrent( const tr_session * ); -tr_priority_t tr_torrentGetPriority( const tr_torrent * ); -void tr_torrentSetPriority( tr_torrent *, tr_priority_t ); - /** * Load all the torrents in tr_getTorrentDir(). @@ -1075,6 +1072,12 @@ void tr_torrentUseSessionLimits ( tr_torrent *, tr_bool ); tr_bool tr_torrentUsesSessionLimits ( const tr_torrent * ); +tr_priority_t tr_torrentGetPriority( const tr_torrent * ); +void tr_torrentSetPriority( tr_torrent *, tr_priority_t ); + +tr_bool tr_torrentIsStreaming( const tr_torrent * ); +void tr_torrentSetStreaming( tr_torrent *, tr_bool ); + /**** ***** Ratio Limits ****/