multi-legacy.c 5.2 KB

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