Changeset 6923


Ignore:
Timestamp:
Oct 18, 2008, 3:45:12 PM (12 years ago)
Author:
charles
Message:

this seems to work pretty well. maybe it's done now?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/web.c

    r6922 r6923  
    2323#include "web.h"
    2424
     25/* arbitrary number */
    2526#define MAX_CONCURRENT_TASKS 24
    2627
    27 #define DEFAULT_TIMER_MSEC 2500
     28/* arbitrary number */
     29#define DEFAULT_TIMER_MSEC 2000
    2830
    2931#define dbgmsg( ... )  tr_deepLog( __FILE__, __LINE__, "web", __VA_ARGS__ )
    30 /* #define dbgmsg(...)  do { fprintf( stderr, __VA_ARGS__ ); fprintf( stderr, "\n" ); } while( 0 ) */
     32/* #define dbgmsg(...) \
     33 do { fprintf( stderr, __VA_ARGS__ ); fprintf( stderr, "\n" ); } while( 0 ) */
    3134
    3235struct tr_web
     
    9497        if( !task->range && session->isProxyEnabled ) {
    9598            curl_easy_setopt( easy, CURLOPT_PROXY, session->proxy );
     99            curl_easy_setopt( easy, CURLOPT_PROXYAUTH, CURLAUTH_ANY );
    96100            curl_easy_setopt( easy, CURLOPT_PROXYPORT, session->proxyPort );
    97101            curl_easy_setopt( easy, CURLOPT_PROXYTYPE,
    98102                                      getCurlProxyType( session->proxyType ) );
    99             curl_easy_setopt( easy, CURLOPT_PROXYAUTH, CURLAUTH_ANY );
    100103        }
    101104        if( !task->range && session->isProxyAuthEnabled ) {
     
    106109        }
    107110
     111        curl_easy_setopt( easy, CURLOPT_FOLLOWLOCATION, 1 );
     112        curl_easy_setopt( easy, CURLOPT_MAXREDIRS, 16 );
     113        curl_easy_setopt( easy, CURLOPT_NOSIGNAL, 1 );
    108114        curl_easy_setopt( easy, CURLOPT_PRIVATE, task );
    109         curl_easy_setopt( easy, CURLOPT_URL, task->url );
    110         curl_easy_setopt( easy, CURLOPT_WRITEFUNCTION, writeFunc );
    111         curl_easy_setopt( easy, CURLOPT_WRITEDATA, task );
    112         curl_easy_setopt( easy, CURLOPT_USERAGENT,
    113                                              TR_NAME "/" LONG_VERSION_STRING );
    114115        curl_easy_setopt( easy, CURLOPT_SSL_VERIFYHOST, 0 );
    115116        curl_easy_setopt( easy, CURLOPT_SSL_VERIFYPEER, 0 );
    116         curl_easy_setopt( easy, CURLOPT_NOSIGNAL, 1 );
    117         curl_easy_setopt( easy, CURLOPT_FOLLOWLOCATION, 1 );
    118         curl_easy_setopt( easy, CURLOPT_MAXREDIRS, 5 );
    119         curl_easy_setopt( easy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
     117        curl_easy_setopt( easy, CURLOPT_URL, task->url );
     118        curl_easy_setopt( easy, CURLOPT_USERAGENT,
     119                                           TR_NAME "/" LONG_VERSION_STRING );
    120120        curl_easy_setopt( easy, CURLOPT_VERBOSE,
    121                                          getenv( "TR_CURL_VERBOSE" ) != NULL );
     121                                       getenv( "TR_CURL_VERBOSE" ) != NULL );
     122        curl_easy_setopt( easy, CURLOPT_WRITEDATA, task );
     123        curl_easy_setopt( easy, CURLOPT_WRITEFUNCTION, writeFunc );
    122124        if( task->range )
    123125            curl_easy_setopt( easy, CURLOPT_RANGE, task->range );
     
    127129        if( web->still_running >= MAX_CONCURRENT_TASKS ) {
    128130            tr_list_append( &web->easy_queue, easy );
    129             dbgmsg( " >> adding a task to the curl queue... size is now %d",
    130                                              tr_list_size( web->easy_queue ) );
     131            dbgmsg( " >> enqueueing a task ... size is now %d",
     132                                           tr_list_size( web->easy_queue ) );
    131133        } else {
    132             CURLMcode rc = curl_multi_add_handle( web->multi, easy );
     134            const CURLMcode rc = curl_multi_add_handle( web->multi, easy );
    133135            if( rc == CURLM_OK )
    134136                ++web->still_running;
     
    150152
    151153static void
    152 finish_task( struct tr_web_task * task, long response_code )
     154task_free( struct tr_web_task * task )
     155{
     156    evbuffer_free( task->response );
     157    tr_free( task->range );
     158    tr_free( task->url );
     159    tr_free( task );
     160}
     161
     162static void
     163task_finish( struct tr_web_task * task, long response_code )
    153164{
    154165    dbgmsg( "finished a web task... response code is %ld", response_code );
     
    159170                     EVBUFFER_LENGTH( task->response ),
    160171                     task->done_func_user_data );
    161     evbuffer_free( task->response );
    162     tr_free( task->range );
    163     tr_free( task->url );
    164     tr_free( task );
     172    task_free( task );
    165173}
    166174
     
    195203                curl_multi_remove_handle( g->multi, easy );
    196204                curl_easy_cleanup( easy );
    197                 finish_task( task, code );
     205                task_finish( task, code );
    198206            }
    199207        }
     
    217225restart_timer( tr_web * g )
    218226{
    219     struct timeval timeout;
     227    struct timeval interval;
    220228    stop_timer( g );
    221     dbgmsg( "adding a timeout for %ld seconds from now", g->timer_ms/1000l );
    222     tr_timevalMsec( g->timer_ms, &timeout );
    223     timeout_add( &g->timer_event, &timeout );
     229    dbgmsg( "adding a timeout for %ld seconds from now", g->timer_ms/1000L );
     230    tr_timevalMsec( g->timer_ms, &interval );
     231    evtimer_add( &g->timer_event, &interval );
    224232}
    225233
     
    237245                tr_err( "%s", curl_multi_strerror( rc ) );
    238246            else {
    239                 dbgmsg( "pumped a task out of the curl queue... %d remain", tr_list_size( g->easy_queue ) );
     247                dbgmsg( "pumped the task queue, %d remain",
     248                        tr_list_size( g->easy_queue ) );
    240249                ++g->still_running;
    241250            }
     
    245254
    246255static void
    247 webDestroy( tr_web * web )
    248 {
    249     stop_timer( web );
    250     curl_multi_cleanup( web->multi );
    251     tr_free( web );
    252 }
    253 
    254 /* note: this function can free the tr_web if it's been flagged for deletion
    255    and there are no more tasks remaining.  so, callers need to make sure to
    256    not reference their g pointer after calling this function */
     256web_close( tr_web * g )
     257{
     258    stop_timer( g );
     259    curl_multi_cleanup( g->multi );
     260    tr_free( g );
     261}
     262
     263/* note: this function can free the tr_web if its 'closing' flag is set
     264   and no tasks remain.  callers must not reference their g pointer
     265   after calling this function */
    257266static void
    258267tr_multi_socket_action( tr_web * g, int fd, int mask )
     
    266275    /* invoke libcurl's processing */
    267276    do {
    268         dbgmsg( "event_cb calling socket_action fd %d, mask %d", fd, mask );
    269277        rc = curl_multi_socket_action( g->multi, fd, mask, &g->still_running );
    270         dbgmsg( "event_cb(): still_running is %d", g->still_running );
     278        dbgmsg( "event_cb(): fd %d, mask %d, still_running is %d",
     279                fd, mask, g->still_running );
    271280    } while( rc == CURLM_CALL_MULTI_PERFORM );
    272281    if( rc != CURLM_OK )
     
    280289        stop_timer( g );
    281290        if( g->closing ) {
    282             webDestroy( g );
     291            web_close( g );
    283292            closed = TRUE;
    284293        }
     
    339348                   | (( action & CURL_POLL_IN ) ? EV_READ : 0 )
    340349                   | (( action & CURL_POLL_OUT ) ? EV_WRITE : 0 );
    341     dbgmsg( "setsock: fd is %d, curl action is %d, libevent action is %d", sockfd, action, kind );
     350    dbgmsg( "setsock: fd is %d, curl action is %d, libevent action is %d",
     351            sockfd, action, kind );
    342352    if( f->evset )
    343353        event_del( &f->ev );
     
    381391
    382392
    383 /* from libcurl documentation: "If 0, it means you should proceed immediately
     393/* libcurl documentation: "If 0, it means you should proceed immediately
    384394 * without waiting for anything. If it returns -1, there's no timeout at all
    385395 * set ... (but) you must not wait too long (more than a few seconds perhaps)
     
    445455    web->timer_ms = DEFAULT_TIMER_MSEC; /* overwritten by multi_timer_cb() */
    446456
    447     timeout_set( &web->timer_event, timer_cb, web );
     457    evtimer_set( &web->timer_event, timer_cb, web );
    448458    curl_multi_setopt( web->multi, CURLMOPT_SOCKETDATA, web );
    449459    curl_multi_setopt( web->multi, CURLMOPT_SOCKETFUNCTION, sock_cb );
     
    460470    *web_in = NULL;
    461471    if( web->still_running < 1 )
    462         webDestroy( web );
     472        web_close( web );
    463473    else
    464474        web->closing = 1;
Note: See TracChangeset for help on using the changeset viewer.