multi-event.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 libevent
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <event2/event.h>
  31. #include <curl/curl.h>
  32. static struct event_base *base;
  33. static CURLM *curl_handle;
  34. static struct event *timeout;
  35. typedef struct curl_context_s {
  36. struct event *event;
  37. curl_socket_t sockfd;
  38. } curl_context_t;
  39. static void curl_perform(int fd, short event, void *arg);
  40. static curl_context_t *create_curl_context(curl_socket_t sockfd)
  41. {
  42. curl_context_t *context;
  43. context = (curl_context_t *) malloc(sizeof(*context));
  44. context->sockfd = sockfd;
  45. context->event = event_new(base, sockfd, 0, curl_perform, context);
  46. return context;
  47. }
  48. static void destroy_curl_context(curl_context_t *context)
  49. {
  50. event_del(context->event);
  51. event_free(context->event);
  52. free(context);
  53. }
  54. static void add_download(const char *url, int num)
  55. {
  56. char filename[50];
  57. FILE *file;
  58. CURL *handle;
  59. snprintf(filename, 50, "%d.download", num);
  60. file = fopen(filename, "wb");
  61. if(!file) {
  62. fprintf(stderr, "Error opening %s\n", filename);
  63. return;
  64. }
  65. handle = curl_easy_init();
  66. curl_easy_setopt(handle, CURLOPT_WRITEDATA, file);
  67. curl_easy_setopt(handle, CURLOPT_PRIVATE, file);
  68. curl_easy_setopt(handle, CURLOPT_URL, url);
  69. curl_multi_add_handle(curl_handle, handle);
  70. fprintf(stderr, "Added download %s -> %s\n", url, filename);
  71. }
  72. static void check_multi_info(void)
  73. {
  74. char *done_url;
  75. CURLMsg *message;
  76. int pending;
  77. CURL *easy_handle;
  78. FILE *file;
  79. while((message = curl_multi_info_read(curl_handle, &pending))) {
  80. switch(message->msg) {
  81. case CURLMSG_DONE:
  82. /* Do not use message data after calling curl_multi_remove_handle() and
  83. curl_easy_cleanup(). As per curl_multi_info_read() docs:
  84. "WARNING: The data the returned pointer points to does not survive
  85. calling curl_multi_cleanup, curl_multi_remove_handle or
  86. curl_easy_cleanup." */
  87. easy_handle = message->easy_handle;
  88. curl_easy_getinfo(easy_handle, CURLINFO_EFFECTIVE_URL, &done_url);
  89. curl_easy_getinfo(easy_handle, CURLINFO_PRIVATE, &file);
  90. printf("%s DONE\n", done_url);
  91. curl_multi_remove_handle(curl_handle, easy_handle);
  92. curl_easy_cleanup(easy_handle);
  93. if(file) {
  94. fclose(file);
  95. }
  96. break;
  97. default:
  98. fprintf(stderr, "CURLMSG default\n");
  99. break;
  100. }
  101. }
  102. }
  103. static void curl_perform(int fd, short event, void *arg)
  104. {
  105. int running_handles;
  106. int flags = 0;
  107. curl_context_t *context;
  108. (void)fd;
  109. if(event & EV_READ)
  110. flags |= CURL_CSELECT_IN;
  111. if(event & EV_WRITE)
  112. flags |= CURL_CSELECT_OUT;
  113. context = (curl_context_t *) arg;
  114. curl_multi_socket_action(curl_handle, context->sockfd, flags,
  115. &running_handles);
  116. check_multi_info();
  117. }
  118. static void on_timeout(evutil_socket_t fd, short events, void *arg)
  119. {
  120. int running_handles;
  121. (void)fd;
  122. (void)events;
  123. (void)arg;
  124. curl_multi_socket_action(curl_handle, CURL_SOCKET_TIMEOUT, 0,
  125. &running_handles);
  126. check_multi_info();
  127. }
  128. static int start_timeout(CURLM *multi, long timeout_ms, void *userp)
  129. {
  130. (void)multi;
  131. (void)userp;
  132. if(timeout_ms < 0) {
  133. evtimer_del(timeout);
  134. }
  135. else {
  136. struct timeval tv;
  137. if(timeout_ms == 0)
  138. timeout_ms = 1; /* 0 means call socket_action asap */
  139. tv.tv_sec = timeout_ms / 1000;
  140. tv.tv_usec = (timeout_ms % 1000) * 1000;
  141. evtimer_del(timeout);
  142. evtimer_add(timeout, &tv);
  143. }
  144. return 0;
  145. }
  146. static int handle_socket(CURL *easy, curl_socket_t s, int action, void *userp,
  147. void *socketp)
  148. {
  149. curl_context_t *curl_context;
  150. int events = 0;
  151. (void)easy;
  152. (void)userp;
  153. switch(action) {
  154. case CURL_POLL_IN:
  155. case CURL_POLL_OUT:
  156. case CURL_POLL_INOUT:
  157. curl_context = socketp ?
  158. (curl_context_t *) socketp : create_curl_context(s);
  159. curl_multi_assign(curl_handle, s, (void *) curl_context);
  160. if(action != CURL_POLL_IN)
  161. events |= EV_WRITE;
  162. if(action != CURL_POLL_OUT)
  163. events |= EV_READ;
  164. events |= EV_PERSIST;
  165. event_del(curl_context->event);
  166. event_assign(curl_context->event, base, curl_context->sockfd,
  167. (short)events, curl_perform, curl_context);
  168. event_add(curl_context->event, NULL);
  169. break;
  170. case CURL_POLL_REMOVE:
  171. if(socketp) {
  172. event_del(((curl_context_t*) socketp)->event);
  173. destroy_curl_context((curl_context_t*) socketp);
  174. curl_multi_assign(curl_handle, s, NULL);
  175. }
  176. break;
  177. default:
  178. abort();
  179. }
  180. return 0;
  181. }
  182. int main(int argc, char **argv)
  183. {
  184. if(argc <= 1)
  185. return 0;
  186. if(curl_global_init(CURL_GLOBAL_ALL)) {
  187. fprintf(stderr, "Could not init curl\n");
  188. return 1;
  189. }
  190. base = event_base_new();
  191. timeout = evtimer_new(base, on_timeout, NULL);
  192. curl_handle = curl_multi_init();
  193. curl_multi_setopt(curl_handle, CURLMOPT_SOCKETFUNCTION, handle_socket);
  194. curl_multi_setopt(curl_handle, CURLMOPT_TIMERFUNCTION, start_timeout);
  195. while(argc-- > 1) {
  196. add_download(argv[argc], argc);
  197. }
  198. event_base_dispatch(base);
  199. curl_multi_cleanup(curl_handle);
  200. event_free(timeout);
  201. event_base_free(base);
  202. libevent_global_shutdown();
  203. curl_global_cleanup();
  204. return 0;
  205. }