1#ifndef __CURL_MULTI_H
2#define __CURL_MULTI_H
3/***************************************************************************
4 *                                  _   _ ____  _
5 *  Project                     ___| | | |  _ \| |
6 *                             / __| | | | |_) | |
7 *                            | (__| |_| |  _ <| |___
8 *                             \___|\___/|_| \_\_____|
9 *
10 * Copyright (C) 1998 - 2007, 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
52extern "C" {
53#endif
54
55typedef void CURLM;
56
57typedef 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_LAST
68} CURLMcode;
69
70/* just to make code nicer when using curl_multi_socket() you can now check
71   for CURLM_CALL_MULTI_SOCKET too in the same style it works for
72   curl_multi_perform() and CURLM_CALL_MULTI_PERFORM */
73#define CURLM_CALL_MULTI_SOCKET CURLM_CALL_MULTI_PERFORM
74
75typedef enum {
76  CURLMSG_NONE, /* first, not used */
77  CURLMSG_DONE, /* This easy handle has completed. 'result' contains
78                   the CURLcode of the transfer */
79  CURLMSG_LAST /* last, not used */
80} CURLMSG;
81
82struct CURLMsg {
83  CURLMSG msg;       /* what this message means */
84  CURL *easy_handle; /* the handle it concerns */
85  union {
86    void *whatever;    /* message-specific data */
87    CURLcode result;   /* return code for transfer */
88  } data;
89};
90typedef struct CURLMsg CURLMsg;
91
92/*
93 * Name:    curl_multi_init()
94 *
95 * Desc:    inititalize multi-style curl usage
96 *
97 * Returns: a new CURLM handle to use in all 'curl_multi' functions.
98 */
99CURL_EXTERN CURLM *curl_multi_init(void);
100
101/*
102 * Name:    curl_multi_add_handle()
103 *
104 * Desc:    add a standard curl handle to the multi stack
105 *
106 * Returns: CURLMcode type, general multi error code.
107 */
108CURL_EXTERN CURLMcode curl_multi_add_handle(CURLM *multi_handle,
109                                            CURL *curl_handle);
110
111 /*
112  * Name:    curl_multi_remove_handle()
113  *
114  * Desc:    removes a curl handle from the multi stack again
115  *
116  * Returns: CURLMcode type, general multi error code.
117  */
118CURL_EXTERN CURLMcode curl_multi_remove_handle(CURLM *multi_handle,
119                                               CURL *curl_handle);
120
121 /*
122  * Name:    curl_multi_fdset()
123  *
124  * Desc:    Ask curl for its fd_set sets. The app can use these to select() or
125  *          poll() on. We want curl_multi_perform() called as soon as one of
126  *          them are ready.
127  *
128  * Returns: CURLMcode type, general multi error code.
129  */
130CURL_EXTERN CURLMcode curl_multi_fdset(CURLM *multi_handle,
131                                       fd_set *read_fd_set,
132                                       fd_set *write_fd_set,
133                                       fd_set *exc_fd_set,
134                                       int *max_fd);
135
136 /*
137  * Name:    curl_multi_perform()
138  *
139  * Desc:    When the app thinks there's data available for curl it calls this
140  *          function to read/write whatever there is right now. This returns
141  *          as soon as the reads and writes are done. This function does not
142  *          require that there actually is data available for reading or that
143  *          data can be written, it can be called just in case. It returns
144  *          the number of handles that still transfer data in the second
145  *          argument's integer-pointer.
146  *
147  * Returns: CURLMcode type, general multi error code. *NOTE* that this only
148  *          returns errors etc regarding the whole multi stack. There might
149  *          still have occurred problems on invidual transfers even when this
150  *          returns OK.
151  */
152CURL_EXTERN CURLMcode curl_multi_perform(CURLM *multi_handle,
153                                         int *running_handles);
154
155 /*
156  * Name:    curl_multi_cleanup()
157  *
158  * Desc:    Cleans up and removes a whole multi stack. It does not free or
159  *          touch any individual easy handles in any way. We need to define
160  *          in what state those handles will be if this function is called
161  *          in the middle of a transfer.
162  *
163  * Returns: CURLMcode type, general multi error code.
164  */
165CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle);
166
167/*
168 * Name:    curl_multi_info_read()
169 *
170 * Desc:    Ask the multi handle if there's any messages/informationals from
171 *          the individual transfers. Messages include informationals such as
172 *          error code from the transfer or just the fact that a transfer is
173 *          completed. More details on these should be written down as well.
174 *
175 *          Repeated calls to this function will return a new struct each
176 *          time, until a special "end of msgs" struct is returned as a signal
177 *          that there is no more to get at this point.
178 *
179 *          The data the returned pointer points to will not survive calling
180 *          curl_multi_cleanup().
181 *
182 *          The 'CURLMsg' struct is meant to be very simple and only contain
183 *          very basic informations. If more involved information is wanted,
184 *          we will provide the particular "transfer handle" in that struct
185 *          and that should/could/would be used in subsequent
186 *          curl_easy_getinfo() calls (or similar). The point being that we
187 *          must never expose complex structs to applications, as then we'll
188 *          undoubtably get backwards compatibility problems in the future.
189 *
190 * Returns: A pointer to a filled-in struct, or NULL if it failed or ran out
191 *          of structs. It also writes the number of messages left in the
192 *          queue (after this read) in the integer the second argument points
193 *          to.
194 */
195CURL_EXTERN CURLMsg *curl_multi_info_read(CURLM *multi_handle,
196                                          int *msgs_in_queue);
197
198/*
199 * Name:    curl_multi_strerror()
200 *
201 * Desc:    The curl_multi_strerror function may be used to turn a CURLMcode
202 *          value into the equivalent human readable error string.  This is
203 *          useful for printing meaningful error messages.
204 *
205 * Returns: A pointer to a zero-terminated error message.
206 */
207CURL_EXTERN const char *curl_multi_strerror(CURLMcode);
208
209/*
210 * Name:    curl_multi_socket() and
211 *          curl_multi_socket_all()
212 *
213 * Desc:    An alternative version of curl_multi_perform() that allows the
214 *          application to pass in one of the file descriptors that have been
215 *          detected to have "action" on them and let libcurl perform.
216 *          See man page for details.
217 */
218#define CURL_POLL_NONE   0
219#define CURL_POLL_IN     1
220#define CURL_POLL_OUT    2
221#define CURL_POLL_INOUT  3
222#define CURL_POLL_REMOVE 4
223
224#define CURL_SOCKET_TIMEOUT CURL_SOCKET_BAD
225
226#define CURL_CSELECT_IN   0x01
227#define CURL_CSELECT_OUT  0x02
228#define CURL_CSELECT_ERR  0x04
229
230typedef int (*curl_socket_callback)(CURL *easy,      /* easy handle */
231                                    curl_socket_t s, /* socket */
232                                    int what,        /* see above */
233                                    void *userp,     /* private callback
234                                                        pointer */
235                                    void *socketp);  /* private socket
236                                                        pointer */
237/*
238 * Name:    curl_multi_timer_callback
239 *
240 * Desc:    Called by libcurl whenever the library detects a change in the
241 *          maximum number of milliseconds the app is allowed to wait before
242 *          curl_multi_socket() or curl_multi_perform() must be called
243 *          (to allow libcurl's timed events to take place).
244 *
245 * Returns: The callback should return zero.
246 */
247typedef int (*curl_multi_timer_callback)(CURLM *multi,    /* multi handle */
248                                         long timeout_ms, /* see above */
249                                         void *userp);    /* private callback
250                                                             pointer */
251
252CURL_EXTERN CURLMcode curl_multi_socket(CURLM *multi_handle, curl_socket_t s,
253                                        int *running_handles);
254
255CURL_EXTERN CURLMcode curl_multi_socket_action(CURLM *multi_handle,
256                                               curl_socket_t s,
257                                               int ev_bitmask,
258                                               int *running_handles);
259
260CURL_EXTERN CURLMcode curl_multi_socket_all(CURLM *multi_handle,
261                                            int *running_handles);
262
263#ifndef CURL_ALLOW_OLD_MULTI_SOCKET
264/* This macro below was added in 7.16.3 to push users who recompile to use
265   the new curl_multi_socket_action() instead of the old curl_multi_socket()
266*/
267#define curl_multi_socket(x,y,z) curl_multi_socket_action(x,y,0,z)
268#endif
269
270/*
271 * Name:    curl_multi_timeout()
272 *
273 * Desc:    Returns the maximum number of milliseconds the app is allowed to
274 *          wait before curl_multi_socket() or curl_multi_perform() must be
275 *          called (to allow libcurl's timed events to take place).
276 *
277 * Returns: CURLM error code.
278 */
279CURL_EXTERN CURLMcode curl_multi_timeout(CURLM *multi_handle,
280                                         long *milliseconds);
281
282#undef CINIT /* re-using the same name as in curl.h */
283
284#ifdef CURL_ISOCPP
285#define CINIT(name,type,num) CURLMOPT_ ## name = CURLOPTTYPE_ ## type + num
286#else
287/* The macro "##" is ISO C, we assume pre-ISO C doesn't support it. */
288#define LONG          CURLOPTTYPE_LONG
289#define OBJECTPOINT   CURLOPTTYPE_OBJECTPOINT
290#define FUNCTIONPOINT CURLOPTTYPE_FUNCTIONPOINT
291#define OFF_T         CURLOPTTYPE_OFF_T
292#define CINIT(name,type,number) CURLMOPT_/**/name = type + number
293#endif
294
295typedef enum {
296  /* This is the socket callback function pointer */
297  CINIT(SOCKETFUNCTION, FUNCTIONPOINT, 1),
298
299  /* This is the argument passed to the socket callback */
300  CINIT(SOCKETDATA, OBJECTPOINT, 2),
301
302    /* set to 1 to enable pipelining for this multi handle */
303  CINIT(PIPELINING, LONG, 3),
304
305   /* This is the timer callback function pointer */
306  CINIT(TIMERFUNCTION, FUNCTIONPOINT, 4),
307
308  /* This is the argument passed to the timer callback */
309  CINIT(TIMERDATA, OBJECTPOINT, 5),
310
311  /* maximum number of entries in the connection cache */
312  CINIT(MAXCONNECTS, LONG, 6),
313
314  CURLMOPT_LASTENTRY /* the last unused */
315} CURLMoption;
316
317
318/*
319 * Name:    curl_multi_setopt()
320 *
321 * Desc:    Sets options for the multi handle.
322 *
323 * Returns: CURLM error code.
324 */
325CURL_EXTERN CURLMcode curl_multi_setopt(CURLM *multi_handle,
326                                        CURLMoption option, ...);
327
328
329/*
330 * Name:    curl_multi_assign()
331 *
332 * Desc:    This function sets an association in the multi handle between the
333 *          given socket and a private pointer of the application. This is
334 *          (only) useful for curl_multi_socket uses.
335 *
336 * Returns: CURLM error code.
337 */
338CURL_EXTERN CURLMcode curl_multi_assign(CURLM *multi_handle,
339                                        curl_socket_t sockfd, void *sockp);
340
341#ifdef __cplusplus
342} /* end of extern "C" */
343#endif
344
345#endif
346