2
0

multi-single.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. * using the multi interface to do a single download
  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. #ifdef _WIN32
  34. #define WAITMS(x) Sleep(x)
  35. #else
  36. /* Portable sleep for platforms other than Windows. */
  37. #define WAITMS(x) \
  38. struct timeval wait = { 0, (x) * 1000 }; \
  39. (void)select(0, NULL, NULL, NULL, &wait);
  40. #endif
  41. /*
  42. * Simply download a HTTP file.
  43. */
  44. int main(void)
  45. {
  46. CURL *http_handle;
  47. CURLM *multi_handle;
  48. int still_running = 0; /* keep number of running handles */
  49. int repeats = 0;
  50. curl_global_init(CURL_GLOBAL_DEFAULT);
  51. http_handle = curl_easy_init();
  52. /* set the options (I left out a few, you'll get the point anyway) */
  53. curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/");
  54. /* init a multi stack */
  55. multi_handle = curl_multi_init();
  56. /* add the individual transfers */
  57. curl_multi_add_handle(multi_handle, http_handle);
  58. /* we start some action by calling perform right away */
  59. curl_multi_perform(multi_handle, &still_running);
  60. while(still_running) {
  61. CURLMcode mc; /* curl_multi_wait() return code */
  62. int numfds;
  63. /* wait for activity, timeout or "nothing" */
  64. mc = curl_multi_wait(multi_handle, NULL, 0, 1000, &numfds);
  65. if(mc != CURLM_OK) {
  66. fprintf(stderr, "curl_multi_wait() failed, code %d.\n", mc);
  67. break;
  68. }
  69. /* 'numfds' being zero means either a timeout or no file descriptors to
  70. wait for. Try timeout on first occurrence, then assume no file
  71. descriptors and no file descriptors to wait for means wait for 100
  72. milliseconds. */
  73. if(!numfds) {
  74. repeats++; /* count number of repeated zero numfds */
  75. if(repeats > 1) {
  76. WAITMS(100); /* sleep 100 milliseconds */
  77. }
  78. }
  79. else
  80. repeats = 0;
  81. curl_multi_perform(multi_handle, &still_running);
  82. }
  83. curl_multi_remove_handle(multi_handle, http_handle);
  84. curl_easy_cleanup(http_handle);
  85. curl_multi_cleanup(multi_handle);
  86. curl_global_cleanup();
  87. return 0;
  88. }