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
|
#define PULSE_MSEC 200
|
||||||
static void pulse( int socket UNUSED, short action UNUSED, void * vweb );
|
static void pulse( int socket UNUSED, short action UNUSED, void * vweb );
|
||||||
#endif
|
#endif
|
||||||
|
static void processCompletedTasks( tr_web * );
|
||||||
|
|
||||||
#define dbgmsg(fmt...) tr_deepLog( __FILE__, __LINE__, "web", ##fmt )
|
#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
|
static void
|
||||||
pump( tr_web * web )
|
pump( tr_web * web )
|
||||||
{
|
{
|
||||||
|
int unused;
|
||||||
CURLMcode rc;
|
CURLMcode rc;
|
||||||
do
|
do
|
||||||
#ifdef USE_CURL_MULTI_SOCKET
|
#ifdef USE_CURL_MULTI_SOCKET
|
||||||
rc = curl_multi_socket_all( web->cm, &web->remain );
|
rc = curl_multi_socket_all( web->cm, &unused );
|
||||||
#else
|
#else
|
||||||
rc = curl_multi_perform( web->cm, &web->remain );
|
rc = curl_multi_perform( web->cm, &unused );
|
||||||
#endif
|
#endif
|
||||||
while( rc == CURLM_CALL_MULTI_PERFORM );
|
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) );
|
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_WRITEDATA, task );
|
||||||
curl_easy_setopt( ch, CURLOPT_USERAGENT, TR_NAME "/" LONG_VERSION_STRING );
|
curl_easy_setopt( ch, CURLOPT_USERAGENT, TR_NAME "/" LONG_VERSION_STRING );
|
||||||
curl_easy_setopt( ch, CURLOPT_SSL_VERIFYPEER, 0 );
|
curl_easy_setopt( ch, CURLOPT_SSL_VERIFYPEER, 0 );
|
||||||
curl_easy_setopt( ch, CURLOPT_TIMEOUT, 30 );
|
|
||||||
|
|
||||||
curl_multi_add_handle( web->cm, ch );
|
curl_multi_add_handle( web->cm, ch );
|
||||||
|
|
||||||
#ifdef USE_CURL_MULTI_SOCKET
|
|
||||||
pump( web );
|
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
|
static void
|
||||||
processCompletedTasks( tr_web * web )
|
processCompletedTasks( tr_web * web )
|
||||||
{
|
{
|
||||||
int more = 0;
|
int more = 0;
|
||||||
|
CURL * easy;
|
||||||
|
CURLMsg * msg;
|
||||||
|
CURLcode res;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
CURLMsg * msg = curl_multi_info_read( web->cm, &more );
|
/* find a completed task. this idea is from the "hiperinfo.c"
|
||||||
if( msg && ( msg->msg == CURLMSG_DONE ) )
|
* sample that questions the safety of removing an easy handle
|
||||||
{
|
* inside the curl_multi_info_read loop */
|
||||||
CURL * ch;
|
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;
|
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 )
|
--web->remain;
|
||||||
tr_err( "%s", curl_easy_strerror( msg->data.result ) );
|
dbgmsg( "task #%lu done (%d tasks remain)", task->tag, web->remain );
|
||||||
|
|
||||||
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 );
|
|
||||||
task->done_func( web->session,
|
task->done_func( web->session,
|
||||||
response_code,
|
response_code,
|
||||||
EVBUFFER_DATA(task->response),
|
EVBUFFER_DATA(task->response),
|
||||||
EVBUFFER_LENGTH(task->response),
|
EVBUFFER_LENGTH(task->response),
|
||||||
task->done_func_user_data );
|
task->done_func_user_data );
|
||||||
|
|
||||||
curl_multi_remove_handle( web->cm, ch );
|
curl_multi_remove_handle( web->cm, easy );
|
||||||
curl_easy_cleanup( ch );
|
curl_easy_cleanup( easy );
|
||||||
|
|
||||||
evbuffer_free( task->response );
|
evbuffer_free( task->response );
|
||||||
tr_free( task );
|
tr_free( task );
|
||||||
}
|
}
|
||||||
}
|
} while( easy );
|
||||||
while( more );
|
|
||||||
|
|
||||||
/* remove timeout if there are no transfers left */
|
|
||||||
if( !web->remain && evtimer_initialized( &web->timer ) )
|
|
||||||
evtimer_del( &web->timer );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_CURL_MULTI_SOCKET
|
#ifdef USE_CURL_MULTI_SOCKET
|
||||||
|
|
||||||
/* libevent says that sock is ready to be processed, so tell libcurl */
|
/* libevent says that sock is ready to be processed, so tell libcurl */
|
||||||
static void
|
static void
|
||||||
event_callback( int sock, short action, void * vweb )
|
event_callback( int sock, short action, void * vweb )
|
||||||
{
|
{
|
||||||
tr_web * web = vweb;
|
tr_web * web = vweb;
|
||||||
CURLMcode rc;
|
CURLMcode rc;
|
||||||
int mask;
|
int mask, unused;
|
||||||
|
|
||||||
switch (action & (EV_READ|EV_WRITE)) {
|
switch (action & (EV_READ|EV_WRITE)) {
|
||||||
case EV_READ: mask = CURL_CSELECT_IN; break;
|
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;
|
default: tr_err( "Unknown event %hd\n", action ); return;
|
||||||
}
|
}
|
||||||
|
|
||||||
do
|
do rc = curl_multi_socket_action( web->cm, sock, mask, &unused );
|
||||||
rc = curl_multi_socket_action( web->cm, sock, mask, &web->remain );
|
|
||||||
while( rc == CURLM_CALL_MULTI_PERFORM );
|
while( rc == CURLM_CALL_MULTI_PERFORM );
|
||||||
|
|
||||||
if ( rc != CURLM_OK )
|
if ( rc != CURLM_OK )
|
||||||
|
@ -183,79 +176,79 @@ event_callback( int sock, short action, void * vweb )
|
||||||
processCompletedTasks( web );
|
processCompletedTasks( web );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* CURLMPOPT_SOCKETFUNCTION */
|
||||||
/* libcurl wants us to tell it when sock is ready to be processed */
|
/* libcurl wants us to tell it when sock is ready to be processed */
|
||||||
static int
|
static void
|
||||||
socket_callback( CURL * easy UNUSED,
|
multi_sock_cb( CURL * easy UNUSED,
|
||||||
curl_socket_t sock,
|
curl_socket_t sock,
|
||||||
int action,
|
int action,
|
||||||
void * vweb,
|
void * vweb,
|
||||||
void * assigndata )
|
void * assigndata )
|
||||||
{
|
{
|
||||||
tr_web * web = vweb;
|
tr_web * web = vweb;
|
||||||
int events = EV_PERSIST;
|
|
||||||
struct event * ev = assigndata;
|
struct event * ev = assigndata;
|
||||||
|
|
||||||
if( ev )
|
if( action == CURL_POLL_REMOVE ) {
|
||||||
event_del( ev );
|
if( ev ) {
|
||||||
else {
|
event_del( ev );
|
||||||
ev = tr_new0( struct event, 1 );
|
tr_free( ev );
|
||||||
curl_multi_assign( web->cm, sock, 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 */
|
/* libevent says that timeout_ms have passed, so tell libcurl */
|
||||||
static void
|
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;
|
CURLMcode rc;
|
||||||
|
tr_web * web = vweb;
|
||||||
|
|
||||||
do rc = curl_multi_socket( web->cm, CURL_SOCKET_TIMEOUT, &web->remain );
|
do {
|
||||||
while( rc == CURLM_CALL_MULTI_PERFORM );
|
rc = curl_multi_socket( web->cm, CURL_SOCKET_TIMEOUT, &unused );
|
||||||
if( rc != CURLM_OK )
|
} while( rc == CURLM_CALL_MULTI_PERFORM );
|
||||||
tr_err( "%s", curl_multi_strerror( rc ) );
|
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
|
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;
|
tr_web * web = vweb;
|
||||||
struct timeval tv = tr_timevalMsec( timeout_ms );
|
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 );
|
evtimer_add( &web->timer, &tv );
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
static void
|
static void
|
||||||
pulse( int socket UNUSED, short action UNUSED, void * vweb )
|
pulse( int socket UNUSED, short action UNUSED, void * vweb )
|
||||||
{
|
{
|
||||||
tr_web * web = vweb;
|
tr_web * web = vweb;
|
||||||
|
struct timeval tv = tr_timevalMsec( PULSE_MSEC );
|
||||||
|
|
||||||
pump( web );
|
pump( web );
|
||||||
processCompletedTasks( web );
|
processCompletedTasks( web );
|
||||||
|
|
||||||
if( web->remain > 0 ) {
|
evtimer_del( &web->timer );
|
||||||
struct timeval tv = tr_timevalMsec( PULSE_MSEC );
|
evtimer_add( &web->timer, &tv );
|
||||||
evtimer_add( &web->timer, &tv );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -263,6 +256,9 @@ pulse( int socket UNUSED, short action UNUSED, void * vweb )
|
||||||
tr_web*
|
tr_web*
|
||||||
tr_webInit( tr_session * session )
|
tr_webInit( tr_session * session )
|
||||||
{
|
{
|
||||||
|
#ifndef USE_CURL_MULTI_SOCKET
|
||||||
|
struct timeval tv = tr_timevalMsec( PULSE_MSEC );
|
||||||
|
#endif
|
||||||
static int curlInited = FALSE;
|
static int curlInited = FALSE;
|
||||||
tr_web * web;
|
tr_web * web;
|
||||||
|
|
||||||
|
@ -280,17 +276,22 @@ tr_webInit( tr_session * session )
|
||||||
web->session = session;
|
web->session = session;
|
||||||
|
|
||||||
#ifdef USE_CURL_MULTI_SOCKET
|
#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_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_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
|
#endif
|
||||||
#if CURL_CHECK_VERSION(7,16,3)
|
#if CURL_CHECK_VERSION(7,16,3)
|
||||||
curl_multi_setopt( web->cm, CURLMOPT_MAXCONNECTS, 20 );
|
curl_multi_setopt( web->cm, CURLMOPT_MAXCONNECTS, 10 );
|
||||||
#endif
|
#endif
|
||||||
#if CURL_CHECK_VERSION(7,16,0)
|
#if CURL_CHECK_VERSION(7,16,0)
|
||||||
curl_multi_setopt( web->cm, CURLMOPT_PIPELINING, 1 );
|
curl_multi_setopt( web->cm, CURLMOPT_PIPELINING, 1 );
|
||||||
#endif
|
#endif
|
||||||
|
pump( web );
|
||||||
|
|
||||||
return web;
|
return web;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue