Annotation of embedaddon/curl/docs/examples/10-at-a-time.c, revision 1.1.1.1

1.1       misho       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:  * Download many files in parallel, in the same thread.
                     24:  * </DESC>
                     25:  */
                     26: 
                     27: #include <errno.h>
                     28: #include <stdlib.h>
                     29: #include <string.h>
                     30: #ifndef WIN32
                     31: #  include <unistd.h>
                     32: #endif
                     33: #include <curl/curl.h>
                     34: 
                     35: static const char *urls[] = {
                     36:   "https://www.microsoft.com",
                     37:   "https://opensource.org",
                     38:   "https://www.google.com",
                     39:   "https://www.yahoo.com",
                     40:   "https://www.ibm.com",
                     41:   "https://www.mysql.com",
                     42:   "https://www.oracle.com",
                     43:   "https://www.ripe.net",
                     44:   "https://www.iana.org",
                     45:   "https://www.amazon.com",
                     46:   "https://www.netcraft.com",
                     47:   "https://www.heise.de",
                     48:   "https://www.chip.de",
                     49:   "https://www.ca.com",
                     50:   "https://www.cnet.com",
                     51:   "https://www.mozilla.org",
                     52:   "https://www.cnn.com",
                     53:   "https://www.wikipedia.org",
                     54:   "https://www.dell.com",
                     55:   "https://www.hp.com",
                     56:   "https://www.cert.org",
                     57:   "https://www.mit.edu",
                     58:   "https://www.nist.gov",
                     59:   "https://www.ebay.com",
                     60:   "https://www.playstation.com",
                     61:   "https://www.uefa.com",
                     62:   "https://www.ieee.org",
                     63:   "https://www.apple.com",
                     64:   "https://www.symantec.com",
                     65:   "https://www.zdnet.com",
                     66:   "https://www.fujitsu.com/global/",
                     67:   "https://www.supermicro.com",
                     68:   "https://www.hotmail.com",
                     69:   "https://www.ietf.org",
                     70:   "https://www.bbc.co.uk",
                     71:   "https://news.google.com",
                     72:   "https://www.foxnews.com",
                     73:   "https://www.msn.com",
                     74:   "https://www.wired.com",
                     75:   "https://www.sky.com",
                     76:   "https://www.usatoday.com",
                     77:   "https://www.cbs.com",
                     78:   "https://www.nbc.com/",
                     79:   "https://slashdot.org",
                     80:   "https://www.informationweek.com",
                     81:   "https://apache.org",
                     82:   "https://www.un.org",
                     83: };
                     84: 
                     85: #define MAX_PARALLEL 10 /* number of simultaneous transfers */
                     86: #define NUM_URLS sizeof(urls)/sizeof(char *)
                     87: 
                     88: static size_t write_cb(char *data, size_t n, size_t l, void *userp)
                     89: {
                     90:   /* take care of the data here, ignored in this example */
                     91:   (void)data;
                     92:   (void)userp;
                     93:   return n*l;
                     94: }
                     95: 
                     96: static void add_transfer(CURLM *cm, int i)
                     97: {
                     98:   CURL *eh = curl_easy_init();
                     99:   curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, write_cb);
                    100:   curl_easy_setopt(eh, CURLOPT_URL, urls[i]);
                    101:   curl_easy_setopt(eh, CURLOPT_PRIVATE, urls[i]);
                    102:   curl_multi_add_handle(cm, eh);
                    103: }
                    104: 
                    105: int main(void)
                    106: {
                    107:   CURLM *cm;
                    108:   CURLMsg *msg;
                    109:   unsigned int transfers = 0;
                    110:   int msgs_left = -1;
                    111:   int still_alive = 1;
                    112: 
                    113:   curl_global_init(CURL_GLOBAL_ALL);
                    114:   cm = curl_multi_init();
                    115: 
                    116:   /* Limit the amount of simultaneous connections curl should allow: */
                    117:   curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)MAX_PARALLEL);
                    118: 
                    119:   for(transfers = 0; transfers < MAX_PARALLEL; transfers++)
                    120:     add_transfer(cm, transfers);
                    121: 
                    122:   do {
                    123:     curl_multi_perform(cm, &still_alive);
                    124: 
                    125:     while((msg = curl_multi_info_read(cm, &msgs_left))) {
                    126:       if(msg->msg == CURLMSG_DONE) {
                    127:         char *url;
                    128:         CURL *e = msg->easy_handle;
                    129:         curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url);
                    130:         fprintf(stderr, "R: %d - %s <%s>\n",
                    131:                 msg->data.result, curl_easy_strerror(msg->data.result), url);
                    132:         curl_multi_remove_handle(cm, e);
                    133:         curl_easy_cleanup(e);
                    134:       }
                    135:       else {
                    136:         fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
                    137:       }
                    138:       if(transfers < NUM_URLS)
                    139:         add_transfer(cm, transfers++);
                    140:     }
                    141:     if(still_alive)
                    142:       curl_multi_wait(cm, NULL, 0, 1000, NULL);
                    143: 
                    144:   } while(still_alive || (transfers < NUM_URLS));
                    145: 
                    146:   curl_multi_cleanup(cm);
                    147:   curl_global_cleanup();
                    148: 
                    149:   return EXIT_SUCCESS;
                    150: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>