Changeset 6921


Ignore:
Timestamp:
Oct 17, 2008, 8:57:54 PM (13 years ago)
Author:
charles
Message:

more hacking on libcurl + libevent integration

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/web.c

    r6919 r6921  
    2929#endif
    3030
     31#define DEFAULT_TIMER_MSEC 2000
     32
    3133#define dbgmsg( ... )  tr_deepLog( __FILE__, __LINE__, "web", __VA_ARGS__ )
    3234/* #define dbgmsg(...)  do { fprintf( stderr, __VA_ARGS__ ); fprintf( stderr, "\n" ); } while( 0 ) */
     
    3739    int prev_running;
    3840    int still_running;
     41    long timer_ms;
    3942    CURLM * multi;
    4043    tr_session * session;
     44    tr_list * easy_queue;
    4145    struct event timer_event;
    42     tr_list * easy_queue;
    4346};
    4447
     
    5659    tr_web_done_func * done_func;
    5760    void * done_func_user_data;
     61    long timer_ms;
    5862};
    5963
     
    96100            curl_easy_setopt( easy, CURLOPT_PROXY, session->proxy );
    97101            curl_easy_setopt( easy, CURLOPT_PROXYPORT, session->proxyPort );
    98             curl_easy_setopt( easy, CURLOPT_PROXYTYPE, getCurlProxyType( session->proxyType ) );
     102            curl_easy_setopt( easy, CURLOPT_PROXYTYPE,
     103                                      getCurlProxyType( session->proxyType ) );
    99104            curl_easy_setopt( easy, CURLOPT_PROXYAUTH, CURLAUTH_ANY );
    100105        }
    101106        if( !task->range && session->isProxyAuthEnabled ) {
    102             char * str = tr_strdup_printf( "%s:%s", session->proxyUsername, session->proxyPassword );
     107            char * str = tr_strdup_printf( "%s:%s", session->proxyUsername,
     108                                                    session->proxyPassword );
    103109            curl_easy_setopt( easy, CURLOPT_PROXYUSERPWD, str );
    104110            tr_free( str );
     
    109115        curl_easy_setopt( easy, CURLOPT_WRITEFUNCTION, writeFunc );
    110116        curl_easy_setopt( easy, CURLOPT_WRITEDATA, task );
    111         curl_easy_setopt( easy, CURLOPT_USERAGENT, TR_NAME "/" LONG_VERSION_STRING );
     117        curl_easy_setopt( easy, CURLOPT_USERAGENT,
     118                                             TR_NAME "/" LONG_VERSION_STRING );
    112119        curl_easy_setopt( easy, CURLOPT_SSL_VERIFYHOST, 0 );
    113120        curl_easy_setopt( easy, CURLOPT_SSL_VERIFYPEER, 0 );
     
    117124        curl_easy_setopt( easy, CURLOPT_MAXREDIRS, 5 );
    118125        curl_easy_setopt( easy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
    119         curl_easy_setopt( easy, CURLOPT_VERBOSE, getenv( "TR_CURL_VERBOSE" ) != NULL );
     126        curl_easy_setopt( easy, CURLOPT_VERBOSE,
     127                                         getenv( "TR_CURL_VERBOSE" ) != NULL );
    120128        if( task->range )
    121129            curl_easy_setopt( easy, CURLOPT_RANGE, task->range );
    122         else /* don't set encoding if range is sent; it messes up binary data */
     130        else /* don't set encoding on webseeds; it messes up binary data */
    123131            curl_easy_setopt( easy, CURLOPT_ENCODING, "" );
    124132
    125133        if( web->still_running >= MAX_CONCURRENT_TASKS ) {
    126134            tr_list_append( &web->easy_queue, easy );
    127             dbgmsg( " >> adding a task to the curl queue... size is now %d", tr_list_size( web->easy_queue ) );
     135            dbgmsg( " >> adding a task to the curl queue... size is now %d",
     136                                             tr_list_size( web->easy_queue ) );
    128137        } else {
    129138            CURLMcode rc = curl_multi_add_handle( web->multi, easy );
     
    246255}
    247256
     257
     258static void
     259reset_timer( tr_web * g )
     260{
     261    struct timeval timeout;
     262
     263    if( evtimer_pending( &g->timer_event, NULL ) )
     264        evtimer_del( &g->timer_event );
     265
     266    dbgmsg( "adding a timeout for %ld seconds from now", g->timer_ms/1000l );
     267    tr_timevalMsec( g->timer_ms, &timeout );
     268    timeout_add( &g->timer_event, &timeout );
     269}
     270
    248271/* libevent says that sock is ready to be processed, so wake up libcurl */
    249272static void
     
    273296        tr_err( "%s", curl_multi_strerror( rc ) );
    274297
     298    reset_timer( g );
    275299    check_run_count( g );
    276300}
    277301
    278 /* libevent says that timeout_ms have passed, so wake up libcurl */
     302/* libevent says that timer_ms have passed, so wake up libcurl */
    279303static void
    280304timer_cb( int socket UNUSED, short action UNUSED, void * vg )
     
    294318        tr_err( "%s", curl_multi_strerror( rc ) );
    295319
     320    reset_timer( g );
    296321    check_run_count( g );
    297322}
     
    359384
    360385
    361 /* libcurl wants us to tell it when timeout_ms have passed */
    362 static void
    363 multi_timer_cb( CURLM *multi UNUSED, long timeout_ms, void * vweb )
    364 {
    365     tr_web * web = vweb;
    366     struct timeval timeout;
    367     dbgmsg( "adding a timeout for %ld seconds from now", timeout_ms/1000l );
    368     tr_timevalMsec( timeout_ms, &timeout );
    369     timeout_add( &web->timer_event, &timeout );
     386/* from libcurl documentation: "The timeout value returned in the long timeout
     387   points to, is in number of milliseconds at this very moment. If 0, it means
     388   you should proceed immediately without waiting for anything. If it
     389   returns -1, there's no timeout at all set.  Note: if libcurl returns a -1
     390   timeout here, it just means that libcurl currently has no stored timeout
     391   value. You must not wait too long (more than a few seconds perhaps) before
     392   you call curl_multi_perform() again."  */
     393static void
     394multi_timer_cb( CURLM *multi UNUSED, long timer_ms, void * g )
     395{
     396    if( timer_ms < 1 ) {
     397        if( timer_ms == 0 ) /* call it immediately */
     398            timer_cb( 0, 0, g );
     399        timer_ms = DEFAULT_TIMER_MSEC;
     400    }
     401    reset_timer( g );
    370402}
    371403
     
    417449    web->multi = curl_multi_init( );
    418450    web->session = session;
     451    web->timer_ms = DEFAULT_TIMER_MSEC; /* default value to overwrite in multi_timer_cb() */
    419452
    420453    timeout_set( &web->timer_event, timer_cb, web );
Note: See TracChangeset for help on using the changeset viewer.