multi-double.c
Go to the documentation of this file.
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  * multi interface code doing two parallel HTTP transfers
24  * </DESC>
25  */
26 #include <stdio.h>
27 #include <string.h>
28 
29 /* somewhat unix-specific */
30 #include <sys/time.h>
31 #include <unistd.h>
32 
33 /* curl stuff */
34 #include <curl/curl.h>
35 
36 /*
37  * Simply download two HTTP files!
38  */
39 int main(void)
40 {
41  CURL *http_handle;
42  CURL *http_handle2;
44 
45  int still_running; /* keep number of running handles */
46 
47  http_handle = curl_easy_init();
48  http_handle2 = curl_easy_init();
49 
50  /* set options */
51  curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
52 
53  /* set options */
54  curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/");
55 
56  /* init a multi stack */
57  multi_handle = curl_multi_init();
58 
59  /* add the individual transfers */
60  curl_multi_add_handle(multi_handle, http_handle);
61  curl_multi_add_handle(multi_handle, http_handle2);
62 
63  /* we start some action by calling perform right away */
64  curl_multi_perform(multi_handle, &still_running);
65 
66  do {
67  struct timeval timeout;
68  int rc; /* select() return code */
69  CURLMcode mc; /* curl_multi_fdset() return code */
70 
71  fd_set fdread;
72  fd_set fdwrite;
73  fd_set fdexcep;
74  int maxfd = -1;
75 
76  long curl_timeo = -1;
77 
78  FD_ZERO(&fdread);
79  FD_ZERO(&fdwrite);
80  FD_ZERO(&fdexcep);
81 
82  /* set a suitable timeout to play around with */
83  timeout.tv_sec = 1;
84  timeout.tv_usec = 0;
85 
86  curl_multi_timeout(multi_handle, &curl_timeo);
87  if(curl_timeo >= 0) {
88  timeout.tv_sec = curl_timeo / 1000;
89  if(timeout.tv_sec > 1)
90  timeout.tv_sec = 1;
91  else
92  timeout.tv_usec = (curl_timeo % 1000) * 1000;
93  }
94 
95  /* get file descriptors from the transfers */
96  mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
97 
98  if(mc != CURLM_OK) {
99  fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
100  break;
101  }
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 
109  if(maxfd == -1) {
110 #ifdef _WIN32
111  Sleep(100);
112  rc = 0;
113 #else
114  /* Portable sleep for platforms other than Windows. */
115  struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
116  rc = select(0, NULL, NULL, NULL, &wait);
117 #endif
118  }
119  else {
120  /* Note that on some platforms 'timeout' may be modified by select().
121  If you need access to the original value save a copy beforehand. */
122  rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
123  }
124 
125  switch(rc) {
126  case -1:
127  /* select error */
128  break;
129  case 0:
130  default:
131  /* timeout or readable/writable sockets */
132  curl_multi_perform(multi_handle, &still_running);
133  break;
134  }
135  } while(still_running);
136 
137  curl_multi_cleanup(multi_handle);
138 
139  curl_easy_cleanup(http_handle);
140  curl_easy_cleanup(http_handle2);
141 
142  return 0;
143 }
CURL_EXTERN CURLMcode curl_multi_fdset(CURLM *multi_handle, fd_set *read_fd_set, fd_set *write_fd_set, fd_set *exc_fd_set, int *max_fd)
CURL_EXTERN CURLMcode curl_multi_add_handle(CURLM *multi_handle, CURL *curl_handle)
CURL_EXTERN CURLMcode curl_multi_perform(CURLM *multi_handle, int *running_handles)
CURL_EXTERN CURLMcode curl_multi_timeout(CURLM *multi_handle, long *milliseconds)
#define curl_easy_setopt(handle, option, value)
Definition: typecheck-gcc.h:41
CURL_EXTERN CURLM * curl_multi_init(void)
Definition: multi.c:355
static CURLM * multi_handle
Definition: fopen.c:91
UNITTEST_START int rc
Definition: unit1301.c:31
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
CURLMcode
Definition: multi.h:61
void CURLM
Definition: multi.h:58
void CURL
Definition: curl.h:102
Definition: multi.h:64
#define fprintf
Definition: curl_printf.h:41
int main(void)
Definition: multi-double.c:39
CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle)


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:16