Changeset 9740 for trunk/libtransmission/web.c
- Timestamp:
- Dec 13, 2009, 5:54:01 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/web.c
r9717 r9740 14 14 #include <stdlib.h> /* bsearch */ 15 15 16 #include <event.h>17 18 16 #define CURL_DISABLE_TYPECHECK /* otherwise -Wunreachable-code goes insane */ 19 17 #include <curl/curl.h> 18 #include <event.h> 20 19 21 20 #include "transmission.h" 22 #include "net.h" /* socklen_t */21 #include "net.h" 23 22 #include "session.h" 24 #include "trevent.h" /* tr_runInEventThread() */23 #include "trevent.h" 25 24 #include "utils.h" 26 25 #include "version.h" … … 29 28 enum 30 29 { 31 /* arbitrary number */ 32 DEFAULT_TIMER_MSEC = 1500 30 DEFAULT_TIMER_MSEC = 1500 /* arbitrary */ 33 31 }; 34 32 … … 121 119 } 122 120 121 static int 122 getTimeoutFromURL( const char * url ) 123 { 124 if( strstr( url, "scrape" ) != NULL ) 125 return 20; 126 if( strstr( url, "announce" ) != NULL ) 127 return 30; 128 return 240; 129 } 130 123 131 static void 124 132 addTask( void * vtask ) … … 129 137 if( session && session->web ) 130 138 { 139 CURLMcode mcode; 140 CURL * easy = curl_easy_init( ); 131 141 struct tr_web * web = session->web; 132 CURL * easy;133 long timeout;142 const long timeout = getTimeoutFromURL( task->url ); 143 const long verbose = getenv( "TR_CURL_VERBOSE" ) != NULL; 134 144 135 145 dbgmsg( "adding task #%lu [%s]", task->tag, task->url ); 136 137 easy = curl_easy_init( );138 146 139 147 if( !task->range && session->isProxyEnabled ) { … … 152 160 153 161 curl_easy_setopt( easy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); 154 155 /* set a time limit for announces & scrapes */156 if( strstr( task->url, "scrape" ) != NULL )157 timeout = 20L;158 else if( strstr( task->url, "announce" ) != NULL )159 timeout = 30L;160 else161 timeout = 240L;162 162 curl_easy_setopt( easy, CURLOPT_TIMEOUT, timeout ); 163 163 curl_easy_setopt( easy, CURLOPT_CONNECTTIMEOUT, timeout-5 ); 164 dbgmsg( "new task's timeout is %ld\n", timeout );165 166 164 curl_easy_setopt( easy, CURLOPT_SOCKOPTFUNCTION, sockoptfunction ); 167 165 curl_easy_setopt( easy, CURLOPT_SOCKOPTDATA, task ); … … 176 174 curl_easy_setopt( easy, CURLOPT_SSL_VERIFYPEER, 0L ); 177 175 curl_easy_setopt( easy, CURLOPT_URL, task->url ); 178 curl_easy_setopt( easy, CURLOPT_USERAGENT, 179 TR_NAME "/" LONG_VERSION_STRING);180 curl_easy_setopt( easy, CURLOPT_ VERBOSE,181 getenv( "TR_CURL_VERBOSE" ) != NULL);176 curl_easy_setopt( easy, CURLOPT_USERAGENT, TR_NAME "/" LONG_VERSION_STRING ); 177 curl_easy_setopt( easy, CURLOPT_VERBOSE, verbose ); 178 curl_easy_setopt( easy, CURLOPT_WRITEDATA, task ); 179 curl_easy_setopt( easy, CURLOPT_WRITEFUNCTION, writeFunc ); 182 180 if( web->haveAddr ) 183 181 curl_easy_setopt( easy, CURLOPT_INTERFACE, tr_ntop_non_ts( &web->addr ) ); 184 curl_easy_setopt( easy, CURLOPT_WRITEDATA, task );185 curl_easy_setopt( easy, CURLOPT_WRITEFUNCTION, writeFunc );186 182 if( task->range ) 187 183 curl_easy_setopt( easy, CURLOPT_RANGE, task->range ); … … 189 185 curl_easy_setopt( easy, CURLOPT_ENCODING, "" ); 190 186 191 { 192 const CURLMcode mcode = curl_multi_add_handle( web->multi, easy ); 193 tr_assert( mcode == CURLM_OK, "curl_multi_add_handle() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) ); 194 if( mcode == CURLM_OK ) 195 ++web->still_running; 196 else 197 tr_err( "%s", curl_multi_strerror( mcode ) ); 198 199 tr_multi_perform( web, CURL_SOCKET_TIMEOUT ); 200 } 187 mcode = curl_multi_add_handle( web->multi, easy ); 188 if( mcode == CURLM_OK ) 189 ++web->still_running; 190 else 191 tr_err( "%s", curl_multi_strerror( mcode ) ); 192 193 /*tr_multi_perform( web, CURL_SOCKET_TIMEOUT );*/ 201 194 } 202 195 } … … 233 226 remove_finished_tasks( tr_web * g ) 234 227 { 235 CURL * easy; 236 237 do 238 { 239 CURLMsg * msg; 240 int msgs_left; 241 242 easy = NULL; 243 while(( msg = curl_multi_info_read( g->multi, &msgs_left ))) { 244 if( msg->msg == CURLMSG_DONE ) { 245 easy = msg->easy_handle; 246 break; 247 } 248 } 249 250 if( easy ) 251 { 228 CURLMsg * msg; 229 int msgs_left; 230 231 while(( msg = curl_multi_info_read( g->multi, &msgs_left ))) { 232 if(( msg->msg == CURLMSG_DONE ) && ( msg->easy_handle != NULL )) { 252 233 long code; 253 234 struct tr_web_task * task; 235 CURL * easy = msg->easy_handle; 254 236 curl_easy_getinfo( easy, CURLINFO_PRIVATE, (void*)&task ); 255 237 curl_easy_getinfo( easy, CURLINFO_RESPONSE_CODE, &code ); … … 259 241 } 260 242 } 261 while ( easy );262 243 263 244 g->prev_running = g->still_running; … … 312 293 /* invoke libcurl's processing */ 313 294 do { 314 dbgmsg( "calling curl_multi_socket_action..." );315 295 mcode = curl_multi_socket_action( g->multi, fd, 0, &g->still_running ); 316 fd = CURL_SOCKET_TIMEOUT;317 dbgmsg( "done calling curl_multi_socket_action..." );318 296 } while( mcode == CURLM_CALL_MULTI_SOCKET ); 319 tr_assert( mcode == CURLM_OK, "curl_multi_perform() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) );320 297 if( mcode != CURLM_OK ) 321 298 tr_err( "%s", curl_multi_strerror( mcode ) ); … … 344 321 /* CURLMOPT_SOCKETFUNCTION */ 345 322 static int 346 sock_cb( CURL * easy UNUSED, 347 curl_socket_t fd, 348 int action, 349 void * vweb, 350 void * vevent ) 323 sock_cb( CURL * easy UNUSED, curl_socket_t fd, int action, void * vweb, void * vevent ) 351 324 { 352 325 /*static int num_events = 0;*/ … … 379 352 } 380 353 381 dbgmsg( "enabling (libevent %hd, libcurl %d) polling on io_event %p, fd %d", events, action, io_event, fd ); 354 dbgmsg( "enabling (libevent %hd, libcurl %d) polling on io_event %p, fd %d", 355 events, action, io_event, fd ); 382 356 event_set( io_event, fd, events, event_cb, web ); 383 357 event_add( io_event, NULL ); 384 358 } 385 359 386 return 0; /* libcurl doc sez: "The callback MUST return 0." */360 return 0; /* libcurl documentation: "The callback MUST return 0." */ 387 361 } 388 362 … … 467 441 web = tr_new0( struct tr_web, 1 ); 468 442 web->session = session; 469 470 443 web->timer_msec = DEFAULT_TIMER_MSEC; /* overwritten by multi_timer_cb() */ 471 444 evtimer_set( &web->timer_event, libevent_timer_cb, web );
Note: See TracChangeset
for help on using the changeset viewer.