From 9fc62a8dd0e940668c8ee6310fa9d4530cda744a Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 4 Mar 2002 10:15:44 +0000 Subject: multi interface using examples --- docs/examples/multi-single.c | 80 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 docs/examples/multi-single.c (limited to 'docs/examples/multi-single.c') diff --git a/docs/examples/multi-single.c b/docs/examples/multi-single.c new file mode 100644 index 000000000..d17e03396 --- /dev/null +++ b/docs/examples/multi-single.c @@ -0,0 +1,80 @@ +/* + * This is a very simple example using the multi interface. + */ + +#include +#include + +/* somewhat unix-specific */ +#include +#include + +/* To start with, we include the header from the lib directory. This should + later of course be moved to the proper include dir. */ +#include "../lib/multi.h" + +/* + * Simply download a HTTP file. + */ +int main(int argc, char **argv) +{ + CURL *http_handle; + CURLM *multi_handle; + + int still_running; /* keep number of running handles */ + + http_handle = curl_easy_init(); + + /* set the options (I left out a few, you'll get the point anyway) */ + curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.haxx.se/"); + + /* init a multi stack */ + multi_handle = curl_multi_init(); + + /* add the individual transfers */ + curl_multi_add_handle(multi_handle, http_handle); + + /* we start some action by calling perform right away */ + while(CURLM_CALL_MULTI_PERFORM == + curl_multi_perform(multi_handle, &still_running)); + + while(still_running) { + struct timeval timeout; + int rc; /* select() return code */ + + fd_set fdread; + fd_set fdwrite; + fd_set fdexcep; + int maxfd; + + 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; + + /* get file descriptors from the transfers */ + curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); + + 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; + } + } + + curl_multi_cleanup(multi_handle); + + curl_easy_cleanup(http_handle); + + return 0; +} -- cgit v1.2.1 From 28939dd45cf0f21c039af961120ee25f2bd391f9 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 19 Mar 2002 14:00:47 +0000 Subject: fixed include and added header --- docs/examples/multi-single.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'docs/examples/multi-single.c') diff --git a/docs/examples/multi-single.c b/docs/examples/multi-single.c index d17e03396..562886a10 100644 --- a/docs/examples/multi-single.c +++ b/docs/examples/multi-single.c @@ -1,4 +1,12 @@ -/* +/***************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * $Id$ + * * This is a very simple example using the multi interface. */ @@ -9,9 +17,8 @@ #include #include -/* To start with, we include the header from the lib directory. This should - later of course be moved to the proper include dir. */ -#include "../lib/multi.h" +/* curl stuff */ +#include /* * Simply download a HTTP file. -- cgit v1.2.1 From 16e0da2c4b5048129fdb04896dbb0ec86a927ae0 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 9 Jan 2003 11:42:07 +0000 Subject: call curl_multi_perform() correctly --- docs/examples/multi-single.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'docs/examples/multi-single.c') diff --git a/docs/examples/multi-single.c b/docs/examples/multi-single.c index 562886a10..1998d25db 100644 --- a/docs/examples/multi-single.c +++ b/docs/examples/multi-single.c @@ -74,7 +74,8 @@ int main(int argc, char **argv) case 0: default: /* timeout or readable/writable sockets */ - curl_multi_perform(multi_handle, &still_running); + while(CURLM_CALL_MULTI_PERFORM == + curl_multi_perform(multi_handle, &still_running)); break; } } -- cgit v1.2.1 From 9d99af532912a3c3dcd3b489c4073ddb439ddcf6 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 2 Apr 2004 06:40:31 +0000 Subject: if select returns -1, bail out of the loop --- docs/examples/multi-single.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'docs/examples/multi-single.c') diff --git a/docs/examples/multi-single.c b/docs/examples/multi-single.c index 1998d25db..b6ba0c5c0 100644 --- a/docs/examples/multi-single.c +++ b/docs/examples/multi-single.c @@ -70,6 +70,8 @@ int main(int argc, char **argv) switch(rc) { case -1: /* select error */ + still_running = 0; + printf("select() returns error, this is badness\n"); break; case 0: default: -- cgit v1.2.1 From 120394cc4585a0d79682a946c81c5084e1467249 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 24 May 2004 15:16:29 +0000 Subject: remove trailing whitespace --- docs/examples/multi-single.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'docs/examples/multi-single.c') diff --git a/docs/examples/multi-single.c b/docs/examples/multi-single.c index b6ba0c5c0..0ba932f7a 100644 --- a/docs/examples/multi-single.c +++ b/docs/examples/multi-single.c @@ -1,8 +1,8 @@ /***************************************************************************** - * _ _ ____ _ - * Project ___| | | | _ \| | - * / __| | | | |_) | | - * | (__| |_| | _ <| |___ + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * * $Id$ -- cgit v1.2.1 From f53347631eb4a5a075589e6fece43aced010a5bb Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 13 Oct 2006 14:01:19 +0000 Subject: Added comments about checking return code and the maxfd counter --- docs/examples/multi-single.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'docs/examples/multi-single.c') diff --git a/docs/examples/multi-single.c b/docs/examples/multi-single.c index 0ba932f7a..b23f3c9d4 100644 --- a/docs/examples/multi-single.c +++ b/docs/examples/multi-single.c @@ -65,6 +65,10 @@ int main(int argc, char **argv) /* get file descriptors from the transfers */ curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); + /* In a real-world program you OF COURSE check the return code of the + function calls, *and* you make sure that maxfd is bigger than -1 so + that the call to select() below makes sense! */ + rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); switch(rc) { -- cgit v1.2.1 From 2309b4e330b96bc2e1f8e36b6184015e59544037 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 24 Mar 2010 11:02:54 +0100 Subject: remove the CVSish $Id$ lines --- docs/examples/multi-single.c | 1 - 1 file changed, 1 deletion(-) (limited to 'docs/examples/multi-single.c') diff --git a/docs/examples/multi-single.c b/docs/examples/multi-single.c index b23f3c9d4..3e236f326 100644 --- a/docs/examples/multi-single.c +++ b/docs/examples/multi-single.c @@ -5,7 +5,6 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * $Id$ * * This is a very simple example using the multi interface. */ -- cgit v1.2.1 From d487ade72c5f31703ce097e8460e0225fad80348 Mon Sep 17 00:00:00 2001 From: Kamil Dudka Date: Sat, 24 Apr 2010 12:14:21 +0200 Subject: test536: do not fail with threaded DNS resolver Also tweaked comments in certain examples using curl_multi_fdset(). --- docs/examples/multi-single.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'docs/examples/multi-single.c') diff --git a/docs/examples/multi-single.c b/docs/examples/multi-single.c index 3e236f326..cba4f32cc 100644 --- a/docs/examples/multi-single.c +++ b/docs/examples/multi-single.c @@ -51,7 +51,7 @@ int main(int argc, char **argv) fd_set fdread; fd_set fdwrite; fd_set fdexcep; - int maxfd; + int maxfd = -1; FD_ZERO(&fdread); FD_ZERO(&fdwrite); @@ -65,8 +65,10 @@ int main(int argc, char **argv) curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); /* In a real-world program you OF COURSE check the return code of the - function calls, *and* you make sure that maxfd is bigger than -1 so - that the call to select() below makes sense! */ + function calls. On success, the value of maxfd is guaranteed to be + greater or equal than -1. We call select(maxfd + 1, ...), specially in + case of (maxfd == -1), we call select(0, ...), which is basically equal + to sleep. */ rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); -- cgit v1.2.1 From bc0699f226ec55fde58a823fb818d8f8106c8fbd Mon Sep 17 00:00:00 2001 From: Constantine Sapuntzakis Date: Wed, 14 Jul 2010 00:32:53 +0200 Subject: examples: add curl_multi_timeout Make the multi-interface using examples use curl_multi_timeout to properly educate users how to do things. --- docs/examples/multi-single.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'docs/examples/multi-single.c') diff --git a/docs/examples/multi-single.c b/docs/examples/multi-single.c index cba4f32cc..d3f9cfde0 100644 --- a/docs/examples/multi-single.c +++ b/docs/examples/multi-single.c @@ -53,6 +53,8 @@ int main(int argc, char **argv) fd_set fdexcep; int maxfd = -1; + long curl_timeo = -1; + FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fdexcep); @@ -61,6 +63,15 @@ int main(int argc, char **argv) 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 */ curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); -- cgit v1.2.1 From 5fb4279ec7199e291d9bf4c903f89395f60a3d31 Mon Sep 17 00:00:00 2001 From: Dirk Manske Date: Thu, 30 Sep 2010 11:33:33 +0200 Subject: multi & hiper examples: updates and cleanups all multi and hiper examples: * don't loop curl_multi_perform calls, that was <7.20.0 style, currently the exported multi functions will not return CURLM_CALL_MULTI_PERFORM all hiper examples: * renamed check_run_count to check_multi_info * don't compare current running handle count with previous value, this was the wrong way to check for finished requests, simply call curl_multi_info_read * it's also safe to call curl_multi_remove_handle inside the curl_multi_info_read loop. ghiper.c: * replaced curl_multi_socket (that function is marked as obsolete) calls with curl_multi_socket_action calls (as in hiperfifo.c and evhiperfifo.c) ghiper.c and evhiperfifo.c: * be smart as hiperfifo.c, don't do uncessary curl_multi_* calls in new_conn and main --- docs/examples/multi-single.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'docs/examples/multi-single.c') diff --git a/docs/examples/multi-single.c b/docs/examples/multi-single.c index d3f9cfde0..fdcb5d8a8 100644 --- a/docs/examples/multi-single.c +++ b/docs/examples/multi-single.c @@ -41,8 +41,7 @@ int main(int argc, char **argv) curl_multi_add_handle(multi_handle, http_handle); /* we start some action by calling perform right away */ - while(CURLM_CALL_MULTI_PERFORM == - curl_multi_perform(multi_handle, &still_running)); + curl_multi_perform(multi_handle, &still_running); while(still_running) { struct timeval timeout; @@ -92,8 +91,7 @@ int main(int argc, char **argv) case 0: default: /* timeout or readable/writable sockets */ - while(CURLM_CALL_MULTI_PERFORM == - curl_multi_perform(multi_handle, &still_running)); + curl_multi_perform(multi_handle, &still_running); break; } } -- cgit v1.2.1 From 18e7b52e8e95f7204d8ef7ff0f0ff0043afe45a9 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 5 Oct 2010 15:00:19 +0200 Subject: examples: use example.com in example URLs --- docs/examples/multi-single.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs/examples/multi-single.c') diff --git a/docs/examples/multi-single.c b/docs/examples/multi-single.c index fdcb5d8a8..f248afe76 100644 --- a/docs/examples/multi-single.c +++ b/docs/examples/multi-single.c @@ -32,7 +32,7 @@ int main(int argc, char **argv) http_handle = curl_easy_init(); /* set the options (I left out a few, you'll get the point anyway) */ - curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.haxx.se/"); + curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/"); /* init a multi stack */ multi_handle = curl_multi_init(); -- cgit v1.2.1 From 9583b4af9057c9e35ec3dd3270d4c4813b5f7aaa Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 17 Dec 2010 23:34:26 +0100 Subject: examples: fix compiler warnings --- docs/examples/multi-single.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs/examples/multi-single.c') diff --git a/docs/examples/multi-single.c b/docs/examples/multi-single.c index f248afe76..ca632e08c 100644 --- a/docs/examples/multi-single.c +++ b/docs/examples/multi-single.c @@ -22,7 +22,7 @@ /* * Simply download a HTTP file. */ -int main(int argc, char **argv) +int main(void) { CURL *http_handle; CURLM *multi_handle; -- cgit v1.2.1 From 1aeb635cdd296c16acb375a4a83a78f13166ccab Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 10 Mar 2011 11:48:02 +0100 Subject: sources: update source headers All C and H files now (should) feature the proper project curl source code header, which includes basic info, a copyright statement and some basic disclaimers. --- docs/examples/multi-single.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'docs/examples/multi-single.c') diff --git a/docs/examples/multi-single.c b/docs/examples/multi-single.c index ca632e08c..0d3542706 100644 --- a/docs/examples/multi-single.c +++ b/docs/examples/multi-single.c @@ -1,13 +1,25 @@ -/***************************************************************************** +/*************************************************************************** * _ _ ____ _ * Project ___| | | | _ \| | * / __| | | | |_) | | * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * + * Copyright (C) 1998 - 2011, Daniel Stenberg, , et al. * - * This is a very simple example using the multi interface. - */ + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at http://curl.haxx.se/docs/copyright.html. + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ***************************************************************************/ +/* This is a very simple example using the multi interface. */ #include #include -- cgit v1.2.1 From 4c070de4fb01b4fbf29f8c463ba96da97b36bd2f Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Mon, 23 Jul 2012 15:34:25 +0000 Subject: examples: use do/while loop for multi examples It's conceivable that after the first time curl_multi_perform returns, the outvalue still_running will be 0, but work will have been done. This is shown by a workload of small, purely file:// based URLs. Ensure that we always read pending messages off the multi handle by forcing the while loop to run at least once. --- docs/examples/multi-single.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'docs/examples/multi-single.c') diff --git a/docs/examples/multi-single.c b/docs/examples/multi-single.c index 0d3542706..aeda71419 100644 --- a/docs/examples/multi-single.c +++ b/docs/examples/multi-single.c @@ -55,7 +55,7 @@ int main(void) /* we start some action by calling perform right away */ curl_multi_perform(multi_handle, &still_running); - while(still_running) { + do { struct timeval timeout; int rc; /* select() return code */ @@ -106,7 +106,7 @@ int main(void) curl_multi_perform(multi_handle, &still_running); break; } - } + } while(still_running); curl_multi_cleanup(multi_handle); -- cgit v1.2.1