more hacking on the curl code, based on libcurl's "hiperfifo.c" sample code
This commit is contained in:
parent
37d4da23b7
commit
cffd7f92e2
|
@ -30,6 +30,7 @@
|
|||
#define PULSE_MSEC 200
|
||||
static void pulse( int socket UNUSED, short action UNUSED, void * vweb );
|
||||
#endif
|
||||
static void processCompletedTasks( tr_web * );
|
||||
|
||||
#define dbgmsg(fmt...) tr_deepLog( __FILE__, __LINE__, "web", ##fmt )
|
||||
|
||||
|
@ -60,16 +61,18 @@ writeFunc( void * ptr, size_t size, size_t nmemb, void * task )
|
|||
static void
|
||||
pump( tr_web * web )
|
||||
{
|
||||
int unused;
|
||||
CURLMcode rc;
|
||||
do
|
||||
#ifdef USE_CURL_MULTI_SOCKET
|
||||
rc = curl_multi_socket_all( web->cm, &web->remain );
|
||||
rc = curl_multi_socket_all( web->cm, &unused );
|
||||
#else
|
||||
rc = curl_multi_perform( web->cm, &web->remain );
|
||||
rc = curl_multi_perform( web->cm, &unused );
|
||||
#endif
|
||||
while( rc == CURLM_CALL_MULTI_PERFORM );
|
||||
dbgmsg( "%d tasks remain", web->remain );
|
||||
if ( rc != CURLM_OK )
|
||||
if ( rc == CURLM_OK )
|
||||
processCompletedTasks( web );
|
||||
else
|
||||
tr_err( "%s", curl_multi_strerror(rc) );
|
||||
}
|
||||
|
||||
|
@ -100,71 +103,62 @@ tr_webRun( tr_session * session,
|
|||
curl_easy_setopt( ch, CURLOPT_WRITEDATA, task );
|
||||
curl_easy_setopt( ch, CURLOPT_USERAGENT, TR_NAME "/" LONG_VERSION_STRING );
|
||||
curl_easy_setopt( ch, CURLOPT_SSL_VERIFYPEER, 0 );
|
||||
curl_easy_setopt( ch, CURLOPT_TIMEOUT, 30 );
|
||||
|
||||
curl_multi_add_handle( web->cm, ch );
|
||||
|
||||
#ifdef USE_CURL_MULTI_SOCKET
|
||||
pump( web );
|
||||
#else
|
||||
if( !evtimer_initialized( &web->timer ) )
|
||||
evtimer_set( &web->timer, pulse, web );
|
||||
if( !evtimer_pending( &web->timer, NULL ) ) {
|
||||
struct timeval tv = tr_timevalMsec( PULSE_MSEC );
|
||||
evtimer_add( &web->timer, &tv );
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static void
|
||||
processCompletedTasks( tr_web * web )
|
||||
{
|
||||
int more = 0;
|
||||
CURL * easy;
|
||||
CURLMsg * msg;
|
||||
CURLcode res;
|
||||
|
||||
do {
|
||||
CURLMsg * msg = curl_multi_info_read( web->cm, &more );
|
||||
if( msg && ( msg->msg == CURLMSG_DONE ) )
|
||||
{
|
||||
CURL * ch;
|
||||
/* find a completed task. this idea is from the "hiperinfo.c"
|
||||
* sample that questions the safety of removing an easy handle
|
||||
* inside the curl_multi_info_read loop */
|
||||
easy = NULL;
|
||||
while(( msg = curl_multi_info_read( web->cm, &more ))) {
|
||||
if( msg->msg == CURLMSG_DONE ) {
|
||||
easy = msg->easy_handle;
|
||||
res = msg->data.result;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( easy ) {
|
||||
struct tr_web_task * task;
|
||||
long response_code;
|
||||
int response_code;
|
||||
curl_easy_getinfo( easy, CURLINFO_PRIVATE, &task );
|
||||
curl_easy_getinfo( easy, CURLINFO_RESPONSE_CODE, &response_code );
|
||||
|
||||
if( msg->data.result != CURLE_OK )
|
||||
tr_err( "%s", curl_easy_strerror( msg->data.result ) );
|
||||
|
||||
ch = msg->easy_handle;
|
||||
curl_easy_getinfo( ch, CURLINFO_PRIVATE, &task );
|
||||
curl_easy_getinfo( ch, CURLINFO_RESPONSE_CODE, &response_code );
|
||||
|
||||
dbgmsg( "task #%lu done", task->tag );
|
||||
--web->remain;
|
||||
dbgmsg( "task #%lu done (%d tasks remain)", task->tag, web->remain );
|
||||
task->done_func( web->session,
|
||||
response_code,
|
||||
EVBUFFER_DATA(task->response),
|
||||
EVBUFFER_LENGTH(task->response),
|
||||
task->done_func_user_data );
|
||||
|
||||
curl_multi_remove_handle( web->cm, ch );
|
||||
curl_easy_cleanup( ch );
|
||||
|
||||
curl_multi_remove_handle( web->cm, easy );
|
||||
curl_easy_cleanup( easy );
|
||||
evbuffer_free( task->response );
|
||||
tr_free( task );
|
||||
}
|
||||
}
|
||||
while( more );
|
||||
|
||||
/* remove timeout if there are no transfers left */
|
||||
if( !web->remain && evtimer_initialized( &web->timer ) )
|
||||
evtimer_del( &web->timer );
|
||||
} while( easy );
|
||||
}
|
||||
|
||||
#ifdef USE_CURL_MULTI_SOCKET
|
||||
|
||||
/* libevent says that sock is ready to be processed, so tell libcurl */
|
||||
static void
|
||||
event_callback( int sock, short action, void * vweb )
|
||||
{
|
||||
tr_web * web = vweb;
|
||||
CURLMcode rc;
|
||||
int mask;
|
||||
int mask, unused;
|
||||
|
||||
switch (action & (EV_READ|EV_WRITE)) {
|
||||
case EV_READ: mask = CURL_CSELECT_IN; break;
|
||||
|
@ -173,8 +167,7 @@ event_callback( int sock, short action, void * vweb )
|
|||
default: tr_err( "Unknown event %hd\n", action ); return;
|
||||
}
|
||||
|
||||
do
|
||||
rc = curl_multi_socket_action( web->cm, sock, mask, &web->remain );
|
||||
do rc = curl_multi_socket_action( web->cm, sock, mask, &unused );
|
||||
while( rc == CURLM_CALL_MULTI_PERFORM );
|
||||
|
||||
if ( rc != CURLM_OK )
|
||||
|
@ -183,79 +176,79 @@ event_callback( int sock, short action, void * vweb )
|
|||
processCompletedTasks( web );
|
||||
}
|
||||
|
||||
/* CURLMPOPT_SOCKETFUNCTION */
|
||||
/* libcurl wants us to tell it when sock is ready to be processed */
|
||||
static int
|
||||
socket_callback( CURL * easy UNUSED,
|
||||
curl_socket_t sock,
|
||||
int action,
|
||||
void * vweb,
|
||||
void * assigndata )
|
||||
static void
|
||||
multi_sock_cb( CURL * easy UNUSED,
|
||||
curl_socket_t sock,
|
||||
int action,
|
||||
void * vweb,
|
||||
void * assigndata )
|
||||
{
|
||||
tr_web * web = vweb;
|
||||
int events = EV_PERSIST;
|
||||
struct event * ev = assigndata;
|
||||
|
||||
if( ev )
|
||||
event_del( ev );
|
||||
else {
|
||||
ev = tr_new0( struct event, 1 );
|
||||
curl_multi_assign( web->cm, sock, ev );
|
||||
if( action == CURL_POLL_REMOVE ) {
|
||||
if( ev ) {
|
||||
event_del( ev );
|
||||
tr_free( ev );
|
||||
curl_multi_assign( web->cm, sock, NULL );
|
||||
}
|
||||
} else {
|
||||
int kind;
|
||||
if( ev ) {
|
||||
event_del( ev );
|
||||
} else {
|
||||
ev = tr_new0( struct event, 1 );
|
||||
curl_multi_assign( web->cm, sock, ev );
|
||||
}
|
||||
kind = EV_PERSIST;
|
||||
if( action & CURL_POLL_IN ) kind |= EV_READ;
|
||||
if( action & CURL_POLL_OUT ) kind |= EV_WRITE;
|
||||
event_set( ev, sock, kind, event_callback, web );
|
||||
event_add( ev, NULL );
|
||||
}
|
||||
|
||||
switch (action) {
|
||||
case CURL_POLL_IN: events |= EV_READ; break;
|
||||
case CURL_POLL_OUT: events |= EV_WRITE; break;
|
||||
case CURL_POLL_INOUT: events |= EV_READ|EV_WRITE; break;
|
||||
case CURL_POLL_REMOVE: tr_free( ev ); /* fallthrough */
|
||||
case CURL_POLL_NONE: return 0;
|
||||
default: tr_err( "Unknown socket action %d", action ); return -1;
|
||||
}
|
||||
|
||||
event_set( ev, sock, events, event_callback, web );
|
||||
event_add( ev, NULL );
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* libevent says that timeout_ms have passed, so tell libcurl */
|
||||
static void
|
||||
timeout_callback( int socket UNUSED, short action UNUSED, void * vweb )
|
||||
event_timer_cb( int socket UNUSED, short action UNUSED, void * vweb )
|
||||
{
|
||||
tr_web * web = vweb;
|
||||
int unused;
|
||||
CURLMcode rc;
|
||||
tr_web * web = vweb;
|
||||
|
||||
do rc = curl_multi_socket( web->cm, CURL_SOCKET_TIMEOUT, &web->remain );
|
||||
while( rc == CURLM_CALL_MULTI_PERFORM );
|
||||
if( rc != CURLM_OK )
|
||||
tr_err( "%s", curl_multi_strerror( rc ) );
|
||||
do {
|
||||
rc = curl_multi_socket( web->cm, CURL_SOCKET_TIMEOUT, &unused );
|
||||
} while( rc == CURLM_CALL_MULTI_PERFORM );
|
||||
if ( rc == CURLM_OK )
|
||||
processCompletedTasks( web );
|
||||
else
|
||||
tr_err( "%s", curl_multi_strerror(rc) );
|
||||
}
|
||||
|
||||
/* libcurl wants us to tell it when timeout_ms have passed */
|
||||
/* CURLMPOPT_TIMERFUNCTION */
|
||||
static void
|
||||
timer_callback( CURLM *multi UNUSED, long timeout_ms, void * vweb )
|
||||
multi_timer_cb( CURLM *multi UNUSED, long timeout_ms, void * vweb )
|
||||
{
|
||||
tr_web * web = vweb;
|
||||
struct timeval tv = tr_timevalMsec( timeout_ms );
|
||||
|
||||
if( evtimer_initialized( &web->timer ) )
|
||||
evtimer_del( &web->timer );
|
||||
|
||||
evtimer_set( &web->timer, timeout_callback, vweb );
|
||||
evtimer_add( &web->timer, &tv );
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static void
|
||||
pulse( int socket UNUSED, short action UNUSED, void * vweb )
|
||||
{
|
||||
tr_web * web = vweb;
|
||||
struct timeval tv = tr_timevalMsec( PULSE_MSEC );
|
||||
|
||||
pump( web );
|
||||
processCompletedTasks( web );
|
||||
|
||||
if( web->remain > 0 ) {
|
||||
struct timeval tv = tr_timevalMsec( PULSE_MSEC );
|
||||
evtimer_add( &web->timer, &tv );
|
||||
}
|
||||
evtimer_del( &web->timer );
|
||||
evtimer_add( &web->timer, &tv );
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -263,6 +256,9 @@ pulse( int socket UNUSED, short action UNUSED, void * vweb )
|
|||
tr_web*
|
||||
tr_webInit( tr_session * session )
|
||||
{
|
||||
#ifndef USE_CURL_MULTI_SOCKET
|
||||
struct timeval tv = tr_timevalMsec( PULSE_MSEC );
|
||||
#endif
|
||||
static int curlInited = FALSE;
|
||||
tr_web * web;
|
||||
|
||||
|
@ -280,17 +276,22 @@ tr_webInit( tr_session * session )
|
|||
web->session = session;
|
||||
|
||||
#ifdef USE_CURL_MULTI_SOCKET
|
||||
evtimer_set( &web->timer, event_timer_cb, web );
|
||||
curl_multi_setopt( web->cm, CURLMOPT_SOCKETDATA, web );
|
||||
curl_multi_setopt( web->cm, CURLMOPT_SOCKETFUNCTION, socket_callback );
|
||||
curl_multi_setopt( web->cm, CURLMOPT_SOCKETFUNCTION, multi_sock_cb );
|
||||
curl_multi_setopt( web->cm, CURLMOPT_TIMERDATA, web );
|
||||
curl_multi_setopt( web->cm, CURLMOPT_TIMERFUNCTION, timer_callback );
|
||||
curl_multi_setopt( web->cm, CURLMOPT_TIMERFUNCTION, multi_timer_cb );
|
||||
#else
|
||||
evtimer_set( &web->timer, pulse, web );
|
||||
evtimer_add( &web->timer, &tv );
|
||||
#endif
|
||||
#if CURL_CHECK_VERSION(7,16,3)
|
||||
curl_multi_setopt( web->cm, CURLMOPT_MAXCONNECTS, 20 );
|
||||
curl_multi_setopt( web->cm, CURLMOPT_MAXCONNECTS, 10 );
|
||||
#endif
|
||||
#if CURL_CHECK_VERSION(7,16,0)
|
||||
curl_multi_setopt( web->cm, CURLMOPT_PIPELINING, 1 );
|
||||
#endif
|
||||
pump( web );
|
||||
|
||||
return web;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue