1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2011, 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 http://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 /* This is an example application source code using the multi interface. */
23
24 #include <stdio.h>
25 #include <string.h>
26
27 /* somewhat unix-specific */
28 #include <sys/time.h>
29 #include <unistd.h>
30
31 /* curl stuff */
32 #include <curl/curl.h>
33
34 /*
35 * Download a HTTP file and upload an FTP file simultaneously.
36 */
37
38 #define HANDLECOUNT 2 /* Number of simultaneous transfers */
39 #define HTTP_HANDLE 0 /* Index for the HTTP transfer */
40 #define FTP_HANDLE 1 /* Index for the FTP transfer */
41
main(void)42 int main(void)
43 {
44 CURL *handles[HANDLECOUNT];
45 CURLM *multi_handle;
46
47 int still_running; /* keep number of running handles */
48 int i;
49
50 CURLMsg *msg; /* for picking up messages with the transfer status */
51 int msgs_left; /* how many messages are left */
52
53 /* Allocate one CURL handle per transfer */
54 for (i=0; i<HANDLECOUNT; i++)
55 handles[i] = curl_easy_init();
56
57 /* set the options (I left out a few, you'll get the point anyway) */
58 curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "http://example.com");
59
60 curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://example.com");
61 curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
62
63 /* init a multi stack */
64 multi_handle = curl_multi_init();
65
66 /* add the individual transfers */
67 for (i=0; i<HANDLECOUNT; i++)
68 curl_multi_add_handle(multi_handle, handles[i]);
69
70 /* we start some action by calling perform right away */
71 curl_multi_perform(multi_handle, &still_running);
72
73 do {
74 struct timeval timeout;
75 int rc; /* select() return code */
76 CURLMcode mc; /* curl_multi_fdset() return code */
77
78 fd_set fdread;
79 fd_set fdwrite;
80 fd_set fdexcep;
81 int maxfd = -1;
82
83 long curl_timeo = -1;
84
85 FD_ZERO(&fdread);
86 FD_ZERO(&fdwrite);
87 FD_ZERO(&fdexcep);
88
89 /* set a suitable timeout to play around with */
90 timeout.tv_sec = 1;
91 timeout.tv_usec = 0;
92
93 curl_multi_timeout(multi_handle, &curl_timeo);
94 if(curl_timeo >= 0) {
95 timeout.tv_sec = curl_timeo / 1000;
96 if(timeout.tv_sec > 1)
97 timeout.tv_sec = 1;
98 else
99 timeout.tv_usec = (curl_timeo % 1000) * 1000;
100 }
101
102 /* get file descriptors from the transfers */
103 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
104
105 if(mc != CURLM_OK)
106 {
107 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
108 break;
109 }
110
111 /* On success the value of maxfd is guaranteed to be >= -1. We call
112 select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
113 no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
114 to sleep 100ms, which is the minimum suggested value in the
115 curl_multi_fdset() doc. */
116
117 if(maxfd == -1) {
118 #ifdef _WIN32
119 Sleep(100);
120 rc = 0;
121 #else
122 /* Portable sleep for platforms other than Windows. */
123 struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
124 rc = select(0, NULL, NULL, NULL, &wait);
125 #endif
126 }
127 else {
128 /* Note that on some platforms 'timeout' may be modified by select().
129 If you need access to the original value save a copy beforehand. */
130 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
131 }
132
133 switch(rc) {
134 case -1:
135 /* select error */
136 break;
137 case 0: /* timeout */
138 default: /* action */
139 curl_multi_perform(multi_handle, &still_running);
140 break;
141 }
142 } while(still_running);
143
144 /* See how the transfers went */
145 while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
146 if (msg->msg == CURLMSG_DONE) {
147 int idx, found = 0;
148
149 /* Find out which handle this message is about */
150 for (idx=0; idx<HANDLECOUNT; idx++) {
151 found = (msg->easy_handle == handles[idx]);
152 if(found)
153 break;
154 }
155
156 switch (idx) {
157 case HTTP_HANDLE:
158 printf("HTTP transfer completed with status %d\n", msg->data.result);
159 break;
160 case FTP_HANDLE:
161 printf("FTP transfer completed with status %d\n", msg->data.result);
162 break;
163 }
164 }
165 }
166
167 curl_multi_cleanup(multi_handle);
168
169 /* Free the CURL handles */
170 for (i=0; i<HANDLECOUNT; i++)
171 curl_easy_cleanup(handles[i]);
172
173 return 0;
174 }
175