pop3-multi.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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. * POP3 example using the multi interface
  24. * </DESC>
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <curl/curl.h>
  29. /* This is a simple example showing how to retrieve mail using libcurl's POP3
  30. * capabilities. It builds on the pop3-retr.c example to demonstrate how to use
  31. * libcurl's multi interface.
  32. *
  33. * Note that this example requires libcurl 7.20.0 or above.
  34. */
  35. #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
  36. static struct timeval tvnow(void)
  37. {
  38. struct timeval now;
  39. /* time() returns the value of time in seconds since the epoch */
  40. now.tv_sec = (long)time(NULL);
  41. now.tv_usec = 0;
  42. return now;
  43. }
  44. static long tvdiff(struct timeval newer, struct timeval older)
  45. {
  46. return (newer.tv_sec - older.tv_sec) * 1000 +
  47. (newer.tv_usec - older.tv_usec) / 1000;
  48. }
  49. int main(void)
  50. {
  51. CURL *curl;
  52. CURLM *mcurl;
  53. int still_running = 1;
  54. struct timeval mp_start;
  55. curl_global_init(CURL_GLOBAL_DEFAULT);
  56. curl = curl_easy_init();
  57. if(!curl)
  58. return 1;
  59. mcurl = curl_multi_init();
  60. if(!mcurl)
  61. return 2;
  62. /* Set username and password */
  63. curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
  64. curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
  65. /* This will retrieve message 1 from the user's mailbox */
  66. curl_easy_setopt(curl, CURLOPT_URL, "pop3://pop.example.com/1");
  67. /* Tell the multi stack about our easy handle */
  68. curl_multi_add_handle(mcurl, curl);
  69. /* Record the start time which we can use later */
  70. mp_start = tvnow();
  71. /* We start some action by calling perform right away */
  72. curl_multi_perform(mcurl, &still_running);
  73. while(still_running) {
  74. struct timeval timeout;
  75. fd_set fdread;
  76. fd_set fdwrite;
  77. fd_set fdexcep;
  78. int maxfd = -1;
  79. int rc;
  80. CURLMcode mc; /* curl_multi_fdset() return code */
  81. long curl_timeo = -1;
  82. /* Initialise the file descriptors */
  83. FD_ZERO(&fdread);
  84. FD_ZERO(&fdwrite);
  85. FD_ZERO(&fdexcep);
  86. /* Set a suitable timeout to play around with */
  87. timeout.tv_sec = 1;
  88. timeout.tv_usec = 0;
  89. curl_multi_timeout(mcurl, &curl_timeo);
  90. if(curl_timeo >= 0) {
  91. timeout.tv_sec = curl_timeo / 1000;
  92. if(timeout.tv_sec > 1)
  93. timeout.tv_sec = 1;
  94. else
  95. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  96. }
  97. /* get file descriptors from the transfers */
  98. mc = curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
  99. if(mc != CURLM_OK) {
  100. fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
  101. break;
  102. }
  103. /* On success the value of maxfd is guaranteed to be >= -1. We call
  104. select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
  105. no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
  106. to sleep 100ms, which is the minimum suggested value in the
  107. curl_multi_fdset() doc. */
  108. if(maxfd == -1) {
  109. #ifdef _WIN32
  110. Sleep(100);
  111. rc = 0;
  112. #else
  113. /* Portable sleep for platforms other than Windows. */
  114. struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
  115. rc = select(0, NULL, NULL, NULL, &wait);
  116. #endif
  117. }
  118. else {
  119. /* Note that on some platforms 'timeout' may be modified by select().
  120. If you need access to the original value save a copy beforehand. */
  121. rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  122. }
  123. if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
  124. fprintf(stderr,
  125. "ABORTING: Since it seems that we would have run forever.\n");
  126. break;
  127. }
  128. switch(rc) {
  129. case -1: /* select error */
  130. break;
  131. case 0: /* timeout */
  132. default: /* action */
  133. curl_multi_perform(mcurl, &still_running);
  134. break;
  135. }
  136. }
  137. /* Always cleanup */
  138. curl_multi_remove_handle(mcurl, curl);
  139. curl_multi_cleanup(mcurl);
  140. curl_easy_cleanup(curl);
  141. curl_global_cleanup();
  142. return 0;
  143. }