From 6df85adf3eff596ba7414c53f85db2ab088161e3 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 12 Sep 2006 11:25:00 +0000 Subject: hiperfifo.c by Jeff Pohlmeyer --- docs/examples/hiperfifo.c | 412 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 412 insertions(+) create mode 100644 docs/examples/hiperfifo.c (limited to 'docs/examples/hiperfifo.c') diff --git a/docs/examples/hiperfifo.c b/docs/examples/hiperfifo.c new file mode 100644 index 000000000..625ea761f --- /dev/null +++ b/docs/examples/hiperfifo.c @@ -0,0 +1,412 @@ +/***************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * $Id$ + * + * Example application source code using the multi socket interface to + * download many files at once. + * + * Written by Jeff Pohlmeyer + +Requires libevent and a (POSIX?) system that has mkfifo(). + +This is an adaptation of libcurl's "hipev.c" and libevent's "event-test.c" +sample programs. + +When running, the program creates the named pipe "hiper.fifo" + +Whenever there is input into the fifo, the program reads the input as a list +of URL's and creates some new easy handles to fetch each URL via the +curl_multi "hiper" API. + + +Thus, you can try a single URL: + % echo http://www.yahoo.com > hiper.fifo + +Or a whole bunch of them: + % cat my-url-list > hiper.fifo + +The fifo buffer is handled almost instantly, so you can even add more URL's +while the previous requests are still being downloaded. + +Note: + For the sake of simplicity, URL length is limited to 1023 char's ! + +This is purely a demo app, all retrieved data is simply discarded by the write +callback. + +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define MSG_OUT stdout /* Send info to stdout, change to stderr if you want */ + + +/* Global information, common to all connections */ +typedef struct _GlobalInfo { + struct event fifo_event; + struct event timer_event; + CURLM *multi; + int prev_running; + int still_running; + FILE* input; +} GlobalInfo; + + +/* Information associated with a specific easy handle */ +typedef struct _ConnInfo { + CURL *easy; + char *url; + GlobalInfo *global; + char error[CURL_ERROR_SIZE]; +} ConnInfo; + + +/* Information associated with a specific socket */ +typedef struct _SockInfo { + curl_socket_t sockfd; + CURL *easy; + int action; + long timeout; + struct event ev; + int evset; + GlobalInfo *global; +} SockInfo; + + + +/* Update the event timer after curl_multi library calls */ +static void update_timeout(GlobalInfo *g) +{ + long timeout_ms; + struct timeval timeout; + + curl_multi_timeout(g->multi, &timeout_ms); + timeout.tv_sec = timeout_ms/1000; + timeout.tv_usec = (timeout_ms%1000)*1000; + evtimer_add(&g->timer_event, &timeout); +} + + + +/* Die if we get a bad CURLMcode somewhere */ +void mcode_or_die(char *where, CURLMcode code) { + if ( CURLM_OK != code ) { + 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; + case CURLM_INTERNAL_ERROR: s="CURLM_INTERNAL_ERROR"; break; + case CURLM_BAD_SOCKET: s="CURLM_BAD_SOCKET"; break; + case CURLM_UNKNOWN_OPTION: s="CURLM_UNKNOWN_OPTION"; break; + case CURLM_LAST: s="CURLM_LAST"; break; + default: s="CURLM_unknown"; + } + fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s); + exit(code); + } +} + + + +/* Check for completed transfers, and remove their easy handles */ +static void check_run_count(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; + } + } + 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 ); + } + g->prev_running = g->still_running; +} + + + +/* Called by libevent when we get action on a multi socket */ +static void event_cb(int fd, short kind, void *userp) +{ + GlobalInfo *g = (GlobalInfo*) userp; + CURLMcode rc; + + do { + rc = curl_multi_socket(g->multi, fd, &g->still_running); + } while (rc == CURLM_CALL_MULTI_PERFORM); + mcode_or_die("event_cb: curl_multi_socket", rc); + check_run_count(g); + if(g->still_running) { + update_timeout(g); + } else { + fprintf(MSG_OUT, "last transfer done, kill timeout\n"); + if (evtimer_pending(&g->timer_event, NULL)) { + evtimer_del(&g->timer_event); + } + } +} + + + +/* Called by libevent when our timeout expires */ +static void timer_cb(int fd, short kind, void *userp) +{ + (void)fd; + (void)kind; + GlobalInfo *g = (GlobalInfo *)userp; + CURLMcode rc; + + do { + rc = curl_multi_socket(g->multi, CURL_SOCKET_TIMEOUT, &g->still_running); + } while (rc == CURLM_CALL_MULTI_PERFORM); + mcode_or_die("timer_cb: curl_multi_socket", rc); + check_run_count(g); + if ( g->still_running ) { update_timeout(g); } +} + + + +/* Clean up the SockInfo structure */ +static void remsock(SockInfo *f) +{ + if (!f) { return; } + if (f->evset) { event_del(&f->ev); } + free(f); +} + + + +/* Assign information to a SockInfo structure */ +static void setsock(SockInfo*f, curl_socket_t s, CURL*e, int act, GlobalInfo*g) +{ + int kind = + (act&CURL_POLL_IN?EV_READ:0)|(act&CURL_POLL_OUT?EV_WRITE:0)|EV_PERSIST; + + f->sockfd = s; + f->action = act; + f->easy = e; + if (f->evset) { event_del(&f->ev); } + event_set( &f->ev, f->sockfd, kind, event_cb, g); + f->evset=1; + event_add(&f->ev, NULL); +} + + + +/* Initialize a new SockInfo structure */ +static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g) { + SockInfo *fdp = calloc(sizeof(SockInfo), 1); + + fdp->global = g; + setsock(fdp, s, easy, action, g); + curl_multi_assign(g->multi, s, fdp); +} + + + +/* CURLMOPT_SOCKETFUNCTION */ +static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp) +{ + GlobalInfo *g = (GlobalInfo*) cbp; + SockInfo *fdp = (SockInfo*) sockp; + char *whatstr[]={ "none", "IN", "OUT", "INOUT", "REMOVE" }; + + fprintf(MSG_OUT, + "socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]); + if (what == CURL_POLL_REMOVE) { + fprintf(MSG_OUT, "\n"); + remsock(fdp); + } else { + if (!fdp) { + fprintf(MSG_OUT, "Adding data: %s%s\n", + what&CURL_POLL_IN?"READ":"", + what&CURL_POLL_OUT?"WRITE":"" ); + addsock(s, e, what, g); + } + else { + fprintf(MSG_OUT, + "Changing action from %d to %d\n", fdp->action, what); + setsock(fdp, s, e, what, g); + } + } + return 0; +} + + + +/* CURLOPT_WRITEFUNCTION */ +static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data) +{ + size_t realsize = size * nmemb; + ConnInfo *conn = (ConnInfo*) data; + (void)ptr; + (void)conn; + return realsize; +} + + +/* CURLOPT_PROGRESSFUNCTION */ +int prog_cb (void *p, double dltotal, double dlnow, double ult, double uln) +{ + ConnInfo *conn = (ConnInfo *)p; + fprintf(MSG_OUT, "Progress: %s (%g/%g)\n", conn->url, dlnow, dltotal); + return 0; +} + + +/* Create a new easy handle, and add it to the global curl_multi */ +void new_conn(char *url, GlobalInfo *g ) { + ConnInfo *conn; + CURLMcode rc; + + conn = calloc(1, sizeof(ConnInfo)); + memset(conn, 0, sizeof(ConnInfo)); + conn->error[0]='\0'; + + conn->easy = curl_easy_init(); + if (!conn->easy) { + fprintf(MSG_OUT, "curl_easy_init() failed, exiting!\n"); + exit(2); + } + conn->global = g; + conn->url = strdup(url); + curl_easy_setopt(conn->easy, CURLOPT_URL, conn->url); + curl_easy_setopt(conn->easy, CURLOPT_WRITEFUNCTION, write_cb); + curl_easy_setopt(conn->easy, CURLOPT_WRITEDATA, &conn); + curl_easy_setopt(conn->easy, CURLOPT_VERBOSE, 0); + curl_easy_setopt(conn->easy, CURLOPT_ERRORBUFFER, conn->error); + curl_easy_setopt(conn->easy, CURLOPT_PRIVATE, conn); + curl_easy_setopt(conn->easy, CURLOPT_NOPROGRESS, 0); + curl_easy_setopt(conn->easy, CURLOPT_PROGRESSFUNCTION, prog_cb); + 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); + mcode_or_die("new_conn: curl_multi_add_handle", rc); + do { + rc = curl_multi_socket_all(g->multi, &g->still_running); + } while (CURLM_CALL_MULTI_PERFORM == rc); + mcode_or_die("new_conn: curl_multi_socket_all", rc); + check_run_count(g); +} + + + +/* This gets called whenever data is received from the fifo */ +void fifo_cb(int fd, short event, void *arg) { + char s[1024]; + long int rv=0; + int n=0; + GlobalInfo *g = (GlobalInfo *)arg; + + do { + s[0]='\0'; + rv=fscanf(g->input, "%1023s%n", s, &n); + s[n]='\0'; + if ( n && s[0] ) { + new_conn(s,arg); /* if we read a URL, go get it! */ + } else break; + } while ( rv != EOF); +} + + + +/* Create a named pipe and tell libevent to monitor it */ +int init_fifo (GlobalInfo *g) { + struct stat st; + char *fifo = "hiper.fifo"; + int socket; + + fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo); + if (lstat (fifo, &st) == 0) { + if ((st.st_mode & S_IFMT) == S_IFREG) { + errno = EEXIST; + perror("lstat"); + exit (1); + } + } + unlink(fifo); + if (mkfifo (fifo, 0600) == -1) { + perror("mkfifo"); + exit (1); + } + socket = open(fifo, O_RDWR | O_NONBLOCK, 0); + if (socket == -1) { + perror("open"); + exit (1); + } + g->input = fdopen(socket, "r"); + + fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo); + event_set(&g->fifo_event, socket, EV_READ | EV_PERSIST, fifo_cb, g); + event_add(&g->fifo_event, NULL); + return (0); +} + + + +int main(int argc, char **argv) +{ + GlobalInfo g; + CURLMcode rc; + + memset(&g, 0, sizeof(GlobalInfo)); + event_init(); + init_fifo(&g); + g.multi = curl_multi_init(); + evtimer_set(&g.timer_event, timer_cb, &g); + curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb); + curl_multi_setopt(g.multi, CURLMOPT_SOCKETDATA, &g); + do { + rc = curl_multi_socket_all(g.multi, &g.still_running); + } while (CURLM_CALL_MULTI_PERFORM == rc); + update_timeout(&g); + event_dispatch(); + curl_multi_cleanup(g.multi); + return 0; +} -- cgit v1.2.1 From 56fcf85ab61b4dc44796f1241e1b7c2349afc6ea Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Sun, 8 Oct 2006 22:19:25 +0000 Subject: slightly improved --- docs/examples/hiperfifo.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'docs/examples/hiperfifo.c') diff --git a/docs/examples/hiperfifo.c b/docs/examples/hiperfifo.c index 625ea761f..f640bf9dc 100644 --- a/docs/examples/hiperfifo.c +++ b/docs/examples/hiperfifo.c @@ -98,6 +98,9 @@ static void update_timeout(GlobalInfo *g) struct timeval timeout; curl_multi_timeout(g->multi, &timeout_ms); + if(timeout_ms < 0) + return; + timeout.tv_sec = timeout_ms/1000; timeout.tv_usec = (timeout_ms%1000)*1000; evtimer_add(&g->timer_event, &timeout); @@ -152,6 +155,7 @@ static void check_run_count(GlobalInfo *g) if (msg->msg == CURLMSG_DONE) { easy=msg->easy_handle; res=msg->data.result; + break; } } if (easy) { -- cgit v1.2.1 From 1afb67e31b3e8f952e39e4de4f1abeac3fc84376 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 2 May 2007 13:52:38 +0000 Subject: - Jeff Pohlmeyer improved the hiperfifo.c example to use the CURLMOPT_TIMERFUNCTION callback option. --- docs/examples/hiperfifo.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) (limited to 'docs/examples/hiperfifo.c') diff --git a/docs/examples/hiperfifo.c b/docs/examples/hiperfifo.c index f640bf9dc..e8d767133 100644 --- a/docs/examples/hiperfifo.c +++ b/docs/examples/hiperfifo.c @@ -92,18 +92,15 @@ typedef struct _SockInfo { /* Update the event timer after curl_multi library calls */ -static void update_timeout(GlobalInfo *g) +static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g) { - long timeout_ms; struct timeval timeout; - curl_multi_timeout(g->multi, &timeout_ms); - if(timeout_ms < 0) - return; - timeout.tv_sec = timeout_ms/1000; timeout.tv_usec = (timeout_ms%1000)*1000; + fprintf(MSG_OUT, "multi_timer_cb: Setting timeout to %ld ms\n", timeout_ms); evtimer_add(&g->timer_event, &timeout); + return 0; } @@ -185,9 +182,7 @@ static void event_cb(int fd, short kind, void *userp) } while (rc == CURLM_CALL_MULTI_PERFORM); mcode_or_die("event_cb: curl_multi_socket", rc); check_run_count(g); - if(g->still_running) { - update_timeout(g); - } else { + if ( g->still_running <= 0 ) { fprintf(MSG_OUT, "last transfer done, kill timeout\n"); if (evtimer_pending(&g->timer_event, NULL)) { evtimer_del(&g->timer_event); @@ -210,7 +205,6 @@ static void timer_cb(int fd, short kind, void *userp) } while (rc == CURLM_CALL_MULTI_PERFORM); mcode_or_die("timer_cb: curl_multi_socket", rc); check_run_count(g); - if ( g->still_running ) { update_timeout(g); } } @@ -406,10 +400,11 @@ int main(int argc, char **argv) evtimer_set(&g.timer_event, timer_cb, &g); curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb); 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); - update_timeout(&g); event_dispatch(); curl_multi_cleanup(g.multi); return 0; -- cgit v1.2.1 From 49ce3e5160a9576e797bf87cef012b09d1c54ecb Mon Sep 17 00:00:00 2001 From: Dan Fandrich Date: Thu, 12 Jul 2007 21:11:10 +0000 Subject: Fixed some compile warnings and errors and improved portability in the examples. Removed ftp3rdparty.c since libcurl doesn't support 3rd party FTP transfers any longer. --- docs/examples/hiperfifo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'docs/examples/hiperfifo.c') diff --git a/docs/examples/hiperfifo.c b/docs/examples/hiperfifo.c index e8d767133..76e506fb5 100644 --- a/docs/examples/hiperfifo.c +++ b/docs/examples/hiperfifo.c @@ -252,7 +252,7 @@ static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp) { GlobalInfo *g = (GlobalInfo*) cbp; SockInfo *fdp = (SockInfo*) sockp; - char *whatstr[]={ "none", "IN", "OUT", "INOUT", "REMOVE" }; + const char *whatstr[]={ "none", "IN", "OUT", "INOUT", "REMOVE" }; fprintf(MSG_OUT, "socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]); @@ -357,7 +357,7 @@ void fifo_cb(int fd, short event, void *arg) { /* Create a named pipe and tell libevent to monitor it */ int init_fifo (GlobalInfo *g) { struct stat st; - char *fifo = "hiper.fifo"; + static const char *fifo = "hiper.fifo"; int socket; fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo); -- cgit v1.2.1 From e059efda1b3db101536a2f9da2e9e63b71a554f4 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 16 May 2008 21:14:50 +0000 Subject: removed lots of warnings --- docs/examples/hiperfifo.c | 52 ++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 23 deletions(-) (limited to 'docs/examples/hiperfifo.c') diff --git a/docs/examples/hiperfifo.c b/docs/examples/hiperfifo.c index 76e506fb5..065a974ad 100644 --- a/docs/examples/hiperfifo.c +++ b/docs/examples/hiperfifo.c @@ -95,6 +95,7 @@ typedef struct _SockInfo { static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g) { struct timeval timeout; + (void)multi; /* unused */ timeout.tv_sec = timeout_ms/1000; timeout.tv_usec = (timeout_ms%1000)*1000; @@ -103,12 +104,11 @@ static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g) return 0; } - - /* Die if we get a bad CURLMcode somewhere */ -void mcode_or_die(char *where, CURLMcode code) { +static void mcode_or_die(const char *where, CURLMcode code) +{ if ( CURLM_OK != code ) { - char *s; + const char *s; switch (code) { case CURLM_CALL_MULTI_PERFORM: s="CURLM_CALL_MULTI_PERFORM"; break; case CURLM_OK: s="CURLM_OK"; break; @@ -176,6 +176,7 @@ static void event_cb(int fd, short kind, void *userp) { GlobalInfo *g = (GlobalInfo*) userp; CURLMcode rc; + (void)kind; /* unused */ do { rc = curl_multi_socket(g->multi, fd, &g->still_running); @@ -195,10 +196,10 @@ static void event_cb(int fd, short kind, void *userp) /* Called by libevent when our timeout expires */ static void timer_cb(int fd, short kind, void *userp) { - (void)fd; - (void)kind; GlobalInfo *g = (GlobalInfo *)userp; CURLMcode rc; + (void)fd; + (void)kind; do { rc = curl_multi_socket(g->multi, CURL_SOCKET_TIMEOUT, &g->still_running); @@ -289,16 +290,21 @@ static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data) /* CURLOPT_PROGRESSFUNCTION */ -int prog_cb (void *p, double dltotal, double dlnow, double ult, double uln) +static int prog_cb (void *p, double dltotal, double dlnow, double ult, + double uln) { ConnInfo *conn = (ConnInfo *)p; + (void)ult; + (void)uln; + fprintf(MSG_OUT, "Progress: %s (%g/%g)\n", conn->url, dlnow, dltotal); return 0; } /* Create a new easy handle, and add it to the global curl_multi */ -void new_conn(char *url, GlobalInfo *g ) { +static void new_conn(char *url, GlobalInfo *g ) +{ ConnInfo *conn; CURLMcode rc; @@ -333,14 +339,15 @@ void new_conn(char *url, GlobalInfo *g ) { check_run_count(g); } - - /* This gets called whenever data is received from the fifo */ -void fifo_cb(int fd, short event, void *arg) { +static void fifo_cb(int fd, short event, void *arg) +{ char s[1024]; long int rv=0; int n=0; GlobalInfo *g = (GlobalInfo *)arg; + (void)fd; /* unused */ + (void)event; /* unused */ do { s[0]='\0'; @@ -352,13 +359,12 @@ void fifo_cb(int fd, short event, void *arg) { } while ( rv != EOF); } - - /* Create a named pipe and tell libevent to monitor it */ -int init_fifo (GlobalInfo *g) { +static int init_fifo (GlobalInfo *g) +{ struct stat st; static const char *fifo = "hiper.fifo"; - int socket; + int sockfd; fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo); if (lstat (fifo, &st) == 0) { @@ -373,25 +379,25 @@ int init_fifo (GlobalInfo *g) { perror("mkfifo"); exit (1); } - socket = open(fifo, O_RDWR | O_NONBLOCK, 0); - if (socket == -1) { - perror("open"); - exit (1); + sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0); + if (sockfd == -1) { + perror("open"); + exit (1); } - g->input = fdopen(socket, "r"); + g->input = fdopen(sockfd, "r"); fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo); - event_set(&g->fifo_event, socket, EV_READ | EV_PERSIST, fifo_cb, g); + event_set(&g->fifo_event, sockfd, EV_READ | EV_PERSIST, fifo_cb, g); event_add(&g->fifo_event, NULL); return (0); } - - int main(int argc, char **argv) { GlobalInfo g; CURLMcode rc; + (void)argc; + (void)argv; memset(&g, 0, sizeof(GlobalInfo)); event_init(); -- cgit v1.2.1 From c2a84aa6f02ee81c5c3e12b6d35cd0307fb24d7b Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 19 May 2008 20:40:53 +0000 Subject: change the code style to be more curlish, and changed some of the output to be more descriptive and finally set VERBOSE mode to 1 by default --- docs/examples/hiperfifo.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'docs/examples/hiperfifo.c') diff --git a/docs/examples/hiperfifo.c b/docs/examples/hiperfifo.c index 065a974ad..a3b3b55eb 100644 --- a/docs/examples/hiperfifo.c +++ b/docs/examples/hiperfifo.c @@ -116,10 +116,14 @@ static void mcode_or_die(const char *where, CURLMcode code) case CURLM_BAD_EASY_HANDLE: s="CURLM_BAD_EASY_HANDLE"; break; case CURLM_OUT_OF_MEMORY: s="CURLM_OUT_OF_MEMORY"; break; case CURLM_INTERNAL_ERROR: s="CURLM_INTERNAL_ERROR"; break; - case CURLM_BAD_SOCKET: s="CURLM_BAD_SOCKET"; break; case CURLM_UNKNOWN_OPTION: s="CURLM_UNKNOWN_OPTION"; break; case CURLM_LAST: s="CURLM_LAST"; break; default: s="CURLM_unknown"; + break; + case CURLM_BAD_SOCKET: s="CURLM_BAD_SOCKET"; + fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s); + /* ignore this error */ + return; } fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s); exit(code); @@ -213,9 +217,11 @@ static void timer_cb(int fd, short kind, void *userp) /* Clean up the SockInfo structure */ static void remsock(SockInfo *f) { - if (!f) { return; } - if (f->evset) { event_del(&f->ev); } - free(f); + if (f) { + if (f->evset) + event_del(&f->ev); + free(f); + } } @@ -229,8 +235,9 @@ static void setsock(SockInfo*f, curl_socket_t s, CURL*e, int act, GlobalInfo*g) f->sockfd = s; f->action = act; f->easy = e; - if (f->evset) { event_del(&f->ev); } - event_set( &f->ev, f->sockfd, kind, event_cb, g); + if (f->evset) + event_del(&f->ev); + event_set(&f->ev, f->sockfd, kind, event_cb, g); f->evset=1; event_add(&f->ev, NULL); } @@ -246,8 +253,6 @@ static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g) { curl_multi_assign(g->multi, s, fdp); } - - /* CURLMOPT_SOCKETFUNCTION */ static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp) { @@ -260,16 +265,16 @@ static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp) if (what == CURL_POLL_REMOVE) { fprintf(MSG_OUT, "\n"); remsock(fdp); - } else { + } + else { if (!fdp) { - fprintf(MSG_OUT, "Adding data: %s%s\n", - what&CURL_POLL_IN?"READ":"", - what&CURL_POLL_OUT?"WRITE":"" ); + fprintf(MSG_OUT, "Adding data: %s\n", whatstr[what]); addsock(s, e, what, g); } else { fprintf(MSG_OUT, - "Changing action from %d to %d\n", fdp->action, what); + "Changing action from %s to %s\n", + whatstr[fdp->action], whatstr[what]); setsock(fdp, s, e, what, g); } } @@ -322,7 +327,7 @@ static void new_conn(char *url, GlobalInfo *g ) curl_easy_setopt(conn->easy, CURLOPT_URL, conn->url); curl_easy_setopt(conn->easy, CURLOPT_WRITEFUNCTION, write_cb); curl_easy_setopt(conn->easy, CURLOPT_WRITEDATA, &conn); - curl_easy_setopt(conn->easy, CURLOPT_VERBOSE, 0); + curl_easy_setopt(conn->easy, CURLOPT_VERBOSE, 1); curl_easy_setopt(conn->easy, CURLOPT_ERRORBUFFER, conn->error); curl_easy_setopt(conn->easy, CURLOPT_PRIVATE, conn); curl_easy_setopt(conn->easy, CURLOPT_NOPROGRESS, 0); -- cgit v1.2.1 From e664cd5826d43930fcc5b5dbaedbec94af33184b Mon Sep 17 00:00:00 2001 From: Dan Fandrich Date: Thu, 22 May 2008 21:20:07 +0000 Subject: Fixed a surprising number of example programs that were passing int arguments to curl_easy_setopt instead of long. --- docs/examples/hiperfifo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'docs/examples/hiperfifo.c') diff --git a/docs/examples/hiperfifo.c b/docs/examples/hiperfifo.c index a3b3b55eb..95ca8ad0d 100644 --- a/docs/examples/hiperfifo.c +++ b/docs/examples/hiperfifo.c @@ -327,10 +327,10 @@ static void new_conn(char *url, GlobalInfo *g ) curl_easy_setopt(conn->easy, CURLOPT_URL, conn->url); curl_easy_setopt(conn->easy, CURLOPT_WRITEFUNCTION, write_cb); curl_easy_setopt(conn->easy, CURLOPT_WRITEDATA, &conn); - curl_easy_setopt(conn->easy, CURLOPT_VERBOSE, 1); + curl_easy_setopt(conn->easy, CURLOPT_VERBOSE, 1L); curl_easy_setopt(conn->easy, CURLOPT_ERRORBUFFER, conn->error); curl_easy_setopt(conn->easy, CURLOPT_PRIVATE, conn); - curl_easy_setopt(conn->easy, CURLOPT_NOPROGRESS, 0); + curl_easy_setopt(conn->easy, CURLOPT_NOPROGRESS, 0L); curl_easy_setopt(conn->easy, CURLOPT_PROGRESSFUNCTION, prog_cb); curl_easy_setopt(conn->easy, CURLOPT_PROGRESSDATA, conn); fprintf(MSG_OUT, -- cgit v1.2.1 From 22d4db1cf2f2661c6d7292c9545051937be25ed2 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 19 Nov 2008 15:30:41 +0000 Subject: I updated this example to use the modern paradigms of the socket API where *_socket_all() and *_socket() aren't used at all but only *_socket_action() is. --- docs/examples/hiperfifo.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'docs/examples/hiperfifo.c') diff --git a/docs/examples/hiperfifo.c b/docs/examples/hiperfifo.c index 95ca8ad0d..d19048506 100644 --- a/docs/examples/hiperfifo.c +++ b/docs/examples/hiperfifo.c @@ -180,12 +180,17 @@ static void event_cb(int fd, short kind, void *userp) { GlobalInfo *g = (GlobalInfo*) userp; CURLMcode rc; - (void)kind; /* unused */ + + int action = + (kind&EV_READ:CURL_CSELECT_IN)| + (kind&EV_WRITE:CURL_CSELECT_OUT); do { - rc = curl_multi_socket(g->multi, fd, &g->still_running); + rc = curl_multi_socket_action(g->multi, fd, action, &g->still_running); } while (rc == CURLM_CALL_MULTI_PERFORM); - mcode_or_die("event_cb: curl_multi_socket", rc); + + mcode_or_die("event_cb: curl_multi_socket_action", rc); + check_run_count(g); if ( g->still_running <= 0 ) { fprintf(MSG_OUT, "last transfer done, kill timeout\n"); @@ -206,9 +211,10 @@ static void timer_cb(int fd, short kind, void *userp) (void)kind; do { - rc = curl_multi_socket(g->multi, CURL_SOCKET_TIMEOUT, &g->still_running); + 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); + mcode_or_die("timer_cb: curl_multi_socket_action", rc); check_run_count(g); } @@ -337,11 +343,9 @@ static void new_conn(char *url, GlobalInfo *g ) "Adding easy %p to multi %p (%s)\n", conn->easy, g->multi, url); rc =curl_multi_add_handle(g->multi, conn->easy); mcode_or_die("new_conn: curl_multi_add_handle", rc); - do { - rc = curl_multi_socket_all(g->multi, &g->still_running); - } while (CURLM_CALL_MULTI_PERFORM == 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 */ @@ -409,13 +413,16 @@ int main(int argc, char **argv) init_fifo(&g); g.multi = curl_multi_init(); evtimer_set(&g.timer_event, timer_cb, &g); + + /* setup the generic multi interface options we want */ curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb); 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! */ + event_dispatch(); curl_multi_cleanup(g.multi); return 0; -- cgit v1.2.1 From 0b489c7e611bc0bb5e224fc5e680fb7ae57a0557 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 19 Nov 2008 15:31:55 +0000 Subject: and now it compiles too! --- docs/examples/hiperfifo.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'docs/examples/hiperfifo.c') diff --git a/docs/examples/hiperfifo.c b/docs/examples/hiperfifo.c index d19048506..4102408ce 100644 --- a/docs/examples/hiperfifo.c +++ b/docs/examples/hiperfifo.c @@ -182,8 +182,8 @@ static void event_cb(int fd, short kind, void *userp) CURLMcode rc; int action = - (kind&EV_READ:CURL_CSELECT_IN)| - (kind&EV_WRITE:CURL_CSELECT_OUT); + (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); @@ -404,7 +404,6 @@ static int init_fifo (GlobalInfo *g) int main(int argc, char **argv) { GlobalInfo g; - CURLMcode rc; (void)argc; (void)argv; -- cgit v1.2.1 From 2309b4e330b96bc2e1f8e36b6184015e59544037 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 24 Mar 2010 11:02:54 +0100 Subject: remove the CVSish $Id$ lines --- docs/examples/hiperfifo.c | 1 - 1 file changed, 1 deletion(-) (limited to 'docs/examples/hiperfifo.c') diff --git a/docs/examples/hiperfifo.c b/docs/examples/hiperfifo.c index 4102408ce..f5b422b4a 100644 --- a/docs/examples/hiperfifo.c +++ b/docs/examples/hiperfifo.c @@ -5,7 +5,6 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * $Id$ * * Example application source code using the multi socket interface to * download many files at once. -- cgit v1.2.1 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/hiperfifo.c | 78 +++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 50 deletions(-) (limited to 'docs/examples/hiperfifo.c') 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 -- cgit v1.2.1 From bcfb9ea34cc7cddbbf74376aa16043681e4745a7 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Sat, 18 Dec 2010 17:12:44 +0100 Subject: examples: socket type cleanup --- docs/examples/hiperfifo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs/examples/hiperfifo.c') diff --git a/docs/examples/hiperfifo.c b/docs/examples/hiperfifo.c index c9096871b..a885c0ce4 100644 --- a/docs/examples/hiperfifo.c +++ b/docs/examples/hiperfifo.c @@ -350,7 +350,7 @@ static int init_fifo (GlobalInfo *g) { struct stat st; static const char *fifo = "hiper.fifo"; - int sockfd; + curl_socket_t sockfd; fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo); if (lstat (fifo, &st) == 0) { -- cgit v1.2.1 From 1aeb635cdd296c16acb375a4a83a78f13166ccab Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 10 Mar 2011 11:48:02 +0100 Subject: sources: update source headers All C and H files now (should) feature the proper project curl source code header, which includes basic info, a copyright statement and some basic disclaimers. --- docs/examples/hiperfifo.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'docs/examples/hiperfifo.c') diff --git a/docs/examples/hiperfifo.c b/docs/examples/hiperfifo.c index a885c0ce4..6036643b1 100644 --- a/docs/examples/hiperfifo.c +++ b/docs/examples/hiperfifo.c @@ -1,15 +1,28 @@ -/***************************************************************************** +/*************************************************************************** * _ _ ____ _ * Project ___| | | | _ \| | * / __| | | | |_) | | * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * + * Copyright (C) 1998 - 2011, Daniel Stenberg, , et al. * - * Example application source code using the multi socket interface to - * download many files at once. + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at http://curl.haxx.se/docs/copyright.html. * - * Written by Jeff Pohlmeyer + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ***************************************************************************/ +/* Example application source code using the multi socket interface to + download many files at once. + +Written by Jeff Pohlmeyer Requires libevent and a (POSIX?) system that has mkfifo(). -- cgit v1.2.1