| Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1 | /*************************************************************************** |
| Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 8 | * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al. |
| Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 9 | * |
| Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 http://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 | /* Example application source code using the multi interface to download many |
| Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 23 | * files, but with a capped maximum amount of simultaneous transfers. |
| 24 | * |
| 25 | * Written by Michael Wallner |
| 26 | */ |
| 27 | |
| 28 | #include <errno.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #ifndef WIN32 |
| 32 | # include <unistd.h> |
| 33 | #endif |
| 34 | #include <curl/multi.h> |
| 35 | |
| 36 | static const char *urls[] = { |
| 37 | "http://www.microsoft.com", |
| 38 | "http://www.opensource.org", |
| 39 | "http://www.google.com", |
| 40 | "http://www.yahoo.com", |
| 41 | "http://www.ibm.com", |
| 42 | "http://www.mysql.com", |
| 43 | "http://www.oracle.com", |
| 44 | "http://www.ripe.net", |
| 45 | "http://www.iana.org", |
| 46 | "http://www.amazon.com", |
| 47 | "http://www.netcraft.com", |
| 48 | "http://www.heise.de", |
| 49 | "http://www.chip.de", |
| 50 | "http://www.ca.com", |
| 51 | "http://www.cnet.com", |
| 52 | "http://www.news.com", |
| 53 | "http://www.cnn.com", |
| 54 | "http://www.wikipedia.org", |
| 55 | "http://www.dell.com", |
| 56 | "http://www.hp.com", |
| 57 | "http://www.cert.org", |
| 58 | "http://www.mit.edu", |
| 59 | "http://www.nist.gov", |
| 60 | "http://www.ebay.com", |
| 61 | "http://www.playstation.com", |
| 62 | "http://www.uefa.com", |
| 63 | "http://www.ieee.org", |
| 64 | "http://www.apple.com", |
| Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 65 | "http://www.symantec.com", |
| 66 | "http://www.zdnet.com", |
| 67 | "http://www.fujitsu.com", |
| 68 | "http://www.supermicro.com", |
| 69 | "http://www.hotmail.com", |
| 70 | "http://www.ecma.com", |
| 71 | "http://www.bbc.co.uk", |
| 72 | "http://news.google.com", |
| 73 | "http://www.foxnews.com", |
| 74 | "http://www.msn.com", |
| 75 | "http://www.wired.com", |
| 76 | "http://www.sky.com", |
| 77 | "http://www.usatoday.com", |
| 78 | "http://www.cbs.com", |
| 79 | "http://www.nbc.com", |
| 80 | "http://slashdot.org", |
| 81 | "http://www.bloglines.com", |
| 82 | "http://www.techweb.com", |
| 83 | "http://www.newslink.org", |
| 84 | "http://www.un.org", |
| 85 | }; |
| 86 | |
| 87 | #define MAX 10 /* number of simultaneous transfers */ |
| 88 | #define CNT sizeof(urls)/sizeof(char*) /* total number of transfers to do */ |
| 89 | |
| 90 | static size_t cb(char *d, size_t n, size_t l, void *p) |
| 91 | { |
| 92 | /* take care of the data here, ignored in this example */ |
| 93 | (void)d; |
| 94 | (void)p; |
| 95 | return n*l; |
| 96 | } |
| 97 | |
| 98 | static void init(CURLM *cm, int i) |
| 99 | { |
| 100 | CURL *eh = curl_easy_init(); |
| 101 | |
| 102 | curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, cb); |
| 103 | curl_easy_setopt(eh, CURLOPT_HEADER, 0L); |
| 104 | curl_easy_setopt(eh, CURLOPT_URL, urls[i]); |
| 105 | curl_easy_setopt(eh, CURLOPT_PRIVATE, urls[i]); |
| 106 | curl_easy_setopt(eh, CURLOPT_VERBOSE, 0L); |
| 107 | |
| 108 | curl_multi_add_handle(cm, eh); |
| 109 | } |
| 110 | |
| 111 | int main(void) |
| 112 | { |
| 113 | CURLM *cm; |
| 114 | CURLMsg *msg; |
| 115 | long L; |
| 116 | unsigned int C=0; |
| 117 | int M, Q, U = -1; |
| 118 | fd_set R, W, E; |
| 119 | struct timeval T; |
| 120 | |
| 121 | curl_global_init(CURL_GLOBAL_ALL); |
| 122 | |
| 123 | cm = curl_multi_init(); |
| 124 | |
| 125 | /* we can optionally limit the total amount of connections this multi handle |
| 126 | uses */ |
| 127 | curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)MAX); |
| 128 | |
| 129 | for (C = 0; C < MAX; ++C) { |
| 130 | init(cm, C); |
| 131 | } |
| 132 | |
| 133 | while (U) { |
| 134 | curl_multi_perform(cm, &U); |
| 135 | |
| 136 | if (U) { |
| 137 | FD_ZERO(&R); |
| 138 | FD_ZERO(&W); |
| 139 | FD_ZERO(&E); |
| 140 | |
| 141 | if (curl_multi_fdset(cm, &R, &W, &E, &M)) { |
| 142 | fprintf(stderr, "E: curl_multi_fdset\n"); |
| 143 | return EXIT_FAILURE; |
| 144 | } |
| 145 | |
| 146 | if (curl_multi_timeout(cm, &L)) { |
| 147 | fprintf(stderr, "E: curl_multi_timeout\n"); |
| 148 | return EXIT_FAILURE; |
| 149 | } |
| 150 | if (L == -1) |
| 151 | L = 100; |
| 152 | |
| 153 | if (M == -1) { |
| 154 | #ifdef WIN32 |
| 155 | Sleep(L); |
| 156 | #else |
| 157 | sleep(L / 1000); |
| 158 | #endif |
| 159 | } else { |
| 160 | T.tv_sec = L/1000; |
| 161 | T.tv_usec = (L%1000)*1000; |
| 162 | |
| 163 | if (0 > select(M+1, &R, &W, &E, &T)) { |
| 164 | fprintf(stderr, "E: select(%i,,,,%li): %i: %s\n", |
| 165 | M+1, L, errno, strerror(errno)); |
| 166 | return EXIT_FAILURE; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | while ((msg = curl_multi_info_read(cm, &Q))) { |
| 172 | if (msg->msg == CURLMSG_DONE) { |
| 173 | char *url; |
| 174 | CURL *e = msg->easy_handle; |
| 175 | curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url); |
| 176 | fprintf(stderr, "R: %d - %s <%s>\n", |
| 177 | msg->data.result, curl_easy_strerror(msg->data.result), url); |
| 178 | curl_multi_remove_handle(cm, e); |
| 179 | curl_easy_cleanup(e); |
| 180 | } |
| 181 | else { |
| 182 | fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg); |
| 183 | } |
| 184 | if (C < CNT) { |
| 185 | init(cm, C++); |
| 186 | U++; /* just to prevent it from remaining at 0 if there are more |
| 187 | URLs to get */ |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | curl_multi_cleanup(cm); |
| 193 | curl_global_cleanup(); |
| 194 | |
| 195 | return EXIT_SUCCESS; |
| 196 | } |