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