multi-event.c 6.0 KB

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