Changeset 3105
- Timestamp:
- Sep 20, 2007, 4:32:01 PM (16 years ago)
- Location:
- trunk
- Files:
-
- 5 deleted
- 53 edited
- 14 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/gtk/main.c
r2554 r3105 168 168 169 169 static void 170 safepipe(void);171 static void172 170 setupsighandlers(void); 173 171 static void … … 229 227 gboolean didinit, didlock, sendquit, startpaused; 230 228 231 safepipe(); /* ignore SIGPIPE */232 229 argfiles = readargs( argc, argv, &sendquit, &startpaused ); 233 230 didinit = cf_init( tr_getPrefsDirectory(), NULL ); … … 1108 1105 1109 1106 static void 1110 safepipe(void) {1111 struct sigaction sa;1112 1113 memset(&sa, 0, sizeof(sa));1114 sa.sa_handler = SIG_IGN;1115 sigaction(SIGPIPE, &sa, NULL);1116 }1117 1118 static void1119 1107 setupsighandlers(void) { 1120 1108 int sigs[] = {SIGHUP, SIGINT, SIGQUIT, SIGTERM}; -
trunk/gtk/torrent-inspector.c
r2851 r3105 34 34 #include "tr_torrent.h" 35 35 #include "dot-icons.h" 36 #include "lock-icon.h" 36 37 #include "hig.h" 37 38 #include "torrent-inspector.h" … … 237 238 PEER_COL_PROGRESS, 238 239 PEER_COL_IS_CONNECTED, 240 PEER_COL_IS_ENCRYPTED, 239 241 PEER_COL_IS_DOWNLOADING, 240 242 PEER_COL_DOWNLOAD_RATE, … … 251 253 N_("Progress"), 252 254 " ", 255 " ", 253 256 N_("Downloading"), 254 257 N_("DL Rate"), … … 284 287 PEER_COL_PORT, peer->port, 285 288 PEER_COL_CLIENT, client, 289 PEER_COL_IS_ENCRYPTED, peer->isEncrypted, 286 290 PEER_COL_PROGRESS, (int)(100.0*peer->progress + 0.5), 287 291 PEER_COL_IS_CONNECTED, peer->isConnected, … … 315 319 G_TYPE_INT, /* progress [0..100] */ 316 320 G_TYPE_BOOLEAN, /* isConnected */ 321 G_TYPE_BOOLEAN, /* isEncrypted */ 317 322 G_TYPE_BOOLEAN, /* isDownloading */ 318 323 G_TYPE_FLOAT, /* downloadFromRate */ … … 349 354 350 355 static void 356 render_encrypted (GtkTreeViewColumn * column UNUSED, 357 GtkCellRenderer * renderer, 358 GtkTreeModel * tree_model, 359 GtkTreeIter * iter, 360 gpointer data UNUSED) 361 { 362 static GdkPixbuf * lock = NULL; 363 gboolean is_encrypted = FALSE; 364 gtk_tree_model_get (tree_model, iter, PEER_COL_IS_ENCRYPTED, &is_encrypted, 365 -1); 366 if (!lock) lock = gdk_pixbuf_new_from_inline (-1, lock_icon, FALSE, NULL); 367 g_object_set (renderer, "xalign", (gfloat)0.0, 368 "yalign", (gfloat)0.5, 369 "pixbuf", (is_encrypted ? lock : NULL), 370 NULL); 371 } 372 373 static void 351 374 render_ul_rate (GtkTreeViewColumn * column UNUSED, 352 375 GtkCellRenderer * renderer, … … 495 518 /* TODO: make this configurable? */ 496 519 int view_columns[] = { PEER_COL_IS_CONNECTED, 520 PEER_COL_IS_ENCRYPTED, 497 521 PEER_COL_ADDRESS, 498 522 PEER_COL_CLIENT, … … 537 561 c = gtk_tree_view_column_new_with_attributes ( 538 562 _("Progress"), r, "value", PEER_COL_PROGRESS, NULL); 563 break; 564 565 case PEER_COL_IS_ENCRYPTED: 566 resizable = FALSE; 567 r = gtk_cell_renderer_pixbuf_new (); 568 c = gtk_tree_view_column_new_with_attributes (t, r, NULL); 569 gtk_tree_view_column_set_sizing (c, GTK_TREE_VIEW_COLUMN_FIXED); 570 gtk_tree_view_column_set_fixed_width (c, 32); 571 gtk_tree_view_column_set_cell_data_func (c, r, render_encrypted, 572 NULL, NULL); 539 573 break; 540 574 -
trunk/libtransmission/Makefile.am
r2829 r3105 10 10 basename.c \ 11 11 bencode.c \ 12 choking.c \13 12 clients.c \ 14 13 completion.c \ 14 crypto.c \ 15 15 dirname.c \ 16 16 fastresume.c \ 17 17 fdlimit.c \ 18 handshake.c \ 18 19 http.c \ 19 20 inout.c \ … … 24 25 natpmp.c \ 25 26 net.c \ 26 peer.c \ 27 peer-io.c \ 28 peer-mgr.c \ 29 peer-msgs.c \ 27 30 platform.c \ 28 31 ptrarray.c \ 29 32 publish.c \ 30 33 ratecontrol.c \ 31 sha1.c \32 34 shared.c \ 33 35 strlcat.c \ 34 36 strlcpy.c \ 35 timer.c \36 37 torrent.c \ 37 38 tracker.c \ … … 46 47 bsdqueue.h \ 47 48 bsdtree.h \ 48 choking.h \49 49 clients.h \ 50 crypto.h \ 50 51 completion.h \ 51 52 fastresume.h \ 52 53 fdlimit.h \ 54 handshake.h \ 53 55 http.h \ 54 56 inout.h \ … … 60 62 natpmp.h \ 61 63 net.h \ 62 peeraz.h \ 63 peerext.h \ 64 peer.h \ 65 peermessages.h \ 66 peerparse.h \ 67 peertree.h \ 68 peerutils.h \ 64 peer-io.h \ 65 peer-mgr.h \ 66 peer-mgr-private.h \ 67 peer-msgs.h \ 69 68 platform.h \ 70 69 ptrarray.h \ 71 70 publish.h \ 72 71 ratecontrol.h \ 73 sha1.h \74 72 shared.h \ 75 timer.h \76 73 tracker.h \ 77 74 transmission.h \ -
trunk/libtransmission/bencode.c
r2555 r3105 365 365 benc_val_t * item; 366 366 367 assert( TYPE_LIST == list->type);367 assert( tr_bencIsList( list ) ); 368 368 assert( list->val.l.count < list->val.l.alloc ); 369 369 … … 379 379 benc_val_t * keyval, * itemval; 380 380 381 assert( TYPE_DICT == dict->type);381 assert( tr_bencIsDict( dict ) ); 382 382 assert( dict->val.l.count + 2 <= dict->val.l.alloc ); 383 383 … … 480 480 return 0; 481 481 } 482 483 /** 484 *** 485 **/ 486 487 int 488 tr_bencIsStr ( const benc_val_t * val ) 489 { 490 return val!=NULL && val->type==TYPE_STR; 491 } 492 493 int 494 tr_bencIsInt ( const benc_val_t * val ) 495 { 496 return val!=NULL && val->type==TYPE_INT; 497 } 498 499 int 500 tr_bencIsList( const benc_val_t * val ) 501 { 502 return val!=NULL && val->type==TYPE_LIST; 503 } 504 505 int 506 tr_bencIsDict( const benc_val_t * val ) 507 { 508 return val!=NULL && val->type==TYPE_DICT; 509 } -
trunk/libtransmission/bencode.h
r2076 r3105 91 91 int * used, int * max ); 92 92 93 int tr_bencIsStr ( const benc_val_t * val ); 94 int tr_bencIsInt ( const benc_val_t * val ); 95 int tr_bencIsList ( const benc_val_t * val ); 96 int tr_bencIsDict ( const benc_val_t * val ); 97 93 98 #endif -
trunk/libtransmission/clients.c
r2713 r3105 45 45 } 46 46 47 char * tr_clientForId( uint8_t * id )47 char * tr_clientForId( const uint8_t * id ) 48 48 { 49 49 char * ret = NULL; -
trunk/libtransmission/clients.h
r261 r3105 23 23 *****************************************************************************/ 24 24 25 char * tr_clientForId( uint8_t * );25 char * tr_clientForId( const uint8_t * ); -
trunk/libtransmission/completion.c
r2897 r3105 30 30 #include "utils.h" 31 31 32 struct tr_completion_s 33 { 34 tr_torrent_t * tor; 35 36 /* true if a peer is requesting this block */ 37 tr_bitfield_t * blockRequested; 32 struct tr_completion 33 { 34 tr_torrent * tor; 38 35 39 36 /* do we have this block? */ 40 tr_bitfield _t* blockBitfield;37 tr_bitfield * blockBitfield; 41 38 42 39 /* do we have this piece? */ 43 tr_bitfield _t* pieceBitfield;40 tr_bitfield * pieceBitfield; 44 41 45 42 /* a block is complete if and only if we have it */ … … 52 49 }; 53 50 54 tr_completion _t * tr_cpInit( tr_torrent_t * tor )55 { 56 tr_completion _t* cp;57 58 cp = tr_new( tr_completion _t, 1 );51 tr_completion * tr_cpInit( tr_torrent * tor ) 52 { 53 tr_completion * cp; 54 55 cp = tr_new( tr_completion, 1 ); 59 56 cp->tor = tor; 60 57 cp->blockBitfield = tr_bitfieldNew( tor->blockCount ); 61 cp->blockRequested = tr_bitfieldNew( tor->blockCount );62 58 cp->pieceBitfield = tr_bitfieldNew( tor->info.pieceCount ); 63 59 cp->completeBlocks = tr_new( uint16_t, tor->info.pieceCount ); … … 68 64 } 69 65 70 void tr_cpClose( tr_completion _t* cp )66 void tr_cpClose( tr_completion * cp ) 71 67 { 72 68 tr_free( cp->completeBlocks ); 73 69 tr_bitfieldFree( cp->pieceBitfield ); 74 tr_bitfieldFree( cp->blockRequested );75 70 tr_bitfieldFree( cp->blockBitfield ); 76 71 tr_free( cp ); 77 72 } 78 73 79 void tr_cpReset( tr_completion _t* cp )80 { 81 tr_torrent _t* tor = cp->tor;74 void tr_cpReset( tr_completion * cp ) 75 { 76 tr_torrent * tor = cp->tor; 82 77 83 78 tr_bitfieldClear( cp->pieceBitfield ); 84 79 tr_bitfieldClear( cp->blockBitfield ); 85 tr_bitfieldClear( cp->blockRequested );86 80 memset( cp->completeBlocks, 0, sizeof(uint16_t) * tor->info.pieceCount ); 87 81 … … 97 91 98 92 static void 99 tr_cpEnsureDoneValid( const tr_completion _t* ccp )100 { 101 const tr_torrent _t* tor = ccp->tor;93 tr_cpEnsureDoneValid( const tr_completion * ccp ) 94 { 95 const tr_torrent * tor = ccp->tor; 102 96 const tr_info_t * info = &tor->info; 103 97 uint64_t have=0, total=0; 104 98 int i; 105 tr_completion _t* cp ;99 tr_completion * cp ; 106 100 107 101 if( !ccp->doneDirty ) … … 109 103 110 104 /* too bad C doesn't have 'mutable' */ 111 cp = (tr_completion _t*) ccp;105 cp = (tr_completion*) ccp; 112 106 cp->doneDirty = FALSE; 113 107 … … 136 130 137 131 void 138 tr_cpInvalidateDND ( tr_completion _t* cp )132 tr_cpInvalidateDND ( tr_completion * cp ) 139 133 { 140 134 cp->doneDirty = TRUE; 141 135 } 142 136 143 int tr_cpPieceIsComplete( const tr_completion_t * cp, int piece ) 137 int 138 tr_cpPieceIsComplete( const tr_completion * cp, int piece ) 144 139 { 145 140 return cp->completeBlocks[piece] >= tr_torPieceCountBlocks(cp->tor,piece); 146 141 } 147 142 148 const tr_bitfield _t * tr_cpPieceBitfield( const tr_completion_t* cp )143 const tr_bitfield * tr_cpPieceBitfield( const tr_completion * cp ) 149 144 { 150 145 return cp->pieceBitfield; 151 146 } 152 147 153 void tr_cpPieceAdd( tr_completion _t* cp, int piece )154 { 155 const tr_torrent _t* tor = cp->tor;148 void tr_cpPieceAdd( tr_completion * cp, int piece ) 149 { 150 const tr_torrent * tor = cp->tor; 156 151 const int start = tr_torPieceFirstBlock(tor,piece); 157 152 const int end = start + tr_torPieceCountBlocks(tor,piece); … … 162 157 } 163 158 164 void tr_cpPieceRem( tr_completion _t* cp, int piece )165 { 166 const tr_torrent _t* tor = cp->tor;159 void tr_cpPieceRem( tr_completion * cp, int piece ) 160 { 161 const tr_torrent * tor = cp->tor; 167 162 const int start = tr_torPieceFirstBlock(tor,piece); 168 163 const int end = start + tr_torPieceCountBlocks(tor,piece); … … 191 186 } 192 187 193 /* Blocks */ 194 void tr_cpDownloaderAdd( tr_completion_t * cp, int block ) 195 { 196 tr_bitfieldAdd( cp->blockRequested, block ); 197 } 198 199 void tr_cpDownloaderRem( tr_completion_t * cp, int block ) 200 { 201 tr_bitfieldRem( cp->blockRequested, block ); 202 } 203 204 int tr_cpBlockIsComplete( const tr_completion_t * cp, int block ) 205 { 206 return tr_bitfieldHas( cp->blockBitfield, block ); 188 int tr_cpBlockIsComplete( const tr_completion * cp, int block ) 189 { 190 return tr_bitfieldHas( cp->blockBitfield, block ) ? 1 : 0; 207 191 } 208 192 209 193 void 210 tr_cpBlockAdd( tr_completion _t* cp, int block )211 { 212 const tr_torrent _t* tor = cp->tor;194 tr_cpBlockAdd( tr_completion * cp, int block ) 195 { 196 const tr_torrent * tor = cp->tor; 213 197 214 198 if( !tr_cpBlockIsComplete( cp, block ) ) … … 231 215 } 232 216 233 const tr_bitfield _t * tr_cpBlockBitfield( const tr_completion_t* cp )217 const tr_bitfield * tr_cpBlockBitfield( const tr_completion * cp ) 234 218 { 235 219 assert( cp != NULL ); … … 239 223 240 224 void 241 tr_cpBlockBitfieldSet( tr_completion _t * cp, tr_bitfield_t* bitfield )225 tr_cpBlockBitfieldSet( tr_completion * cp, tr_bitfield * bitfield ) 242 226 { 243 227 int i; … … 253 237 } 254 238 255 float tr_cpPercentBlocksInPiece( const tr_completion _t* cp, int piece )239 float tr_cpPercentBlocksInPiece( const tr_completion * cp, int piece ) 256 240 { 257 241 assert( cp != NULL ); 258 242 259 243 return (double)cp->completeBlocks[piece] / tr_torPieceCountBlocks(cp->tor,piece); 260 }261 262 int263 tr_cpMissingBlocksForPiece( const tr_completion_t * cp, int piece )264 {265 int i;266 int n;267 const tr_torrent_t * tor = cp->tor;268 const int start = tr_torPieceFirstBlock(tor,piece);269 const int end = start + tr_torPieceCountBlocks(tor,piece);270 271 n = 0;272 for( i = start; i < end; ++i )273 if( !tr_cpBlockIsComplete( cp, i ) && !tr_bitfieldHas( cp->blockRequested, i ) )274 ++n;275 276 return n;277 }278 279 int tr_cpMissingBlockInPiece( const tr_completion_t * cp, int piece )280 {281 int i;282 const tr_torrent_t * tor = cp->tor;283 const int start = tr_torPieceFirstBlock(tor,piece);284 const int end = start + tr_torPieceCountBlocks(tor,piece);285 286 for( i = start; i < end; ++i )287 if( !tr_cpBlockIsComplete( cp, i ) && !tr_bitfieldHas( cp->blockRequested, i ) )288 return i;289 290 return -1;291 244 } 292 245 … … 296 249 297 250 float 298 tr_cpPercentComplete ( const tr_completion _t* cp )251 tr_cpPercentComplete ( const tr_completion * cp ) 299 252 { 300 253 return (double)cp->completeHave / cp->tor->info.totalSize; … … 302 255 303 256 uint64_t 304 tr_cpLeftUntilComplete ( const tr_completion _t* cp )257 tr_cpLeftUntilComplete ( const tr_completion * cp ) 305 258 { 306 259 return cp->tor->info.totalSize - cp->completeHave; … … 308 261 309 262 float 310 tr_cpPercentDone( const tr_completion _t* cp )263 tr_cpPercentDone( const tr_completion * cp ) 311 264 { 312 265 tr_cpEnsureDoneValid( cp ); … … 316 269 317 270 uint64_t 318 tr_cpLeftUntilDone ( const tr_completion _t* cp )271 tr_cpLeftUntilDone ( const tr_completion * cp ) 319 272 { 320 273 tr_cpEnsureDoneValid( cp ); … … 324 277 325 278 cp_status_t 326 tr_cpGetStatus ( const tr_completion _t* cp )279 tr_cpGetStatus ( const tr_completion * cp ) 327 280 { 328 281 if( cp->completeHave >= cp->tor->info.totalSize ) … … 338 291 339 292 uint64_t 340 tr_cpDownloadedValid( const tr_completion _t* cp )293 tr_cpDownloadedValid( const tr_completion * cp ) 341 294 { 342 295 uint64_t b = 0; 343 const tr_torrent _t* tor = cp->tor;296 const tr_torrent * tor = cp->tor; 344 297 const tr_info_t * info = &tor->info; 345 298 int i; -
trunk/libtransmission/completion.h
r2891 r3105 28 28 #include "transmission.h" 29 29 30 typedef struct tr_completion _s tr_completion_t;30 typedef struct tr_completion tr_completion; 31 31 32 tr_completion _t * tr_cpInit( tr_torrent_t * );33 void tr_cpClose( tr_completion_t* );34 void tr_cpReset( tr_completion_t* );32 tr_completion * tr_cpInit( tr_torrent * ); 33 void tr_cpClose( tr_completion * ); 34 void tr_cpReset( tr_completion * ); 35 35 36 36 /* General */ 37 37 38 cp_status_t tr_cpGetStatus ( const tr_completion_t* );39 uint64_t tr_cpDownloadedValid( const tr_completion_t* );40 uint64_t tr_cpLeftUntilComplete( const tr_completion_t* );41 uint64_t tr_cpLeftUntilDone( const tr_completion_t* );42 float tr_cpPercentComplete( const tr_completion_t* );43 float tr_cpPercentDone( const tr_completion_t* );44 void tr_cpInvalidateDND ( tr_completion_t* );38 cp_status_t tr_cpGetStatus ( const tr_completion * ); 39 uint64_t tr_cpDownloadedValid( const tr_completion * ); 40 uint64_t tr_cpLeftUntilComplete( const tr_completion * ); 41 uint64_t tr_cpLeftUntilDone( const tr_completion * ); 42 float tr_cpPercentComplete( const tr_completion * ); 43 float tr_cpPercentDone( const tr_completion * ); 44 void tr_cpInvalidateDND ( tr_completion * ); 45 45 46 46 /* Pieces */ 47 int tr_cpPieceIsComplete( const tr_completion_t*, int piece );48 void tr_cpPieceAdd( tr_completion_t*, int piece );49 void tr_cpPieceRem( tr_completion_t*, int piece );47 int tr_cpPieceIsComplete( const tr_completion *, int piece ); 48 void tr_cpPieceAdd( tr_completion *, int piece ); 49 void tr_cpPieceRem( tr_completion *, int piece ); 50 50 51 51 /* Blocks */ 52 void tr_cpDownloaderAdd( tr_completion_t *, int block ); 53 void tr_cpDownloaderRem( tr_completion_t *, int block ); 54 int tr_cpBlockIsComplete( const tr_completion_t *, int block ); 55 void tr_cpBlockAdd( tr_completion_t *, int block ); 56 void tr_cpBlockBitfieldSet( tr_completion_t *, struct tr_bitfield_s * ); 57 float tr_cpPercentBlocksInPiece( const tr_completion_t * cp, int piece ); 58 /* Missing = we don't have it and we are not getting it from any peer yet */ 59 int tr_cpMissingBlocksForPiece( const tr_completion_t * cp, int piece ); 60 int tr_cpMissingBlockInPiece( const tr_completion_t *, int piece ); 52 int tr_cpBlockIsComplete( const tr_completion *, int block ); 53 void tr_cpBlockAdd( tr_completion *, int block ); 54 void tr_cpBlockBitfieldSet( tr_completion *, struct tr_bitfield * ); 55 float tr_cpPercentBlocksInPiece( const tr_completion * cp, int piece ); 61 56 62 const struct tr_bitfield _s * tr_cpPieceBitfield( const tr_completion_t* );63 const struct tr_bitfield _s * tr_cpBlockBitfield( const tr_completion_t* );57 const struct tr_bitfield * tr_cpPieceBitfield( const tr_completion* ); 58 const struct tr_bitfield * tr_cpBlockBitfield( const tr_completion * ); 64 59 65 60 #endif -
trunk/libtransmission/fastresume.c
r2891 r3105 56 56 #include "completion.h" 57 57 #include "fastresume.h" 58 #include "peer .h"58 #include "peer-mgr.h" 59 59 #include "platform.h" 60 60 #include "utils.h" … … 75 75 76 76 /* IPs and ports of connectable peers */ 77 FR_ID_PEERS = 4,77 FR_ID_PEERS_OLD = 4, 78 78 79 79 /* progress data: … … 89 89 /* transfer speeds 90 90 * uint32_t: dl speed rate to use when the mode is single 91 * uint32_t: dl's tr_speedlimit _t91 * uint32_t: dl's tr_speedlimit 92 92 * uint32_t: ul speed rate to use when the mode is single 93 * uint32_t: ul's tr_speedlimit _t93 * uint32_t: ul's tr_speedlimit 94 94 */ 95 95 FR_ID_SPEED = 8, … … 101 101 102 102 /* number of corrupt bytes downloaded */ 103 FR_ID_CORRUPT = 10 103 FR_ID_CORRUPT = 10, 104 105 /* IPs and ports of connectable peers */ 106 FR_ID_PEERS = 11 104 107 }; 105 108 … … 115 118 116 119 static void 117 fastResumeFileName( char * buf, size_t buflen, const tr_torrent _t* tor, int tag )120 fastResumeFileName( char * buf, size_t buflen, const tr_torrent * tor, int tag ) 118 121 { 119 122 const char * cacheDir = tr_getCacheDirectory (); … … 133 136 134 137 static tr_time_t* 135 getMTimes( const tr_torrent _t* tor, int * setme_n )138 getMTimes( const tr_torrent * tor, int * setme_n ) 136 139 { 137 140 int i; … … 172 175 173 176 void 174 tr_fastResumeSave( const tr_torrent _t* tor )177 tr_fastResumeSave( const tr_torrent * tor ) 175 178 { 176 179 char path[MAX_PATH_LENGTH]; … … 195 198 uint8_t * buf = malloc( FR_PROGRESS_LEN( tor ) ); 196 199 uint8_t * walk = buf; 197 const tr_bitfield _t* bitfield;200 const tr_bitfield * bitfield; 198 201 199 202 /* mtimes */ … … 284 287 285 288 /* Write download and upload totals */ 289 286 290 total = tor->downloadedCur + tor->downloadedPrev; 287 291 fastResumeWriteData( FR_ID_DOWNLOADED, &total, 8, 1, file ); 292 288 293 total = tor->uploadedCur + tor->uploadedPrev; 289 294 fastResumeWriteData( FR_ID_UPLOADED, &total, 8, 1, file ); 295 290 296 total = tor->corruptCur + tor->corruptPrev; 291 297 fastResumeWriteData( FR_ID_CORRUPT, &total, 8, 1, file ); … … 293 299 if( !( TR_FLAG_PRIVATE & tor->info.flags ) ) 294 300 { 295 /* Write IPs and ports of connectable peers, if any */ 296 int size; 297 uint8_t * buf = NULL; 298 if( ( size = tr_peerGetConnectable( tor, &buf ) ) > 0 ) 299 { 300 fastResumeWriteData( FR_ID_PEERS, buf, size, 1, file ); 301 free( buf ); 302 } 301 tr_pex * pex; 302 const int count = tr_peerMgrGetPeers( tor->handle->peerMgr, 303 tor->info.hash, 304 &pex ); 305 if( count > 0 ) 306 fastResumeWriteData( FR_ID_PEERS, pex, sizeof(tr_pex), count, file ); 307 tr_free( pex ); 303 308 } 304 309 … … 309 314 310 315 static int 311 loadSpeeds( tr_torrent _t* tor, FILE * file )316 loadSpeeds( tr_torrent * tor, FILE * file ) 312 317 { 313 318 const size_t len = FR_SPEED_LEN; … … 326 331 tr_torrentSetSpeedLimit( tor, TR_DOWN, i16 ); 327 332 memcpy( &i8, walk, 1 ); walk += 1; 328 tr_torrentSetSpeedMode( tor, TR_DOWN, (tr_speedlimit _t)i8 );333 tr_torrentSetSpeedMode( tor, TR_DOWN, (tr_speedlimit)i8 ); 329 334 memcpy( &i16, walk, 2 ); walk += 2; 330 335 tr_torrentSetSpeedLimit( tor, TR_UP, i16 ); 331 336 memcpy( &i8, walk, 1 ); walk += 1; 332 tr_torrentSetSpeedMode( tor, TR_UP, (tr_speedlimit _t)i8 );337 tr_torrentSetSpeedMode( tor, TR_UP, (tr_speedlimit)i8 ); 333 338 334 339 tr_free( buf ); … … 338 343 339 344 static int 340 loadPriorities( tr_torrent _t* tor,341 FILE 345 loadPriorities( tr_torrent * tor, 346 FILE * file ) 342 347 { 343 348 const size_t n = tor->info.fileCount; … … 388 393 389 394 static int 390 fastResumeLoadProgress( const tr_torrent _t* tor,391 tr_bitfield _t* uncheckedPieces,392 FILE 395 fastResumeLoadProgress( const tr_torrent * tor, 396 tr_bitfield * uncheckedPieces, 397 FILE * file ) 393 398 { 394 399 int i; … … 410 415 for( i=0; i<n; ++i ) { 411 416 if ( curMTimes[i]!=oldMTimes[i] ) { 412 const tr_file _t* file = &tor->info.files[i];417 const tr_file * file = &tor->info.files[i]; 413 418 tr_dbg( "File '%s' mtimes differ-- flagging pieces [%d..%d] for recheck", 414 419 file->name, file->firstPiece, file->lastPiece); … … 423 428 /* get the completion bitfield */ 424 429 if (1) { 425 tr_bitfield _tbitfield;430 tr_bitfield bitfield; 426 431 memset( &bitfield, 0, sizeof bitfield ); 427 432 bitfield.len = FR_BLOCK_BITFIELD_LEN( tor ); … … 442 447 443 448 static uint64_t 444 fastResumeLoadOld( tr_torrent _t* tor,445 tr_bitfield _t* uncheckedPieces,446 FILE 449 fastResumeLoadOld( tr_torrent * tor, 450 tr_bitfield * uncheckedPieces, 451 FILE * file ) 447 452 { 448 453 uint64_t ret = 0; … … 476 481 477 482 static uint64_t 478 fastResumeLoadImpl ( tr_torrent _t* tor,479 tr_bitfield _t* uncheckedPieces )483 fastResumeLoadImpl ( tr_torrent * tor, 484 tr_bitfield * uncheckedPieces ) 480 485 { 481 486 char path[MAX_PATH_LENGTH]; … … 637 642 break; 638 643 644 case FR_ID_PEERS_OLD: 645 if( !( TR_FLAG_PRIVATE & tor->info.flags ) ) 646 { 647 uint8_t * buf = malloc( len ); 648 if( 1 != fread( buf, len, 1, file ) ) 649 { 650 free( buf ); 651 fclose( file ); 652 return ret; 653 } 654 655 tr_peerMgrAddPeers( tor->handle->peerMgr, 656 tor->info.hash, 657 TR_PEER_FROM_CACHE, 658 buf, len / 6 ); 659 660 tr_dbg( "found %i peers in resume file", len/6 ); 661 free( buf ); 662 ret |= TR_FR_PEERS; 663 } 664 639 665 case FR_ID_PEERS: 640 666 if( !( TR_FLAG_PRIVATE & tor->info.flags ) ) 641 667 { 642 int used; 643 uint8_t * buf = malloc( len ); 644 if( 1 != fread( buf, len, 1, file ) ) 645 { 646 free( buf ); 647 fclose( file ); 648 return ret; 649 } 650 used = tr_torrentAddCompact( tor, TR_PEER_FROM_CACHE, 651 buf, len / 6 ); 652 tr_dbg( "found %i peers in resume file, used %i", 653 len / 6, used ); 654 free( buf ); 668 const int count = len / sizeof(tr_pex); 669 tr_pex * pex = tr_new0( tr_pex, count ); 670 if( 1 != fread( pex, sizeof(tr_pex), count, file ) ) 671 { 672 free( pex ); 673 fclose( file ); 674 return ret; 675 } 676 677 tr_peerMgrAddPex( tor->handle->peerMgr, 678 tor->info.hash, 679 TR_PEER_FROM_CACHE, 680 pex, count ); 681 682 tr_dbg( "found %i peers in resume file", len/6 ); 683 free( pex ); 655 684 ret |= TR_FR_PEERS; 656 685 } … … 671 700 672 701 uint64_t 673 tr_fastResumeLoad( tr_torrent _t* tor,674 tr_bitfield _t* uncheckedPieces )702 tr_fastResumeLoad( tr_torrent * tor, 703 tr_bitfield * uncheckedPieces ) 675 704 { 676 705 const uint64_t ret = fastResumeLoadImpl( tor, uncheckedPieces ); -
trunk/libtransmission/fastresume.h
r2891 r3105 26 26 #define TR_FAST_RESUME_H 27 27 28 void tr_fastResumeSave( const tr_torrent _t* tor );28 void tr_fastResumeSave( const tr_torrent * tor ); 29 29 30 30 enum … … 43 43 * Returns a bitwise-or'ed set of the data loaded from fastresume 44 44 */ 45 uint64_t tr_fastResumeLoad( tr_torrent _t* tor,46 struct tr_bitfield _s* uncheckedPieces );45 uint64_t tr_fastResumeLoad( tr_torrent * tor, 46 struct tr_bitfield * uncheckedPieces ); 47 47 48 48 #endif -
trunk/libtransmission/fdlimit.c
r2596 r3105 64 64 typedef struct tr_fd_s 65 65 { 66 tr_lock _t* lock;67 tr_cond _t* cond;66 tr_lock * lock; 67 tr_cond * cond; 68 68 69 69 int reserved; … … 391 391 void tr_fdSocketClose( int s ) 392 392 { 393 tr_lockLock( gFd->lock ); 393 if( s >= 0 ) 394 { 395 tr_lockLock( gFd->lock ); 394 396 #ifdef BEOS_NETSERVER 395 closesocket( s );397 closesocket( s ); 396 398 #else 397 close( s );399 close( s ); 398 400 #endif 399 if( SocketGetPriority( s ) ) 400 gFd->reserved--; 401 else 402 gFd->normal--; 403 tr_lockUnlock( gFd->lock ); 401 if( SocketGetPriority( s ) ) 402 gFd->reserved--; 403 else 404 gFd->normal--; 405 tr_lockUnlock( gFd->lock ); 406 } 404 407 } 405 408 -
trunk/libtransmission/http.c
r2686 r3105 32 32 #include <sys/types.h> 33 33 34 #include "evdns.h" 35 34 36 #ifdef __BEOS__ 35 37 extern int vasprintf( char **, const char *, va_list ); … … 85 87 #define HTTP_LENGTH_CHUNKED 4 86 88 char lengthtype; 87 tr_resolve_t * resolve;88 89 char * host; 89 90 int port; … … 568 569 } 569 570 571 static void 572 resolve_cb( int result, char type, int count, int ttl, void *addresses, void *arg) 573 { 574 tr_http_t * http = (tr_http_t *) arg; 575 576 if( (result!=DNS_ERR_NONE) || (type!=DNS_IPv4_A) || (ttl<0) || (count<1) ) 577 http->state = HTTP_STATE_ERROR; 578 else { 579 struct in_addr *in_addrs = addresses; 580 http->sock = tr_netOpenTCP( &in_addrs[0], htons(http->port), 1 ); 581 http->state = HTTP_STATE_CONNECT; 582 } 583 } 584 570 585 tr_tristate_t 571 586 tr_httpPulse( tr_http_t * http, const char ** data, int * len ) … … 587 602 break; 588 603 } 589 http->resolve = tr_netResolveInit( http->host ); 590 if( NULL == http->resolve ) 604 if( evdns_resolve_ipv4( http->host, 0, resolve_cb, http ) ) 591 605 { 592 606 goto err; … … 596 610 597 611 case HTTP_STATE_RESOLVE: 598 switch( tr_netResolvePulse( http->resolve, &addr ) ) 599 { 600 case TR_NET_WAIT: 601 return TR_NET_WAIT; 602 case TR_NET_ERROR: 603 goto err; 604 case TR_NET_OK: 605 tr_netResolveClose( http->resolve ); 606 http->resolve = NULL; 607 http->sock = tr_netOpenTCP( &addr, htons( http->port ), 1 ); 608 http->state = HTTP_STATE_CONNECT; 609 } 610 /* fallthrough */ 612 return TR_NET_WAIT; 611 613 612 614 case HTTP_STATE_CONNECT: … … 898 900 tr_httpClose( tr_http_t * http ) 899 901 { 900 if( NULL != http->resolve )901 {902 tr_netResolveClose( http->resolve );903 }904 902 free( http->host ); 905 if( 0 <= http->sock ) 906 { 907 tr_netClose( http->sock ); 908 } 903 tr_netClose( http->sock ); 909 904 free( http->header.buf ); 910 905 free( http->body.buf ); -
trunk/libtransmission/inout.c
r2994 r3105 21 21 #include "transmission.h" 22 22 #include "completion.h" 23 #include "crypto.h" 24 #include "fastresume.h" 23 25 #include "fdlimit.h" 24 26 #include "inout.h" 27 #include "list.h" 25 28 #include "net.h" 26 #include "p eer.h"27 #include " sha1.h"29 #include "platform.h" 30 #include "peer-mgr.h" 28 31 #include "utils.h" 29 32 30 struct tr_io _s31 { 32 tr_torrent _t* tor;33 struct tr_io 34 { 35 tr_torrent * tor; 33 36 }; 34 37 … … 37 40 ****/ 38 41 39 enum { TR_IO_READ, TR_IO_WRITE };40 41 42 #ifdef WIN32 42 43 #define lseek _lseeki64 43 44 #endif 44 45 45 static int 46 readOrWriteBytes ( const tr_torrent_t * tor, 46 enum { TR_IO_READ, TR_IO_WRITE }; 47 48 static int 49 readOrWriteBytes ( const tr_torrent * tor, 47 50 int ioMode, 48 51 int fileIndex, … … 51 54 size_t buflen ) 52 55 { 53 const tr_info _t* info = &tor->info;54 const tr_file _t* file = &info->files[fileIndex];56 const tr_info * info = &tor->info; 57 const tr_file * file = &info->files[fileIndex]; 55 58 typedef size_t (* iofunc) ( int, void *, size_t ); 56 59 iofunc func = ioMode == TR_IO_READ ? (iofunc)read : (iofunc)write; … … 86 89 87 90 static void 88 findFileLocation ( const tr_torrent _t* tor,91 findFileLocation ( const tr_torrent * tor, 89 92 int pieceIndex, 90 93 int pieceOffset, … … 92 95 uint64_t * fileOffset ) 93 96 { 94 const tr_info _t* info = &tor->info;97 const tr_info * info = &tor->info; 95 98 96 99 int i; … … 113 116 114 117 static int 115 ensureMinimumFileSize ( const tr_torrent _t* tor,118 ensureMinimumFileSize ( const tr_torrent * tor, 116 119 int fileIndex, 117 120 uint64_t minSize ) /* in bytes */ … … 120 123 int ret; 121 124 struct stat sb; 122 const tr_file _t* file = &tor->info.files[fileIndex];125 const tr_file * file = &tor->info.files[fileIndex]; 123 126 124 127 assert ( 0<=fileIndex && fileIndex<tor->info.fileCount ); … … 144 147 145 148 static int 146 readOrWritePiece ( tr_torrent _t* tor,147 int 148 int 149 int 150 uint8_t 151 size_t 149 readOrWritePiece ( tr_torrent * tor, 150 int ioMode, 151 int pieceIndex, 152 int pieceOffset, 153 uint8_t * buf, 154 size_t buflen ) 152 155 { 153 156 int ret = 0; 154 157 int fileIndex; 155 158 uint64_t fileOffset; 156 const tr_info _t* info = &tor->info;159 const tr_info * info = &tor->info; 157 160 158 161 assert( 0<=pieceIndex && pieceIndex<tor->info.pieceCount ); … … 163 166 while( buflen && !ret ) 164 167 { 165 const tr_file _t* file = &info->files[fileIndex];168 const tr_file * file = &info->files[fileIndex]; 166 169 const uint64_t bytesThisPass = MIN( buflen, file->length - fileOffset ); 167 170 … … 182 185 183 186 int 184 tr_ioRead( tr_ io_t * io, int pieceIndex, int begin, int len, uint8_t * buf )185 { 186 return readOrWritePiece ( io->tor, TR_IO_READ, pieceIndex, begin, buf, len );187 tr_ioRead( tr_torrent * tor, int pieceIndex, int begin, int len, uint8_t * buf ) 188 { 189 return readOrWritePiece ( tor, TR_IO_READ, pieceIndex, begin, buf, len ); 187 190 } 188 191 189 192 int 190 tr_ioWrite( tr_ io_t * io, int pieceIndex, int begin, int len, uint8_t * buf )191 { 192 return readOrWritePiece ( io->tor, TR_IO_WRITE, pieceIndex, begin, buf, len );193 tr_ioWrite( tr_torrent * tor, int pieceIndex, int begin, int len, uint8_t * buf ) 194 { 195 return readOrWritePiece ( tor, TR_IO_WRITE, pieceIndex, begin, buf, len ); 193 196 } 194 197 … … 198 201 199 202 static int 200 tr_ioRecalculateHash ( tr_torrent _t* tor,203 tr_ioRecalculateHash ( tr_torrent * tor, 201 204 int pieceIndex, 202 205 uint8_t * setme ) … … 205 208 int ret; 206 209 uint8_t * buf; 207 const tr_info _t* info;210 const tr_info * info; 208 211 209 212 assert( tor != NULL ); … … 216 219 buf = malloc( n ); 217 220 ret = readOrWritePiece ( tor, TR_IO_READ, pieceIndex, 0, buf, n ); 218 if( !ret ) { 219 SHA1( buf, n, setme ); 220 } 221 if( !ret ) 222 tr_sha1( setme, buf, n, NULL ); 221 223 free( buf ); 222 224 … … 225 227 226 228 static int 227 checkPiece ( tr_torrent _t* tor, int pieceIndex )229 checkPiece ( tr_torrent * tor, int pieceIndex ) 228 230 { 229 231 uint8_t hash[SHA_DIGEST_LENGTH]; … … 235 237 } 236 238 239 /** 240 *** 241 **/ 242 237 243 void 238 tr_ioCheckFiles( tr_torrent_t * tor ) 239 { 240 assert( tor != NULL ); 241 assert( tor->completion != NULL ); 242 assert( tor->info.pieceCount > 0 ); 243 244 if( tor->uncheckedPieces != NULL ) 244 tr_ioClose( const tr_torrent * tor ) 245 { 246 int i; 247 const tr_info * info = &tor->info; 248 249 for( i=0; i<info->fileCount; ++i ) 250 tr_fdFileClose( tor->destination, info->files[i].name ); 251 } 252 253 int 254 tr_ioHash( tr_torrent * tor, int pieceIndex ) 255 { 256 int ret; 257 const int success = !checkPiece( tor, pieceIndex ); 258 259 if( success ) 260 { 261 tr_dbg( "Piece %d hash OK", pieceIndex ); 262 tr_cpPieceAdd( tor->completion, pieceIndex ); 263 ret = TR_OK; 264 } 265 else 266 { 267 tr_err( "Piece %d hash FAILED", pieceIndex ); 268 tr_cpPieceRem( tor->completion, pieceIndex ); 269 ret = TR_ERROR; 270 } 271 272 tr_peerMgrSetBlame( tor->handle->peerMgr, tor->info.hash, pieceIndex, success ); 273 274 return ret; 275 } 276 277 /** 278 *** 279 **/ 280 281 struct recheck_node 282 { 283 tr_torrent * torrent; 284 tr_recheck_done_cb recheck_done_cb; 285 run_status_t status_when_done; 286 }; 287 288 static void 289 fireCheckDone( tr_torrent * torrent, 290 tr_recheck_done_cb recheck_done_cb, 291 run_status_t status_when_done ) 292 { 293 torrent->runStatus = status_when_done; 294 (*recheck_done_cb)( torrent ); 295 } 296 297 struct recheck_node currentNode; 298 299 static tr_list * recheckList = NULL; 300 301 static tr_thread * recheckThread = NULL; 302 303 static int stopCurrent = FALSE; 304 305 static void 306 recheckThreadFunc( void * unused UNUSED ) 307 { 308 for( ;; ) 245 309 { 246 310 int i; 311 tr_torrent * tor; 312 313 struct recheck_node * node = (struct recheck_node*) recheckList ? recheckList->data : NULL; 314 if( node == NULL ) 315 break; 316 317 currentNode = *node; 318 tor = currentNode.torrent; 319 tr_list_remove_data( &recheckList, node ); 320 tr_free( node ); 321 322 if( tor->uncheckedPieces == NULL ) { 323 fireCheckDone( tor, currentNode.recheck_done_cb, currentNode.status_when_done ); 324 continue; 325 } 326 327 tor->runStatus = TR_RUN_CHECKING; 247 328 248 329 /* remove the unchecked pieces from completion... */ … … 253 334 tr_inf( "Verifying some pieces of \"%s\"", tor->info.name ); 254 335 255 for( i=0; i<tor->info.pieceCount ; ++i )336 for( i=0; i<tor->info.pieceCount && !stopCurrent; ++i ) 256 337 { 257 338 if( !tr_bitfieldHas( tor->uncheckedPieces, i ) ) … … 262 343 } 263 344 264 tr_bitfieldFree( tor->uncheckedPieces ); 265 tor->uncheckedPieces = NULL; 266 tor->fastResumeDirty = TRUE; 267 } 268 } 269 270 /**** 271 ***** Life Cycle 272 ****/ 273 274 tr_io_t* 275 tr_ioNew ( tr_torrent_t * tor ) 276 { 277 tr_io_t * io = tr_calloc( 1, sizeof( tr_io_t ) ); 278 io->tor = tor; 279 return io; 280 } 281 345 if( !stopCurrent ) 346 { 347 tr_bitfieldFree( tor->uncheckedPieces ); 348 tor->uncheckedPieces = NULL; 349 } 350 stopCurrent = FALSE; 351 tr_fastResumeSave( tor ); 352 fireCheckDone( tor, currentNode.recheck_done_cb, currentNode.status_when_done ); 353 } 354 355 recheckThread = NULL; 356 } 282 357 283 358 void 284 tr_ioSync( tr_io_t * io ) 285 { 286 if( io != NULL ) 287 { 288 int i; 289 const tr_info_t * info = &io->tor->info; 290 291 for( i=0; i<info->fileCount; ++i ) 292 tr_fdFileClose( io->tor->destination, info->files[i].name ); 293 } 359 tr_ioRecheckAdd( tr_torrent * tor, 360 tr_recheck_done_cb recheck_done_cb, 361 run_status_t status_when_done ) 362 { 363 if( tor->uncheckedPieces == NULL ) 364 { 365 fireCheckDone( tor, recheck_done_cb, status_when_done ); 366 } 367 else 368 { 369 struct recheck_node * node; 370 node = tr_new( struct recheck_node, 1 ); 371 node->torrent = tor; 372 node->recheck_done_cb = recheck_done_cb; 373 node->status_when_done = status_when_done; 374 tr_list_append( &recheckList, node ); 375 376 tor->runStatus = TR_RUN_CHECKING_WAIT; 377 378 if( recheckThread == NULL ) 379 recheckThread = tr_threadNew( recheckThreadFunc, NULL, "recheckThreadFunc" ); 380 } 381 } 382 383 static int 384 compareRecheckByTorrent( const void * va, const void * vb ) 385 { 386 const struct recheck_node * a = ( const struct recheck_node * ) va; 387 const struct recheck_node * b = ( const struct recheck_node * ) vb; 388 return a->torrent - b->torrent; 294 389 } 295 390 296 391 void 297 tr_ioClose( tr_io_t * io ) 298 { 299 if( io != NULL ) 300 { 301 tr_ioSync( io ); 302 tr_free( io ); 303 } 304 } 305 306 int 307 tr_ioHash( tr_io_t * io, int pieceIndex ) 308 { 309 int i; 310 int ret; 311 tr_torrent_t * tor = io->tor; 312 const int success = !checkPiece( tor, pieceIndex ); 313 314 if( success ) 315 { 316 tr_dbg( "Piece %d hash OK", pieceIndex ); 317 tr_cpPieceAdd( tor->completion, pieceIndex ); 318 ret = TR_OK; 319 } 320 else 321 { 322 tr_err( "Piece %d hash FAILED", pieceIndex ); 323 tr_cpPieceRem( tor->completion, pieceIndex ); 324 ret = TR_ERROR; 325 } 326 327 /* Assign blame or credit to peers */ 328 for( i=0; i<tor->peerCount; ++i ) 329 tr_peerBlame( tor->peers[i], pieceIndex, success ); 330 331 return ret; 332 } 392 tr_ioRecheckRemove( tr_torrent * tor ) 393 { 394 if( tor == currentNode.torrent ) 395 stopCurrent = TRUE; 396 else { 397 struct recheck_node tmp; 398 tmp.torrent = tor; 399 struct recheck_node * node = tr_list_remove( &recheckList, &tmp, compareRecheckByTorrent ); 400 if( node != NULL ) { 401 fireCheckDone( tor, node->recheck_done_cb, node->status_when_done ); 402 tr_free( node ); 403 } 404 } 405 } -
trunk/libtransmission/inout.h
r2462 r3105 26 26 #define TR_IO_H 1 27 27 28 typedef struct tr_io_s tr_io_t;28 struct tr_torrent; 29 29 30 void tr_ioCheckFiles ( tr_torrent_t * ); 31 32 tr_io_t * tr_ioNew ( tr_torrent_t * ); 30 typedef struct tr_io tr_io; 33 31 34 32 /*********************************************************************** … … 40 38 * TR_ERROR_IO_* otherwise. 41 39 **********************************************************************/ 42 int tr_ioRead ( tr_io_t*, int index, int begin, int len, uint8_t * );43 int tr_ioWrite ( tr_io_t *, int index, int begin, int len, uint8_t * );40 int tr_ioRead ( struct tr_torrent*, int index, int begin, int len, uint8_t * ); 41 int tr_ioWrite ( struct tr_torrent *, int index, int begin, int len, uint8_t * ); 44 42 45 /*********************************************************************** 46 * tr_ioHash 47 *********************************************************************** 48 * Hashes the specified piece and updates the completion accordingly. 49 **********************************************************************/ 50 int tr_ioHash ( tr_io_t *, int piece ); 43 /* hashes the specified piece and updates the completion accordingly. */ 44 int tr_ioHash ( tr_torrent*, int piece ); 51 45 52 /*********************************************************************** 53 * tr_ioSync 54 *********************************************************************** 55 * Flush all data on disc by closing all files, and update the cache 56 * file. 57 **********************************************************************/ 58 void tr_ioSync( tr_io_t * ); 46 /* close all the files associated with this torrent*/ 47 void tr_ioClose( const tr_torrent * ); 59 48 60 void tr_ioClose ( tr_io_t * ); 49 /** 50 *** 51 **/ 52 53 typedef void (*tr_recheck_done_cb)( tr_torrent * tor ); 54 55 void tr_ioRecheckAdd( tr_torrent * tor, 56 tr_recheck_done_cb recheck_done_cb, 57 run_status_t status_when_done ); 58 59 void tr_ioRecheckRemove( tr_torrent * tor ); 60 61 /** 62 *** 63 **/ 61 64 62 65 #endif -
trunk/libtransmission/internal.h
r2891 r3105 46 46 #endif 47 47 48 int tr_trackerInfoInit( struct tr_tracker_info _s* info,49 const char 50 int 48 int tr_trackerInfoInit( struct tr_tracker_info * info, 49 const char * address, 50 int address_len ); 51 51 52 void tr_trackerInfoClear( struct tr_tracker_info_s * info ); 53 54 struct tr_peer_s; 52 void tr_trackerInfoClear( struct tr_tracker_info * info ); 55 53 56 54 void tr_peerIdNew ( char* buf, int buflen ); 57 55 58 void tr_torrentResetTransferStats( tr_torrent _t* );56 void tr_torrentResetTransferStats( tr_torrent * ); 59 57 60 int tr_torrentAddCompact( tr_torrent_t * tor, int from, 61 const uint8_t * buf, int count ); 62 int tr_torrentAttachPeer( tr_torrent_t * tor, struct tr_peer_s * ); 58 void tr_torrentSetHasPiece( tr_torrent * tor, int pieceIndex, int has ); 63 59 64 void tr_torrentSetHasPiece( tr_torrent_t * tor, int pieceIndex, int has ); 60 void tr_torrentLock ( const tr_torrent * ); 61 void tr_torrentUnlock ( const tr_torrent * ); 65 62 66 void tr_torrentReaderLock ( const tr_torrent_t * ); 67 void tr_torrentReaderUnlock ( const tr_torrent_t * ); 68 void tr_torrentWriterLock ( tr_torrent_t * ); 69 void tr_torrentWriterUnlock ( tr_torrent_t * ); 63 void tr_torrentChangeMyPort ( tr_torrent *, int port ); 70 64 71 void tr_torrentChangeMyPort ( tr_torrent_t *, int port ); 65 int tr_torrentExists( tr_handle *, const uint8_t * ); 66 tr_torrent* tr_torrentFindFromHash( tr_handle *, const uint8_t * ); 67 tr_torrent* tr_torrentFindFromObfuscatedHash( tr_handle *, const uint8_t* ); 72 68 73 69 /* get the index of this piece's first block */ … … 90 86 91 87 #define tr_block(a,b) _tr_block(tor,a,b) 92 int _tr_block( const tr_torrent _t* tor, int index, int begin );88 int _tr_block( const tr_torrent * tor, int index, int begin ); 93 89 94 90 95 91 typedef enum 96 92 { 97 TR_RUN_CHECKING = (1<<0), /* checking files' checksums */ 98 TR_RUN_RUNNING = (1<<1), /* seeding or leeching */ 99 TR_RUN_STOPPING = (1<<2), /* stopping */ 100 TR_RUN_STOPPING_NET_WAIT = (1<<3), /* waiting on network -- we're 101 telling tracker we've stopped */ 93 TR_RUN_CHECKING_WAIT = (1<<0), /* waiting to be checked */ 94 TR_RUN_CHECKING = (1<<1), /* checking files' checksums */ 95 TR_RUN_RUNNING = (1<<2), /* seeding or leeching */ 96 TR_RUN_STOPPING = (1<<3), /* waiting for acknowledgment from tracker */ 102 97 TR_RUN_STOPPED = (1<<4) /* stopped */ 103 98 } … … 106 101 #define TR_ID_LEN 20 107 102 108 struct tr_torrent _s103 struct tr_torrent 109 104 { 110 tr_handle _t* handle;111 tr_info _tinfo;105 tr_handle * handle; 106 tr_info info; 112 107 113 tr_speedlimit_t uploadLimitMode; 114 tr_speedlimit_t downloadLimitMode; 115 struct tr_ratecontrol_s * upload; 116 struct tr_ratecontrol_s * download; 117 struct tr_ratecontrol_s * swarmspeed; 108 tr_speedlimit uploadLimitMode; 109 tr_speedlimit downloadLimitMode; 110 struct tr_ratecontrol * upload; 111 struct tr_ratecontrol * download; 112 struct tr_ratecontrol * swarmspeed; 113 114 struct tr_timer * saveTimer; 118 115 119 116 int error; 120 117 char errorString[128]; 121 118 int hasChangedState; 119 120 uint8_t obfuscatedHash[SHA_DIGEST_LENGTH]; 122 121 123 122 uint8_t * azId; … … 137 136 int blockCountInLastPiece; 138 137 139 struct tr_completion _s* completion;138 struct tr_completion * completion; 140 139 141 140 volatile char dieFlag; 142 struct tr_bitfield _s* uncheckedPieces;141 struct tr_bitfield * uncheckedPieces; 143 142 run_status_t runStatus; 144 143 run_status_t runStatusToSave; 145 144 char runStatusToSaveIsSet; 146 145 cp_status_t cpStatus; 147 struct tr_thread_s * thread; 148 struct tr_rwlock_s * lock; 146 struct tr_lock * lock; 149 147 150 struct tr_tracker _s* tracker;148 struct tr_tracker * tracker; 151 149 struct tr_publisher_tag * trackerSubscription; 152 struct tr_io_s * io;153 150 uint64_t startDate; 154 151 uint64_t stopDate; 155 char ioLoaded;156 char fastResumeDirty;157 158 int peerCount;159 struct tr_peer_s * peers[TR_MAX_PEER_COUNT];160 152 161 153 uint64_t downloadedCur; … … 170 162 171 163 int8_t statCur; 172 tr_stat _tstats[2];164 tr_stat stats[2]; 173 165 174 tr_torrent _t* next;166 tr_torrent * next; 175 167 }; 176 168 177 struct tr_handle _s169 struct tr_handle 178 170 { 179 struct tr_event_handle_s * events; 171 tr_encryption_mode encryptionMode; 172 173 struct tr_event_handle * events; 180 174 181 175 int torrentCount; 182 tr_torrent _t* torrentList;176 tr_torrent * torrentList; 183 177 184 178 char * tag; … … 187 181 char useUploadLimit; 188 182 char useDownloadLimit; 189 struct tr_ratecontrol _s* upload;190 struct tr_ratecontrol _s* download;183 struct tr_ratecontrol * upload; 184 struct tr_ratecontrol * download; 191 185 192 struct tr_shared_s * shared; 186 struct tr_peerMgr * peerMgr; 187 struct tr_shared * shared; 193 188 194 tr_handle_status _tstats[2];189 tr_handle_status stats[2]; 195 190 int statCur; 191 192 uint8_t isClosed; 196 193 197 194 #define TR_AZ_ID_LEN 20 -
trunk/libtransmission/ipcparse.c
r2391 r3105 586 586 587 587 int 588 ipc_addinfo( benc_val_t * list, int tor, const tr_info _t* inf, int types )588 ipc_addinfo( benc_val_t * list, int tor, const tr_info * inf, int types ) 589 589 { 590 590 benc_val_t * dict, * item, * file, * tier; … … 728 728 int 729 729 ipc_addstat( benc_val_t * list, int tor, 730 const tr_stat _t* st, int types )730 const tr_stat * st, int types ) 731 731 { 732 732 benc_val_t * dict, * item; -
trunk/libtransmission/ipcparse.h
r2342 r3105 28 28 #include <inttypes.h> 29 29 30 /* yay for typedefs, we can't forward declare benc_val_t or tr_info _t30 /* yay for typedefs, we can't forward declare benc_val_t or tr_info 31 31 like with structs */ 32 32 #include "bencode.h" … … 160 160 uint8_t * ipc_mkgetinfo( struct ipc_info *, size_t *, enum ipc_msg, int64_t, 161 161 int, const int * ); 162 int ipc_addinfo ( benc_val_t *, int, const tr_info _t*, int );163 int ipc_addstat ( benc_val_t *, int, const tr_stat _t*, int );162 int ipc_addinfo ( benc_val_t *, int, const tr_info *, int ); 163 int ipc_addstat ( benc_val_t *, int, const tr_stat *, int ); 164 164 165 165 /* sets errno to EINVAL on parse error or -
trunk/libtransmission/list.c
r2851 r3105 15 15 #include "utils.h" 16 16 17 static tr_list _t*17 static tr_list* 18 18 node_alloc( void ) 19 19 { 20 return tr_new0( tr_list _t, 1 );20 return tr_new0( tr_list, 1 ); 21 21 } 22 22 23 23 static void 24 node_free( tr_list _t* node )24 node_free( tr_list* node ) 25 25 { 26 26 tr_free( node ); … … 32 32 33 33 void 34 tr_list_free( tr_list _t** list )34 tr_list_free( tr_list** list ) 35 35 { 36 36 while( *list ) 37 37 { 38 tr_list _t* node = *list;38 tr_list * node = *list; 39 39 *list = (*list)->next; 40 40 node_free( node ); … … 43 43 44 44 void 45 tr_list_prepend( tr_list _t** list, void * data )45 tr_list_prepend( tr_list ** list, void * data ) 46 46 { 47 tr_list _t* node = node_alloc ();47 tr_list * node = node_alloc (); 48 48 node->data = data; 49 49 node->next = *list; … … 54 54 55 55 void 56 tr_list_append( tr_list _t** list, void * data )56 tr_list_append( tr_list ** list, void * data ) 57 57 { 58 tr_list _t* node = node_alloc( );58 tr_list * node = node_alloc( ); 59 59 node->data = data; 60 60 if( !*list ) 61 61 *list = node; 62 62 else { 63 tr_list _t* l = *list;63 tr_list * l = *list; 64 64 while( l->next ) 65 65 l = l->next; … … 70 70 71 71 void 72 tr_list_insert_sorted( tr_list _t** list,72 tr_list_insert_sorted( tr_list ** list, 73 73 void * data, 74 74 int compare(const void*,const void*) ) 75 75 { 76 76 /* find l, the node that we'll insert this data before */ 77 tr_list _t* l;77 tr_list * l; 78 78 for( l=*list; l!=NULL; l=l->next ) { 79 79 const int c = (compare)( data, l->data ); … … 87 87 tr_list_prepend( list, data ); 88 88 else { 89 tr_list _t* node = node_alloc( );89 tr_list * node = node_alloc( ); 90 90 node->data = data; 91 91 if( l->prev ) { node->prev = l->prev; node->prev->next = node; } … … 96 96 97 97 98 tr_list _t*99 tr_list_find_data ( tr_list _t* list, const void * data )98 tr_list* 99 tr_list_find_data ( tr_list * list, const void * data ) 100 100 { 101 101 for(; list; list=list->next ) … … 106 106 } 107 107 108 void 109 tr_list_remove_ data ( tr_list_t ** list, const void * data)108 static void* 109 tr_list_remove_node ( tr_list ** list, tr_list * node ) 110 110 { 111 tr_list_t * node = tr_list_find_data( *list, data );112 tr_list _t* prev = node ? node->prev : NULL;113 tr_list _t* next = node ? node->next : NULL;111 void * data; 112 tr_list * prev = node ? node->prev : NULL; 113 tr_list * next = node ? node->next : NULL; 114 114 if( prev ) prev->next = next; 115 115 if( next ) next->prev = prev; 116 116 if( *list == node ) *list = next; 117 data = node ? node->data : NULL; 117 118 node_free( node ); 119 return data; 118 120 } 119 121 120 tr_list_t* 121 tr_list_find ( tr_list_t * list , const void * b, TrListCompareFunc func ) 122 void* 123 tr_list_pop_front( tr_list ** list ) 124 { 125 void * ret = NULL; 126 if( *list != NULL ) 127 { 128 ret = (*list)->data; 129 tr_list_remove_node( list, *list ); 130 } 131 return ret; 132 } 133 134 void* 135 tr_list_remove_data ( tr_list ** list, const void * data ) 136 { 137 return tr_list_remove_node( list, tr_list_find_data( *list, data ) ); 138 } 139 140 void* 141 tr_list_remove( tr_list ** list, 142 const void * b, 143 TrListCompareFunc compare_func ) 144 { 145 return tr_list_remove_node( list, tr_list_find( *list, b, compare_func ) ); 146 } 147 148 149 tr_list* 150 tr_list_find ( tr_list * list , const void * b, TrListCompareFunc func ) 122 151 { 123 152 for( ; list; list=list->next ) … … 129 158 130 159 void 131 tr_list_foreach( tr_list _t* list, TrListForeachFunc func )160 tr_list_foreach( tr_list * list, TrListForeachFunc func ) 132 161 { 133 while( list ) 134 { 162 while( list != NULL ) { 135 163 func( list->data ); 136 164 list = list->next; 137 165 } 138 166 } 167 168 int 169 tr_list_size( const tr_list * list ) 170 { 171 int size = 0; 172 while( list != NULL ) { 173 ++size; 174 list = list->next; 175 } 176 return size; 177 } -
trunk/libtransmission/list.h
r2849 r3105 14 14 #define TR_LIST_H 15 15 16 typedef struct tr_list _s16 typedef struct tr_list 17 17 { 18 void 19 struct tr_list _s* next;20 struct tr_list _s* prev;18 void * data; 19 struct tr_list * next; 20 struct tr_list * prev; 21 21 } 22 tr_list _t;22 tr_list; 23 23 24 24 typedef int (*TrListCompareFunc)(const void * a, const void * b); 25 25 typedef void (*TrListForeachFunc)(void *); 26 26 27 void tr_list_free ( tr_list_t ** list );27 int tr_list_size ( const tr_list * list ); 28 28 29 void tr_list_append ( tr_list_t ** list, 30 void * data ); 29 void tr_list_free ( tr_list ** list ); 31 30 32 void tr_list_ prepend ( tr_list_t ** list,33 void 31 void tr_list_append ( tr_list ** list, 32 void * data ); 34 33 35 void tr_list_ remove_data ( tr_list_t ** list,36 const void* data );34 void tr_list_prepend ( tr_list ** list, 35 void * data ); 37 36 38 void tr_list_insert_sorted ( tr_list_t ** list, 39 void * data, 40 TrListCompareFunc compare_func ); 37 void* tr_list_pop_front ( tr_list ** list ); 41 38 42 tr_list_t* tr_list_find ( tr_list_t * list, 43 const void * b, 44 TrListCompareFunc compare_func ); 39 void* tr_list_remove_data ( tr_list ** list, 40 const void * data ); 45 41 46 tr_list_t* tr_list_find_data ( tr_list_t * list, 47 const void * data ); 42 void* tr_list_remove ( tr_list ** list, 43 const void * b, 44 TrListCompareFunc compare_func ); 48 45 49 void tr_list_foreach ( tr_list_t * list, 50 TrListForeachFunc foreach_func ); 46 void tr_list_insert_sorted ( tr_list ** list, 47 void * data, 48 TrListCompareFunc compare_func ); 49 50 tr_list* tr_list_find ( tr_list * list, 51 const void * b, 52 TrListCompareFunc compare_func ); 53 54 tr_list* tr_list_find_data ( tr_list * list, 55 const void * data ); 56 57 void tr_list_foreach ( tr_list * list, 58 TrListForeachFunc foreach_func ); 51 59 52 60 #endif /* TR_LIST_H */ -
trunk/libtransmission/makemeta.c
r2851 r3105 22 22 #include <dirent.h> 23 23 24 #include "trcompat.h" /* for strlcpy */ 24 #include "crypto.h" /* tr_sha1 */ 25 #include "trcompat.h" /* strlcpy */ 25 26 #include "transmission.h" 26 #include "internal.h" /* for tr_torrent_t */27 27 #include "bencode.h" 28 28 #include "makemeta.h" 29 29 #include "platform.h" /* threads, locks */ 30 30 #include "shared.h" /* shared lock */ 31 #include "sha1.h"32 31 #include "utils.h" /* buildpath */ 33 32 #include "version.h" … … 232 231 assert( bufptr-buf == (int)thisPieceSize ); 233 232 assert( pieceRemain == 0 ); 234 SHA1( buf, thisPieceSize, walk);233 tr_sha1( buf, walk, thisPieceSize, NULL ); 235 234 walk += SHA_DIGEST_LENGTH; 236 235 … … 411 410 static tr_metainfo_builder_t * queue = NULL; 412 411 413 static tr_thread _t* workerThread = NULL;414 415 static tr_lock _t* getQueueLock( tr_handle_t * h )416 { 417 static tr_lock _t* lock = NULL;412 static tr_thread * workerThread = NULL; 413 414 static tr_lock* getQueueLock( tr_handle_t * h ) 415 { 416 static tr_lock * lock = NULL; 418 417 419 418 tr_sharedLock( h->shared ); … … 434 433 435 434 /* find the next builder to process */ 436 tr_lock _t* lock = getQueueLock ( handle );435 tr_lock * lock = getQueueLock ( handle ); 437 436 tr_lockLock( lock ); 438 437 if( queue != NULL ) { … … 459 458 int isPrivate ) 460 459 { 461 tr_lock _t* lock;460 tr_lock * lock; 462 461 463 462 builder->abortFlag = 0; -
trunk/libtransmission/metainfo.c
r2857 r3105 34 34 #include "transmission.h" 35 35 #include "bencode.h" 36 #include "crypto.h" /* tr_sha1 */ 36 37 #include "http.h" /* tr_httpParseUrl */ 37 38 #include "metainfo.h" 38 39 #include "platform.h" 39 #include "sha1.h"40 40 #include "utils.h" 41 41 … … 45 45 * Local prototypes 46 46 **********************************************************************/ 47 static int realparse( tr_info _t* inf, const uint8_t * buf, size_t len );47 static int realparse( tr_info * inf, const uint8_t * buf, size_t len ); 48 48 static void savedname( char * name, size_t len, const char * hash, 49 49 const char * tag ); … … 53 53 static int getfile( char * buf, int size, 54 54 const char * prefix, benc_val_t * name ); 55 static int getannounce( tr_info _t* inf, benc_val_t * meta );55 static int getannounce( tr_info * inf, benc_val_t * meta ); 56 56 static char * announceToScrape( const char * announce ); 57 static int parseFiles( tr_info _t* inf, benc_val_t * name,57 static int parseFiles( tr_info * inf, benc_val_t * name, 58 58 benc_val_t * files, benc_val_t * length ); 59 59 … … 64 64 **********************************************************************/ 65 65 int 66 tr_metainfoParseFile( tr_info _t* inf, const char * tag,66 tr_metainfoParseFile( tr_info * inf, const char * tag, 67 67 const char * path, int save ) 68 68 { … … 103 103 104 104 int 105 tr_metainfoParseData( tr_info _t* inf, const char * tag,105 tr_metainfoParseData( tr_info * inf, const char * tag, 106 106 const uint8_t * data, size_t size, int save ) 107 107 { … … 124 124 125 125 int 126 tr_metainfoParseHash( tr_info _t* inf, const char * tag, const char * hash )126 tr_metainfoParseHash( tr_info * inf, const char * tag, const char * hash ) 127 127 { 128 128 struct stat sb; … … 173 173 174 174 static int 175 realparse( tr_info _t* inf, const uint8_t * buf, size_t size )175 realparse( tr_info * inf, const uint8_t * buf, size_t size ) 176 176 { 177 177 benc_val_t meta, * beInfo, * val, * val2; … … 193 193 return TR_EINVALID; 194 194 } 195 SHA1( (uint8_t *) beInfo->begin, 196 (long) beInfo->end - (long) beInfo->begin, inf->hash ); 195 196 tr_sha1( inf->hash, beInfo->begin, beInfo->end - beInfo->begin, NULL ); 197 197 198 for( i = 0; i < SHA_DIGEST_LENGTH; i++ ) 198 199 { … … 255 256 inf->pieceCount = val->val.s.i / SHA_DIGEST_LENGTH; 256 257 257 inf->pieces = calloc ( inf->pieceCount, sizeof(tr_piece _t) );258 inf->pieces = calloc ( inf->pieceCount, sizeof(tr_piece) ); 258 259 259 260 for ( i=0; i<inf->pieceCount; ++i ) … … 308 309 } 309 310 310 void tr_metainfoFree( tr_info _t* inf )311 void tr_metainfoFree( tr_info * inf ) 311 312 { 312 313 int i, j; … … 323 324 tr_free( inf->trackerList ); 324 325 325 memset( inf, '\0', sizeof(tr_info _t) );326 memset( inf, '\0', sizeof(tr_info) ); 326 327 } 327 328 … … 382 383 } 383 384 384 static int getannounce( tr_info _t* inf, benc_val_t * meta )385 static int getannounce( tr_info * inf, benc_val_t * meta ) 385 386 { 386 387 benc_val_t * val, * subval, * urlval; 387 388 char * address, * announce; 388 389 int ii, jj, port, random, subcount; 389 tr_tracker_info _t* sublist;390 tr_tracker_info * sublist; 390 391 void * swapping; 391 392 … … 412 413 for( jj = 0; jj < subval->val.l.count; jj++ ) 413 414 { 414 tr_tracker_info _ttmp;415 tr_tracker_info tmp; 415 416 416 417 urlval = &subval->val.l.vals[jj]; … … 559 560 560 561 int 561 tr_trackerInfoInit( tr_tracker_info _t* info,562 const char 563 int 562 tr_trackerInfoInit( tr_tracker_info * info, 563 const char * address, 564 int address_len ) 564 565 { 565 566 int ret = tr_httpParseUrl( address, address_len, … … 574 575 575 576 void 576 tr_trackerInfoClear( tr_tracker_info _t* info )577 tr_trackerInfoClear( tr_tracker_info * info ) 577 578 { 578 579 tr_free( info->address ); 579 580 tr_free( info->announce ); 580 581 tr_free( info->scrape ); 581 memset( info, '\0', sizeof(tr_tracker_info _t) );582 memset( info, '\0', sizeof(tr_tracker_info) ); 582 583 } 583 584 … … 691 692 692 693 static int 693 parseFiles( tr_info _t* inf, benc_val_t * name,694 parseFiles( tr_info * inf, benc_val_t * name, 694 695 benc_val_t * files, benc_val_t * length ) 695 696 { -
trunk/libtransmission/metainfo.h
r2207 r3105 28 28 #include "transmission.h" 29 29 30 int tr_metainfoParseFile( tr_info _t*, const char * tag,30 int tr_metainfoParseFile( tr_info *, const char * tag, 31 31 const char * path, int save ); 32 int tr_metainfoParseData( tr_info _t*, const char * tag,32 int tr_metainfoParseData( tr_info *, const char * tag, 33 33 const uint8_t * data, size_t size, int save ); 34 int tr_metainfoParseHash( tr_info _t*, const char * tag, const char * hash );35 void tr_metainfoFree( tr_info _t* inf );34 int tr_metainfoParseHash( tr_info *, const char * tag, const char * hash ); 35 void tr_metainfoFree( tr_info * inf ); 36 36 void tr_metainfoRemoveSaved( const char * hashString, const char * tag ); 37 37 -
trunk/libtransmission/net.c
r2614 r3105 58 58 /*********************************************************************** 59 59 * DNS resolution 60 **********************************************************************/ 61 62 /*********************************************************************** 63 * tr_netResolve 64 *********************************************************************** 60 * 65 61 * Synchronous "resolution": only works with character strings 66 62 * representing numbers expressed in the Internet standard `.' notation. … … 71 67 addr->s_addr = inet_addr( address ); 72 68 return ( addr->s_addr == 0xFFFFFFFF ); 73 }74 75 static tr_thread_t * resolveThread;76 static tr_lock_t * resolveLock;77 static tr_cond_t * resolveCond;78 static volatile int resolveDie;79 static tr_resolve_t * resolveQueue;80 81 static void resolveRelease ( tr_resolve_t * );82 static void resolveFunc ( void * );83 84 struct tr_resolve_s85 {86 tr_tristate_t status;87 char * address;88 struct in_addr addr;89 90 int refcount;91 tr_resolve_t * next;92 };93 94 /***********************************************************************95 * tr_netResolveThreadInit96 ***********************************************************************97 * Initializes the static variables used for resolution and launch the98 * gethostbyname thread.99 **********************************************************************/100 void tr_netResolveThreadInit( void )101 {102 resolveDie = 0;103 resolveQueue = NULL;104 resolveLock = tr_lockNew( );105 resolveCond = tr_condNew( );106 resolveThread = tr_threadNew( resolveFunc, NULL, "resolve" );107 }108 109 /***********************************************************************110 * tr_netResolveThreadClose111 ***********************************************************************112 * Notices the gethostbyname thread that is should terminate. Doesn't113 * wait until it does, in case it is stuck in a resolution: we let it114 * die and clean itself up.115 **********************************************************************/116 void tr_netResolveThreadClose( void )117 {118 tr_lockLock( resolveLock );119 resolveDie = 1;120 tr_lockUnlock( resolveLock );121 tr_condSignal( resolveCond );122 tr_wait( 200 );123 }124 125 /***********************************************************************126 * tr_netResolveInit127 ***********************************************************************128 * Adds an address to the resolution queue.129 **********************************************************************/130 tr_resolve_t * tr_netResolveInit( const char * address )131 {132 tr_resolve_t * r = tr_new0( tr_resolve_t, 1 );133 r->status = TR_NET_WAIT;134 r->address = strdup( address );135 r->refcount = 2;136 r->next = NULL;137 138 tr_lockLock( resolveLock );139 if( !resolveQueue )140 {141 resolveQueue = r;142 }143 else144 {145 tr_resolve_t * iter;146 for( iter = resolveQueue; iter->next; iter = iter->next );147 iter->next = r;148 }149 tr_lockUnlock( resolveLock );150 tr_condSignal( resolveCond );151 152 return r;153 }154 155 /***********************************************************************156 * tr_netResolvePulse157 ***********************************************************************158 * Checks the current status of a resolution.159 **********************************************************************/160 tr_tristate_t tr_netResolvePulse( tr_resolve_t * r, struct in_addr * addr )161 {162 tr_tristate_t ret;163 164 tr_lockLock( resolveLock );165 ret = r->status;166 if( ret == TR_NET_OK )167 {168 *addr = r->addr;169 }170 tr_lockUnlock( resolveLock );171 172 return ret;173 }174 175 /***********************************************************************176 * tr_netResolveClose177 ***********************************************************************178 *179 **********************************************************************/180 void tr_netResolveClose( tr_resolve_t * r )181 {182 resolveRelease( r );183 }184 185 /***********************************************************************186 * resolveRelease187 ***********************************************************************188 * The allocated tr_resolve_t structures should be freed when189 * tr_netResolveClose was called *and* it was removed from the queue.190 * This can happen in any order, so we use a refcount to know we can191 * take it out.192 **********************************************************************/193 static void resolveRelease( tr_resolve_t * r )194 {195 if( --r->refcount < 1 )196 {197 free( r->address );198 free( r );199 }200 }201 202 /***********************************************************************203 * resolveFunc204 ***********************************************************************205 * Keeps waiting for addresses to resolve, and removes them from the206 * queue once resolution is done.207 **********************************************************************/208 static void resolveFunc( void * arg UNUSED )209 {210 tr_resolve_t * r;211 struct hostent * host;212 213 tr_lockLock( resolveLock );214 215 while( !resolveDie )216 {217 if( !( r = resolveQueue ) )218 {219 tr_condWait( resolveCond, resolveLock );220 continue;221 }222 223 /* Blocking resolution */224 tr_lockUnlock( resolveLock );225 host = gethostbyname( r->address );226 tr_lockLock( resolveLock );227 228 if( host )229 {230 memcpy( &r->addr, host->h_addr, host->h_length );231 r->status = TR_NET_OK;232 }233 else234 {235 r->status = TR_NET_ERROR;236 }237 238 resolveQueue = r->next;239 resolveRelease( r );240 }241 242 /* Clean up */243 tr_lockUnlock( resolveLock );244 tr_lockFree( resolveLock );245 resolveLock = NULL;246 while( ( r = resolveQueue ) )247 {248 resolveQueue = r->next;249 resolveRelease( r );250 }251 69 } 252 70 … … 470 288 } 471 289 472 void tr_netClose( int s ) 290 void 291 tr_netClose( int s ) 473 292 { 474 293 tr_fdSocketClose( s ); 475 294 } 476 295 477 void tr_netNtop( const struct in_addr * addr, char * buf, int len ) 296 void 297 tr_netNtop( const struct in_addr * addr, char * buf, int len ) 478 298 { 479 299 const uint8_t * cast; -
trunk/libtransmission/net.h
r2615 r3105 68 68 int tr_netResolve( const char *, struct in_addr * ); 69 69 70 typedef struct tr_resolve_s tr_resolve_t;71 void tr_netResolveThreadInit( void );72 void tr_netResolveThreadClose( void );73 tr_resolve_t * tr_netResolveInit( const char * address );74 tr_tristate_t tr_netResolvePulse( tr_resolve_t *, struct in_addr * );75 void tr_netResolveClose( tr_resolve_t * );76 77 70 78 71 /*********************************************************************** -
trunk/libtransmission/peer.c
r2991 r3105 175 175 struct tr_peer_s 176 176 { 177 tr_torrent _t* tor;177 tr_torrent * tor; 178 178 179 179 struct in_addr addr; … … 215 215 216 216 /* The pieces that the peer has */ 217 tr_bitfield _t* bitfield;217 tr_bitfield * bitfield; 218 218 219 219 /* blocks we've requested from this peer */ 220 tr_bitfield _t* reqfield;220 tr_bitfield * reqfield; 221 221 int pieceCount; 222 222 float progress; … … 226 226 int banned; 227 227 /* The pieces that the peer is contributing to */ 228 tr_bitfield _t* blamefield;228 tr_bitfield * blamefield; 229 229 /* The bad pieces that the peer has contributed to */ 230 tr_bitfield _t* banfield;230 tr_bitfield * banfield; 231 231 232 232 uint8_t * buf; … … 250 250 int inLength; 251 251 252 tr_list _t* outRequests;252 tr_list * outRequests; 253 253 uint64_t outDate; 254 254 255 tr_ratecontrol _t* download;256 tr_ratecontrol _t* upload;255 tr_ratecontrol * download; 256 tr_ratecontrol * upload; 257 257 258 258 char * client; … … 290 290 const uint32_t u = htonl( a ); 291 291 memcpy ( p, &u, sizeof( uint32_t ) ); 292 }293 294 static const char* getPeerId( void )295 {296 static char * peerId = NULL;297 if( !peerId ) {298 peerId = tr_new0( char, TR_ID_LEN + 1 );299 tr_peerIdNew( peerId, TR_ID_LEN + 1 );300 }301 return peerId;302 292 } 303 293 … … 354 344 void tr_peerDestroy( tr_peer_t * peer ) 355 345 { 356 tr_torrent _t* tor = peer->tor;346 tr_torrent * tor = peer->tor; 357 347 tr_request_t * r; 358 348 int i, block; … … 424 414 } 425 415 426 void tr_peerSetTorrent( tr_peer_t * peer, tr_torrent _t* tor )416 void tr_peerSetTorrent( tr_peer_t * peer, tr_torrent * tor ) 427 417 { 428 418 peer->tor = tor; … … 436 426 int tr_peerRead( tr_peer_t * peer ) 437 427 { 438 tr_torrent _t* tor = peer->tor;428 tr_torrent * tor = peer->tor; 439 429 int ret; 440 430 uint64_t date; … … 543 533 int tr_peerPulse( tr_peer_t * peer ) 544 534 { 545 tr_torrent _t* tor = peer->tor;535 tr_torrent * tor = peer->tor; 546 536 int ret, size; 547 537 uint8_t * p; … … 581 571 { 582 572 uint8_t buf[HANDSHAKE_SIZE]; 583 const tr_info _t* inf;573 const tr_info * inf; 584 574 585 575 inf = tr_torrentInfo( tor ); … … 794 784 } 795 785 796 int tr_peer IsFrom( const tr_peer_t * peer )786 int tr_peerGetFrom( const tr_peer_t * peer ) 797 787 { 798 788 return peer->from; … … 886 876 void tr_peerBlame( tr_peer_t * peer, int piece, int success ) 887 877 { 888 tr_torrent _t* tor = peer->tor;878 tr_torrent * tor = peer->tor; 889 879 890 880 if( !peer->blamefield || !tr_bitfieldHas( peer->blamefield, piece ) ) … … 927 917 } 928 918 929 int tr_peerGetConnectable( const tr_torrent _t* tor, uint8_t ** _buf )919 int tr_peerGetConnectable( const tr_torrent * tor, uint8_t ** _buf ) 930 920 { 931 921 int count = 0; … … 970 960 tr_peerPieceIsCorrupt( tr_peer_t * peer, int pieceIndex ) 971 961 { 972 tr_torrent _t* tor = peer->tor;962 tr_torrent * tor = peer->tor; 973 963 974 964 const uint64_t byteCount = tr_torPieceCountBytes( tor, pieceIndex ); … … 987 977 tr_peerSentBlockToUs( tr_peer_t * peer, int byteCount ) 988 978 { 989 tr_torrent _t* tor = peer->tor;979 tr_torrent * tor = peer->tor; 990 980 991 981 assert( byteCount >= 0 ); … … 1003 993 tr_peerGotBlockFromUs ( tr_peer_t * peer, int byteCount ) 1004 994 { 1005 tr_torrent _t* tor = peer->tor;995 tr_torrent * tor = peer->tor; 1006 996 1007 997 assert( byteCount >= 0 ); … … 1017 1007 1018 1008 static void 1019 tr_torrentSwiftPulse ( tr_torrent _t* tor )1009 tr_torrentSwiftPulse ( tr_torrent * tor ) 1020 1010 { 1021 1011 /* Preferred # of seconds for the request queue's turnaround time. … … 1092 1082 if( lastPulseTime + SWIFT_REFRESH_INTERVAL_SEC <= time( NULL ) ) 1093 1083 { 1094 tr_torrent _t* tor;1084 tr_torrent * tor; 1095 1085 for( tor=h->torrentList; tor; tor=tor->next ) 1096 1086 tr_torrentSwiftPulse( tor ); -
trunk/libtransmission/peer.h
r2573 r3105 35 35 const char *tr_peerClient ( tr_peer_t * ); 36 36 void tr_peerSetPrivate ( tr_peer_t *, int ); 37 void tr_peerSetTorrent ( tr_peer_t *, struct tr_torrent _s* );37 void tr_peerSetTorrent ( tr_peer_t *, struct tr_torrent * ); 38 38 void tr_peerSentBlockToUs ( tr_peer_t *, int byteCount ); 39 39 void tr_peerGotBlockFromUs ( tr_peer_t *, int byteCount ); … … 43 43 int tr_peerPulse ( tr_peer_t * ); 44 44 int tr_peerIsConnected ( const tr_peer_t * ); 45 int tr_peer IsFrom( const tr_peer_t * );45 int tr_peerGetFrom ( const tr_peer_t * ); 46 46 int tr_peerTimesChoked ( const tr_peer_t * ); 47 47 int tr_peerIsChokingUs ( const tr_peer_t * ); … … 61 61 void tr_peerBlame ( tr_peer_t *, int piece, int success ); 62 62 struct in_addr * tr_peerAddress ( tr_peer_t * ); 63 int tr_peerGetConnectable ( const struct tr_torrent _s*, uint8_t ** );63 int tr_peerGetConnectable ( const struct tr_torrent *, uint8_t ** ); 64 64 65 65 void tr_swiftPulse ( tr_handle_t * ); -
trunk/libtransmission/peeraz.h
r2911 r3105 98 98 99 99 static uint8_t * 100 makeAZHandshake( tr_torrent _t* tor, tr_peer_t * peer, int * buflen )100 makeAZHandshake( tr_torrent * tor, tr_peer_t * peer, int * buflen ) 101 101 { 102 102 char * buf; … … 225 225 226 226 static int 227 makeAZPex( tr_torrent _t* tor, tr_peer_t * peer, char ** buf, int * len )227 makeAZPex( tr_torrent * tor, tr_peer_t * peer, char ** buf, int * len ) 228 228 { 229 229 benc_val_t val; … … 236 236 237 237 static int 238 sendAZHandshake( tr_torrent _t* tor, tr_peer_t * peer )238 sendAZHandshake( tr_torrent * tor, tr_peer_t * peer ) 239 239 { 240 240 uint8_t * buf; … … 373 373 { 374 374 benc_val_t val, * sub, * sub2, * dict, * subsub; 375 tr_bitfield _t* msgs;375 tr_bitfield * msgs; 376 376 int ii, idx, newclient; 377 377 char * client; … … 493 493 494 494 static int 495 parseAZPex( tr_torrent _t* tor, tr_peer_t * peer, uint8_t * buf, int len )496 { 497 tr_info _t* info = &tor->info;495 parseAZPex( tr_torrent * tor, tr_peer_t * peer, uint8_t * buf, int len ) 496 { 497 tr_info * info = &tor->info; 498 498 benc_val_t val, * list, * pair; 499 499 int ii, used; -
trunk/libtransmission/peerext.h
r2781 r3105 27 27 28 28 static int 29 makeCommonPex( tr_torrent _t* tor, tr_peer_t * peer,29 makeCommonPex( tr_torrent * tor, tr_peer_t * peer, 30 30 int ( *peerfunc )( tr_peertree_t *, benc_val_t * ), 31 31 const char * extrakey, benc_val_t * extraval, … … 123 123 124 124 static char * 125 makeExtendedHandshake( tr_torrent _t* tor, tr_peer_t * peer, int * len )125 makeExtendedHandshake( tr_torrent * tor, tr_peer_t * peer, int * len ) 126 126 { 127 127 benc_val_t val, * msgsval; … … 206 206 207 207 static int 208 makeUTPex( tr_torrent _t* tor, tr_peer_t * peer, char ** buf, int * len )208 makeUTPex( tr_torrent * tor, tr_peer_t * peer, char ** buf, int * len ) 209 209 { 210 210 benc_val_t val; … … 300 300 301 301 static int 302 parseUTPex( tr_torrent _t* tor, tr_peer_t * peer, uint8_t * buf, int len )302 parseUTPex( tr_torrent * tor, tr_peer_t * peer, uint8_t * buf, int len ) 303 303 { 304 304 benc_val_t val, * sub; … … 327 327 if( NULL != sub && TYPE_STR == sub->type && 0 == sub->val.s.i % 6 ) 328 328 { 329 #if 0 329 330 used = tr_torrentAddCompact( tor, TR_PEER_FROM_PEX, 330 331 ( uint8_t * )sub->val.s.s, … … 332 333 peer_dbg( "GET extended-pex, got %i peers, used %i", 333 334 sub->val.s.i / 6, used ); 335 #endif 334 336 } 335 337 else -
trunk/libtransmission/peermessages.h
r2746 r3105 106 106 107 107 static uint8_t * 108 blockPending( tr_torrent _t* tor,109 tr_peer_t 110 int 108 blockPending( tr_torrent * tor, 109 tr_peer_t * peer, 110 int * size ) 111 111 { 112 112 if( !peer->outBlockLoaded ) /* we need to load the block for the next request */ … … 144 144 buf += 4; 145 145 146 tr_ioRead( tor ->io, r->index, r->begin, r->length, buf );146 tr_ioRead( tor, r->index, r->begin, r->length, buf ); 147 147 148 148 peer_dbg( "SEND piece %d/%d (%d bytes)", … … 275 275 * - bitfield (X bytes) 276 276 **********************************************************************/ 277 static void sendBitfield( tr_torrent _t* tor, tr_peer_t * peer )277 static void sendBitfield( tr_torrent * tor, tr_peer_t * peer ) 278 278 { 279 279 uint8_t * p; 280 const tr_bitfield _t* bitfield;280 const tr_bitfield * bitfield; 281 281 282 282 bitfield = tr_cpPieceBitfield( tor->completion ); … … 293 293 * 294 294 **********************************************************************/ 295 static void sendRequest( tr_torrent _t* tor, tr_peer_t * peer, int block )296 { 297 tr_info _t* inf = &tor->info;295 static void sendRequest( tr_torrent * tor, tr_peer_t * peer, int block ) 296 { 297 tr_info * inf = &tor->info; 298 298 tr_request_t * r; 299 299 uint8_t * p; … … 344 344 * 345 345 **********************************************************************/ 346 static void broadcastCancel( tr_torrent _t* tor, int index, int begin,346 static void broadcastCancel( tr_torrent * tor, int index, int begin, 347 347 int length ) 348 348 { … … 386 386 * - data (X bytes) 387 387 **********************************************************************/ 388 static int sendExtended( tr_torrent _t* tor, tr_peer_t * peer, int id )388 static int sendExtended( tr_torrent * tor, tr_peer_t * peer, int id ) 389 389 { 390 390 uint8_t * p; … … 434 434 * 435 435 **********************************************************************/ 436 static int sendAZPex( tr_torrent _t* tor, tr_peer_t * peer )436 static int sendAZPex( tr_torrent * tor, tr_peer_t * peer ) 437 437 { 438 438 uint8_t * p; -
trunk/libtransmission/peerparse.h
r2891 r3105 33 33 **********************************************************************/ 34 34 35 static int parseChoke( tr_torrent _t* tor,36 tr_peer_t 37 int 38 int 35 static int parseChoke( tr_torrent * tor, 36 tr_peer_t * peer, 37 int len, 38 int choking ) 39 39 { 40 40 tr_request_t * r; … … 102 102 * 103 103 **********************************************************************/ 104 static int parseHave( tr_torrent _t* tor, tr_peer_t * peer,105 106 { 107 tr_info _t* inf = &tor->info;104 static int parseHave( tr_torrent * tor, tr_peer_t * peer, 105 uint8_t * p, int len ) 106 { 107 tr_info * inf = &tor->info; 108 108 uint32_t piece; 109 109 … … 140 140 } 141 141 142 static int parseBitfield( tr_torrent _t* tor, tr_peer_t * peer,143 144 { 145 tr_info _t* inf = &tor->info;142 static int parseBitfield( tr_torrent * tor, tr_peer_t * peer, 143 uint8_t * p, int len ) 144 { 145 tr_info * inf = &tor->info; 146 146 int bitfieldSize; 147 147 int i; … … 195 195 } 196 196 197 static int parseRequest( tr_torrent _t* tor, tr_peer_t * peer,198 199 { 200 tr_info _t* inf = &tor->info;197 static int parseRequest( tr_torrent * tor, tr_peer_t * peer, 198 uint8_t * p, int len ) 199 { 200 tr_info * inf = &tor->info; 201 201 int index, begin, length; 202 202 tr_request_t * r; … … 282 282 } 283 283 284 static int parsePiece( tr_torrent _t* tor, tr_peer_t * peer,285 286 { 287 tr_info _t* inf = &tor->info;284 static int parsePiece( tr_torrent * tor, tr_peer_t * peer, 285 uint8_t * p, int len ) 286 { 287 tr_info * inf = &tor->info; 288 288 int index, begin, block, i, ret; 289 289 … … 335 335 336 336 /* Write to disk */ 337 if( ( ret = tr_ioWrite( tor ->io, index, begin, len - 8, &p[8] ) ) )337 if( ( ret = tr_ioWrite( tor, index, begin, len - 8, &p[8] ) ) ) 338 338 { 339 339 return ret; … … 380 380 } 381 381 382 static int parseCancel( tr_torrent _t* tor, tr_peer_t * peer,383 384 { 385 tr_info _t* inf = &tor->info;382 static int parseCancel( tr_torrent * tor, tr_peer_t * peer, 383 uint8_t * p, int len ) 384 { 385 tr_info * inf = &tor->info; 386 386 int index, begin, length; 387 387 tr_request_t req; 388 tr_list _t* l;388 tr_list * l; 389 389 390 390 if( len != 12 ) … … 475 475 } 476 476 477 static int parseMessage( tr_torrent _t* tor, tr_peer_t * peer,478 477 static int parseMessage( tr_torrent * tor, tr_peer_t * peer, 478 int id, uint8_t * p, int len ) 479 479 { 480 480 int extid; … … 598 598 } 599 599 600 static int parseHandshake( tr_torrent _t* tor, tr_peer_t * peer )601 { 602 tr_info _t* inf = &tor->info;600 static int parseHandshake( tr_torrent * tor, tr_peer_t * peer ) 601 { 602 tr_info * inf = &tor->info; 603 603 int ii, extmsgs, azproto; 604 604 char * dbgsup, * dbgwant; … … 695 695 } 696 696 697 static int sendInitial( tr_torrent _t* tor, tr_peer_t * peer )697 static int sendInitial( tr_torrent * tor, tr_peer_t * peer ) 698 698 { 699 699 if( PEER_STATUS_CONNECTED != peer->status ) … … 716 716 } 717 717 718 static int parseBuf( tr_torrent _t* tor, tr_peer_t * peer )718 static int parseBuf( tr_torrent * tor, tr_peer_t * peer ) 719 719 { 720 720 int len, ret, msgid; -
trunk/libtransmission/peerutils.h
r2672 r3105 39 39 { 40 40 int ret; 41 tr_torrent _t* tor = peer->tor;41 tr_torrent * tor = peer->tor; 42 42 const uint64_t now = tr_date( ); 43 43 const uint64_t idleTime = now - peer->date; … … 138 138 } 139 139 140 static int isPieceInteresting( const tr_torrent _t* tor,141 const tr_peer_t 142 int 140 static int isPieceInteresting( const tr_torrent * tor, 141 const tr_peer_t * peer, 142 int piece ) 143 143 { 144 144 if( tor->info.pieces[piece].dnd ) /* we don't want it */ … … 163 163 * haven't completed, or 0 otherwise. 164 164 **********************************************************************/ 165 static int isInteresting( const tr_torrent _t* tor, const tr_peer_t * peer )165 static int isInteresting( const tr_torrent * tor, const tr_peer_t * peer ) 166 166 { 167 167 int i; 168 const tr_bitfield _t* bitfield = tr_cpPieceBitfield( tor->completion );168 const tr_bitfield * bitfield = tr_cpPieceBitfield( tor->completion ); 169 169 170 170 if( !peer->bitfield ) … … 184 184 185 185 static void 186 updateInterest( tr_torrent _t* tor, tr_peer_t * peer )186 updateInterest( tr_torrent * tor, tr_peer_t * peer ) 187 187 { 188 188 const int i = !!isInteresting( tor, peer ); … … 224 224 } 225 225 226 static int* getPreferredPieces( const tr_torrent _t* tor,227 const tr_peer_t 228 int 229 int 230 { 231 const tr_info _t* inf = &tor->info;226 static int* getPreferredPieces( const tr_torrent * tor, 227 const tr_peer_t * peer, 228 int * pieceCount, 229 int * isEndgame ) 230 { 231 const tr_info * inf = &tor->info; 232 232 233 233 int i; -
trunk/libtransmission/platform.c
r2975 r3105 57 57 ***/ 58 58 59 struct tr_thread _s59 struct tr_thread 60 60 { 61 61 void (* func ) ( void * ); 62 62 void * arg; 63 c har* name;63 const char * name; 64 64 65 65 #ifdef __BEOS__ … … 83 83 ThreadFunc( void * _t ) 84 84 { 85 tr_thread _t* t = _t;86 c har* name = tr_strdup( t->name );85 tr_thread * t = _t; 86 const char * name = t->name; 87 87 88 88 #ifdef __BEOS__ … … 96 96 tr_dbg( "Thread '%s' exited", name ); 97 97 98 tr_free( name );99 100 98 #ifdef WIN32 101 99 _endthreadex( 0 ); … … 104 102 } 105 103 106 tr_thread _t*104 tr_thread * 107 105 tr_threadNew( void (*func)(void *), 108 106 void * arg, 109 107 const char * name ) 110 108 { 111 tr_thread _t * t = tr_new0( tr_thread_t, 1 );109 tr_thread * t = tr_new0( tr_thread, 1 ); 112 110 t->func = func; 113 111 t->arg = arg; 114 t->name = tr_strdup( name );112 t->name = name; 115 113 116 114 #ifdef __BEOS__ … … 127 125 128 126 int 129 tr_amInThread ( const tr_thread_t * t ) 130 { 131 int ret; 132 #ifdef __BEOS__ 133 ret = find_thread(NULL) == t->thread; 134 #elif defined(WIN32) 135 ret = GetCurrentThreadId() == t->thread_id; 136 #else 137 ret = pthread_equal( t->thread, pthread_self( ) ); 138 #endif 139 return ret; 127 tr_amInThread ( const tr_thread * t ) 128 { 129 #ifdef __BEOS__ 130 return find_thread(NULL) == t->thread; 131 #elif defined(WIN32) 132 return GetCurrentThreadId() == t->thread_id; 133 #else 134 return pthread_equal( t->thread, pthread_self( ) ); 135 #endif 140 136 } 141 137 142 138 void 143 tr_threadJoin( tr_thread _t* t )139 tr_threadJoin( tr_thread * t ) 144 140 { 145 141 if( t != NULL ) … … 156 152 157 153 tr_dbg( "Thread '%s' joined", t->name ); 158 tr_free( t->name );159 154 t->name = NULL; 160 155 t->func = NULL; … … 167 162 ***/ 168 163 169 struct tr_lock _s164 struct tr_lock 170 165 { 171 166 #ifdef __BEOS__ … … 178 173 }; 179 174 180 tr_lock _t*175 tr_lock* 181 176 tr_lockNew( void ) 182 177 { 183 tr_lock _t * l = tr_new0( tr_lock_t, 1 );178 tr_lock * l = tr_new0( tr_lock, 1 ); 184 179 185 180 #ifdef __BEOS__ … … 195 190 196 191 void 197 tr_lockFree( tr_lock _t* l )192 tr_lockFree( tr_lock * l ) 198 193 { 199 194 #ifdef __BEOS__ … … 208 203 209 204 int 210 tr_lockTryLock( tr_lock _t* l ) /* success on zero! */205 tr_lockTryLock( tr_lock * l ) /* success on zero! */ 211 206 { 212 207 #ifdef __BEOS__ … … 220 215 221 216 void 222 tr_lockLock( tr_lock _t* l )217 tr_lockLock( tr_lock * l ) 223 218 { 224 219 #ifdef __BEOS__ … … 232 227 233 228 void 234 tr_lockUnlock( tr_lock _t* l )229 tr_lockUnlock( tr_lock * l ) 235 230 { 236 231 #ifdef __BEOS__ … … 241 236 pthread_mutex_unlock( &l->lock ); 242 237 #endif 243 }244 245 /***246 **** RW LOCK247 ***/248 249 struct tr_rwlock_s250 {251 tr_lock_t * lock;252 tr_cond_t * readCond;253 tr_cond_t * writeCond;254 size_t readCount;255 size_t wantToRead;256 size_t wantToWrite;257 int haveWriter;258 };259 260 static void261 tr_rwSignal( tr_rwlock_t * rw )262 {263 if ( rw->wantToWrite )264 tr_condSignal( rw->writeCond );265 else if ( rw->wantToRead )266 tr_condBroadcast( rw->readCond );267 }268 269 tr_rwlock_t*270 tr_rwNew ( void )271 {272 tr_rwlock_t * rw = tr_new0( tr_rwlock_t, 1 );273 rw->lock = tr_lockNew( );274 rw->readCond = tr_condNew( );275 rw->writeCond = tr_condNew( );276 return rw;277 }278 279 void280 tr_rwReaderLock( tr_rwlock_t * rw )281 {282 tr_lockLock( rw->lock );283 rw->wantToRead++;284 while( rw->haveWriter || rw->wantToWrite )285 tr_condWait( rw->readCond, rw->lock );286 rw->wantToRead--;287 rw->readCount++;288 tr_lockUnlock( rw->lock );289 }290 291 int292 tr_rwReaderTrylock( tr_rwlock_t * rw )293 {294 int ret = FALSE;295 tr_lockLock( rw->lock );296 if ( !rw->haveWriter && !rw->wantToWrite ) {297 rw->readCount++;298 ret = TRUE;299 }300 tr_lockUnlock( rw->lock );301 return ret;302 303 }304 305 void306 tr_rwReaderUnlock( tr_rwlock_t * rw )307 {308 tr_lockLock( rw->lock );309 --rw->readCount;310 if( !rw->readCount )311 tr_rwSignal( rw );312 tr_lockUnlock( rw->lock );313 }314 315 void316 tr_rwWriterLock( tr_rwlock_t * rw )317 {318 tr_lockLock( rw->lock );319 rw->wantToWrite++;320 while( rw->haveWriter || rw->readCount )321 tr_condWait( rw->writeCond, rw->lock );322 rw->wantToWrite--;323 rw->haveWriter = TRUE;324 tr_lockUnlock( rw->lock );325 }326 327 int328 tr_rwWriterTrylock( tr_rwlock_t * rw )329 {330 int ret = FALSE;331 tr_lockLock( rw->lock );332 if( !rw->haveWriter && !rw->readCount )333 ret = rw->haveWriter = TRUE;334 tr_lockUnlock( rw->lock );335 return ret;336 }337 void338 tr_rwWriterUnlock( tr_rwlock_t * rw )339 {340 tr_lockLock( rw->lock );341 rw->haveWriter = FALSE;342 tr_rwSignal( rw );343 tr_lockUnlock( rw->lock );344 }345 346 void347 tr_rwFree( tr_rwlock_t * rw )348 {349 tr_condFree( rw->writeCond );350 tr_condFree( rw->readCond );351 tr_lockFree( rw->lock );352 tr_free( rw );353 238 } 354 239 … … 357 242 ***/ 358 243 359 struct tr_cond _s244 struct tr_cond 360 245 { 361 246 #ifdef __BEOS__ … … 365 250 #elif defined(WIN32) 366 251 tr_list * events; 367 tr_lock _t* lock;252 tr_lock * lock; 368 253 #else 369 254 pthread_cond_t cond; … … 384 269 #endif 385 270 386 tr_cond _t*271 tr_cond* 387 272 tr_condNew( void ) 388 273 { 389 tr_cond _t * c = tr_new0( tr_cond_t, 1 );274 tr_cond * c = tr_new0( tr_cond, 1 ); 390 275 #ifdef __BEOS__ 391 276 c->sem = create_sem( 1, "" ); … … 402 287 403 288 void 404 tr_condWait( tr_cond _t * c, tr_lock_t* l )289 tr_condWait( tr_cond * c, tr_lock * l ) 405 290 { 406 291 #ifdef __BEOS__ … … 450 335 451 336 #ifdef __BEOS__ 452 static int condTrySignal( tr_cond _t* c )337 static int condTrySignal( tr_cond * c ) 453 338 { 454 339 if( c->start == c->end ) … … 474 359 #endif 475 360 void 476 tr_condSignal( tr_cond _t* c )361 tr_condSignal( tr_cond * c ) 477 362 { 478 363 #ifdef __BEOS__ … … 497 382 498 383 void 499 tr_condBroadcast( tr_cond _t* c )384 tr_condBroadcast( tr_cond * c ) 500 385 { 501 386 #ifdef __BEOS__ … … 521 406 522 407 void 523 tr_condFree( tr_cond _t* c )408 tr_condFree( tr_cond * c ) 524 409 { 525 410 #ifdef __BEOS__ -
trunk/libtransmission/platform.h
r2975 r3105 25 25 #define TR_PLATFORM_H 26 26 27 typedef struct tr_lock _s tr_lock_t;28 typedef struct tr_cond _s tr_cond_t;29 typedef struct tr_thread _s tr_thread_t;27 typedef struct tr_lock tr_lock; 28 typedef struct tr_cond tr_cond; 29 typedef struct tr_thread tr_thread; 30 30 31 31 const char * tr_getHomeDirectory( void ); … … 33 33 const char * tr_getTorrentsDirectory( void ); 34 34 35 tr_thread_t* tr_threadNew ( void (*func)(void *), void * arg, const char * name ); 36 void tr_threadJoin ( tr_thread_t * ); 35 tr_thread* tr_threadNew ( void (*func)(void *), void * arg, const char * name ); 36 void tr_threadJoin ( tr_thread * ); 37 int tr_amInThread ( const tr_thread * ); 37 38 int tr_amInThread ( const tr_thread_t * ); 38 39 39 tr_lock _t *tr_lockNew ( void );40 void tr_lockFree ( tr_lock_t* );41 int tr_lockTryLock ( tr_lock_t* );42 void tr_lockLock ( tr_lock_t* );43 void tr_lockUnlock ( tr_lock_t* );40 tr_lock * tr_lockNew ( void ); 41 void tr_lockFree ( tr_lock * ); 42 int tr_lockTryLock ( tr_lock * ); 43 void tr_lockLock ( tr_lock * ); 44 void tr_lockUnlock ( tr_lock * ); 44 45 45 tr_cond_t * tr_condNew ( void ); 46 void tr_condFree ( tr_cond_t * ); 47 void tr_condSignal ( tr_cond_t * ); 48 void tr_condBroadcast ( tr_cond_t * ); 49 void tr_condWait ( tr_cond_t *, tr_lock_t * ); 50 51 /*** 52 **** RW lock: 53 **** The lock can be had by one writer or any number of readers. 54 ***/ 55 56 typedef struct tr_rwlock_s tr_rwlock_t; 57 58 tr_rwlock_t* tr_rwNew ( void ); 59 void tr_rwFree ( tr_rwlock_t * ); 60 void tr_rwReaderLock ( tr_rwlock_t * ); 61 int tr_rwReaderTrylock ( tr_rwlock_t * ); 62 void tr_rwReaderUnlock ( tr_rwlock_t * ); 63 void tr_rwWriterLock ( tr_rwlock_t * ); 64 int tr_rwWriterTrylock ( tr_rwlock_t * ); 65 void tr_rwWriterUnlock ( tr_rwlock_t * ); 66 46 tr_cond * tr_condNew ( void ); 47 void tr_condFree ( tr_cond * ); 48 void tr_condSignal ( tr_cond * ); 49 void tr_condBroadcast ( tr_cond * ); 50 void tr_condWait ( tr_cond *, tr_lock * ); 67 51 68 52 struct in_addr; /* forward declaration to calm gcc down */ -
trunk/libtransmission/ptrarray.c
r2851 r3105 20 20 #define GROW 32 21 21 22 struct tr_ptrArray _s22 struct tr_ptrArray 23 23 { 24 24 void ** items; … … 27 27 }; 28 28 29 tr_ptrArray _t*29 tr_ptrArray* 30 30 tr_ptrArrayNew( void ) 31 31 { 32 tr_ptrArray _t* p;32 tr_ptrArray * p; 33 33 34 p = tr_new( tr_ptrArray _t, 1 );34 p = tr_new( tr_ptrArray, 1 ); 35 35 p->n_items = 0; 36 36 p->n_alloc = GROW; … … 41 41 42 42 void 43 tr_ptrArrayFree( tr_ptrArray _t* t )43 tr_ptrArrayFree( tr_ptrArray * t ) 44 44 { 45 45 assert( t != NULL ); … … 51 51 52 52 void** 53 tr_ptrArrayPeek( tr_ptrArray _t* t, int * size )53 tr_ptrArrayPeek( tr_ptrArray * t, int * size ) 54 54 { 55 55 *size = t->n_items; … … 57 57 } 58 58 59 void* 60 tr_ptrArrayNth( tr_ptrArray* t, int i ) 61 { 62 assert( t != NULL ); 63 assert( i >= 0 ); 64 assert( i < t->n_items ); 65 66 return t->items[i]; 67 } 68 59 69 int 60 tr_ptrArraySize( const tr_ptrArray _t* t )70 tr_ptrArraySize( const tr_ptrArray * t ) 61 71 { 62 72 return t->n_items; … … 64 74 65 75 int 66 tr_ptrArrayEmpty( const tr_ptrArray _t* t )76 tr_ptrArrayEmpty( const tr_ptrArray * t ) 67 77 { 68 78 return t->n_items == 0; … … 70 80 71 81 void 72 tr_ptrArrayClear( tr_ptrArray _t* t )82 tr_ptrArrayClear( tr_ptrArray * t ) 73 83 { 74 84 t->n_items = 0; … … 76 86 77 87 int 78 tr_ptrArrayInsert( tr_ptrArray _t* t, void * ptr, int pos )88 tr_ptrArrayInsert( tr_ptrArray * t, void * ptr, int pos ) 79 89 { 80 90 if( pos<0 || pos>t->n_items ) … … 96 106 97 107 int 98 tr_ptrArrayAppend( tr_ptrArray _t* t, void * ptr )108 tr_ptrArrayAppend( tr_ptrArray * t, void * ptr ) 99 109 { 100 110 return tr_ptrArrayInsert( t, ptr, -1 ); … … 102 112 103 113 void 104 tr_ptrArrayErase( tr_ptrArray _t* t, int begin, int end )114 tr_ptrArrayErase( tr_ptrArray * t, int begin, int end ) 105 115 { 106 116 assert( begin >= 0 ); … … 121 131 122 132 int 123 tr_ptrArrayLowerBound( const tr_ptrArray _t* t,124 void* ptr,125 int 126 int 133 tr_ptrArrayLowerBound( const tr_ptrArray * t, 134 const void * ptr, 135 int compare( const void *,const void * ), 136 int * exact_match ) 127 137 { 128 int c = -1;129 138 int len = t->n_items; 130 139 int first = 0; … … 134 143 int half = len / 2; 135 144 int middle = first + half; 136 c = compare( t->items[middle], ptr );145 const int c = compare( t->items[middle], ptr ); 137 146 if( c < 0 ) { 138 147 first = middle + 1; … … 155 164 156 165 int 157 tr_ptrArrayInsertSorted( tr_ptrArray _t* t,158 void 159 int 166 tr_ptrArrayInsertSorted( tr_ptrArray * t, 167 void * ptr, 168 int compare(const void*,const void*) ) 160 169 { 161 170 const int pos = tr_ptrArrayLowerBound( t, ptr, compare, NULL ); … … 164 173 165 174 void* 166 tr_ptrArrayFindSorted( tr_ptrArray _t* t,167 void* ptr,168 int 175 tr_ptrArrayFindSorted( tr_ptrArray * t, 176 const void * ptr, 177 int compare(const void*,const void*) ) 169 178 { 170 179 int match; … … 174 183 175 184 void* 176 tr_ptrArrayRemoveSorted( tr_ptrArray _t* t,177 void 178 int 185 tr_ptrArrayRemoveSorted( tr_ptrArray * t, 186 void * ptr, 187 int compare(const void*,const void*) ) 179 188 { 180 189 void * ret = NULL; -
trunk/libtransmission/ptrarray.h
r2849 r3105 17 17 * A simple pointer array that resizes itself dynamically. 18 18 */ 19 typedef struct tr_ptrArray _s tr_ptrArray_t;19 typedef struct tr_ptrArray tr_ptrArray; 20 20 21 tr_ptrArray_t * tr_ptrArrayNew ( void ); 22 void tr_ptrArrayFree ( tr_ptrArray_t* ); 23 void** tr_ptrArrayPeek ( tr_ptrArray_t*, int * size ); 24 void** tr_ptrArrayBase ( tr_ptrArray_t* ); 25 void tr_ptrArrayClear ( tr_ptrArray_t* ); 26 int tr_ptrArrayInsert ( tr_ptrArray_t*, void*, int pos ); 27 int tr_ptrArrayAppend ( tr_ptrArray_t*, void* ); 28 void tr_ptrArrayErase ( tr_ptrArray_t*, int begin, int end ); 29 int tr_ptrArraySize ( const tr_ptrArray_t* ); 30 int tr_ptrArrayEmpty ( const tr_ptrArray_t* ); 21 tr_ptrArray * tr_ptrArrayNew ( void ); 31 22 32 int tr_ptrArrayInsertSorted( tr_ptrArray_t*, void*, 33 int compare(const void*,const void*) ); 34 void* tr_ptrArrayRemoveSorted( tr_ptrArray_t*, void*, 35 int compare(const void*,const void*) ); 36 void* tr_ptrArrayFindSorted ( tr_ptrArray_t*, void*, 37 int compare(const void*,const void*) ); 23 void tr_ptrArrayFree ( tr_ptrArray* ); 24 void* tr_ptrArrayNth ( tr_ptrArray*, int n ); 25 void** tr_ptrArrayPeek ( tr_ptrArray*, int * size ); 26 void** tr_ptrArrayBase ( tr_ptrArray* ); 27 void tr_ptrArrayClear ( tr_ptrArray* ); 28 int tr_ptrArrayInsert ( tr_ptrArray*, void*, int pos ); 29 int tr_ptrArrayAppend ( tr_ptrArray*, void* ); 30 void tr_ptrArrayErase ( tr_ptrArray*, int begin, int end ); 31 int tr_ptrArraySize ( const tr_ptrArray* ); 32 int tr_ptrArrayEmpty ( const tr_ptrArray* ); 33 int tr_ptrArrayInsertSorted ( tr_ptrArray*, void*, 34 int compare(const void*,const void*) ); 35 void* tr_ptrArrayRemoveSorted ( tr_ptrArray*, void*, 36 int compare(const void*,const void*) ); 37 void* tr_ptrArrayFindSorted ( tr_ptrArray*, const void*, 38 int compare(const void*,const void*) ); 38 39 39 40 #endif -
trunk/libtransmission/publish.c
r2851 r3105 11 11 */ 12 12 13 #include <assert.h> 13 14 #include "list.h" 14 15 #include "publish.h" … … 23 24 struct tr_publisher_s 24 25 { 25 tr_list _t* list;26 tr_list * list; 26 27 }; 27 28 … … 33 34 34 35 void 35 tr_publisherFree( tr_publisher_t * p )36 tr_publisherFree( tr_publisher_t ** p ) 36 37 { 37 tr_list_free( &p->list ); 38 tr_free( p ); 38 assert( p != NULL ); 39 assert( *p != NULL ); 40 41 tr_list_free( &(*p)->list ); 42 tr_free( *p ); 43 *p = NULL; 39 44 } 40 45 … … 64 69 void * event ) 65 70 { 66 tr_list _t* walk;71 tr_list * walk; 67 72 for( walk=p->list; walk!=NULL; ) 68 73 { 69 tr_list _t* next = walk->next;74 tr_list * next = walk->next; 70 75 struct tr_publisher_node * node = (struct tr_publisher_node*)walk->data; 71 76 (node->func)(source, event, node->user_data); -
trunk/libtransmission/publish.h
r2849 r3105 43 43 tr_publisher_t * tr_publisherNew ( void ); 44 44 45 void tr_publisherFree ( tr_publisher_t 45 void tr_publisherFree ( tr_publisher_t ** publisher ); 46 46 47 47 void tr_publisherPublish ( tr_publisher_t * publisher, -
trunk/libtransmission/ratecontrol.c
r2590 r3105 39 39 { 40 40 uint64_t date; 41 intsize;41 uint64_t size; 42 42 } 43 43 tr_transfer_t; 44 44 45 struct tr_ratecontrol _s45 struct tr_ratecontrol 46 46 { 47 tr_lock _t* lock;47 tr_lock * lock; 48 48 int limit; 49 49 int newest; … … 53 53 /* return the xfer rate over the last `interval' seconds in KiB/sec */ 54 54 static float 55 rateForInterval( const tr_ratecontrol _t* r, int interval_msec )55 rateForInterval( const tr_ratecontrol * r, int interval_msec ) 56 56 { 57 57 uint64_t bytes = 0; 58 const uint64_t now = tr_date ();58 const uint64_t cutoff = tr_date () - interval_msec; 59 59 int i = r->newest; 60 60 for( ;; ) 61 61 { 62 if( r->transfers[i].date + interval_msec < now)62 if( r->transfers[i].date < cutoff ) 63 63 break; 64 64 … … 76 76 ***/ 77 77 78 tr_ratecontrol _t*78 tr_ratecontrol* 79 79 tr_rcInit( void ) 80 80 { 81 tr_ratecontrol _t * r = tr_new0( tr_ratecontrol_t, 1 );81 tr_ratecontrol * r = tr_new0( tr_ratecontrol, 1 ); 82 82 r->limit = 0; 83 83 r->lock = tr_lockNew( ); … … 86 86 87 87 void 88 tr_rcClose( tr_ratecontrol _t* r )88 tr_rcClose( tr_ratecontrol * r ) 89 89 { 90 90 tr_rcReset( r ); … … 98 98 99 99 int 100 tr_rcCanTransfer( const tr_ratecontrol _t* r )100 tr_rcCanTransfer( const tr_ratecontrol * r ) 101 101 { 102 102 int ret; 103 tr_lockLock( (tr_lock_t*)r->lock );104 103 105 ret = rateForInterval( r, SHORT_INTERVAL_MSEC ) < r->limit; 104 if( r == NULL ) 105 ret = 0; 106 else { 107 tr_lockLock( (tr_lock*)r->lock ); 108 ret = rateForInterval( r, SHORT_INTERVAL_MSEC ) < r->limit; 109 tr_lockUnlock( (tr_lock*)r->lock ); 110 } 106 111 107 tr_lockUnlock( (tr_lock_t*)r->lock );108 112 return ret; 109 113 } 110 114 111 115 float 112 tr_rcRate( const tr_ratecontrol _t* r )116 tr_rcRate( const tr_ratecontrol * r ) 113 117 { 114 118 float ret; 115 tr_lockLock( (tr_lock_t*)r->lock );116 119 117 ret = rateForInterval( r, LONG_INTERVAL_MSEC ); 120 if( r == NULL ) 121 ret = 0.0f; 122 else { 123 tr_lockLock( (tr_lock*)r->lock ); 124 ret = rateForInterval( r, LONG_INTERVAL_MSEC ); 125 tr_lockUnlock( (tr_lock*)r->lock ); 126 } 118 127 119 tr_lockUnlock( (tr_lock_t*)r->lock );120 128 return ret; 121 129 } … … 126 134 127 135 void 128 tr_rcTransferred( tr_ratecontrol _t * r, int size )136 tr_rcTransferred( tr_ratecontrol * r, size_t size ) 129 137 { 130 138 uint64_t now; … … 133 141 return; 134 142 135 tr_lockLock( (tr_lock _t*)r->lock );143 tr_lockLock( (tr_lock*)r->lock ); 136 144 137 145 now = tr_date (); … … 144 152 } 145 153 146 tr_lockUnlock( (tr_lock _t*)r->lock );154 tr_lockUnlock( (tr_lock*)r->lock ); 147 155 } 148 156 149 157 void 150 tr_rcReset( tr_ratecontrol _t* r )158 tr_rcReset( tr_ratecontrol * r ) 151 159 { 152 tr_lockLock( (tr_lock _t*)r->lock );160 tr_lockLock( (tr_lock*)r->lock ); 153 161 r->newest = 0; 154 162 memset( r->transfers, 0, sizeof(tr_transfer_t) * HISTORY_SIZE ); 155 tr_lockUnlock( (tr_lock _t*)r->lock );163 tr_lockUnlock( (tr_lock*)r->lock ); 156 164 } 157 165 158 166 void 159 tr_rcSetLimit( tr_ratecontrol _t* r, int limit )167 tr_rcSetLimit( tr_ratecontrol * r, int limit ) 160 168 { 161 tr_lockLock( (tr_lock _t*)r->lock );169 tr_lockLock( (tr_lock*)r->lock ); 162 170 r->limit = limit; 163 tr_lockUnlock( (tr_lock _t*)r->lock );171 tr_lockUnlock( (tr_lock*)r->lock ); 164 172 } 165 173 166 174 int 167 tr_rcGetLimit( const tr_ratecontrol _t* r )175 tr_rcGetLimit( const tr_ratecontrol * r ) 168 176 { 169 177 return r->limit; -
trunk/libtransmission/ratecontrol.h
r2388 r3105 26 26 #define _TR_RATECONTROL_H_ 27 27 28 typedef struct tr_ratecontrol _s tr_ratecontrol_t;28 typedef struct tr_ratecontrol tr_ratecontrol; 29 29 30 tr_ratecontrol _t* tr_rcInit( void );31 void tr_rcSetLimit( tr_ratecontrol_t*, int );32 int tr_rcGetLimit( const tr_ratecontrol_t* );33 int tr_rcCanTransfer( const tr_ratecontrol_t* );34 void tr_rcTransferred( tr_ratecontrol_t *, int );35 float tr_rcRate( const tr_ratecontrol_t* );36 void tr_rcReset( tr_ratecontrol_t* );37 void tr_rcClose( tr_ratecontrol_t* );30 tr_ratecontrol * tr_rcInit( void ); 31 void tr_rcSetLimit( tr_ratecontrol *, int ); 32 int tr_rcGetLimit( const tr_ratecontrol * ); 33 int tr_rcCanTransfer( const tr_ratecontrol * ); 34 void tr_rcTransferred( tr_ratecontrol *, size_t byteCount ); 35 float tr_rcRate( const tr_ratecontrol * ); 36 void tr_rcReset( tr_ratecontrol * ); 37 void tr_rcClose( tr_ratecontrol * ); 38 38 39 39 #endif -
trunk/libtransmission/shared.c
r2805 r3105 26 26 #include <stdlib.h> 27 27 #include <string.h> 28 #include <stdio.h> 28 29 29 30 #include <sys/types.h> 30 31 31 32 #include "transmission.h" 32 #include " choking.h"33 #include "handshake.h" 33 34 #include "natpmp.h" 34 35 #include "net.h" 35 #include "peer.h" 36 #include "peer-io.h" 37 #include "peer-mgr.h" 36 38 #include "platform.h" 37 39 #include "shared.h" 40 #include "trevent.h" 38 41 #include "upnp.h" 39 42 #include "utils.h" 40 43 41 /* Maximum number of peers that we keep in our local list */ 42 /* This is an arbitrary number, but it seems to work well */ 43 #define MAX_PEER_COUNT 128 44 45 struct tr_shared_s 46 { 47 tr_handle_t * h; 48 volatile int die; 49 tr_thread_t * thread; 50 tr_lock_t * lock; 44 struct tr_shared 45 { 46 tr_handle * h; 47 tr_lock * lock; 48 tr_timer * pulseTimer; 51 49 52 50 /* Incoming connections */ 53 int publicPort; 54 int bindPort; 55 int bindSocket; 56 int peerCount; 57 tr_peer_t * peers[MAX_PEER_COUNT]; 51 int publicPort; 52 int bindPort; 53 int bindSocket; 58 54 59 55 /* NAT-PMP/UPnP */ 60 56 tr_natpmp_t * natpmp; 61 57 tr_upnp_t * upnp; 62 63 /* Choking */64 tr_choking_t * choking;65 58 }; 66 59 … … 68 61 * Local prototypes 69 62 **********************************************************************/ 70 static void SharedLoop( void * ); 71 static void SetPublicPort( tr_shared_t *, int ); 72 static void AcceptPeers( tr_shared_t * ); 73 static void ReadPeers( tr_shared_t * ); 74 static void DispatchPeers( tr_shared_t * ); 63 static int SharedLoop( void * ); 64 static void SetPublicPort( tr_shared *, int ); 65 static void AcceptPeers( tr_shared * ); 75 66 76 67 … … 80 71 * 81 72 **********************************************************************/ 82 tr_shared _t * tr_sharedInit( tr_handle_t* h )83 { 84 tr_shared _t * s = calloc( 1, sizeof( tr_shared_t) );73 tr_shared * tr_sharedInit( tr_handle * h ) 74 { 75 tr_shared * s = calloc( 1, sizeof( tr_shared ) ); 85 76 86 77 s->h = h; … … 91 82 s->natpmp = tr_natpmpInit(); 92 83 s->upnp = tr_upnpInit(); 93 s->choking = tr_chokingInit( h ); 94 s->die = 0; 95 s->thread = tr_threadNew( SharedLoop, s, "shared" ); 84 s->pulseTimer = tr_timerNew( h, SharedLoop, s, 250 ); 96 85 97 86 return s; … … 103 92 * 104 93 **********************************************************************/ 105 void tr_sharedClose( tr_shared_t * s ) 106 { 107 int ii; 108 109 /* Stop the thread */ 110 s->die = 1; 111 tr_threadJoin( s->thread ); 112 113 /* Clean up */ 114 for( ii = 0; ii < s->peerCount; ii++ ) 115 { 116 tr_peerDestroy( s->peers[ii] ); 117 } 118 if( s->bindSocket > -1 ) 119 { 120 tr_netClose( s->bindSocket ); 121 } 94 void tr_sharedClose( tr_shared * s ) 95 { 96 fprintf( stderr, "deleting pulse tag\n" ); 97 tr_timerFree( &s->pulseTimer ); 98 99 tr_netClose( s->bindSocket ); 122 100 tr_lockFree( s->lock ); 123 101 tr_natpmpClose( s->natpmp ); 124 102 tr_upnpClose( s->upnp ); 125 tr_chokingClose( s->choking );126 103 free( s ); 127 104 } 128 105 129 /*********************************************************************** 130 * tr_sharedLock, tr_sharedUnlock 131 *********************************************************************** 132 * 133 **********************************************************************/ 134 void tr_sharedLock( tr_shared_t * s ) 106 /** 107 *** 108 **/ 109 110 void tr_sharedLock( tr_shared * s ) 135 111 { 136 112 tr_lockLock( s->lock ); 137 113 } 138 void tr_sharedUnlock( tr_shared _t* s )114 void tr_sharedUnlock( tr_shared * s ) 139 115 { 140 116 tr_lockUnlock( s->lock ); … … 146 122 * 147 123 **********************************************************************/ 148 void tr_sharedSetPort( tr_shared _t* s, int port )124 void tr_sharedSetPort( tr_shared * s, int port ) 149 125 { 150 126 #ifdef BEOS_NETSERVER … … 197 173 } 198 174 199 /*********************************************************************** 200 * tr_sharedGetPublicPort 201 *********************************************************************** 202 * 203 **********************************************************************/ 204 int tr_sharedGetPublicPort( tr_shared_t * s ) 175 int tr_sharedGetPublicPort( tr_shared * s ) 205 176 { 206 177 return s->publicPort; … … 212 183 * 213 184 **********************************************************************/ 214 void tr_sharedTraversalEnable( tr_shared _t* s, int enable )185 void tr_sharedTraversalEnable( tr_shared * s, int enable ) 215 186 { 216 187 if( enable ) … … 226 197 } 227 198 228 int tr_sharedTraversalStatus( tr_shared _t* s )199 int tr_sharedTraversalStatus( tr_shared * s ) 229 200 { 230 201 int statuses[] = { … … 256 227 } 257 228 258 /***********************************************************************259 * tr_sharedSetLimit260 **********************************************************************/261 void tr_sharedSetLimit( tr_shared_t * s, int limit )262 {263 tr_chokingSetLimit( s->choking, limit );264 }265 266 229 267 230 /*********************************************************************** … … 272 235 * SharedLoop 273 236 **********************************************************************/ 274 static void SharedLoop( void * _s )275 { 276 tr_shared_t * s = _s; 277 uint64_t date1, date2, lastchoke = 0;278 int newPort;237 static int 238 SharedLoop( void * vs ) 239 { 240 int newPort; 241 tr_shared * s = vs; 279 242 280 243 tr_sharedLock( s ); 281 244 282 while( !s->die ) 283 { 284 date1 = tr_date(); 285 286 /* NAT-PMP and UPnP pulses */ 287 newPort = -1; 288 tr_natpmpPulse( s->natpmp, &newPort ); 289 if( 0 < newPort && newPort != s->publicPort ) 290 { 291 SetPublicPort( s, newPort ); 292 } 293 tr_upnpPulse( s->upnp ); 294 295 /* Handle incoming connections */ 296 AcceptPeers( s ); 297 ReadPeers( s ); 298 DispatchPeers( s ); 299 300 /* Update choking every second */ 301 if( date1 > lastchoke + 1000 ) 302 { 303 tr_chokingPulse( s->choking ); 304 lastchoke = date1; 305 } 306 307 tr_swiftPulse ( s->h ); 308 309 /* Wait up to 20 ms */ 310 date2 = tr_date(); 311 if( date2 < date1 + 20 ) 312 { 313 tr_sharedUnlock( s ); 314 tr_wait( date1 + 20 - date2 ); 315 tr_sharedLock( s ); 316 } 317 } 245 /* NAT-PMP and UPnP pulses */ 246 newPort = -1; 247 tr_natpmpPulse( s->natpmp, &newPort ); 248 if( 0 < newPort && newPort != s->publicPort ) 249 SetPublicPort( s, newPort ); 250 tr_upnpPulse( s->upnp ); 251 252 /* Handle incoming connections */ 253 AcceptPeers( s ); 318 254 319 255 tr_sharedUnlock( s ); 256 257 return TRUE; 320 258 } 321 259 … … 323 261 * SetPublicPort 324 262 **********************************************************************/ 325 static void SetPublicPort( tr_shared _t* s, int port )326 { 327 tr_handle _t* h = s->h;328 tr_torrent _t* tor;263 static void SetPublicPort( tr_shared * s, int port ) 264 { 265 tr_handle * h = s->h; 266 tr_torrent * tor; 329 267 330 268 s->publicPort = port; … … 339 277 * Check incoming connections and add the peers to our local list 340 278 **********************************************************************/ 341 static void AcceptPeers( tr_shared_t * s ) 342 { 343 int socket; 344 struct in_addr addr; 345 279 280 static void 281 AcceptPeers( tr_shared * s ) 282 { 346 283 for( ;; ) 347 284 { 348 if( s->bindSocket < 0 || s->peerCount >= MAX_PEER_COUNT ) 349 { 285 int socket; 286 struct in_addr addr; 287 288 if( s->bindSocket < 0 || !tr_peerMgrIsAcceptingConnections( s->h->peerMgr ) ) 350 289 break; 351 }352 290 353 291 socket = tr_netAccept( s->bindSocket, &addr, NULL ); 354 292 if( socket < 0 ) 355 {356 293 break; 357 } 358 s->peers[s->peerCount++] = tr_peerInit( &addr, 0, socket, 359 TR_PEER_FROM_INCOMING ); 360 } 361 } 362 363 /*********************************************************************** 364 * ReadPeers 365 *********************************************************************** 366 * Try to read handshakes 367 **********************************************************************/ 368 static void ReadPeers( tr_shared_t * s ) 369 { 370 int ii; 371 372 for( ii = 0; ii < s->peerCount; ) 373 { 374 if( tr_peerRead( s->peers[ii] ) ) 375 { 376 tr_peerDestroy( s->peers[ii] ); 377 s->peerCount--; 378 memmove( &s->peers[ii], &s->peers[ii+1], 379 ( s->peerCount - ii ) * sizeof( tr_peer_t * ) ); 380 continue; 381 } 382 ii++; 383 } 384 } 385 386 /*********************************************************************** 387 * DispatchPeers 388 *********************************************************************** 389 * If we got a handshake, try to find the torrent for this peer 390 **********************************************************************/ 391 static void DispatchPeers( tr_shared_t * s ) 392 { 393 tr_handle_t * h = s->h; 394 int ii; 395 const uint64_t now = tr_date(); 396 397 for( ii = 0; ii < s->peerCount; ) 398 { 399 const uint8_t * hash = tr_peerHash( s->peers[ii] ); 400 401 if( !hash && now > tr_peerDate( s->peers[ii] ) + 10000 ) 402 { 403 /* 10 seconds and no handshake, drop it */ 404 tr_peerDestroy( s->peers[ii] ); 405 goto removePeer; 406 } 407 if( hash ) 408 { 409 tr_torrent_t * tor; 410 411 for( tor = h->torrentList; tor; tor = tor->next ) 412 { 413 tr_torrentWriterLock( tor ); 414 if( tor->runStatus != TR_RUN_RUNNING ) 415 { 416 tr_torrentWriterUnlock( tor ); 417 continue; 418 } 419 420 if( !memcmp( tor->info.hash, hash, SHA_DIGEST_LENGTH ) ) 421 { 422 /* Found it! */ 423 tr_torrentAttachPeer( tor, s->peers[ii] ); 424 tr_torrentWriterUnlock( tor ); 425 goto removePeer; 426 } 427 tr_torrentWriterUnlock( tor ); 428 } 429 430 /* Couldn't find a torrent, we probably removed it */ 431 tr_peerDestroy( s->peers[ii] ); 432 goto removePeer; 433 } 434 ii++; 435 continue; 436 437 removePeer: 438 s->peerCount--; 439 memmove( &s->peers[ii], &s->peers[ii+1], 440 ( s->peerCount - ii ) * sizeof( tr_peer_t * ) ); 441 } 442 } 443 294 295 tr_peerMgrAddIncoming( s->h->peerMgr, &addr, socket ); 296 } 297 } -
trunk/libtransmission/shared.h
r2555 r3105 28 28 #include "transmission.h" 29 29 30 typedef struct tr_shared _s tr_shared_t;30 typedef struct tr_shared tr_shared; 31 31 32 32 /*********************************************************************** … … 36 36 * among the torrents: NAT-PMP/UPnP, incoming connections, peer choking 37 37 **********************************************************************/ 38 tr_shared _t * tr_sharedInit ( tr_handle_t* );39 void tr_sharedClose ( tr_shared_t* );38 tr_shared * tr_sharedInit ( tr_handle * ); 39 void tr_sharedClose ( tr_shared * ); 40 40 41 41 /*********************************************************************** … … 45 45 * thread 46 46 **********************************************************************/ 47 void tr_sharedLock ( tr_shared _t* );48 void tr_sharedUnlock ( tr_shared _t* );47 void tr_sharedLock ( tr_shared * ); 48 void tr_sharedUnlock ( tr_shared * ); 49 49 50 50 /*********************************************************************** … … 54 54 * should be called with the shared lock held. 55 55 **********************************************************************/ 56 void tr_sharedSetPort ( tr_shared _t*, int port );57 int tr_sharedGetPublicPort ( tr_shared _t* s );56 void tr_sharedSetPort ( tr_shared *, int port ); 57 int tr_sharedGetPublicPort ( tr_shared * s ); 58 58 59 59 /*********************************************************************** … … 63 63 * be called with the shared lock held. 64 64 **********************************************************************/ 65 void tr_sharedTraversalEnable ( tr_shared _t*, int enable );66 int tr_sharedTraversalStatus ( tr_shared _t* );65 void tr_sharedTraversalEnable ( tr_shared *, int enable ); 66 int tr_sharedTraversalStatus ( tr_shared * ); 67 67 68 /***********************************************************************69 * tr_sharedSetLimit70 ***********************************************************************71 *72 **********************************************************************/73 void tr_sharedSetLimit ( tr_shared_t *, int limit );74 68 75 69 #endif -
trunk/libtransmission/torrent.c
r2891 r3105 32 32 #include "transmission.h" 33 33 #include "completion.h" 34 #include "crypto.h" /* for tr_sha1 */ 34 35 #include "fastresume.h" 36 #include "handshake.h" 35 37 #include "inout.h" 36 38 #include "metainfo.h" 37 39 #include "net.h" /* tr_netNtop */ 38 40 #include "peer.h" 41 #include "peer-mgr.h" 39 42 #include "platform.h" 40 43 #include "ratecontrol.h" … … 42 45 #include "tracker.h" 43 46 #include "trcompat.h" /* for strlcpy */ 47 #include "trevent.h" 44 48 #include "utils.h" 49 50 /*** 51 **** 52 ***/ 53 54 int 55 tr_torrentExists( tr_handle * handle, 56 const uint8_t * torrentHash ) 57 { 58 return tr_torrentFindFromHash( handle, torrentHash ) != NULL; 59 } 60 61 tr_torrent* 62 tr_torrentFindFromHash( tr_handle * handle, 63 const uint8_t * torrentHash ) 64 { 65 tr_torrent * tor; 66 67 for( tor = handle->torrentList; tor; tor = tor->next ) 68 if( !memcmp( tor->info.hash, torrentHash, SHA_DIGEST_LENGTH ) ) 69 return tor; 70 71 return NULL; 72 } 73 74 tr_torrent* 75 tr_torrentFindFromObfuscatedHash( tr_handle * handle, 76 const uint8_t * obfuscatedTorrentHash ) 77 { 78 tr_torrent * tor; 79 80 for( tor = handle->torrentList; tor; tor = tor->next ) 81 if( !memcmp( tor->obfuscatedHash, obfuscatedTorrentHash, SHA_DIGEST_LENGTH ) ) 82 return tor; 83 84 return NULL; 85 } 45 86 46 87 /*** … … 49 90 50 91 void 51 tr_torrentReaderLock( const tr_torrent_t * tor ) 52 { 53 tr_rwReaderLock ( (tr_rwlock_t*)tor->lock ); 54 } 55 56 void 57 tr_torrentReaderUnlock( const tr_torrent_t * tor ) 58 { 59 tr_rwReaderUnlock ( (tr_rwlock_t*)tor->lock ); 60 } 61 62 void 63 tr_torrentWriterLock( tr_torrent_t * tor ) 64 { 65 tr_rwWriterLock ( tor->lock ); 66 } 67 68 void 69 tr_torrentWriterUnlock( tr_torrent_t * tor ) 70 { 71 tr_rwWriterUnlock ( tor->lock ); 92 tr_torrentLock( const tr_torrent * tor ) 93 { 94 tr_lockLock ( (tr_lock*)tor->lock ); 95 } 96 97 void 98 tr_torrentUnlock( const tr_torrent * tor ) 99 { 100 tr_lockUnlock ( (tr_lock*)tor->lock ); 72 101 } 73 102 … … 77 106 78 107 void 79 tr_torrentSetSpeedMode( tr_torrent _t* tor,80 int 81 tr_speedlimit _tmode )82 { 83 tr_speedlimit _t* limit = up_or_down==TR_UP108 tr_torrentSetSpeedMode( tr_torrent * tor, 109 int up_or_down, 110 tr_speedlimit mode ) 111 { 112 tr_speedlimit * limit = up_or_down==TR_UP 84 113 ? &tor->uploadLimitMode 85 114 : &tor->downloadLimitMode; … … 87 116 } 88 117 89 tr_speedlimit _t90 tr_torrentGetSpeedMode( const tr_torrent _t* tor,91 int 118 tr_speedlimit 119 tr_torrentGetSpeedMode( const tr_torrent * tor, 120 int up_or_down) 92 121 { 93 122 return up_or_down==TR_UP ? tor->uploadLimitMode … … 96 125 97 126 void 98 tr_torrentSetSpeedLimit( tr_torrent _t* tor,99 int 100 int 101 { 102 tr_ratecontrol _t* rc = up_or_down==TR_UP ? tor->upload : tor->download;127 tr_torrentSetSpeedLimit( tr_torrent * tor, 128 int up_or_down, 129 int single_KiB_sec ) 130 { 131 tr_ratecontrol * rc = up_or_down==TR_UP ? tor->upload : tor->download; 103 132 tr_rcSetLimit( rc, single_KiB_sec ); 104 133 } 105 134 106 135 int 107 tr_torrentGetSpeedLimit( const tr_torrent _t* tor,108 int 109 { 110 tr_ratecontrol _t* rc = up_or_down==TR_UP ? tor->upload : tor->download;136 tr_torrentGetSpeedLimit( const tr_torrent * tor, 137 int up_or_down ) 138 { 139 tr_ratecontrol * rc = up_or_down==TR_UP ? tor->upload : tor->download; 111 140 return tr_rcGetLimit( rc ); 112 141 } … … 116 145 ***/ 117 146 118 static void setRunState( tr_torrent_t *, run_status_t );119 120 147 static void 121 148 onTrackerResponse( void * tracker UNUSED, void * vevent, void * user_data ) 122 149 { 123 tr_torrent _t * tor = (tr_torrent_t *) user_data;150 tr_torrent * tor = (tr_torrent *) user_data; 124 151 tr_tracker_event_t * event = (tr_tracker_event_t *) vevent; 125 152 … … 127 154 { 128 155 case TR_TRACKER_PEERS: 129 tr_torrentAddCompact( tor, TR_PEER_FROM_TRACKER, 130 event->peerCompact, event->peerCount ); 156 tr_peerMgrAddPeers( tor->handle->peerMgr, 157 tor->info.hash, 158 TR_PEER_FROM_TRACKER, 159 event->peerCompact, 160 event->peerCount ); 131 161 break; 132 162 … … 144 174 145 175 case TR_TRACKER_STOPPED: 146 if( tor->runStatus == TR_RUN_STOPPING_NET_WAIT )147 setRunState( tor, TR_RUN_STOPPED );176 //assert( tor->runStatus == TR_RUN_STOPPING ); 177 tor->runStatus = TR_RUN_STOPPED; 148 178 break; 149 179 } … … 157 187 158 188 static int 159 getBytePiece( const tr_info _t* info, uint64_t byteOffset )189 getBytePiece( const tr_info * info, uint64_t byteOffset ) 160 190 { 161 191 assert( info != NULL ); … … 166 196 167 197 static void 168 initFilePieces ( tr_info _t* info, int fileIndex )169 { 170 tr_file _t* file = &info->files[fileIndex];198 initFilePieces ( tr_info * info, int fileIndex ) 199 { 200 tr_file * file = &info->files[fileIndex]; 171 201 uint64_t firstByte, lastByte; 172 202 … … 182 212 183 213 static tr_priority_t 184 calculatePiecePriority ( const tr_torrent _t* tor,185 int 214 calculatePiecePriority ( const tr_torrent * tor, 215 int piece ) 186 216 { 187 217 int i; … … 190 220 for( i=0; i<tor->info.fileCount; ++i ) 191 221 { 192 const tr_file _t* file = &tor->info.files[i];222 const tr_file * file = &tor->info.files[i]; 193 223 if ( file->firstPiece <= piece 194 224 && file->lastPiece >= piece … … 208 238 209 239 static void 210 tr_torrentInitFilePieces( tr_torrent _t* tor )240 tr_torrentInitFilePieces( tr_torrent * tor ) 211 241 { 212 242 int i; … … 225 255 } 226 256 227 static void torrentThreadLoop( void* );257 static void recheckCpState( tr_torrent * ); 228 258 229 259 static void 230 torrentRealInit( tr_handle_t * h, 231 tr_torrent_t * tor, 232 const char * destination, 233 int flags ) 260 tr_torrentStartImpl( tr_torrent * tor ) 261 { 262 assert( tor != NULL ); 263 assert( tor->runStatus == TR_RUN_RUNNING ); 264 265 *tor->errorString = '\0'; 266 tr_torrentResetTransferStats( tor ); 267 recheckCpState( tor ); 268 tor->startDate = tr_date(); 269 tr_trackerStart( tor->tracker ); 270 tr_peerMgrStartTorrent( tor->handle->peerMgr, tor->info.hash ); 271 } 272 273 static void 274 recheckDoneCB( tr_torrent * tor ) 275 { 276 recheckCpState( tor ); 277 278 if( tor->runStatus == TR_RUN_RUNNING ) 279 tr_torrentStartImpl( tor ); 280 } 281 282 static void 283 torrentRealInit( tr_handle_t * h, 284 tr_torrent * tor, 285 const char * destination, 286 int flags ) 234 287 { 235 288 uint64_t loaded; 236 289 uint64_t t; 237 char name[512]; 238 tr_bitfield_t * uncheckedPieces; 239 tr_info_t * info = &tor->info; 290 tr_bitfield * uncheckedPieces; 291 tr_info * info = &tor->info; 240 292 241 293 tor->info.flags |= flags; … … 299 351 tr_torrentInitFilePieces( tor ); 300 352 301 tor->lock = tr_ rwNew( );353 tor->lock = tr_lockNew( ); 302 354 303 355 tor->upload = tr_rcInit(); 304 356 tor->download = tr_rcInit(); 305 357 tor->swarmspeed = tr_rcInit(); 358 359 tr_sha1( tor->obfuscatedHash, "req2", 4, 360 info->hash, SHA_DIGEST_LENGTH, 361 NULL ); 306 362 307 363 /* We have a new torrent */ … … 309 365 310 366 tr_sharedUnlock( h->shared ); 367 368 tr_peerMgrAddTorrent( h->peerMgr, tor ); 311 369 312 370 if( !h->isPortSet ) … … 347 405 tor->tracker = tr_trackerNew( tor ); 348 406 tor->trackerSubscription = tr_trackerSubscribe( tor->tracker, onTrackerResponse, tor ); 349 if( tor->runStatus == TR_RUN_RUNNING )350 tr_trackerStart( tor->tracker );351 407 352 408 tr_sharedLock( h->shared ); … … 356 412 tr_sharedUnlock( h->shared ); 357 413 358 snprintf( name, sizeof( name ), "torrent %p (%s)", tor, tor->info.name ); 359 tor->thread = tr_threadNew( torrentThreadLoop, tor, name ); 414 tr_ioRecheckAdd( tor, recheckDoneCB, tor->runStatus ); 360 415 } 361 416 … … 365 420 const char * name ) 366 421 { 367 const tr_torrent _t* tor;422 const tr_torrent * tor; 368 423 369 424 for( tor=h->torrentList; tor; tor=tor->next ) … … 379 434 const uint8_t * hash ) 380 435 { 381 const tr_torrent _t* tor;436 const tr_torrent * tor; 382 437 383 438 for( tor=h->torrentList; tor; tor=tor->next ) … … 391 446 infoCanAdd( const tr_handle_t * h, 392 447 const char * destination, 393 const tr_info _t* info )448 const tr_info * info ) 394 449 { 395 450 if( hashExists( h, info->hash ) ) … … 406 461 const char * path, 407 462 const char * destination, 408 tr_info _t* setme_info )463 tr_info * setme_info ) 409 464 { 410 465 int ret, doFree; 411 tr_info _ttmp;466 tr_info tmp; 412 467 413 468 if( setme_info == NULL ) 414 469 setme_info = &tmp; 415 470 416 memset( setme_info, 0, sizeof( tr_info _t) );471 memset( setme_info, 0, sizeof( tr_info ) ); 417 472 ret = tr_metainfoParseFile( setme_info, h->tag, path, FALSE ); 418 473 doFree = !ret && (setme_info == &tmp); … … 427 482 } 428 483 429 tr_torrent _t*484 tr_torrent * 430 485 tr_torrentInit( tr_handle_t * h, 431 486 const char * path, … … 436 491 int val; 437 492 int tmpError; 438 tr_torrent _t* tor = NULL;493 tr_torrent * tor = NULL; 439 494 440 495 if( !error ) … … 443 498 if(( val = tr_torrentParse( h, path, destination, NULL ))) 444 499 *error = val; 445 else if(!(( tor = tr_new0( tr_torrent _t, 1 ))))500 else if(!(( tor = tr_new0( tr_torrent, 1 )))) 446 501 *error = TR_EOTHER; 447 502 else { … … 457 512 const char * hashStr, 458 513 const char * destination, 459 tr_info _t* setme_info )514 tr_info * setme_info ) 460 515 { 461 516 int ret, doFree; 462 tr_info _ttmp;517 tr_info tmp; 463 518 464 519 if( setme_info == NULL ) 465 520 setme_info = &tmp; 466 521 467 memset( setme_info, 0, sizeof( tr_info _t) );522 memset( setme_info, 0, sizeof( tr_info ) ); 468 523 ret = tr_metainfoParseHash( setme_info, h->tag, hashStr ); 469 524 doFree = !ret && (setme_info == &tmp); … … 478 533 } 479 534 480 tr_torrent _t*535 tr_torrent * 481 536 tr_torrentInitSaved( tr_handle_t * h, 482 537 const char * hashStr, … … 487 542 int val; 488 543 int tmpError; 489 tr_torrent _t* tor = NULL;544 tr_torrent * tor = NULL; 490 545 491 546 if( !error ) … … 494 549 if(( val = tr_torrentParseHash( h, hashStr, destination, NULL ))) 495 550 *error = val; 496 else if(!(( tor = tr_new0( tr_torrent _t, 1 ))))551 else if(!(( tor = tr_new0( tr_torrent, 1 )))) 497 552 *error = TR_EOTHER; 498 553 else { … … 509 564 size_t size, 510 565 const char * destination, 511 tr_info _t* setme_info )566 tr_info * setme_info ) 512 567 { 513 568 int ret, doFree; 514 tr_info _ttmp;569 tr_info tmp; 515 570 516 571 if( setme_info == NULL ) 517 572 setme_info = &tmp; 518 573 519 memset( setme_info, 0, sizeof( tr_info _t) );574 memset( setme_info, 0, sizeof( tr_info ) ); 520 575 ret = tr_metainfoParseData( setme_info, h->tag, data, size, FALSE ); 521 576 doFree = !ret && (setme_info == &tmp); … … 530 585 } 531 586 532 tr_torrent _t*587 tr_torrent * 533 588 tr_torrentInitData( tr_handle_t * h, 534 589 const uint8_t * data, … … 540 595 int val; 541 596 int tmpError; 542 tr_torrent _t* tor = NULL;597 tr_torrent * tor = NULL; 543 598 544 599 if( !error ) … … 547 602 if(( val = tr_torrentParseData( h, data, size, destination, NULL ))) 548 603 *error = val; 549 else if(!(( tor = tr_new0( tr_torrent _t, 1 ))))604 else if(!(( tor = tr_new0( tr_torrent, 1 )))) 550 605 *error = TR_EOTHER; 551 606 else { … … 557 612 } 558 613 559 const tr_info _t*560 tr_torrentInfo( const tr_torrent _t* tor )614 const tr_info * 615 tr_torrentInfo( const tr_torrent * tor ) 561 616 { 562 617 return &tor->info; … … 568 623 569 624 #if 0 570 int tr_torrentScrape( tr_torrent _t* tor, int * s, int * l, int * d )625 int tr_torrentScrape( tr_torrent * tor, int * s, int * l, int * d ) 571 626 { 572 627 return tr_trackerScrape( tor, s, l, d ); … … 574 629 #endif 575 630 631 static int 632 saveFastResumeNow( void * vtor ) 633 { 634 tr_torrent * tor = (tr_torrent *) vtor; 635 636 tr_fastResumeSave( tor ); 637 recheckCpState( tor ); 638 639 tr_timerFree( &tor->saveTimer ); 640 return FALSE; 641 } 642 576 643 static void 577 fastResumeSave( tr_torrent_t * tor ) 578 { 579 tr_fastResumeSave( tor ); 580 tor->fastResumeDirty = FALSE; 581 } 582 583 void 584 tr_torrentSetFolder( tr_torrent_t * tor, const char * path ) 644 saveFastResumeSoon( void * vtor ) 645 { 646 tr_torrent * tor = (tr_torrent *) vtor; 647 648 if( tor->saveTimer == NULL ) 649 tor->saveTimer = tr_timerNew( tor->handle, saveFastResumeNow, tor, 1000 ); 650 } 651 652 /** 653 *** 654 **/ 655 656 void 657 tr_torrentSetFolder( tr_torrent * tor, const char * path ) 585 658 { 586 659 tr_free( tor->destination ); 587 660 tor->destination = tr_strdup( path ); 588 fastResumeSave( tor );661 saveFastResumeSoon( tor ); 589 662 } 590 663 591 664 const char* 592 tr_torrentGetFolder( const tr_torrent _t* tor )665 tr_torrentGetFolder( const tr_torrent * tor ) 593 666 { 594 667 return tor->destination; … … 596 669 597 670 void 598 tr_torrentChangeMyPort( tr_torrent _t* tor, int port )599 { 600 tr_torrent WriterLock( tor );671 tr_torrentChangeMyPort( tr_torrent * tor, int port ) 672 { 673 tr_torrentLock( tor ); 601 674 602 675 tor->publicPort = port; … … 605 678 tr_trackerChangeMyPort( tor->tracker ); 606 679 607 tr_torrentWriterUnlock( tor ); 608 } 609 610 /*********************************************************************** 611 * torrentReallyStop 612 *********************************************************************** 613 * Joins the download thread and frees/closes everything related to it. 614 **********************************************************************/ 615 616 void tr_torrentDisablePex( tr_torrent_t * tor, int disable ) 617 { 618 tr_torrentWriterLock( tor ); 619 620 if( ! ( TR_FLAG_PRIVATE & tor->info.flags ) ) 621 { 622 if( tor->pexDisabled != disable ) 623 { 624 int i; 625 tor->pexDisabled = disable; 626 for( i=0; i<tor->peerCount; ++i ) 627 tr_peerSetPrivate( tor->peers[i], disable ); 628 } 629 } 630 631 tr_torrentWriterUnlock( tor ); 632 } 633 634 static int tr_didStateChangeTo ( tr_torrent_t * tor, int status ) 680 tr_torrentUnlock( tor ); 681 } 682 683 684 void 685 tr_torrentDisablePex( tr_torrent * tor, int disable ) 686 { 687 assert( tor != NULL ); 688 assert( disable==0 || disable==1 ); 689 //assert( tor->runStatus != TR_RUN_RUNNING ); 690 691 /* pex is ALWAYS disabled for private torrents */ 692 if( tor->info.flags & TR_FLAG_PRIVATE ) 693 disable = TRUE; 694 695 tor->pexDisabled = disable; 696 } 697 698 static int tr_didStateChangeTo ( tr_torrent * tor, int status ) 635 699 { 636 700 int ret; 637 701 638 tr_torrent WriterLock( tor );702 tr_torrentLock( tor ); 639 703 if (( ret = tor->hasChangedState == status )) 640 704 tor->hasChangedState = -1; 641 tr_torrent WriterUnlock( tor );705 tr_torrentUnlock( tor ); 642 706 643 707 return ret; 644 708 } 645 709 646 int tr_getIncomplete( tr_torrent _t* tor )710 int tr_getIncomplete( tr_torrent * tor ) 647 711 { 648 712 return tr_didStateChangeTo( tor, TR_CP_INCOMPLETE ); 649 713 } 650 int tr_getDone( tr_torrent _t* tor )714 int tr_getDone( tr_torrent * tor ) 651 715 { 652 716 return tr_didStateChangeTo( tor, TR_CP_DONE ); 653 717 } 654 int tr_getComplete( tr_torrent _t* tor )718 int tr_getComplete( tr_torrent * tor ) 655 719 { 656 720 return tr_didStateChangeTo( tor, TR_CP_COMPLETE ); … … 658 722 659 723 void 660 tr_manualUpdate( tr_torrent _t* tor )724 tr_manualUpdate( tr_torrent * tor ) 661 725 { 662 726 if( tor->runStatus == TR_RUN_RUNNING ) 663 727 tr_trackerReannounce( tor->tracker ); 664 728 } 665 729 int 666 tr_torrentCanManualUpdate( const tr_torrent _t* tor )730 tr_torrentCanManualUpdate( const tr_torrent * tor ) 667 731 { 668 732 return ( tor != NULL ) … … 671 735 } 672 736 673 const tr_stat_t * 674 tr_torrentStat( tr_torrent_t * tor ) 675 { 676 tr_stat_t * s; 677 struct tr_tracker_s * tc; 678 int i; 679 680 tr_torrentReaderLock( tor ); 737 const tr_stat * 738 tr_torrentStat( tr_torrent * tor ) 739 { 740 tr_stat * s; 741 struct tr_tracker * tc; 742 743 tr_torrentLock( tor ); 681 744 682 745 tor->statCur = ( tor->statCur + 1 ) % 2; … … 690 753 s->tracker = tr_trackerGetAddress( tor->tracker ); 691 754 692 /* peers... */ 693 memset( s->peersFrom, 0, sizeof( s->peersFrom ) ); 694 s->peersTotal = tor->peerCount; 695 s->peersConnected = 0; 696 s->peersSendingToUs = 0; 697 s->peersGettingFromUs = 0; 698 699 for( i=0; i<tor->peerCount; ++i ) 700 { 701 const tr_peer_t * peer = tor->peers[i]; 702 703 if( tr_peerIsConnected( peer ) ) 704 { 705 ++s->peersConnected; 706 ++s->peersFrom[tr_peerIsFrom(peer)]; 707 708 if( tr_peerDownloadRate( peer ) > 0.01 ) 709 ++s->peersSendingToUs; 710 711 if( tr_peerUploadRate( peer ) > 0.01 ) 712 ++s->peersGettingFromUs; 713 } 714 } 755 tr_peerMgrTorrentStats( tor->handle->peerMgr, 756 tor->info.hash, 757 &s->peersTotal, 758 &s->peersConnected, 759 &s->peersSendingToUs, 760 &s->peersGettingFromUs, 761 s->peersFrom ); 715 762 716 763 s->percentComplete = tr_cpPercentComplete ( tor->completion ); … … 719 766 s->left = tr_cpLeftUntilDone( tor->completion ); 720 767 721 if( tor->uncheckedPieces ) 722 s->status = tor->runStatus==TR_RUN_CHECKING 723 ? TR_STATUS_CHECK 724 : TR_STATUS_CHECK_WAIT; 725 else switch( tor->runStatus ) { 726 case TR_RUN_STOPPING: /* fallthrough */ 727 case TR_RUN_STOPPING_NET_WAIT: s->status = TR_STATUS_STOPPING; break; 768 switch( tor->runStatus ) { 769 case TR_RUN_CHECKING_WAIT: s->status = TR_STATUS_CHECK_WAIT; break; 770 case TR_RUN_CHECKING: s->status = TR_STATUS_CHECK; break; 771 case TR_RUN_STOPPING: s->status = TR_STATUS_STOPPING; break; 728 772 case TR_RUN_STOPPED: s->status = TR_STATUS_STOPPED; break; 729 case TR_RUN_CHECKING: s->status = TR_STATUS_CHECK; break;730 773 case TR_RUN_RUNNING: switch( tor->cpStatus ) { 731 774 case TR_CP_INCOMPLETE: s->status = TR_STATUS_DOWNLOAD; break; … … 773 816 : TR_RATIO_NA; 774 817 775 tr_torrent ReaderUnlock( tor );818 tr_torrentUnlock( tor ); 776 819 777 820 return s; … … 783 826 784 827 static uint64_t 785 fileBytesCompleted ( const tr_torrent _t* tor, int fileIndex )786 { 787 const tr_file _t* file = &tor->info.files[fileIndex];828 fileBytesCompleted ( const tr_torrent * tor, int fileIndex ) 829 { 830 const tr_file * file = &tor->info.files[fileIndex]; 788 831 const uint64_t firstBlock = file->offset / tor->blockSize; 789 832 const uint64_t firstBlockOffset = file->offset % tor->blockSize; … … 825 868 } 826 869 827 tr_file_stat _t*828 tr_torrentFiles( const tr_torrent _t* tor, int * fileCount )870 tr_file_stat * 871 tr_torrentFiles( const tr_torrent * tor, int * fileCount ) 829 872 { 830 873 int i; 831 874 const int n = tor->info.fileCount; 832 tr_file_stat _t * files = tr_new0( tr_file_stat_t, n );833 tr_file_stat _t* walk = files;875 tr_file_stat * files = tr_new0( tr_file_stat, n ); 876 tr_file_stat * walk = files; 834 877 835 878 for( i=0; i<n; ++i, ++walk ) 836 879 { 837 const tr_file _t* file = tor->info.files + i;880 const tr_file * file = tor->info.files + i; 838 881 cp_status_t cp; 839 882 … … 860 903 861 904 void 862 tr_torrentFilesFree( tr_file_stat _t* files, int fileCount UNUSED )905 tr_torrentFilesFree( tr_file_stat * files, int fileCount UNUSED ) 863 906 { 864 907 tr_free( files ); … … 869 912 ***/ 870 913 871 tr_peer_stat_t * 872 tr_torrentPeers( const tr_torrent_t * tor, int * peerCount ) 873 { 874 tr_peer_stat_t * peers; 875 876 tr_torrentReaderLock( tor ); 877 878 *peerCount = tor->peerCount; 879 880 peers = tr_new0( tr_peer_stat_t, tor->peerCount ); 881 if (peers != NULL) 882 { 883 tr_peer_t * peer; 884 struct in_addr * addr; 885 int i; 886 for( i=0; i<tor->peerCount; ++i ) 887 { 888 peer = tor->peers[i]; 889 890 addr = tr_peerAddress( peer ); 891 if( NULL != addr ) 892 { 893 tr_netNtop( addr, peers[i].addr, 894 sizeof( peers[i].addr ) ); 895 } 896 897 peers[i].client = tr_peerClient( peer ); 898 peers[i].isConnected = tr_peerIsConnected( peer ); 899 peers[i].from = tr_peerIsFrom( peer ); 900 peers[i].progress = tr_peerProgress( peer ); 901 peers[i].port = tr_peerPort( peer ); 902 903 peers[i].uploadToRate = tr_peerUploadRate( peer ); 904 peers[i].downloadFromRate = tr_peerDownloadRate( peer ); 905 906 peers[i].isDownloading = peers[i].uploadToRate > 0.01; 907 peers[i].isUploading = peers[i].downloadFromRate > 0.01; 908 } 909 } 910 911 tr_torrentReaderUnlock( tor ); 912 913 return peers; 914 } 915 916 void tr_torrentPeersFree( tr_peer_stat_t * peers, int peerCount UNUSED ) 914 tr_peer_stat * 915 tr_torrentPeers( const tr_torrent * tor, int * peerCount ) 916 { 917 return tr_peerMgrPeerStats( tor->handle->peerMgr, 918 tor->info.hash, peerCount ); 919 } 920 921 void tr_torrentPeersFree( tr_peer_stat * peers, int peerCount UNUSED ) 917 922 { 918 923 tr_free( peers ); 919 924 } 920 925 921 void tr_torrentAvailability( const tr_torrent_t * tor, int8_t * tab, int size ) 922 { 923 int i, j, piece; 924 float interval; 925 926 tr_torrentReaderLock( tor ); 927 928 interval = (float)tor->info.pieceCount / (float)size; 929 for( i = 0; i < size; i++ ) 930 { 931 piece = i * interval; 932 933 if( tr_cpPieceIsComplete( tor->completion, piece ) ) 934 { 935 tab[i] = -1; 936 continue; 937 } 938 939 tab[i] = 0; 940 for( j = 0; j < tor->peerCount; j++ ) 941 { 942 if( tr_peerHasPiece( tor->peers[j], piece ) ) 943 { 944 (tab[i])++; 945 } 946 } 947 } 948 949 tr_torrentReaderUnlock( tor ); 950 } 951 952 void tr_torrentAmountFinished( const tr_torrent_t * tor, float * tab, int size ) 926 void tr_torrentAvailability( const tr_torrent * tor, int8_t * tab, int size ) 927 { 928 return tr_peerMgrTorrentAvailability( tor->handle->peerMgr, 929 tor->info.hash, 930 tab, size ); 931 } 932 933 void tr_torrentAmountFinished( const tr_torrent * tor, float * tab, int size ) 953 934 { 954 935 int i; 955 936 float interval; 956 tr_torrent ReaderLock( tor );937 tr_torrentLock( tor ); 957 938 958 939 interval = (float)tor->info.pieceCount / (float)size; … … 963 944 } 964 945 965 tr_torrent ReaderUnlock( tor );966 } 967 968 void 969 tr_torrentResetTransferStats( tr_torrent _t* tor )970 { 971 tr_torrent WriterLock( tor );946 tr_torrentUnlock( tor ); 947 } 948 949 void 950 tr_torrentResetTransferStats( tr_torrent * tor ) 951 { 952 tr_torrentLock( tor ); 972 953 973 954 tor->downloadedPrev += tor->downloadedCur; … … 978 959 tor->corruptCur = 0; 979 960 980 tr_torrent WriterUnlock( tor );981 } 982 983 984 void 985 tr_torrentSetHasPiece( tr_torrent _t* tor, int pieceIndex, int has )986 { 987 tr_torrent WriterLock( tor );961 tr_torrentUnlock( tor ); 962 } 963 964 965 void 966 tr_torrentSetHasPiece( tr_torrent * tor, int pieceIndex, int has ) 967 { 968 tr_torrentLock( tor ); 988 969 989 970 if( has ) … … 992 973 tr_cpPieceRem( tor->completion, pieceIndex ); 993 974 994 tr_torrent WriterUnlock( tor );995 } 996 997 void tr_torrentRemoveSaved( tr_torrent _t* tor )975 tr_torrentUnlock( tor ); 976 } 977 978 void tr_torrentRemoveSaved( tr_torrent * tor ) 998 979 { 999 980 tr_metainfoRemoveSaved( tor->info.hashString, tor->handle->tag ); 1000 981 } 1001 982 1002 void tr_torrentRecheck( tr_torrent _t* tor )983 void tr_torrentRecheck( tr_torrent * tor ) 1003 984 { 1004 985 if( !tor->uncheckedPieces ) 1005 986 tor->uncheckedPieces = tr_bitfieldNew( tor->info.pieceCount ); 1006 987 tr_bitfieldAddRange( tor->uncheckedPieces, 0, tor->info.pieceCount ); 1007 } 1008 1009 1010 int tr_torrentAttachPeer( tr_torrent_t * tor, tr_peer_t * peer ) 1011 { 1012 int i; 1013 tr_peer_t * otherPeer; 1014 1015 assert( tor != NULL ); 1016 assert( peer != NULL ); 1017 1018 if( tor->peerCount >= TR_MAX_PEER_COUNT ) 1019 { 1020 tr_peerDestroy( peer ); 1021 return 0; 1022 } 1023 1024 /* Don't accept two connections from the same IP */ 1025 for( i = 0; i < tor->peerCount; i++ ) 1026 { 1027 otherPeer = tor->peers[i]; 1028 if( !memcmp( tr_peerAddress( peer ), tr_peerAddress( otherPeer ), 4 ) ) 1029 { 1030 tr_peerDestroy( peer ); 1031 return 0; 1032 } 1033 } 1034 1035 tr_peerSetPrivate( peer, tor->info.flags & TR_FLAG_PRIVATE || 1036 tor->pexDisabled ); 1037 tr_peerSetTorrent( peer, tor ); 1038 tor->peers[tor->peerCount++] = peer; 1039 1040 return 1; 1041 } 1042 1043 int tr_torrentAddCompact( tr_torrent_t * tor, int from, 1044 const uint8_t * buf, int count ) 1045 { 1046 struct in_addr addr; 1047 tr_port_t port; 1048 int i, added; 1049 tr_peer_t * peer; 1050 1051 added = 0; 1052 for( i=0; i<count; ++i ) 1053 { 1054 memcpy( &addr, buf, 4 ); buf += 4; 1055 memcpy( &port, buf, 2 ); buf += 2; 1056 peer = tr_peerInit( &addr, port, -1, from ); 1057 added += tr_torrentAttachPeer( tor, peer ); 1058 } 1059 1060 return added; 988 989 tr_ioRecheckAdd( tor, recheckDoneCB, tor->runStatus ); 1061 990 } 1062 991 … … 1065 994 ***/ 1066 995 1067 static void setRunState( tr_torrent_t * tor, run_status_t run ) 1068 { 1069 tr_torrentWriterLock( tor ); 1070 tor->runStatus = run; 1071 tr_torrentWriterUnlock( tor ); 1072 } 1073 1074 void tr_torrentStart( tr_torrent_t * tor ) 1075 { 1076 setRunState( tor, TR_RUN_RUNNING ); 1077 1078 tr_trackerStart( tor->tracker ); 1079 } 1080 1081 void tr_torrentStop( tr_torrent_t * tor ) 1082 { 1083 if((tor->runStatus != TR_RUN_STOPPING) && (tor->runStatus != TR_RUN_STOPPED) ) 1084 setRunState( tor, TR_RUN_STOPPING ); 1085 } 1086 1087 void tr_torrentClose( tr_torrent_t * tor ) 1088 { 1089 tor->runStatusToSave = tor->runStatus; 1090 tor->runStatusToSaveIsSet = TRUE; 1091 tr_torrentStop( tor ); 1092 tor->dieFlag = TRUE; 996 void 997 tr_torrentStart( tr_torrent * tor ) 998 { 999 tr_ioRecheckAdd( tor, recheckDoneCB, TR_RUN_RUNNING ); 1093 1000 } 1094 1001 1095 1002 static void 1096 tr_torrentFree( tr_torrent _t* tor )1097 { 1098 tr_torrent _t* t;1003 tr_torrentFree( tr_torrent * tor ) 1004 { 1005 tr_torrent * t; 1099 1006 tr_handle_t * h = tor->handle; 1100 tr_info_t * inf = &tor->info; 1007 tr_info * inf = &tor->info; 1008 fprintf( stderr, "closing torrent %s\n", tor->info.name ); 1009 1010 assert( tor != NULL ); 1011 assert( tor->runStatus == TR_RUN_STOPPED ); 1101 1012 1102 1013 tr_sharedLock( h->shared ); 1103 1014 1104 tr_ rwFree( tor->lock );1015 tr_lockFree( tor->lock ); 1105 1016 tr_cpClose( tor->completion ); 1106 1017 … … 1114 1025 1115 1026 tr_free( tor->destination ); 1116 1117 tr_metainfoFree( inf );1118 1027 1119 1028 if( tor == h->torrentList ) … … 1126 1035 } 1127 1036 1037 assert( h->torrentCount >= 1 ); 1128 1038 h->torrentCount--; 1129 1039 … … 1131 1041 tor->info.name, h->torrentCount ); 1132 1042 1043 tr_peerMgrRemoveTorrent( h->peerMgr, tor->info.hash ); 1044 1045 tr_metainfoFree( inf ); 1133 1046 tr_free( tor ); 1134 1047 … … 1136 1049 } 1137 1050 1051 static int 1052 freeWhenStopped( void * vtor ) 1053 { 1054 tr_torrent * tor = vtor; 1055 1056 if( tor->runStatus != TR_RUN_STOPPED ) /* keep waiting */ 1057 return TRUE; 1058 1059 tr_torrentFree( tor ); 1060 return FALSE; 1061 } 1062 1138 1063 static void 1139 recheckCpState( tr_torrent_t * tor ) 1064 tr_torrentStopImpl( void * vtor ) 1065 { 1066 tr_torrent * tor = vtor; 1067 1068 switch( tor->runStatus ) 1069 { 1070 case TR_RUN_CHECKING_WAIT: 1071 case TR_RUN_CHECKING: 1072 tr_ioRecheckRemove( tor ); 1073 tr_torrentStop( tor ); 1074 break; 1075 1076 case TR_RUN_RUNNING: 1077 saveFastResumeNow( tor ); 1078 tr_peerMgrStopTorrent( tor->handle->peerMgr, tor->info.hash ); 1079 tor->runStatus = TR_RUN_STOPPING; 1080 tor->stopDate = tr_date( ); 1081 tr_trackerStop( tor->tracker ); 1082 tr_ioClose( tor ); 1083 break; 1084 1085 case TR_RUN_STOPPING: 1086 case TR_RUN_STOPPED: 1087 break; 1088 } 1089 } 1090 1091 void 1092 tr_torrentStop( tr_torrent * tor ) 1093 { 1094 tr_runInEventThread( tor->handle, tr_torrentStopImpl, tor ); 1095 } 1096 1097 void 1098 tr_torrentClose( tr_torrent * tor ) 1099 { 1100 tor->runStatusToSave = tor->runStatus; 1101 tor->runStatusToSaveIsSet = TRUE; 1102 tor->dieFlag = TRUE; 1103 tr_torrentStop( tor ); 1104 tr_timerNew( tor->handle, freeWhenStopped, tor, 250 ); 1105 } 1106 1107 static void 1108 recheckCpState( tr_torrent * tor ) 1140 1109 { 1141 1110 cp_status_t cpStatus; 1142 1111 1143 tr_torrent WriterLock( tor );1112 tr_torrentLock( tor ); 1144 1113 1145 1114 cpStatus = tr_cpGetStatus( tor->completion ); … … 1151 1120 tr_trackerCompleted( tor->tracker ); /* tell the tracker */ 1152 1121 } 1153 tr_ioSync( tor->io ); 1154 fastResumeSave( tor ); 1155 } 1156 tr_torrentWriterUnlock( tor ); 1157 } 1158 1159 static void 1160 torrentThreadLoop ( void * _tor ) 1161 { 1162 static tr_lock_t * checkFilesLock = NULL; 1163 tr_torrent_t * tor = _tor; 1164 1165 /* create the check-files mutex */ 1166 if( !checkFilesLock ) 1167 checkFilesLock = tr_lockNew( ); 1168 1169 /* loop until the torrent is being deleted */ 1170 while( ! ( tor->dieFlag && (tor->runStatus == TR_RUN_STOPPED) ) ) 1171 { 1172 /* sleep a little while */ 1173 tr_wait( tor->runStatus == TR_RUN_STOPPED ? 1600 : 600 ); 1174 1175 if( tor->fastResumeDirty ) 1176 { 1177 fastResumeSave( tor ); 1178 recheckCpState( tor ); 1179 } 1180 1181 /* if we're stopping... */ 1182 if( tor->runStatus == TR_RUN_STOPPING ) 1183 { 1184 int i; 1185 tr_torrentWriterLock( tor ); 1186 1187 /* close the IO */ 1188 tr_ioClose( tor->io ); 1189 tor->io = NULL; 1190 fastResumeSave( tor ); 1191 1192 /* close the peers */ 1193 for( i=0; i<tor->peerCount; ++i ) 1194 tr_peerDestroy( tor->peers[i] ); 1195 tor->peerCount = 0; 1196 1197 /* resest the transfer rates */ 1198 tr_rcReset( tor->download ); 1199 tr_rcReset( tor->upload ); 1200 tr_rcReset( tor->swarmspeed ); 1201 1202 /* tell the tracker we're stopping */ 1203 tr_trackerStop( tor->tracker ); 1204 tor->runStatus = TR_RUN_STOPPING_NET_WAIT; 1205 tor->stopDate = tr_date(); 1206 tr_torrentWriterUnlock( tor ); 1207 } 1208 1209 if( tor->runStatus == TR_RUN_STOPPING_NET_WAIT ) 1210 { 1211 #if 0 1212 tr_torrentWriterLock( tor ); 1213 tor->runStatus = TR_RUN_STOPPED; 1214 tr_torrentWriterUnlock( tor ); 1215 #endif 1216 continue; 1217 } 1218 1219 /* do we need to check files? */ 1220 if( tor->uncheckedPieces ) 1221 { 1222 if( !tr_lockTryLock( checkFilesLock ) ) 1223 { 1224 run_status_t realStatus; 1225 1226 tr_torrentWriterLock( tor ); 1227 realStatus = tor->runStatus; 1228 tor->runStatus = TR_RUN_CHECKING; 1229 tr_torrentWriterUnlock( tor ); 1230 1231 tr_ioCheckFiles( tor ); 1232 setRunState( tor, realStatus ); 1233 1234 tr_torrentWriterLock( tor ); 1235 tor->cpStatus = tr_cpGetStatus( tor->completion ); 1236 tr_torrentWriterUnlock( tor ); 1237 1238 tr_lockUnlock( checkFilesLock ); 1239 } 1240 continue; 1241 } 1242 1243 /* if we're paused or stopped, not much to do... */ 1244 if( tor->runStatus == TR_RUN_STOPPED ) 1245 continue; 1246 1247 /* ping our peers if we're running... */ 1248 if( tor->runStatus == TR_RUN_RUNNING ) 1249 { 1250 int i; 1251 1252 /* starting to run... */ 1253 if( tor->io == NULL ) 1254 { 1255 *tor->errorString = '\0'; 1256 tr_torrentResetTransferStats( tor ); 1257 tor->io = tr_ioNew( tor ); 1258 tor->startDate = tr_date(); 1259 } 1260 1261 /* refresh our completion state */ 1262 recheckCpState( tor ); 1263 1264 /* Shuffle peers */ 1265 if ( tor->peerCount > 1 ) { 1266 tr_peer_t * tmp = tor->peers[0]; 1267 memmove( tor->peers, tor->peers+1, 1268 (tor->peerCount-1) * sizeof(void*) ); 1269 tor->peers[tor->peerCount - 1] = tmp; 1270 } 1271 1272 /* receive/send messages */ 1273 tr_torrentWriterLock( tor ); 1274 for( i=0; i<tor->peerCount; ) { 1275 tr_peer_t * peer = tor->peers[i]; 1276 int ret = tr_peerPulse( peer ); 1277 if( ret & TR_ERROR_IO_MASK ) { 1278 tr_err( "Fatal error, stopping download (%d)", ret ); 1279 tor->runStatus = TR_RUN_STOPPING; 1280 tor->error = ret; 1281 strlcpy( tor->errorString, 1282 tr_errorString(ret), 1283 sizeof(tor->errorString) ); 1284 break; 1285 } 1286 if( ret ) { 1287 tr_peerDestroy( peer ); 1288 tor->peerCount--; 1289 memmove( &tor->peers[i], &tor->peers[i+1], 1290 (tor->peerCount-i)*sizeof(void*) ); 1291 continue; 1292 } 1293 i++; 1294 } 1295 tr_torrentWriterUnlock( tor ); 1296 } 1297 } 1298 1299 tr_ioClose( tor->io ); 1300 tr_torrentFree( tor ); 1301 } 1302 1122 tr_ioClose( tor ); 1123 saveFastResumeSoon( tor ); 1124 } 1125 tr_torrentUnlock( tor ); 1126 } 1303 1127 1304 1128 /** … … 1307 1131 1308 1132 void 1309 tr_torrentSetFilePriority( tr_torrent _t* tor,1310 int 1311 tr_priority_t 1133 tr_torrentSetFilePriority( tr_torrent * tor, 1134 int fileIndex, 1135 tr_priority_t priority ) 1312 1136 { 1313 1137 int i; 1314 tr_file _t* file;1315 1316 tr_torrent WriterLock( tor );1138 tr_file * file; 1139 1140 tr_torrentLock( tor ); 1317 1141 1318 1142 assert( tor != NULL ); … … 1329 1153 priority, tor->info.files[fileIndex].name ); 1330 1154 1331 tor->fastResumeDirty = TRUE;1332 1333 tr_torrent WriterUnlock( tor );1334 } 1335 1336 void 1337 tr_torrentSetFilePriorities( tr_torrent _t* tor,1338 int 1339 int 1340 tr_priority_t 1155 saveFastResumeSoon( tor ); 1156 1157 tr_torrentUnlock( tor ); 1158 } 1159 1160 void 1161 tr_torrentSetFilePriorities( tr_torrent * tor, 1162 int * files, 1163 int fileCount, 1164 tr_priority_t priority ) 1341 1165 { 1342 1166 int i; … … 1346 1170 1347 1171 tr_priority_t 1348 tr_torrentGetFilePriority( const tr_torrent _t* tor, int file )1172 tr_torrentGetFilePriority( const tr_torrent * tor, int file ) 1349 1173 { 1350 1174 tr_priority_t ret; 1351 1175 1352 tr_torrent ReaderLock( tor );1176 tr_torrentLock( tor ); 1353 1177 assert( tor != NULL ); 1354 1178 assert( 0<=file && file<tor->info.fileCount ); 1355 1179 ret = tor->info.files[file].priority; 1356 tr_torrent ReaderUnlock( tor );1180 tr_torrentUnlock( tor ); 1357 1181 1358 1182 return ret; … … 1361 1185 1362 1186 tr_priority_t* 1363 tr_torrentGetFilePriorities( const tr_torrent _t* tor )1187 tr_torrentGetFilePriorities( const tr_torrent * tor ) 1364 1188 { 1365 1189 int i; 1366 1190 tr_priority_t * p; 1367 1191 1368 tr_torrent ReaderLock( tor );1192 tr_torrentLock( tor ); 1369 1193 p = tr_new0( tr_priority_t, tor->info.fileCount ); 1370 1194 for( i=0; i<tor->info.fileCount; ++i ) 1371 1195 p[i] = tor->info.files[i].priority; 1372 tr_torrent ReaderUnlock( tor );1196 tr_torrentUnlock( tor ); 1373 1197 1374 1198 return p; … … 1380 1204 1381 1205 int 1382 tr_torrentGetFileDL( const tr_torrent _t* tor,1383 int 1206 tr_torrentGetFileDL( const tr_torrent * tor, 1207 int file ) 1384 1208 { 1385 1209 int doDownload; 1386 tr_torrent ReaderLock( tor );1210 tr_torrentLock( tor ); 1387 1211 1388 1212 assert( 0<=file && file<tor->info.fileCount ); 1389 1213 doDownload = !tor->info.files[file].dnd; 1390 1214