evhiperfifo.c 12 KB

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