multi-double.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * multi interface code doing two parallel HTTP transfers
  24. * </DESC>
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. /* somewhat unix-specific */
  29. #include <sys/time.h>
  30. #include <unistd.h>
  31. /* curl stuff */
  32. #include <curl/curl.h>
  33. /*
  34. * Simply download two HTTP files!
  35. */
  36. int main(void)
  37. {
  38. CURL *http_handle;
  39. CURL *http_handle2;
  40. CURLM *multi_handle;
  41. int still_running = 0; /* keep number of running handles */
  42. http_handle = curl_easy_init();
  43. http_handle2 = curl_easy_init();
  44. /* set options */
  45. curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/");
  46. /* set options */
  47. curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/");
  48. /* init a multi stack */
  49. multi_handle = curl_multi_init();
  50. /* add the individual transfers */
  51. curl_multi_add_handle(multi_handle, http_handle);
  52. curl_multi_add_handle(multi_handle, http_handle2);
  53. /* we start some action by calling perform right away */
  54. curl_multi_perform(multi_handle, &still_running);
  55. while(still_running) {
  56. struct timeval timeout;
  57. int rc; /* select() return code */
  58. CURLMcode mc; /* curl_multi_fdset() return code */
  59. fd_set fdread;
  60. fd_set fdwrite;
  61. fd_set fdexcep;
  62. int maxfd = -1;
  63. long curl_timeo = -1;
  64. FD_ZERO(&fdread);
  65. FD_ZERO(&fdwrite);
  66. FD_ZERO(&fdexcep);
  67. /* set a suitable timeout to play around with */
  68. timeout.tv_sec = 1;
  69. timeout.tv_usec = 0;
  70. curl_multi_timeout(multi_handle, &curl_timeo);
  71. if(curl_timeo >= 0) {
  72. timeout.tv_sec = curl_timeo / 1000;
  73. if(timeout.tv_sec > 1)
  74. timeout.tv_sec = 1;
  75. else
  76. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  77. }
  78. /* get file descriptors from the transfers */
  79. mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  80. if(mc != CURLM_OK) {
  81. fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
  82. break;
  83. }
  84. /* On success the value of maxfd is guaranteed to be >= -1. We call
  85. select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
  86. no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
  87. to sleep 100ms, which is the minimum suggested value in the
  88. curl_multi_fdset() doc. */
  89. if(maxfd == -1) {
  90. #ifdef _WIN32
  91. Sleep(100);
  92. rc = 0;
  93. #else
  94. /* Portable sleep for platforms other than Windows. */
  95. struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
  96. rc = select(0, NULL, NULL, NULL, &wait);
  97. #endif
  98. }
  99. else {
  100. /* Note that on some platforms 'timeout' may be modified by select().
  101. If you need access to the original value save a copy beforehand. */
  102. rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  103. }
  104. switch(rc) {
  105. case -1:
  106. /* select error */
  107. break;
  108. case 0:
  109. default:
  110. /* timeout or readable/writable sockets */
  111. curl_multi_perform(multi_handle, &still_running);
  112. break;
  113. }
  114. }
  115. curl_multi_cleanup(multi_handle);
  116. curl_easy_cleanup(http_handle);
  117. curl_easy_cleanup(http_handle2);
  118. return 0;
  119. }