multi-app.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * A basic application source code using the multi interface doing two
00024  * transfers in parallel.
00025  * </DESC>
00026  */
00027 
00028 #include <stdio.h>
00029 #include <string.h>
00030 
00031 /* somewhat unix-specific */
00032 #include <sys/time.h>
00033 #include <unistd.h>
00034 
00035 /* curl stuff */
00036 #include <curl/curl.h>
00037 
00038 /*
00039  * Download a HTTP file and upload an FTP file simultaneously.
00040  */
00041 
00042 #define HANDLECOUNT 2   /* Number of simultaneous transfers */
00043 #define HTTP_HANDLE 0   /* Index for the HTTP transfer */
00044 #define FTP_HANDLE 1    /* Index for the FTP transfer */
00045 
00046 int main(void)
00047 {
00048   CURL *handles[HANDLECOUNT];
00049   CURLM *multi_handle;
00050 
00051   int still_running; /* keep number of running handles */
00052   int i;
00053 
00054   CURLMsg *msg; /* for picking up messages with the transfer status */
00055   int msgs_left; /* how many messages are left */
00056 
00057   /* Allocate one CURL handle per transfer */
00058   for(i=0; i<HANDLECOUNT; i++)
00059     handles[i] = curl_easy_init();
00060 
00061   /* set the options (I left out a few, you'll get the point anyway) */
00062   curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "http://example.com");
00063 
00064   curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://example.com");
00065   curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
00066 
00067   /* init a multi stack */
00068   multi_handle = curl_multi_init();
00069 
00070   /* add the individual transfers */
00071   for(i=0; i<HANDLECOUNT; i++)
00072     curl_multi_add_handle(multi_handle, handles[i]);
00073 
00074   /* we start some action by calling perform right away */
00075   curl_multi_perform(multi_handle, &still_running);
00076 
00077   do {
00078     struct timeval timeout;
00079     int rc; /* select() return code */
00080     CURLMcode mc; /* curl_multi_fdset() return code */
00081 
00082     fd_set fdread;
00083     fd_set fdwrite;
00084     fd_set fdexcep;
00085     int maxfd = -1;
00086 
00087     long curl_timeo = -1;
00088 
00089     FD_ZERO(&fdread);
00090     FD_ZERO(&fdwrite);
00091     FD_ZERO(&fdexcep);
00092 
00093     /* set a suitable timeout to play around with */
00094     timeout.tv_sec = 1;
00095     timeout.tv_usec = 0;
00096 
00097     curl_multi_timeout(multi_handle, &curl_timeo);
00098     if(curl_timeo >= 0) {
00099       timeout.tv_sec = curl_timeo / 1000;
00100       if(timeout.tv_sec > 1)
00101         timeout.tv_sec = 1;
00102       else
00103         timeout.tv_usec = (curl_timeo % 1000) * 1000;
00104     }
00105 
00106     /* get file descriptors from the transfers */
00107     mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
00108 
00109     if(mc != CURLM_OK) {
00110       fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
00111       break;
00112     }
00113 
00114     /* On success the value of maxfd is guaranteed to be >= -1. We call
00115        select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
00116        no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
00117        to sleep 100ms, which is the minimum suggested value in the
00118        curl_multi_fdset() doc. */
00119 
00120     if(maxfd == -1) {
00121 #ifdef _WIN32
00122       Sleep(100);
00123       rc = 0;
00124 #else
00125       /* Portable sleep for platforms other than Windows. */
00126       struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
00127       rc = select(0, NULL, NULL, NULL, &wait);
00128 #endif
00129     }
00130     else {
00131       /* Note that on some platforms 'timeout' may be modified by select().
00132          If you need access to the original value save a copy beforehand. */
00133       rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
00134     }
00135 
00136     switch(rc) {
00137     case -1:
00138       /* select error */
00139       break;
00140     case 0: /* timeout */
00141     default: /* action */
00142       curl_multi_perform(multi_handle, &still_running);
00143       break;
00144     }
00145   } while(still_running);
00146 
00147   /* See how the transfers went */
00148   while((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
00149     if(msg->msg == CURLMSG_DONE) {
00150       int idx, found = 0;
00151 
00152       /* Find out which handle this message is about */
00153       for(idx=0; idx<HANDLECOUNT; idx++) {
00154         found = (msg->easy_handle == handles[idx]);
00155         if(found)
00156           break;
00157       }
00158 
00159       switch(idx) {
00160       case HTTP_HANDLE:
00161         printf("HTTP transfer completed with status %d\n", msg->data.result);
00162         break;
00163       case FTP_HANDLE:
00164         printf("FTP transfer completed with status %d\n", msg->data.result);
00165         break;
00166       }
00167     }
00168   }
00169 
00170   curl_multi_cleanup(multi_handle);
00171 
00172   /* Free the CURL handles */
00173   for(i=0; i<HANDLECOUNT; i++)
00174     curl_easy_cleanup(handles[i]);
00175 
00176   return 0;
00177 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:05