Imported Upstream version 7.53.1
[platform/upstream/curl.git] / docs / examples / multi-app.c
index a5f71c5..9a8ecfe 100644 (file)
@@ -5,11 +5,11 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
- * are also available at http://curl.haxx.se/docs/copyright.html.
+ * are also available at https://curl.haxx.se/docs/copyright.html.
  *
  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  * copies of the Software, and permit persons to whom the Software is
  * KIND, either express or implied.
  *
  ***************************************************************************/
-/* This is an example application source code using the multi interface. */
+/* <DESC>
+ * A basic application source code using the multi interface doing two
+ * transfers in parallel.
+ * </DESC>
+ */
 
 #include <stdio.h>
 #include <string.h>
@@ -51,8 +55,8 @@ int main(void)
   int msgs_left; /* how many messages are left */
 
   /* Allocate one CURL handle per transfer */
-  for (i=0; i<HANDLECOUNT; i++)
-      handles[i] = curl_easy_init();
+  for(i=0; i<HANDLECOUNT; i++)
+    handles[i] = curl_easy_init();
 
   /* set the options (I left out a few, you'll get the point anyway) */
   curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "http://example.com");
@@ -64,8 +68,8 @@ int main(void)
   multi_handle = curl_multi_init();
 
   /* add the individual transfers */
-  for (i=0; i<HANDLECOUNT; i++)
-      curl_multi_add_handle(multi_handle, handles[i]);
+  for(i=0; i<HANDLECOUNT; i++)
+    curl_multi_add_handle(multi_handle, handles[i]);
 
   /* we start some action by calling perform right away */
   curl_multi_perform(multi_handle, &still_running);
@@ -73,6 +77,7 @@ int main(void)
   do {
     struct timeval timeout;
     int rc; /* select() return code */
+    CURLMcode mc; /* curl_multi_fdset() return code */
 
     fd_set fdread;
     fd_set fdwrite;
@@ -99,15 +104,34 @@ int main(void)
     }
 
     /* get file descriptors from the transfers */
-    curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
+    mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
 
-    /* In a real-world program you OF COURSE check the return code of the
-       function calls.  On success, the value of maxfd is guaranteed to be
-       greater or equal than -1.  We call select(maxfd + 1, ...), specially in
-       case of (maxfd == -1), we call select(0, ...), which is basically equal
-       to sleep. */
+    if(mc != CURLM_OK) {
+      fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
+      break;
+    }
 
-    rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
+    /* On success the value of maxfd is guaranteed to be >= -1. We call
+       select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
+       no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
+       to sleep 100ms, which is the minimum suggested value in the
+       curl_multi_fdset() doc. */
+
+    if(maxfd == -1) {
+#ifdef _WIN32
+      Sleep(100);
+      rc = 0;
+#else
+      /* Portable sleep for platforms other than Windows. */
+      struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
+      rc = select(0, NULL, NULL, NULL, &wait);
+#endif
+    }
+    else {
+      /* Note that on some platforms 'timeout' may be modified by select().
+         If you need access to the original value save a copy beforehand. */
+      rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
+    }
 
     switch(rc) {
     case -1:
@@ -121,18 +145,18 @@ int main(void)
   } while(still_running);
 
   /* See how the transfers went */
-  while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
-    if (msg->msg == CURLMSG_DONE) {
+  while((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
+    if(msg->msg == CURLMSG_DONE) {
       int idx, found = 0;
 
       /* Find out which handle this message is about */
-      for (idx=0; idx<HANDLECOUNT; idx++) {
+      for(idx=0; idx<HANDLECOUNT; idx++) {
         found = (msg->easy_handle == handles[idx]);
         if(found)
           break;
       }
 
-      switch (idx) {
+      switch(idx) {
       case HTTP_HANDLE:
         printf("HTTP transfer completed with status %d\n", msg->data.result);
         break;
@@ -146,8 +170,8 @@ int main(void)
   curl_multi_cleanup(multi_handle);
 
   /* Free the CURL handles */
-  for (i=0; i<HANDLECOUNT; i++)
-      curl_easy_cleanup(handles[i]);
+  for(i=0; i<HANDLECOUNT; i++)
+    curl_easy_cleanup(handles[i]);
 
   return 0;
 }