multi-uv.c 5.9 KB

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