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 interface together with libev
26 /* Example application source code using the multi socket interface to
27 * download many files at once.
29 * This example features the same basic functionality as hiperfifo.c does,
30 * but this uses libev instead of libevent.
32 * Written by Jeff Pohlmeyer, converted to use libev by Markus Koetter
34 Requires libev and a (POSIX?) system that has mkfifo().
36 This is an adaptation of libcurl's "hipev.c" and libevent's "event-test.c"
39 When running, the program creates the named pipe "hiper.fifo"
41 Whenever there is input into the fifo, the program reads the input as a list
42 of URL's and creates some new easy handles to fetch each URL via the
43 curl_multi "hiper" API.
46 Thus, you can try a single URL:
47 % echo http://www.yahoo.com > hiper.fifo
49 Or a whole bunch of them:
50 % cat my-url-list > hiper.fifo
52 The fifo buffer is handled almost instantly, so you can even add more URL's
53 while the previous requests are still being downloaded.
56 For the sake of simplicity, URL length is limited to 1023 char's !
58 This is purely a demo app, all retrieved data is simply discarded by the write
70 #include <curl/curl.h>
76 #define DPRINT(x...) printf(x)
78 #define MSG_OUT stdout /* Send info to stdout, change to stderr if you want */
81 /* Global information, common to all connections */
82 typedef struct _GlobalInfo
85 struct ev_io fifo_event;
86 struct ev_timer timer_event;
93 /* Information associated with a specific easy handle */
94 typedef struct _ConnInfo
99 char error[CURL_ERROR_SIZE];
103 /* Information associated with a specific socket */
104 typedef struct _SockInfo
106 curl_socket_t sockfd;
115 static void timer_cb(EV_P_ struct ev_timer *w, int revents);
117 /* Update the event timer after curl_multi library calls */
118 static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g)
120 DPRINT("%s %li\n", __PRETTY_FUNCTION__, timeout_ms);
121 ev_timer_stop(g->loop, &g->timer_event);
123 double t = timeout_ms / 1000;
124 ev_timer_init(&g->timer_event, timer_cb, t, 0.);
125 ev_timer_start(g->loop, &g->timer_event);
128 timer_cb(g->loop, &g->timer_event, 0);
132 /* Die if we get a bad CURLMcode somewhere */
133 static void mcode_or_die(const char *where, CURLMcode code)
135 if(CURLM_OK != code) {
138 case CURLM_BAD_HANDLE:
139 s="CURLM_BAD_HANDLE";
141 case CURLM_BAD_EASY_HANDLE:
142 s="CURLM_BAD_EASY_HANDLE";
144 case CURLM_OUT_OF_MEMORY:
145 s="CURLM_OUT_OF_MEMORY";
147 case CURLM_INTERNAL_ERROR:
148 s="CURLM_INTERNAL_ERROR";
150 case CURLM_UNKNOWN_OPTION:
151 s="CURLM_UNKNOWN_OPTION";
159 case CURLM_BAD_SOCKET:
160 s="CURLM_BAD_SOCKET";
161 fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
162 /* ignore this error */
165 fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
172 /* Check for completed transfers, and remove their easy handles */
173 static void check_multi_info(GlobalInfo *g)
182 fprintf(MSG_OUT, "REMAINING: %d\n", g->still_running);
183 while((msg = curl_multi_info_read(g->multi, &msgs_left))) {
184 if(msg->msg == CURLMSG_DONE) {
185 easy = msg->easy_handle;
186 res = msg->data.result;
187 curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn);
188 curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url);
189 fprintf(MSG_OUT, "DONE: %s => (%d) %s\n", eff_url, res, conn->error);
190 curl_multi_remove_handle(g->multi, easy);
192 curl_easy_cleanup(easy);
200 /* Called by libevent when we get action on a multi socket */
201 static void event_cb(EV_P_ struct ev_io *w, int revents)
203 DPRINT("%s w %p revents %i\n", __PRETTY_FUNCTION__, w, revents);
204 GlobalInfo *g = (GlobalInfo*) w->data;
207 int action = (revents&EV_READ?CURL_POLL_IN:0)|
208 (revents&EV_WRITE?CURL_POLL_OUT:0);
209 rc = curl_multi_socket_action(g->multi, w->fd, action, &g->still_running);
210 mcode_or_die("event_cb: curl_multi_socket_action", rc);
212 if(g->still_running <= 0) {
213 fprintf(MSG_OUT, "last transfer done, kill timeout\n");
214 ev_timer_stop(g->loop, &g->timer_event);
218 /* Called by libevent when our timeout expires */
219 static void timer_cb(EV_P_ struct ev_timer *w, int revents)
221 DPRINT("%s w %p revents %i\n", __PRETTY_FUNCTION__, w, revents);
223 GlobalInfo *g = (GlobalInfo *)w->data;
226 rc = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0,
228 mcode_or_die("timer_cb: curl_multi_socket_action", rc);
232 /* Clean up the SockInfo structure */
233 static void remsock(SockInfo *f, GlobalInfo *g)
235 printf("%s \n", __PRETTY_FUNCTION__);
238 ev_io_stop(g->loop, &f->ev);
245 /* Assign information to a SockInfo structure */
246 static void setsock(SockInfo *f, curl_socket_t s, CURL *e, int act,
249 printf("%s \n", __PRETTY_FUNCTION__);
251 int kind = (act&CURL_POLL_IN?EV_READ:0)|(act&CURL_POLL_OUT?EV_WRITE:0);
257 ev_io_stop(g->loop, &f->ev);
258 ev_io_init(&f->ev, event_cb, f->sockfd, kind);
261 ev_io_start(g->loop, &f->ev);
266 /* Initialize a new SockInfo structure */
267 static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g)
269 SockInfo *fdp = calloc(sizeof(SockInfo), 1);
272 setsock(fdp, s, easy, action, g);
273 curl_multi_assign(g->multi, s, fdp);
276 /* CURLMOPT_SOCKETFUNCTION */
277 static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
279 DPRINT("%s e %p s %i what %i cbp %p sockp %p\n",
280 __PRETTY_FUNCTION__, e, s, what, cbp, sockp);
282 GlobalInfo *g = (GlobalInfo*) cbp;
283 SockInfo *fdp = (SockInfo*) sockp;
284 const char *whatstr[]={ "none", "IN", "OUT", "INOUT", "REMOVE"};
287 "socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
288 if(what == CURL_POLL_REMOVE) {
289 fprintf(MSG_OUT, "\n");
294 fprintf(MSG_OUT, "Adding data: %s\n", whatstr[what]);
295 addsock(s, e, what, g);
299 "Changing action from %s to %s\n",
300 whatstr[fdp->action], whatstr[what]);
301 setsock(fdp, s, e, what, g);
308 /* CURLOPT_WRITEFUNCTION */
309 static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data)
311 size_t realsize = size * nmemb;
312 ConnInfo *conn = (ConnInfo*) data;
319 /* CURLOPT_PROGRESSFUNCTION */
320 static int prog_cb(void *p, double dltotal, double dlnow, double ult,
323 ConnInfo *conn = (ConnInfo *)p;
327 fprintf(MSG_OUT, "Progress: %s (%g/%g)\n", conn->url, dlnow, dltotal);
332 /* Create a new easy handle, and add it to the global curl_multi */
333 static void new_conn(char *url, GlobalInfo *g)
338 conn = calloc(1, sizeof(ConnInfo));
339 memset(conn, 0, sizeof(ConnInfo));
342 conn->easy = curl_easy_init();
344 fprintf(MSG_OUT, "curl_easy_init() failed, exiting!\n");
348 conn->url = strdup(url);
349 curl_easy_setopt(conn->easy, CURLOPT_URL, conn->url);
350 curl_easy_setopt(conn->easy, CURLOPT_WRITEFUNCTION, write_cb);
351 curl_easy_setopt(conn->easy, CURLOPT_WRITEDATA, conn);
352 curl_easy_setopt(conn->easy, CURLOPT_VERBOSE, 1L);
353 curl_easy_setopt(conn->easy, CURLOPT_ERRORBUFFER, conn->error);
354 curl_easy_setopt(conn->easy, CURLOPT_PRIVATE, conn);
355 curl_easy_setopt(conn->easy, CURLOPT_NOPROGRESS, 0L);
356 curl_easy_setopt(conn->easy, CURLOPT_PROGRESSFUNCTION, prog_cb);
357 curl_easy_setopt(conn->easy, CURLOPT_PROGRESSDATA, conn);
358 curl_easy_setopt(conn->easy, CURLOPT_LOW_SPEED_TIME, 3L);
359 curl_easy_setopt(conn->easy, CURLOPT_LOW_SPEED_LIMIT, 10L);
362 "Adding easy %p to multi %p (%s)\n", conn->easy, g->multi, url);
363 rc = curl_multi_add_handle(g->multi, conn->easy);
364 mcode_or_die("new_conn: curl_multi_add_handle", rc);
366 /* note that the add_handle() will set a time-out to trigger very soon so
367 that the necessary socket_action() call will be called by this app */
370 /* This gets called whenever data is received from the fifo */
371 static void fifo_cb(EV_P_ struct ev_io *w, int revents)
376 GlobalInfo *g = (GlobalInfo *)w->data;
380 rv=fscanf(g->input, "%1023s%n", s, &n);
383 new_conn(s, g); /* if we read a URL, go get it! */
390 /* Create a named pipe and tell libevent to monitor it */
391 static int init_fifo(GlobalInfo *g)
394 static const char *fifo = "hiper.fifo";
395 curl_socket_t sockfd;
397 fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
398 if(lstat (fifo, &st) == 0) {
399 if((st.st_mode & S_IFMT) == S_IFREG) {
406 if(mkfifo (fifo, 0600) == -1) {
410 sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0);
415 g->input = fdopen(sockfd, "r");
417 fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo);
418 ev_io_init(&g->fifo_event, fifo_cb, sockfd, EV_READ);
419 ev_io_start(g->loop, &g->fifo_event);
423 int main(int argc, char **argv)
430 memset(&g, 0, sizeof(GlobalInfo));
431 g.loop = ev_default_loop(0);
434 g.multi = curl_multi_init();
436 ev_timer_init(&g.timer_event, timer_cb, 0., 0.);
437 g.timer_event.data = &g;
438 g.fifo_event.data = &g;
439 curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
440 curl_multi_setopt(g.multi, CURLMOPT_SOCKETDATA, &g);
441 curl_multi_setopt(g.multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb);
442 curl_multi_setopt(g.multi, CURLMOPT_TIMERDATA, &g);
444 /* we don't call any curl_multi_socket*() function yet as we have no handles
448 curl_multi_cleanup(g.multi);