multi-app.c 5.1 KB

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