Changeset 9740


Ignore:
Timestamp:
Dec 13, 2009, 5:54:01 PM (12 years ago)
Author:
charles
Message:

(trunk libT) it never ends

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/web.c

    r9717 r9740  
    1414#include <stdlib.h> /* bsearch */
    1515
    16 #include <event.h>
    17 
    1816#define CURL_DISABLE_TYPECHECK /* otherwise -Wunreachable-code goes insane */
    1917#include <curl/curl.h>
     18#include <event.h>
    2019
    2120#include "transmission.h"
    22 #include "net.h" /* socklen_t */
     21#include "net.h"
    2322#include "session.h"
    24 #include "trevent.h" /* tr_runInEventThread() */
     23#include "trevent.h"
    2524#include "utils.h"
    2625#include "version.h"
     
    2928enum
    3029{
    31     /* arbitrary number */
    32     DEFAULT_TIMER_MSEC = 1500
     30    DEFAULT_TIMER_MSEC = 1500 /* arbitrary */
    3331};
    3432
     
    121119}
    122120
     121static int
     122getTimeoutFromURL( 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
    123131static void
    124132addTask( void * vtask )
     
    129137    if( session && session->web )
    130138    {
     139        CURLMcode mcode;
     140        CURL * easy = curl_easy_init( );
    131141        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;
    134144
    135145        dbgmsg( "adding task #%lu [%s]", task->tag, task->url );
    136 
    137         easy = curl_easy_init( );
    138146
    139147        if( !task->range && session->isProxyEnabled ) {
     
    152160
    153161        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         else
    161             timeout = 240L;
    162162        curl_easy_setopt( easy, CURLOPT_TIMEOUT, timeout );
    163163        curl_easy_setopt( easy, CURLOPT_CONNECTTIMEOUT, timeout-5 );
    164         dbgmsg( "new task's timeout is %ld\n", timeout );
    165 
    166164        curl_easy_setopt( easy, CURLOPT_SOCKOPTFUNCTION, sockoptfunction );
    167165        curl_easy_setopt( easy, CURLOPT_SOCKOPTDATA, task );
     
    176174        curl_easy_setopt( easy, CURLOPT_SSL_VERIFYPEER, 0L );
    177175        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 );
    182180        if( web->haveAddr )
    183181            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 );
    186182        if( task->range )
    187183            curl_easy_setopt( easy, CURLOPT_RANGE, task->range );
     
    189185            curl_easy_setopt( easy, CURLOPT_ENCODING, "" );
    190186
    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 );*/
    201194    }
    202195}
     
    233226remove_finished_tasks( tr_web * g )
    234227{
    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 )) {
    252233            long code;
    253234            struct tr_web_task * task;
     235            CURL * easy = msg->easy_handle;
    254236            curl_easy_getinfo( easy, CURLINFO_PRIVATE, (void*)&task );
    255237            curl_easy_getinfo( easy, CURLINFO_RESPONSE_CODE, &code );
     
    259241        }
    260242    }
    261     while ( easy );
    262243
    263244    g->prev_running = g->still_running;
     
    312293    /* invoke libcurl's processing */
    313294    do {
    314         dbgmsg( "calling curl_multi_socket_action..." );
    315295        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..." );
    318296    } while( mcode == CURLM_CALL_MULTI_SOCKET );
    319     tr_assert( mcode == CURLM_OK, "curl_multi_perform() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) );
    320297    if( mcode != CURLM_OK )
    321298        tr_err( "%s", curl_multi_strerror( mcode ) );
     
    344321/* CURLMOPT_SOCKETFUNCTION */
    345322static int
    346 sock_cb( CURL            * easy UNUSED,
    347          curl_socket_t     fd,
    348          int               action,
    349          void            * vweb,
    350          void            * vevent )
     323sock_cb( CURL * easy UNUSED, curl_socket_t fd, int action, void * vweb, void * vevent )
    351324{
    352325    /*static int num_events = 0;*/
     
    379352        }
    380353
    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 );
    382356        event_set( io_event, fd, events, event_cb, web );
    383357        event_add( io_event, NULL );
    384358    }
    385359
    386     return 0; /* libcurl doc sez: "The callback MUST return 0." */
     360    return 0; /* libcurl documentation: "The callback MUST return 0." */
    387361}
    388362
     
    467441    web = tr_new0( struct tr_web, 1 );
    468442    web->session = session;
    469 
    470443    web->timer_msec = DEFAULT_TIMER_MSEC; /* overwritten by multi_timer_cb() */
    471444    evtimer_set( &web->timer_event, libevent_timer_cb, web );
Note: See TracChangeset for help on using the changeset viewer.