2
0

multi-legacy.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. timeout.tv_sec = curl_timeo / 1000;
  84. if(timeout.tv_sec > 1)
  85. timeout.tv_sec = 1;
  86. else
  87. timeout.tv_usec = (int)(curl_timeo % 1000) * 1000;
  88. }
  89. /* get file descriptors from the transfers */
  90. mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  91. if(mc != CURLM_OK) {
  92. fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
  93. break;
  94. }
  95. /* On success the value of maxfd is guaranteed to be >= -1. We call
  96. select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
  97. no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
  98. to sleep 100ms, which is the minimum suggested value in the
  99. curl_multi_fdset() doc. */
  100. if(maxfd == -1) {
  101. #ifdef _WIN32
  102. Sleep(100);
  103. rc = 0;
  104. #else
  105. /* Portable sleep for platforms other than Windows. */
  106. struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
  107. rc = select(0, NULL, NULL, NULL, &wait);
  108. #endif
  109. }
  110. else {
  111. /* Note that on some platforms 'timeout' may be modified by select().
  112. If you need access to the original value save a copy beforehand. */
  113. rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  114. }
  115. switch(rc) {
  116. case -1:
  117. /* select error */
  118. break;
  119. case 0: /* timeout */
  120. default: /* action */
  121. curl_multi_perform(multi_handle, &still_running);
  122. break;
  123. }
  124. }
  125. /* See how the transfers went */
  126. /* !checksrc! disable EQUALSNULL 1 */
  127. while((msg = curl_multi_info_read(multi_handle, &msgs_left)) != NULL) {
  128. if(msg->msg == CURLMSG_DONE) {
  129. int idx;
  130. /* Find out which handle this message is about */
  131. for(idx = 0; idx < HANDLECOUNT; idx++) {
  132. int found = (msg->easy_handle == handles[idx]);
  133. if(found)
  134. break;
  135. }
  136. switch(idx) {
  137. case HTTP_HANDLE:
  138. printf("HTTP transfer completed with status %d\n", msg->data.result);
  139. break;
  140. case FTP_HANDLE:
  141. printf("FTP transfer completed with status %d\n", msg->data.result);
  142. break;
  143. }
  144. }
  145. }
  146. curl_multi_cleanup(multi_handle);
  147. /* Free the CURL handles */
  148. for(i = 0; i < HANDLECOUNT; i++)
  149. curl_easy_cleanup(handles[i]);
  150. return 0;
  151. }