diff options
Diffstat (limited to 'docs/examples/hiperfifo.c')
-rw-r--r-- | docs/examples/hiperfifo.c | 78 |
1 files changed, 28 insertions, 50 deletions
diff --git a/docs/examples/hiperfifo.c b/docs/examples/hiperfifo.c index f5b422b4a..c9096871b 100644 --- a/docs/examples/hiperfifo.c +++ b/docs/examples/hiperfifo.c @@ -62,7 +62,6 @@ typedef struct _GlobalInfo { struct event fifo_event; struct event timer_event; CURLM *multi; - int prev_running; int still_running; FILE* input; } GlobalInfo; @@ -110,7 +109,6 @@ static void mcode_or_die(const char *where, CURLMcode code) const char *s; 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; @@ -132,44 +130,29 @@ 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) { - 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; - break; - } - } - 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; } @@ -181,16 +164,13 @@ static void event_cb(int fd, short kind, void *userp) CURLMcode rc; int action = - (kind&EV_READ?CURL_CSELECT_IN:0)| - (kind&EV_WRITE?CURL_CSELECT_OUT:0); - - do { - rc = curl_multi_socket_action(g->multi, fd, action, &g->still_running); - } while (rc == CURLM_CALL_MULTI_PERFORM); + (kind & EV_READ ? CURL_CSELECT_IN : 0) | + (kind & EV_WRITE ? CURL_CSELECT_OUT : 0); + rc = curl_multi_socket_action(g->multi, fd, action, &g->still_running); mcode_or_die("event_cb: curl_multi_socket_action", rc); - check_run_count(g); + check_multi_info(g); if ( g->still_running <= 0 ) { fprintf(MSG_OUT, "last transfer done, kill timeout\n"); if (evtimer_pending(&g->timer_event, NULL)) { @@ -209,12 +189,10 @@ static void timer_cb(int fd, short kind, void *userp) (void)fd; (void)kind; - do { - rc = curl_multi_socket_action(g->multi, + 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_action", rc); - check_run_count(g); + check_multi_info(g); } @@ -340,7 +318,7 @@ static void new_conn(char *url, GlobalInfo *g ) curl_easy_setopt(conn->easy, CURLOPT_PROGRESSDATA, conn); 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); /* note that the add_handle() will set a time-out to trigger very soon so |