summaryrefslogtreecommitdiff
path: root/docs/examples/http2-download.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/http2-download.c
parent738fb63e612ae9f5b19242479ea94eda4a813f24 (diff)
downloadcurl-ae8e11ed5fd2ceefc17f007d90f221f67f0c2e26.tar.gz
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/http2-download.c')
-rw-r--r--docs/examples/http2-download.c79
1 files changed, 8 insertions, 71 deletions
diff --git a/docs/examples/http2-download.c b/docs/examples/http2-download.c
index 1d95b5ce4..6ba82f35b 100644
--- a/docs/examples/http2-download.c
+++ b/docs/examples/http2-download.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
@@ -212,79 +212,16 @@ int main(int argc, char **argv)
curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
- /* we start some action by calling perform right away */
- curl_multi_perform(multi_handle, &still_running);
+ do {
+ CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
- while(still_running) {
- struct timeval timeout;
- int rc; /* select() return code */
- CURLMcode mc; /* curl_multi_fdset() return code */
+ if(still_running)
+ /* wait for activity, timeout or "nothing" */
+ mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
- fd_set fdread;
- fd_set fdwrite;
- fd_set fdexcep;
- int maxfd = -1;
-
- long curl_timeo = -1;
-
- 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(multi_handle, &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(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
-
- if(mc != CURLM_OK) {
- fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
+ if(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);
- }
-
- switch(rc) {
- case -1:
- /* select error */
- break;
- case 0:
- default:
- /* timeout or readable/writable sockets */
- curl_multi_perform(multi_handle, &still_running);
- break;
- }
- }
+ } while(still_running);
for(i = 0; i < num_transfers; i++) {
curl_multi_remove_handle(multi_handle, trans[i].easy);