multi-legacy.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. /* <DESC>
  25. * A basic application source code using the multi interface doing two
  26. * transfers in parallel without curl_multi_wait/poll.
  27. * </DESC>
  28. */
  29. #include <stdio.h>
  30. #include <string.h>
  31. /* somewhat Unix-specific */
  32. #ifndef _WIN32
  33. #include <sys/time.h>
  34. #include <unistd.h>
  35. #endif
  36. /* curl stuff */
  37. #include <curl/curl.h>
  38. /*
  39. * Download an HTTP file and upload an FTP file simultaneously.
  40. */
  41. #define HANDLECOUNT 2 /* Number of simultaneous transfers */
  42. #define HTTP_HANDLE 0 /* Index for the HTTP transfer */
  43. #define FTP_HANDLE 1 /* Index for the FTP transfer */
  44. int main(void)
  45. {
  46. CURL *handles[HANDLECOUNT];
  47. CURLM *multi_handle;
  48. int still_running = 0; /* keep number of running handles */
  49. int i;
  50. CURLMsg *msg; /* for picking up messages with the transfer status */
  51. int msgs_left; /* how many messages are left */
  52. /* Allocate one CURL handle per transfer */
  53. for(i = 0; i < HANDLECOUNT; i++)
  54. handles[i] = curl_easy_init();
  55. /* set the options (I left out a few, you get the point anyway) */
  56. curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "https://example.com");
  57. curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://example.com");
  58. curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
  59. /* init a multi stack */
  60. multi_handle = curl_multi_init();
  61. /* add the individual transfers */
  62. for(i = 0; i < HANDLECOUNT; i++)
  63. curl_multi_add_handle(multi_handle, handles[i]);
  64. /* we start some action by calling perform right away */
  65. curl_multi_perform(multi_handle, &still_running);
  66. while(still_running) {
  67. struct timeval timeout;
  68. int rc; /* select() return code */
  69. CURLMcode mc; /* curl_multi_fdset() return code */
  70. fd_set fdread;
  71. fd_set fdwrite;
  72. fd_set fdexcep;
  73. int maxfd = -1;
  74. long curl_timeo = -1;
  75. FD_ZERO(&fdread);
  76. FD_ZERO(&fdwrite);
  77. FD_ZERO(&fdexcep);
  78. /* set a suitable timeout to play around with */
  79. timeout.tv_sec = 1;
  80. timeout.tv_usec = 0;
  81. curl_multi_timeout(multi_handle, &curl_timeo);
  82. if(curl_timeo >= 0) {
  83. #if defined(MSDOS) || defined(__AMIGA__)
  84. timeout.tv_sec = (time_t)(curl_timeo / 1000);
  85. #else
  86. timeout.tv_sec = curl_timeo / 1000;
  87. #endif
  88. if(timeout.tv_sec > 1)
  89. timeout.tv_sec = 1;
  90. else
  91. #if defined(MSDOS) || defined(__AMIGA__)
  92. timeout.tv_usec = (time_t)(curl_timeo % 1000) * 1000;
  93. #else
  94. timeout.tv_usec = (int)(curl_timeo % 1000) * 1000;
  95. #endif
  96. }
  97. /* get file descriptors from the transfers */
  98. mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  99. if(mc != CURLM_OK) {
  100. fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
  101. break;
  102. }
  103. /* On success the value of maxfd is guaranteed to be >= -1. We call
  104. select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
  105. no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
  106. to sleep 100ms, which is the minimum suggested value in the
  107. curl_multi_fdset() doc. */
  108. if(maxfd == -1) {
  109. #ifdef _WIN32
  110. Sleep(100);
  111. rc = 0;
  112. #else
  113. /* Portable sleep for platforms other than Windows. */
  114. struct timeval wait = {0};
  115. wait.tv_usec = 100 * 1000; /* 100ms */
  116. rc = select(0, NULL, NULL, NULL, &wait);
  117. #endif
  118. }
  119. else {
  120. /* Note that on some platforms 'timeout' may be modified by select().
  121. If you need access to the original value save a copy beforehand. */
  122. rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  123. }
  124. switch(rc) {
  125. case -1:
  126. /* select error */
  127. break;
  128. case 0: /* timeout */
  129. default: /* action */
  130. curl_multi_perform(multi_handle, &still_running);
  131. break;
  132. }
  133. }
  134. /* See how the transfers went */
  135. /* !checksrc! disable EQUALSNULL 1 */
  136. while((msg = curl_multi_info_read(multi_handle, &msgs_left)) != NULL) {
  137. if(msg->msg == CURLMSG_DONE) {
  138. int idx;
  139. /* Find out which handle this message is about */
  140. for(idx = 0; idx < HANDLECOUNT; idx++) {
  141. int found = (msg->easy_handle == handles[idx]);
  142. if(found)
  143. break;
  144. }
  145. switch(idx) {
  146. case HTTP_HANDLE:
  147. printf("HTTP transfer completed with status %d\n", msg->data.result);
  148. break;
  149. case FTP_HANDLE:
  150. printf("FTP transfer completed with status %d\n", msg->data.result);
  151. break;
  152. }
  153. }
  154. }
  155. curl_multi_cleanup(multi_handle);
  156. /* Free the CURL handles */
  157. for(i = 0; i < HANDLECOUNT; i++)
  158. curl_easy_cleanup(handles[i]);
  159. return 0;
  160. }