3c4acb0f6eafdedaa81bcd24af8f0b5353b5f304
[platform/upstream/curl.git] / include / curl / multi.h
1 #ifndef __CURL_MULTI_H
2 #define __CURL_MULTI_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
11  *
12  * This software is licensed as described in the file COPYING, which
13  * you should have received as part of this distribution. The terms
14  * are also available at http://curl.haxx.se/docs/copyright.html.
15  *
16  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17  * copies of the Software, and permit persons to whom the Software is
18  * furnished to do so, under the terms of the COPYING file.
19  *
20  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21  * KIND, either express or implied.
22  *
23  ***************************************************************************/
24 /*
25   This is an "external" header file. Don't give away any internals here!
26
27   GOALS
28
29   o Enable a "pull" interface. The application that uses libcurl decides where
30     and when to ask libcurl to get/send data.
31
32   o Enable multiple simultaneous transfers in the same thread without making it
33     complicated for the application.
34
35   o Enable the application to select() on its own file descriptors and curl's
36     file descriptors simultaneous easily.
37
38 */
39
40 /*
41  * This header file should not really need to include "curl.h" since curl.h
42  * itself includes this file and we expect user applications to do #include
43  * <curl/curl.h> without the need for especially including multi.h.
44  *
45  * For some reason we added this include here at one point, and rather than to
46  * break existing (wrongly written) libcurl applications, we leave it as-is
47  * but with this warning attached.
48  */
49 #include "curl.h"
50
51 #ifdef  __cplusplus
52 extern "C" {
53 #endif
54
55 typedef void CURLM;
56
57 typedef enum {
58   CURLM_CALL_MULTI_PERFORM = -1, /* please call curl_multi_perform() or
59                                     curl_multi_socket*() soon */
60   CURLM_OK,
61   CURLM_BAD_HANDLE,      /* the passed-in handle is not a valid CURLM handle */
62   CURLM_BAD_EASY_HANDLE, /* an easy handle was not good/valid */
63   CURLM_OUT_OF_MEMORY,   /* if you ever get this, you're in deep sh*t */
64   CURLM_INTERNAL_ERROR,  /* this is a libcurl bug */
65   CURLM_BAD_SOCKET,      /* the passed in socket argument did not match */
66   CURLM_UNKNOWN_OPTION,  /* curl_multi_setopt() with unsupported option */
67   CURLM_ADDED_ALREADY,   /* an easy handle already added to a multi handle was
68                             attempted to get added - again */
69   CURLM_LAST
70 } CURLMcode;
71
72 /* just to make code nicer when using curl_multi_socket() you can now check
73    for CURLM_CALL_MULTI_SOCKET too in the same style it works for
74    curl_multi_perform() and CURLM_CALL_MULTI_PERFORM */
75 #define CURLM_CALL_MULTI_SOCKET CURLM_CALL_MULTI_PERFORM
76
77 typedef enum {
78   CURLMSG_NONE, /* first, not used */
79   CURLMSG_DONE, /* This easy handle has completed. 'result' contains
80                    the CURLcode of the transfer */
81   CURLMSG_LAST /* last, not used */
82 } CURLMSG;
83
84 struct CURLMsg {
85   CURLMSG msg;       /* what this message means */
86   CURL *easy_handle; /* the handle it concerns */
87   union {
88     void *whatever;    /* message-specific data */
89     CURLcode result;   /* return code for transfer */
90   } data;
91 };
92 typedef struct CURLMsg CURLMsg;
93
94 /* Based on poll(2) structure and values.
95  * We don't use pollfd and POLL* constants explicitly
96  * to cover platforms without poll(). */
97 #define CURL_WAIT_POLLIN    0x0001
98 #define CURL_WAIT_POLLPRI   0x0002
99 #define CURL_WAIT_POLLOUT   0x0004
100
101 struct curl_waitfd {
102   curl_socket_t fd;
103   short events;
104   short revents; /* not supported yet */
105 };
106
107 /*
108  * Name:    curl_multi_init()
109  *
110  * Desc:    inititalize multi-style curl usage
111  *
112  * Returns: a new CURLM handle to use in all 'curl_multi' functions.
113  */
114 CURL_EXTERN CURLM *curl_multi_init(void);
115
116 /*
117  * Name:    curl_multi_add_handle()
118  *
119  * Desc:    add a standard curl handle to the multi stack
120  *
121  * Returns: CURLMcode type, general multi error code.
122  */
123 CURL_EXTERN CURLMcode curl_multi_add_handle(CURLM *multi_handle,
124                                             CURL *curl_handle);
125
126  /*
127   * Name:    curl_multi_remove_handle()
128   *
129   * Desc:    removes a curl handle from the multi stack again
130   *
131   * Returns: CURLMcode type, general multi error code.
132   */
133 CURL_EXTERN CURLMcode curl_multi_remove_handle(CURLM *multi_handle,
134                                                CURL *curl_handle);
135
136  /*
137   * Name:    curl_multi_fdset()
138   *
139   * Desc:    Ask curl for its fd_set sets. The app can use these to select() or
140   *          poll() on. We want curl_multi_perform() called as soon as one of
141   *          them are ready.
142   *
143   * Returns: CURLMcode type, general multi error code.
144   */
145 CURL_EXTERN CURLMcode curl_multi_fdset(CURLM *multi_handle,
146                                        fd_set *read_fd_set,
147                                        fd_set *write_fd_set,
148                                        fd_set *exc_fd_set,
149                                        int *max_fd);
150
151 /*
152  * Name:     curl_multi_wait()
153  *
154  * Desc:     Poll on all fds within a CURLM set as well as any
155  *           additional fds passed to the function.
156  *
157  * Returns:  CURLMcode type, general multi error code.
158  */
159 CURL_EXTERN CURLMcode curl_multi_wait(CURLM *multi_handle,
160                                       struct curl_waitfd extra_fds[],
161                                       unsigned int extra_nfds,
162                                       int timeout_ms,
163                                       int *ret);
164
165  /*
166   * Name:    curl_multi_perform()
167   *
168   * Desc:    When the app thinks there's data available for curl it calls this
169   *          function to read/write whatever there is right now. This returns
170   *          as soon as the reads and writes are done. This function does not
171   *          require that there actually is data available for reading or that
172   *          data can be written, it can be called just in case. It returns
173   *          the number of handles that still transfer data in the second
174   *          argument's integer-pointer.
175   *
176   * Returns: CURLMcode type, general multi error code. *NOTE* that this only
177   *          returns errors etc regarding the whole multi stack. There might
178   *          still have occurred problems on invidual transfers even when this
179   *          returns OK.
180   */
181 CURL_EXTERN CURLMcode curl_multi_perform(CURLM *multi_handle,
182                                          int *running_handles);
183
184  /*
185   * Name:    curl_multi_cleanup()
186   *
187   * Desc:    Cleans up and removes a whole multi stack. It does not free or
188   *          touch any individual easy handles in any way. We need to define
189   *          in what state those handles will be if this function is called
190   *          in the middle of a transfer.
191   *
192   * Returns: CURLMcode type, general multi error code.
193   */
194 CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle);
195
196 /*
197  * Name:    curl_multi_info_read()
198  *
199  * Desc:    Ask the multi handle if there's any messages/informationals from
200  *          the individual transfers. Messages include informationals such as
201  *          error code from the transfer or just the fact that a transfer is
202  *          completed. More details on these should be written down as well.
203  *
204  *          Repeated calls to this function will return a new struct each
205  *          time, until a special "end of msgs" struct is returned as a signal
206  *          that there is no more to get at this point.
207  *
208  *          The data the returned pointer points to will not survive calling
209  *          curl_multi_cleanup().
210  *
211  *          The 'CURLMsg' struct is meant to be very simple and only contain
212  *          very basic informations. If more involved information is wanted,
213  *          we will provide the particular "transfer handle" in that struct
214  *          and that should/could/would be used in subsequent
215  *          curl_easy_getinfo() calls (or similar). The point being that we
216  *          must never expose complex structs to applications, as then we'll
217  *          undoubtably get backwards compatibility problems in the future.
218  *
219  * Returns: A pointer to a filled-in struct, or NULL if it failed or ran out
220  *          of structs. It also writes the number of messages left in the
221  *          queue (after this read) in the integer the second argument points
222  *          to.
223  */
224 CURL_EXTERN CURLMsg *curl_multi_info_read(CURLM *multi_handle,
225                                           int *msgs_in_queue);
226
227 /*
228  * Name:    curl_multi_strerror()
229  *
230  * Desc:    The curl_multi_strerror function may be used to turn a CURLMcode
231  *          value into the equivalent human readable error string.  This is
232  *          useful for printing meaningful error messages.
233  *
234  * Returns: A pointer to a zero-terminated error message.
235  */
236 CURL_EXTERN const char *curl_multi_strerror(CURLMcode);
237
238 /*
239  * Name:    curl_multi_socket() and
240  *          curl_multi_socket_all()
241  *
242  * Desc:    An alternative version of curl_multi_perform() that allows the
243  *          application to pass in one of the file descriptors that have been
244  *          detected to have "action" on them and let libcurl perform.
245  *          See man page for details.
246  */
247 #define CURL_POLL_NONE   0
248 #define CURL_POLL_IN     1
249 #define CURL_POLL_OUT    2
250 #define CURL_POLL_INOUT  3
251 #define CURL_POLL_REMOVE 4
252
253 #define CURL_SOCKET_TIMEOUT CURL_SOCKET_BAD
254
255 #define CURL_CSELECT_IN   0x01
256 #define CURL_CSELECT_OUT  0x02
257 #define CURL_CSELECT_ERR  0x04
258
259 typedef int (*curl_socket_callback)(CURL *easy,      /* easy handle */
260                                     curl_socket_t s, /* socket */
261                                     int what,        /* see above */
262                                     void *userp,     /* private callback
263                                                         pointer */
264                                     void *socketp);  /* private socket
265                                                         pointer */
266 /*
267  * Name:    curl_multi_timer_callback
268  *
269  * Desc:    Called by libcurl whenever the library detects a change in the
270  *          maximum number of milliseconds the app is allowed to wait before
271  *          curl_multi_socket() or curl_multi_perform() must be called
272  *          (to allow libcurl's timed events to take place).
273  *
274  * Returns: The callback should return zero.
275  */
276 typedef int (*curl_multi_timer_callback)(CURLM *multi,    /* multi handle */
277                                          long timeout_ms, /* see above */
278                                          void *userp);    /* private callback
279                                                              pointer */
280
281 CURL_EXTERN CURLMcode curl_multi_socket(CURLM *multi_handle, curl_socket_t s,
282                                         int *running_handles);
283
284 CURL_EXTERN CURLMcode curl_multi_socket_action(CURLM *multi_handle,
285                                                curl_socket_t s,
286                                                int ev_bitmask,
287                                                int *running_handles);
288
289 CURL_EXTERN CURLMcode curl_multi_socket_all(CURLM *multi_handle,
290                                             int *running_handles);
291
292 #ifndef CURL_ALLOW_OLD_MULTI_SOCKET
293 /* This macro below was added in 7.16.3 to push users who recompile to use
294    the new curl_multi_socket_action() instead of the old curl_multi_socket()
295 */
296 #define curl_multi_socket(x,y,z) curl_multi_socket_action(x,y,0,z)
297 #endif
298
299 /*
300  * Name:    curl_multi_timeout()
301  *
302  * Desc:    Returns the maximum number of milliseconds the app is allowed to
303  *          wait before curl_multi_socket() or curl_multi_perform() must be
304  *          called (to allow libcurl's timed events to take place).
305  *
306  * Returns: CURLM error code.
307  */
308 CURL_EXTERN CURLMcode curl_multi_timeout(CURLM *multi_handle,
309                                          long *milliseconds);
310
311 #undef CINIT /* re-using the same name as in curl.h */
312
313 #ifdef CURL_ISOCPP
314 #define CINIT(name,type,num) CURLMOPT_ ## name = CURLOPTTYPE_ ## type + num
315 #else
316 /* The macro "##" is ISO C, we assume pre-ISO C doesn't support it. */
317 #define LONG          CURLOPTTYPE_LONG
318 #define OBJECTPOINT   CURLOPTTYPE_OBJECTPOINT
319 #define FUNCTIONPOINT CURLOPTTYPE_FUNCTIONPOINT
320 #define OFF_T         CURLOPTTYPE_OFF_T
321 #define CINIT(name,type,number) CURLMOPT_/**/name = type + number
322 #endif
323
324 typedef enum {
325   /* This is the socket callback function pointer */
326   CINIT(SOCKETFUNCTION, FUNCTIONPOINT, 1),
327
328   /* This is the argument passed to the socket callback */
329   CINIT(SOCKETDATA, OBJECTPOINT, 2),
330
331     /* set to 1 to enable pipelining for this multi handle */
332   CINIT(PIPELINING, LONG, 3),
333
334    /* This is the timer callback function pointer */
335   CINIT(TIMERFUNCTION, FUNCTIONPOINT, 4),
336
337   /* This is the argument passed to the timer callback */
338   CINIT(TIMERDATA, OBJECTPOINT, 5),
339
340   /* maximum number of entries in the connection cache */
341   CINIT(MAXCONNECTS, LONG, 6),
342
343   /* maximum number of (pipelining) connections to one host */
344   CINIT(MAX_HOST_CONNECTIONS, LONG, 7),
345
346   /* maximum number of requests in a pipeline */
347   CINIT(MAX_PIPELINE_LENGTH, LONG, 8),
348
349   /* a connection with a content-length longer than this
350      will not be considered for pipelining */
351   CINIT(CONTENT_LENGTH_PENALTY_SIZE, OFF_T, 9),
352
353   /* a connection with a chunk length longer than this
354      will not be considered for pipelining */
355   CINIT(CHUNK_LENGTH_PENALTY_SIZE, OFF_T, 10),
356
357   /* a list of site names(+port) that are blacklisted from
358      pipelining */
359   CINIT(PIPELINING_SITE_BL, OBJECTPOINT, 11),
360
361   /* a list of server types that are blacklisted from
362      pipelining */
363   CINIT(PIPELINING_SERVER_BL, OBJECTPOINT, 12),
364
365   /* maximum number of open connections in total */
366   CINIT(MAX_TOTAL_CONNECTIONS, LONG, 13),
367
368   CURLMOPT_LASTENTRY /* the last unused */
369 } CURLMoption;
370
371
372 /*
373  * Name:    curl_multi_setopt()
374  *
375  * Desc:    Sets options for the multi handle.
376  *
377  * Returns: CURLM error code.
378  */
379 CURL_EXTERN CURLMcode curl_multi_setopt(CURLM *multi_handle,
380                                         CURLMoption option, ...);
381
382
383 /*
384  * Name:    curl_multi_assign()
385  *
386  * Desc:    This function sets an association in the multi handle between the
387  *          given socket and a private pointer of the application. This is
388  *          (only) useful for curl_multi_socket uses.
389  *
390  * Returns: CURLM error code.
391  */
392 CURL_EXTERN CURLMcode curl_multi_assign(CURLM *multi_handle,
393                                         curl_socket_t sockfd, void *sockp);
394
395 #ifdef __cplusplus
396 } /* end of extern "C" */
397 #endif
398
399 #endif