From 5fb4279ec7199e291d9bf4c903f89395f60a3d31 Mon Sep 17 00:00:00 2001 From: Dirk Manske Date: Thu, 30 Sep 2010 11:33:33 +0200 Subject: multi & hiper examples: updates and cleanups all multi and hiper examples: * don't loop curl_multi_perform calls, that was <7.20.0 style, currently the exported multi functions will not return CURLM_CALL_MULTI_PERFORM all hiper examples: * renamed check_run_count to check_multi_info * don't compare current running handle count with previous value, this was the wrong way to check for finished requests, simply call curl_multi_info_read * it's also safe to call curl_multi_remove_handle inside the curl_multi_info_read loop. ghiper.c: * replaced curl_multi_socket (that function is marked as obsolete) calls with curl_multi_socket_action calls (as in hiperfifo.c and evhiperfifo.c) ghiper.c and evhiperfifo.c: * be smart as hiperfifo.c, don't do uncessary curl_multi_* calls in new_conn and main --- docs/examples/evhiperfifo.c | 98 ++++++++++++++++----------------------------- 1 file changed, 34 insertions(+), 64 deletions(-) (limited to 'docs/examples/evhiperfifo.c') diff --git a/docs/examples/evhiperfifo.c b/docs/examples/evhiperfifo.c index 8695ffabf..6cdccf839 100644 --- a/docs/examples/evhiperfifo.c +++ b/docs/examples/evhiperfifo.c @@ -68,7 +68,6 @@ typedef struct _GlobalInfo struct ev_io fifo_event; struct ev_timer timer_event; CURLM *multi; - int prev_running; int still_running; FILE* input; } GlobalInfo; @@ -122,7 +121,6 @@ static void mcode_or_die(const char *where, CURLMcode code) switch ( code ) { case CURLM_CALL_MULTI_PERFORM: s="CURLM_CALL_MULTI_PERFORM"; break; - case CURLM_OK: s="CURLM_OK"; break; case CURLM_BAD_HANDLE: s="CURLM_BAD_HANDLE"; break; case CURLM_BAD_EASY_HANDLE: s="CURLM_BAD_EASY_HANDLE"; break; case CURLM_OUT_OF_MEMORY: s="CURLM_OUT_OF_MEMORY"; break; @@ -144,54 +142,33 @@ static void mcode_or_die(const char *where, CURLMcode code) /* Check for completed transfers, and remove their easy handles */ -static void check_run_count(GlobalInfo *g) +static void check_multi_info(GlobalInfo *g) { - DPRINT("%s prev %i still %i\n", __PRETTY_FUNCTION__, - g->prev_running, g->still_running); - if ( g->prev_running > g->still_running ) - { - char *eff_url=NULL; - CURLMsg *msg; - int msgs_left; - ConnInfo *conn=NULL; - CURL*easy; - CURLcode res; - - fprintf(MSG_OUT, "REMAINING: %d\n", g->still_running); - /* - I am still uncertain whether it is safe to remove an easy - handle from inside the curl_multi_info_read loop, so here I - will search for completed transfers in the inner "while" - loop, and then remove them in the outer "do-while" loop... - */ - do - { - easy=NULL; - while ( (msg = curl_multi_info_read(g->multi, &msgs_left)) ) - { - if ( msg->msg == CURLMSG_DONE ) - { - easy=msg->easy_handle; - res=msg->data.result; - } - - if ( easy ) - { - curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn); - curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url); - fprintf(MSG_OUT, "DONE: %s => (%d) %s\n", eff_url, res, conn->error); - curl_multi_remove_handle(g->multi, easy); - free(conn->url); - curl_easy_cleanup(easy); - free(conn); - } - } - } while ( easy ); + char *eff_url; + CURLMsg *msg; + int msgs_left; + ConnInfo *conn; + CURL *easy; + CURLcode res; + + fprintf(MSG_OUT, "REMAINING: %d\n", g->still_running); + while ((msg = curl_multi_info_read(g->multi, &msgs_left))) { + if (msg->msg == CURLMSG_DONE) { + easy = msg->easy_handle; + res = msg->data.result; + curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn); + curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url); + fprintf(MSG_OUT, "DONE: %s => (%d) %s\n", eff_url, res, conn->error); + curl_multi_remove_handle(g->multi, easy); + free(conn->url); + curl_easy_cleanup(easy); + free(conn); + } } - g->prev_running = g->still_running; } + /* Called by libevent when we get action on a multi socket */ static void event_cb(EV_P_ struct ev_io *w, int revents) { @@ -201,12 +178,9 @@ static void event_cb(EV_P_ struct ev_io *w, int revents) int action = (revents&EV_READ?CURL_POLL_IN:0)| (revents&EV_WRITE?CURL_POLL_OUT:0); - do - { - rc = curl_multi_socket_action(g->multi, w->fd, action, &g->still_running); - } while ( rc == CURLM_CALL_MULTI_PERFORM ); - mcode_or_die("event_cb: curl_multi_socket", rc); - check_run_count(g); + rc = curl_multi_socket_action(g->multi, w->fd, action, &g->still_running); + mcode_or_die("event_cb: curl_multi_socket_action", rc); + check_multi_info(g); if ( g->still_running <= 0 ) { fprintf(MSG_OUT, "last transfer done, kill timeout\n"); @@ -222,12 +196,9 @@ static void timer_cb(EV_P_ struct ev_timer *w, int revents) GlobalInfo *g = (GlobalInfo *)w->data; CURLMcode rc; - do - { - rc = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0, &g->still_running); - } while ( rc == CURLM_CALL_MULTI_PERFORM ); - mcode_or_die("timer_cb: curl_multi_socket", rc); - check_run_count(g); + rc = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0, &g->still_running); + mcode_or_die("timer_cb: curl_multi_socket_action", rc); + check_multi_info(g); } /* Clean up the SockInfo structure */ @@ -364,11 +335,11 @@ static void new_conn(char *url, GlobalInfo *g ) fprintf(MSG_OUT, "Adding easy %p to multi %p (%s)\n", conn->easy, g->multi, url); - rc =curl_multi_add_handle(g->multi, conn->easy); + rc = curl_multi_add_handle(g->multi, conn->easy); mcode_or_die("new_conn: curl_multi_add_handle", rc); - mcode_or_die("new_conn: curl_multi_socket_all", rc); - check_run_count(g); + /* note that the add_handle() will set a time-out to trigger very soon so + that the necessary socket_action() call will be called by this app */ } /* This gets called whenever data is received from the fifo */ @@ -448,10 +419,9 @@ int main(int argc, char **argv) curl_multi_setopt(g.multi, CURLMOPT_SOCKETDATA, &g); curl_multi_setopt(g.multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb); curl_multi_setopt(g.multi, CURLMOPT_TIMERDATA, &g); - do - { - rc = curl_multi_socket_all(g.multi, &g.still_running); - } while ( CURLM_CALL_MULTI_PERFORM == rc ); + + /* we don't call any curl_multi_socket*() function yet as we have no handles + added! */ ev_loop(g.loop, 0); curl_multi_cleanup(g.multi); -- cgit v1.2.3