evhiperfifo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  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.
  13. *
  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.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /* <DESC>
  23. * multi socket interface together with libev
  24. * </DESC>
  25. */
  26. /* Example application source code using the multi socket interface to
  27. * download many files at once.
  28. *
  29. * This example features the same basic functionality as hiperfifo.c does,
  30. * but this uses libev instead of libevent.
  31. *
  32. * Written by Jeff Pohlmeyer, converted to use libev by Markus Koetter
  33. Requires libev and a (POSIX?) system that has mkfifo().
  34. This is an adaptation of libcurl's "hipev.c" and libevent's "event-test.c"
  35. sample programs.
  36. When running, the program creates the named pipe "hiper.fifo"
  37. Whenever there is input into the fifo, the program reads the input as a list
  38. of URL's and creates some new easy handles to fetch each URL via the
  39. curl_multi "hiper" API.
  40. Thus, you can try a single URL:
  41. % echo http://www.yahoo.com > hiper.fifo
  42. Or a whole bunch of them:
  43. % cat my-url-list > hiper.fifo
  44. The fifo buffer is handled almost instantly, so you can even add more URL's
  45. while the previous requests are still being downloaded.
  46. Note:
  47. For the sake of simplicity, URL length is limited to 1023 char's !
  48. This is purely a demo app, all retrieved data is simply discarded by the write
  49. callback.
  50. */
  51. #include <stdio.h>
  52. #include <string.h>
  53. #include <stdlib.h>
  54. #include <sys/time.h>
  55. #include <time.h>
  56. #include <unistd.h>
  57. #include <sys/poll.h>
  58. #include <curl/curl.h>
  59. #include <ev.h>
  60. #include <fcntl.h>
  61. #include <sys/stat.h>
  62. #include <errno.h>
  63. #define DPRINT(x...) printf(x)
  64. #define MSG_OUT stdout /* Send info to stdout, change to stderr if you want */
  65. /* Global information, common to all connections */
  66. typedef struct _GlobalInfo
  67. {
  68. struct ev_loop *loop;
  69. struct ev_io fifo_event;
  70. struct ev_timer timer_event;
  71. CURLM *multi;
  72. int still_running;
  73. FILE *input;
  74. } GlobalInfo;
  75. /* Information associated with a specific easy handle */
  76. typedef struct _ConnInfo
  77. {
  78. CURL *easy;
  79. char *url;
  80. GlobalInfo *global;
  81. char error[CURL_ERROR_SIZE];
  82. } ConnInfo;
  83. /* Information associated with a specific socket */
  84. typedef struct _SockInfo
  85. {
  86. curl_socket_t sockfd;
  87. CURL *easy;
  88. int action;
  89. long timeout;
  90. struct ev_io ev;
  91. int evset;
  92. GlobalInfo *global;
  93. } SockInfo;
  94. static void timer_cb(EV_P_ struct ev_timer *w, int revents);
  95. /* Update the event timer after curl_multi library calls */
  96. static int multi_timer_cb(CURLM *multi, long timeout_ms, GlobalInfo *g)
  97. {
  98. DPRINT("%s %li\n", __PRETTY_FUNCTION__, timeout_ms);
  99. ev_timer_stop(g->loop, &g->timer_event);
  100. if(timeout_ms > 0) {
  101. double t = timeout_ms / 1000;
  102. ev_timer_init(&g->timer_event, timer_cb, t, 0.);
  103. ev_timer_start(g->loop, &g->timer_event);
  104. }
  105. else if(timeout_ms == 0)
  106. timer_cb(g->loop, &g->timer_event, 0);
  107. return 0;
  108. }
  109. /* Die if we get a bad CURLMcode somewhere */
  110. static void mcode_or_die(const char *where, CURLMcode code)
  111. {
  112. if(CURLM_OK != code) {
  113. const char *s;
  114. switch(code) {
  115. case CURLM_BAD_HANDLE:
  116. s="CURLM_BAD_HANDLE";
  117. break;
  118. case CURLM_BAD_EASY_HANDLE:
  119. s="CURLM_BAD_EASY_HANDLE";
  120. break;
  121. case CURLM_OUT_OF_MEMORY:
  122. s="CURLM_OUT_OF_MEMORY";
  123. break;
  124. case CURLM_INTERNAL_ERROR:
  125. s="CURLM_INTERNAL_ERROR";
  126. break;
  127. case CURLM_UNKNOWN_OPTION:
  128. s="CURLM_UNKNOWN_OPTION";
  129. break;
  130. case CURLM_LAST:
  131. s="CURLM_LAST";
  132. break;
  133. default:
  134. s="CURLM_unknown";
  135. break;
  136. case CURLM_BAD_SOCKET:
  137. s="CURLM_BAD_SOCKET";
  138. fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
  139. /* ignore this error */
  140. return;
  141. }
  142. fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s);
  143. exit(code);
  144. }
  145. }
  146. /* Check for completed transfers, and remove their easy handles */
  147. static void check_multi_info(GlobalInfo *g)
  148. {
  149. char *eff_url;
  150. CURLMsg *msg;
  151. int msgs_left;
  152. ConnInfo *conn;
  153. CURL *easy;
  154. CURLcode res;
  155. fprintf(MSG_OUT, "REMAINING: %d\n", g->still_running);
  156. while((msg = curl_multi_info_read(g->multi, &msgs_left))) {
  157. if(msg->msg == CURLMSG_DONE) {
  158. easy = msg->easy_handle;
  159. res = msg->data.result;
  160. curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn);
  161. curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url);
  162. fprintf(MSG_OUT, "DONE: %s => (%d) %s\n", eff_url, res, conn->error);
  163. curl_multi_remove_handle(g->multi, easy);
  164. free(conn->url);
  165. curl_easy_cleanup(easy);
  166. free(conn);
  167. }
  168. }
  169. }
  170. /* Called by libevent when we get action on a multi socket */
  171. static void event_cb(EV_P_ struct ev_io *w, int revents)
  172. {
  173. DPRINT("%s w %p revents %i\n", __PRETTY_FUNCTION__, w, revents);
  174. GlobalInfo *g = (GlobalInfo*) w->data;
  175. CURLMcode rc;
  176. int action = (revents&EV_READ?CURL_POLL_IN:0)|
  177. (revents&EV_WRITE?CURL_POLL_OUT:0);
  178. rc = curl_multi_socket_action(g->multi, w->fd, action, &g->still_running);
  179. mcode_or_die("event_cb: curl_multi_socket_action", rc);
  180. check_multi_info(g);
  181. if(g->still_running <= 0) {
  182. fprintf(MSG_OUT, "last transfer done, kill timeout\n");
  183. ev_timer_stop(g->loop, &g->timer_event);
  184. }
  185. }
  186. /* Called by libevent when our timeout expires */
  187. static void timer_cb(EV_P_ struct ev_timer *w, int revents)
  188. {
  189. DPRINT("%s w %p revents %i\n", __PRETTY_FUNCTION__, w, revents);
  190. GlobalInfo *g = (GlobalInfo *)w->data;
  191. CURLMcode rc;
  192. rc = curl_multi_socket_action(g->multi, CURL_SOCKET_TIMEOUT, 0,
  193. &g->still_running);
  194. mcode_or_die("timer_cb: curl_multi_socket_action", rc);
  195. check_multi_info(g);
  196. }
  197. /* Clean up the SockInfo structure */
  198. static void remsock(SockInfo *f, GlobalInfo *g)
  199. {
  200. printf("%s \n", __PRETTY_FUNCTION__);
  201. if(f) {
  202. if(f->evset)
  203. ev_io_stop(g->loop, &f->ev);
  204. free(f);
  205. }
  206. }
  207. /* Assign information to a SockInfo structure */
  208. static void setsock(SockInfo *f, curl_socket_t s, CURL *e, int act,
  209. GlobalInfo *g)
  210. {
  211. printf("%s \n", __PRETTY_FUNCTION__);
  212. int kind = (act&CURL_POLL_IN?EV_READ:0)|(act&CURL_POLL_OUT?EV_WRITE:0);
  213. f->sockfd = s;
  214. f->action = act;
  215. f->easy = e;
  216. if(f->evset)
  217. ev_io_stop(g->loop, &f->ev);
  218. ev_io_init(&f->ev, event_cb, f->sockfd, kind);
  219. f->ev.data = g;
  220. f->evset=1;
  221. ev_io_start(g->loop, &f->ev);
  222. }
  223. /* Initialize a new SockInfo structure */
  224. static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g)
  225. {
  226. SockInfo *fdp = calloc(sizeof(SockInfo), 1);
  227. fdp->global = g;
  228. setsock(fdp, s, easy, action, g);
  229. curl_multi_assign(g->multi, s, fdp);
  230. }
  231. /* CURLMOPT_SOCKETFUNCTION */
  232. static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
  233. {
  234. DPRINT("%s e %p s %i what %i cbp %p sockp %p\n",
  235. __PRETTY_FUNCTION__, e, s, what, cbp, sockp);
  236. GlobalInfo *g = (GlobalInfo*) cbp;
  237. SockInfo *fdp = (SockInfo*) sockp;
  238. const char *whatstr[]={ "none", "IN", "OUT", "INOUT", "REMOVE"};
  239. fprintf(MSG_OUT,
  240. "socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
  241. if(what == CURL_POLL_REMOVE) {
  242. fprintf(MSG_OUT, "\n");
  243. remsock(fdp, g);
  244. }
  245. else {
  246. if(!fdp) {
  247. fprintf(MSG_OUT, "Adding data: %s\n", whatstr[what]);
  248. addsock(s, e, what, g);
  249. }
  250. else {
  251. fprintf(MSG_OUT,
  252. "Changing action from %s to %s\n",
  253. whatstr[fdp->action], whatstr[what]);
  254. setsock(fdp, s, e, what, g);
  255. }
  256. }
  257. return 0;
  258. }
  259. /* CURLOPT_WRITEFUNCTION */
  260. static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data)
  261. {
  262. size_t realsize = size * nmemb;
  263. ConnInfo *conn = (ConnInfo*) data;
  264. (void)ptr;
  265. (void)conn;
  266. return realsize;
  267. }
  268. /* CURLOPT_PROGRESSFUNCTION */
  269. static int prog_cb(void *p, double dltotal, double dlnow, double ult,
  270. double uln)
  271. {
  272. ConnInfo *conn = (ConnInfo *)p;
  273. (void)ult;
  274. (void)uln;
  275. fprintf(MSG_OUT, "Progress: %s (%g/%g)\n", conn->url, dlnow, dltotal);
  276. return 0;
  277. }
  278. /* Create a new easy handle, and add it to the global curl_multi */
  279. static void new_conn(char *url, GlobalInfo *g)
  280. {
  281. ConnInfo *conn;
  282. CURLMcode rc;
  283. conn = calloc(1, sizeof(ConnInfo));
  284. memset(conn, 0, sizeof(ConnInfo));
  285. conn->error[0]='\0';
  286. conn->easy = curl_easy_init();
  287. if(!conn->easy) {
  288. fprintf(MSG_OUT, "curl_easy_init() failed, exiting!\n");
  289. exit(2);
  290. }
  291. conn->global = g;
  292. conn->url = strdup(url);
  293. curl_easy_setopt(conn->easy, CURLOPT_URL, conn->url);
  294. curl_easy_setopt(conn->easy, CURLOPT_WRITEFUNCTION, write_cb);
  295. curl_easy_setopt(conn->easy, CURLOPT_WRITEDATA, conn);
  296. curl_easy_setopt(conn->easy, CURLOPT_VERBOSE, 1L);
  297. curl_easy_setopt(conn->easy, CURLOPT_ERRORBUFFER, conn->error);
  298. curl_easy_setopt(conn->easy, CURLOPT_PRIVATE, conn);
  299. curl_easy_setopt(conn->easy, CURLOPT_NOPROGRESS, 0L);
  300. curl_easy_setopt(conn->easy, CURLOPT_PROGRESSFUNCTION, prog_cb);
  301. curl_easy_setopt(conn->easy, CURLOPT_PROGRESSDATA, conn);
  302. curl_easy_setopt(conn->easy, CURLOPT_LOW_SPEED_TIME, 3L);
  303. curl_easy_setopt(conn->easy, CURLOPT_LOW_SPEED_LIMIT, 10L);
  304. fprintf(MSG_OUT,
  305. "Adding easy %p to multi %p (%s)\n", conn->easy, g->multi, url);
  306. rc = curl_multi_add_handle(g->multi, conn->easy);
  307. mcode_or_die("new_conn: curl_multi_add_handle", rc);
  308. /* note that the add_handle() will set a time-out to trigger very soon so
  309. that the necessary socket_action() call will be called by this app */
  310. }
  311. /* This gets called whenever data is received from the fifo */
  312. static void fifo_cb(EV_P_ struct ev_io *w, int revents)
  313. {
  314. char s[1024];
  315. long int rv=0;
  316. int n=0;
  317. GlobalInfo *g = (GlobalInfo *)w->data;
  318. do {
  319. s[0]='\0';
  320. rv=fscanf(g->input, "%1023s%n", s, &n);
  321. s[n]='\0';
  322. if(n && s[0]) {
  323. new_conn(s, g); /* if we read a URL, go get it! */
  324. }
  325. else
  326. break;
  327. } while(rv != EOF);
  328. }
  329. /* Create a named pipe and tell libevent to monitor it */
  330. static int init_fifo(GlobalInfo *g)
  331. {
  332. struct stat st;
  333. static const char *fifo = "hiper.fifo";
  334. curl_socket_t sockfd;
  335. fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
  336. if(lstat (fifo, &st) == 0) {
  337. if((st.st_mode & S_IFMT) == S_IFREG) {
  338. errno = EEXIST;
  339. perror("lstat");
  340. exit(1);
  341. }
  342. }
  343. unlink(fifo);
  344. if(mkfifo (fifo, 0600) == -1) {
  345. perror("mkfifo");
  346. exit(1);
  347. }
  348. sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0);
  349. if(sockfd == -1) {
  350. perror("open");
  351. exit(1);
  352. }
  353. g->input = fdopen(sockfd, "r");
  354. fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo);
  355. ev_io_init(&g->fifo_event, fifo_cb, sockfd, EV_READ);
  356. ev_io_start(g->loop, &g->fifo_event);
  357. return (0);
  358. }
  359. int main(int argc, char **argv)
  360. {
  361. GlobalInfo g;
  362. CURLMcode rc;
  363. (void)argc;
  364. (void)argv;
  365. memset(&g, 0, sizeof(GlobalInfo));
  366. g.loop = ev_default_loop(0);
  367. init_fifo(&g);
  368. g.multi = curl_multi_init();
  369. ev_timer_init(&g.timer_event, timer_cb, 0., 0.);
  370. g.timer_event.data = &g;
  371. g.fifo_event.data = &g;
  372. curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
  373. curl_multi_setopt(g.multi, CURLMOPT_SOCKETDATA, &g);
  374. curl_multi_setopt(g.multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb);
  375. curl_multi_setopt(g.multi, CURLMOPT_TIMERDATA, &g);
  376. /* we don't call any curl_multi_socket*() function yet as we have no handles
  377. added! */
  378. ev_loop(g.loop, 0);
  379. curl_multi_cleanup(g.multi);
  380. return 0;
  381. }