summaryrefslogtreecommitdiff
path: root/docs/examples/imap-multi.c
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2021-07-06 10:51:46 +0200
committerDaniel Stenberg <daniel@haxx.se>2021-07-07 08:15:09 +0200
commitae8e11ed5fd2ceefc17f007d90f221f67f0c2e26 (patch)
tree4a24c62fb66f86d9469d082bdbd4978b12720b44 /docs/examples/imap-multi.c
parent738fb63e612ae9f5b19242479ea94eda4a813f24 (diff)
docs/examples: use curl_multi_poll() in multi examples
The API is soon two years old and deserves being shown as the primary way to drive multi code as it makes it much easier to write code. multi-poll: removed multi-legacy: add to show how we did multi API use before curl_multi_wait/poll. Closes #7352
Diffstat (limited to 'docs/examples/imap-multi.c')
-rw-r--r--docs/examples/imap-multi.c108
1 files changed, 8 insertions, 100 deletions
diff --git a/docs/examples/imap-multi.c b/docs/examples/imap-multi.c
index 4283e1a92..d4ad95828 100644
--- a/docs/examples/imap-multi.c
+++ b/docs/examples/imap-multi.c
@@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@@ -32,35 +32,13 @@
/* This is a simple example showing how to fetch mail using libcurl's IMAP
* capabilities. It builds on the imap-fetch.c example to demonstrate how to
* use libcurl's multi interface.
- *
- * Note that this example requires libcurl 7.30.0 or above.
*/
-#define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
-
-static struct timeval tvnow(void)
-{
- struct timeval now;
-
- /* time() returns the value of time in seconds since the epoch */
- now.tv_sec = (long)time(NULL);
- now.tv_usec = 0;
-
- return now;
-}
-
-static long tvdiff(struct timeval newer, struct timeval older)
-{
- return (newer.tv_sec - older.tv_sec) * 1000 +
- (newer.tv_usec - older.tv_usec) / 1000;
-}
-
int main(void)
{
CURL *curl;
CURLM *mcurl;
int still_running = 1;
- struct timeval mp_start;
curl_global_init(CURL_GLOBAL_DEFAULT);
@@ -82,86 +60,16 @@ int main(void)
/* Tell the multi stack about our easy handle */
curl_multi_add_handle(mcurl, curl);
- /* Record the start time which we can use later */
- mp_start = tvnow();
-
- /* We start some action by calling perform right away */
- curl_multi_perform(mcurl, &still_running);
-
- while(still_running) {
- struct timeval timeout;
- fd_set fdread;
- fd_set fdwrite;
- fd_set fdexcep;
- int maxfd = -1;
- int rc;
- CURLMcode mc; /* curl_multi_fdset() return code */
+ do {
+ CURLMcode mc = curl_multi_perform(mcurl, &still_running);
- long curl_timeo = -1;
+ if(still_running)
+ /* wait for activity, timeout or "nothing" */
+ mc = curl_multi_poll(mcurl, NULL, 0, 1000, NULL);
- /* Initialise the file descriptors */
- FD_ZERO(&fdread);
- FD_ZERO(&fdwrite);
- FD_ZERO(&fdexcep);
-
- /* Set a suitable timeout to play around with */
- timeout.tv_sec = 1;
- timeout.tv_usec = 0;
-
- curl_multi_timeout(mcurl, &curl_timeo);
- if(curl_timeo >= 0) {
- timeout.tv_sec = curl_timeo / 1000;
- if(timeout.tv_sec > 1)
- timeout.tv_sec = 1;
- else
- timeout.tv_usec = (curl_timeo % 1000) * 1000;
- }
-
- /* get file descriptors from the transfers */
- mc = curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
-
- if(mc != CURLM_OK) {
- fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
- break;
- }
-
- /* On success the value of maxfd is guaranteed to be >= -1. We call
- select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
- no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
- to sleep 100ms, which is the minimum suggested value in the
- curl_multi_fdset() doc. */
-
- if(maxfd == -1) {
-#ifdef _WIN32
- Sleep(100);
- rc = 0;
-#else
- /* Portable sleep for platforms other than Windows. */
- struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
- rc = select(0, NULL, NULL, NULL, &wait);
-#endif
- }
- else {
- /* Note that on some platforms 'timeout' may be modified by select().
- If you need access to the original value save a copy beforehand. */
- rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
- }
-
- if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
- fprintf(stderr,
- "ABORTING: Since it seems that we would have run forever.\n");
- break;
- }
-
- switch(rc) {
- case -1: /* select error */
- break;
- case 0: /* timeout */
- default: /* action */
- curl_multi_perform(mcurl, &still_running);
+ if(mc)
break;
- }
- }
+ } while(still_running);
/* Always cleanup */
curl_multi_remove_handle(mcurl, curl);