multi-uv.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 API using libuv
  26. * </DESC>
  27. */
  28. /* Use the socket_action interface to download multiple files in parallel,
  29. powered by libuv.
  30. Requires libuv and (of course) libcurl.
  31. See https://docs.libuv.org/en/v1.x/index.html libuv API documentation
  32. */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <uv.h>
  36. #include <curl/curl.h>
  37. /* object to pass to the callbacks */
  38. struct datauv {
  39. uv_timer_t timeout;
  40. uv_loop_t *loop;
  41. CURLM *multi;
  42. };
  43. typedef struct curl_context_s {
  44. uv_poll_t poll_handle;
  45. curl_socket_t sockfd;
  46. struct datauv *uv;
  47. } curl_context_t;
  48. static curl_context_t *create_curl_context(curl_socket_t sockfd,
  49. struct datauv *uv)
  50. {
  51. curl_context_t *context;
  52. context = (curl_context_t *) malloc(sizeof(*context));
  53. context->sockfd = sockfd;
  54. context->uv = uv;
  55. uv_poll_init_socket(uv->loop, &context->poll_handle, sockfd);
  56. context->poll_handle.data = context;
  57. return context;
  58. }
  59. static void curl_close_cb(uv_handle_t *handle)
  60. {
  61. curl_context_t *context = (curl_context_t *) handle->data;
  62. free(context);
  63. }
  64. static void destroy_curl_context(curl_context_t *context)
  65. {
  66. uv_close((uv_handle_t *) &context->poll_handle, curl_close_cb);
  67. }
  68. static void add_download(const char *url, int num, CURLM *multi)
  69. {
  70. char filename[50];
  71. FILE *file;
  72. CURL *handle;
  73. snprintf(filename, 50, "%d.download", num);
  74. file = fopen(filename, "wb");
  75. if(!file) {
  76. fprintf(stderr, "Error opening %s\n", filename);
  77. return;
  78. }
  79. handle = curl_easy_init();
  80. curl_easy_setopt(handle, CURLOPT_WRITEDATA, file);
  81. curl_easy_setopt(handle, CURLOPT_PRIVATE, file);
  82. curl_easy_setopt(handle, CURLOPT_URL, url);
  83. curl_multi_add_handle(multi, handle);
  84. fprintf(stderr, "Added download %s -> %s\n", url, filename);
  85. }
  86. static void check_multi_info(curl_context_t *context)
  87. {
  88. char *done_url;
  89. CURLMsg *message;
  90. int pending;
  91. CURL *easy_handle;
  92. FILE *file;
  93. while((message = curl_multi_info_read(context->uv->multi, &pending))) {
  94. switch(message->msg) {
  95. case CURLMSG_DONE:
  96. /* Do not use message data after calling curl_multi_remove_handle() and
  97. curl_easy_cleanup(). As per curl_multi_info_read() docs:
  98. "WARNING: The data the returned pointer points to does not survive
  99. calling curl_multi_cleanup, curl_multi_remove_handle or
  100. curl_easy_cleanup." */
  101. easy_handle = message->easy_handle;
  102. curl_easy_getinfo(easy_handle, CURLINFO_EFFECTIVE_URL, &done_url);
  103. curl_easy_getinfo(easy_handle, CURLINFO_PRIVATE, &file);
  104. printf("%s DONE\n", done_url);
  105. curl_multi_remove_handle(context->uv->multi, easy_handle);
  106. curl_easy_cleanup(easy_handle);
  107. if(file) {
  108. fclose(file);
  109. }
  110. break;
  111. default:
  112. fprintf(stderr, "CURLMSG default\n");
  113. break;
  114. }
  115. }
  116. }
  117. /* callback from libuv on socket activity */
  118. static void on_uv_socket(uv_poll_t *req, int status, int events)
  119. {
  120. int running_handles;
  121. int flags = 0;
  122. curl_context_t *context = (curl_context_t *) req->data;
  123. (void)status;
  124. if(events & UV_READABLE)
  125. flags |= CURL_CSELECT_IN;
  126. if(events & UV_WRITABLE)
  127. flags |= CURL_CSELECT_OUT;
  128. curl_multi_socket_action(context->uv->multi, context->sockfd, flags,
  129. &running_handles);
  130. check_multi_info(context);
  131. }
  132. /* callback from libuv when timeout expires */
  133. static void on_uv_timeout(uv_timer_t *req)
  134. {
  135. curl_context_t *context = (curl_context_t *) req->data;
  136. if(context) {
  137. int running_handles;
  138. curl_multi_socket_action(context->uv->multi, CURL_SOCKET_TIMEOUT, 0,
  139. &running_handles);
  140. check_multi_info(context);
  141. }
  142. }
  143. /* callback from libcurl to update the timeout expiry */
  144. static int cb_timeout(CURLM *multi, long timeout_ms,
  145. struct datauv *uv)
  146. {
  147. (void)multi;
  148. if(timeout_ms < 0)
  149. uv_timer_stop(&uv->timeout);
  150. else {
  151. if(timeout_ms == 0)
  152. timeout_ms = 1; /* 0 means call curl_multi_socket_action asap but NOT
  153. within the callback itself */
  154. uv_timer_start(&uv->timeout, on_uv_timeout, timeout_ms,
  155. 0); /* do not repeat */
  156. }
  157. return 0;
  158. }
  159. /* callback from libcurl to update socket activity to wait for */
  160. static int cb_socket(CURL *easy, curl_socket_t s, int action,
  161. struct datauv *uv,
  162. void *socketp)
  163. {
  164. curl_context_t *curl_context;
  165. int events = 0;
  166. (void)easy;
  167. switch(action) {
  168. case CURL_POLL_IN:
  169. case CURL_POLL_OUT:
  170. case CURL_POLL_INOUT:
  171. curl_context = socketp ?
  172. (curl_context_t *) socketp : create_curl_context(s, uv);
  173. curl_multi_assign(uv->multi, s, (void *) curl_context);
  174. if(action != CURL_POLL_IN)
  175. events |= UV_WRITABLE;
  176. if(action != CURL_POLL_OUT)
  177. events |= UV_READABLE;
  178. uv_poll_start(&curl_context->poll_handle, events, on_uv_socket);
  179. break;
  180. case CURL_POLL_REMOVE:
  181. if(socketp) {
  182. uv_poll_stop(&((curl_context_t*)socketp)->poll_handle);
  183. destroy_curl_context((curl_context_t*) socketp);
  184. curl_multi_assign(uv->multi, s, NULL);
  185. }
  186. break;
  187. default:
  188. abort();
  189. }
  190. return 0;
  191. }
  192. int main(int argc, char **argv)
  193. {
  194. struct datauv uv = { 0 };
  195. int running_handles;
  196. if(argc <= 1)
  197. return 0;
  198. curl_global_init(CURL_GLOBAL_ALL);
  199. uv.loop = uv_default_loop();
  200. uv_timer_init(uv.loop, &uv.timeout);
  201. uv.multi = curl_multi_init();
  202. curl_multi_setopt(uv.multi, CURLMOPT_SOCKETFUNCTION, cb_socket);
  203. curl_multi_setopt(uv.multi, CURLMOPT_SOCKETDATA, &uv);
  204. curl_multi_setopt(uv.multi, CURLMOPT_TIMERFUNCTION, cb_timeout);
  205. curl_multi_setopt(uv.multi, CURLMOPT_TIMERDATA, &uv);
  206. while(argc-- > 1) {
  207. add_download(argv[argc], argc, uv.multi);
  208. }
  209. /* kickstart the thing */
  210. curl_multi_socket_action(uv.multi, CURL_SOCKET_TIMEOUT, 0, &running_handles);
  211. uv_run(uv.loop, UV_RUN_DEFAULT);
  212. curl_multi_cleanup(uv.multi);
  213. return 0;
  214. }