1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * multi socket API usage with libevent 2
26 /* Example application source code using the multi socket interface to
27 download many files at once.
29 Written by Jeff Pohlmeyer
31 Requires libevent version 2 and a (POSIX?) system that has mkfifo().
33 This is an adaptation of libcurl's "hipev.c" and libevent's "event-test.c"
36 When running, the program creates the named pipe "hiper.fifo"
38 Whenever there is input into the fifo, the program reads the input as a list
39 of URL's and creates some new easy handles to fetch each URL via the
40 curl_multi "hiper" API.
43 Thus, you can try a single URL:
44 % echo http://www.yahoo.com > hiper.fifo
46 Or a whole bunch of them:
47 % cat my-url-list > hiper.fifo
49 The fifo buffer is handled almost instantly, so you can even add more URL's
50 while the previous requests are still being downloaded.
53 For the sake of simplicity, URL length is limited to 1023 char's !
55 This is purely a demo app, all retrieved data is simply discarded by the write
67 #include <curl/curl.h>
68 #include <event2/event.h>
74 #define MSG_OUT stdout /* Send info to stdout, change to stderr if you want */
77 /* Global information, common to all connections */
78 typedef struct _GlobalInfo
80 struct event_base *evbase;
81 struct event *fifo_event;
82 struct event *timer_event;
89 /* Information associated with a specific easy handle */
90 typedef struct _ConnInfo
95 char error[CURL_ERROR_SIZE];
99 /* Information associated with a specific socket */
100 typedef struct _SockInfo
102 curl_socket_t sockfd;
113 /* Update the event timer after curl_multi library calls */
114 static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g)
116 struct timeval timeout;
117 (void)multi; /* unused */
119 timeout.tv_sec = timeout_ms/1000;
120 timeout.tv_usec = (timeout_ms%1000)*1000;
121 fprintf(MSG_OUT, "multi_timer_cb: Setting timeout to %ld ms\n", timeout_ms);
122 evtimer_add(g->timer_event, &timeout);
126 /* Die if we get a bad CURLMcode somewhere */
127 static void mcode_or_die(const char *where, CURLMcode code)
129 if(CURLM_OK != code) {
132 case CURLM_BAD_HANDLE: s="CURLM_BAD_HANDLE"; break;
133 case CURLM_BAD_EASY_HANDLE: s="CURLM_BAD_EASY_HANDLE"; break;
134 case CURLM_OUT_OF_MEMORY: s="CURLM_OUT_OF_MEMORY"; break;
135 case CURLM_INTERNAL_ERROR: s="CURLM_INTERNAL_ERROR"; break;
136 case CURLM_UNKNOWN_OPTION: s="CURLM_UNKNOWN_OPTION"; break;
137 case CURLM_LAST: s="CURLM_LAST"; break;
138 default: s="CURLM_unknown";
140 case CURLM_BAD_SOCKET: s="CURLM_BAD_SOCKET";
141 fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
142 /* ignore this error */
145 fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
152 /* Check for completed transfers, and remove their easy handles */
153 static void check_multi_info(GlobalInfo *g)
162 fprintf(MSG_OUT, "REMAINING: %d\n", g->still_running);
163 while((msg = curl_multi_info_read(g->multi, &msgs_left))) {
164 if(msg->msg == CURLMSG_DONE) {
165 easy = msg->easy_handle;
166 res = msg->data.result;
167 curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn);
168 curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url);
169 fprintf(MSG_OUT, "DONE: %s => (%d) %s\n", eff_url, res, conn->error);
170 curl_multi_remove_handle(g->multi, easy);
172 curl_easy_cleanup(easy);
180 /* Called by libevent when we get action on a multi socket */
181 static void event_cb(int fd, short kind, void *userp)
183 GlobalInfo *g = (GlobalInfo*) userp;
187 (kind & EV_READ ? CURL_CSELECT_IN : 0) |
188 (kind & EV_WRITE ? CURL_CSELECT_OUT : 0);
190 rc = curl_multi_socket_action(g->multi, fd, action, &g->still_running);
191 mcode_or_die("event_cb: curl_multi_socket_action", rc);
194 if(g->still_running <= 0) {
195 fprintf(MSG_OUT, "last transfer done, kill timeout\n");
196 if(evtimer_pending(g->timer_event, NULL)) {
197 evtimer_del(g->timer_event);
204 /* Called by libevent when our timeout expires */
205 static void timer_cb(int fd, short kind, void *userp)
207 GlobalInfo *g = (GlobalInfo *)userp;
212 rc = curl_multi_socket_action(g->multi,
213 CURL_SOCKET_TIMEOUT, 0, &g->still_running);
214 mcode_or_die("timer_cb: curl_multi_socket_action", rc);
220 /* Clean up the SockInfo structure */
221 static void remsock(SockInfo *f)
232 /* Assign information to a SockInfo structure */
233 static void setsock(SockInfo *f, curl_socket_t s, CURL *e, int act,
237 (act&CURL_POLL_IN?EV_READ:0)|(act&CURL_POLL_OUT?EV_WRITE:0)|EV_PERSIST;
244 f->ev = event_new(g->evbase, f->sockfd, kind, event_cb, g);
246 event_add(f->ev, NULL);
251 /* Initialize a new SockInfo structure */
252 static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g)
254 SockInfo *fdp = calloc(sizeof(SockInfo), 1);
257 setsock(fdp, s, easy, action, g);
258 curl_multi_assign(g->multi, s, fdp);
261 /* CURLMOPT_SOCKETFUNCTION */
262 static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
264 GlobalInfo *g = (GlobalInfo*) cbp;
265 SockInfo *fdp = (SockInfo*) sockp;
266 const char *whatstr[]={ "none", "IN", "OUT", "INOUT", "REMOVE" };
269 "socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
270 if(what == CURL_POLL_REMOVE) {
271 fprintf(MSG_OUT, "\n");
276 fprintf(MSG_OUT, "Adding data: %s\n", whatstr[what]);
277 addsock(s, e, what, g);
281 "Changing action from %s to %s\n",
282 whatstr[fdp->action], whatstr[what]);
283 setsock(fdp, s, e, what, g);
291 /* CURLOPT_WRITEFUNCTION */
292 static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data)
294 size_t realsize = size * nmemb;
295 ConnInfo *conn = (ConnInfo*) data;
302 /* CURLOPT_PROGRESSFUNCTION */
303 static int prog_cb(void *p, double dltotal, double dlnow, double ult,
306 ConnInfo *conn = (ConnInfo *)p;
310 fprintf(MSG_OUT, "Progress: %s (%g/%g)\n", conn->url, dlnow, dltotal);
315 /* Create a new easy handle, and add it to the global curl_multi */
316 static void new_conn(char *url, GlobalInfo *g)
321 conn = calloc(1, sizeof(ConnInfo));
322 memset(conn, 0, sizeof(ConnInfo));
325 conn->easy = curl_easy_init();
327 fprintf(MSG_OUT, "curl_easy_init() failed, exiting!\n");
331 conn->url = strdup(url);
332 curl_easy_setopt(conn->easy, CURLOPT_URL, conn->url);
333 curl_easy_setopt(conn->easy, CURLOPT_WRITEFUNCTION, write_cb);
334 curl_easy_setopt(conn->easy, CURLOPT_WRITEDATA, conn);
335 curl_easy_setopt(conn->easy, CURLOPT_VERBOSE, 1L);
336 curl_easy_setopt(conn->easy, CURLOPT_ERRORBUFFER, conn->error);
337 curl_easy_setopt(conn->easy, CURLOPT_PRIVATE, conn);
338 curl_easy_setopt(conn->easy, CURLOPT_NOPROGRESS, 0L);
339 curl_easy_setopt(conn->easy, CURLOPT_PROGRESSFUNCTION, prog_cb);
340 curl_easy_setopt(conn->easy, CURLOPT_PROGRESSDATA, conn);
342 "Adding easy %p to multi %p (%s)\n", conn->easy, g->multi, url);
343 rc = curl_multi_add_handle(g->multi, conn->easy);
344 mcode_or_die("new_conn: curl_multi_add_handle", rc);
346 /* note that the add_handle() will set a time-out to trigger very soon so
347 that the necessary socket_action() call will be called by this app */
350 /* This gets called whenever data is received from the fifo */
351 static void fifo_cb(int fd, short event, void *arg)
356 GlobalInfo *g = (GlobalInfo *)arg;
357 (void)fd; /* unused */
358 (void)event; /* unused */
362 rv=fscanf(g->input, "%1023s%n", s, &n);
365 new_conn(s, arg); /* if we read a URL, go get it! */
372 /* Create a named pipe and tell libevent to monitor it */
373 static const char *fifo = "hiper.fifo";
374 static int init_fifo(GlobalInfo *g)
377 curl_socket_t sockfd;
379 fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
380 if(lstat (fifo, &st) == 0) {
381 if((st.st_mode & S_IFMT) == S_IFREG) {
388 if(mkfifo (fifo, 0600) == -1) {
392 sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0);
397 g->input = fdopen(sockfd, "r");
399 fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo);
400 g->fifo_event = event_new(g->evbase, sockfd, EV_READ|EV_PERSIST, fifo_cb, g);
401 event_add(g->fifo_event, NULL);
405 static void clean_fifo(GlobalInfo *g)
407 event_free(g->fifo_event);
412 int main(int argc, char **argv)
418 memset(&g, 0, sizeof(GlobalInfo));
419 g.evbase = event_base_new();
421 g.multi = curl_multi_init();
422 g.timer_event = evtimer_new(g.evbase, timer_cb, &g);
424 /* setup the generic multi interface options we want */
425 curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
426 curl_multi_setopt(g.multi, CURLMOPT_SOCKETDATA, &g);
427 curl_multi_setopt(g.multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb);
428 curl_multi_setopt(g.multi, CURLMOPT_TIMERDATA, &g);
430 /* we don't call any curl_multi_socket*() function yet as we have no handles
433 event_base_dispatch(g.evbase);
435 /* this, of course, won't get called since only way to stop this program is
436 via ctrl-C, but it is here to show how cleanup /would/ be done. */
438 event_free(g.timer_event);
439 event_base_free(g.evbase);
440 curl_multi_cleanup(g.multi);