[Hiper-related work] Added a function called curl_multi_assign() that will
[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 - 2006, 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  * $Id$
24  ***************************************************************************/
25 /*
26   This is an "external" header file. Don't give away any internals here!
27
28   GOALS
29
30   o Enable a "pull" interface. The application that uses libcurl decides where
31     and when to ask libcurl to get/send data.
32
33   o Enable multiple simultaneous transfers in the same thread without making it
34     complicated for the application.
35
36   o Enable the application to select() on its own file descriptors and curl's
37     file descriptors simultaneous easily.
38
39 */
40 #if defined(_WIN32) && !defined(WIN32)
41 /* Chris Lewis mentioned that he doesn't get WIN32 defined, only _WIN32 so we
42    make this adjustment to catch this. */
43 #define WIN32 1
44 #endif
45
46 #if defined(WIN32) && !defined(_WIN32_WCE) && !defined(__GNUC__) && \
47   !defined(__CYGWIN__) || defined(__MINGW32__)
48 #if !(defined(_WINSOCKAPI_) || defined(_WINSOCK_H))
49 /* The check above prevents the winsock2 inclusion if winsock.h already was
50    included, since they can't co-exist without problems */
51 #include <winsock2.h>
52 #endif
53 #else
54
55 /* HP-UX systems version 9, 10 and 11 lack sys/select.h and so does oldish
56    libc5-based Linux systems. Only include it on system that are known to
57    require it! */
58 #if defined(_AIX) || defined(NETWARE) || defined(__NetBSD__)
59 #include <sys/select.h>
60 #endif
61
62 #ifndef _WIN32_WCE
63 #include <sys/socket.h>
64 #endif
65 #include <sys/time.h>
66 #include <sys/types.h>
67 #endif
68
69 /*
70  * This header file should not really need to include "curl.h" since curl.h
71  * itself includes this file and we expect user applications to do #include
72  * <curl/curl.h> without the need for especially including multi.h.
73  *
74  * For some reason we added this include here at one point, and rather than to
75  * break existing (wrongly written) libcurl applications, we leave it as-is
76  * but with this warning attached.
77  */
78 #include "curl.h"
79
80 #ifdef  __cplusplus
81 extern "C" {
82 #endif
83
84 typedef void CURLM;
85
86 #ifndef curl_socket_typedef
87 /* Public socket typedef */
88 #ifdef WIN32
89 typedef SOCKET curl_socket_t;
90 #define CURL_SOCKET_BAD INVALID_SOCKET
91 #else
92 typedef int curl_socket_t;
93 #define CURL_SOCKET_BAD -1
94 #endif
95 #define curl_socket_typedef
96 #endif /* curl_socket_typedef */
97
98 typedef enum {
99   CURLM_CALL_MULTI_PERFORM = -1, /* please call curl_multi_perform() soon */
100   CURLM_OK,
101   CURLM_BAD_HANDLE,      /* the passed-in handle is not a valid CURLM handle */
102   CURLM_BAD_EASY_HANDLE, /* an easy handle was not good/valid */
103   CURLM_OUT_OF_MEMORY,   /* if you ever get this, you're in deep sh*t */
104   CURLM_INTERNAL_ERROR,  /* this is a libcurl bug */
105   CURLM_BAD_SOCKET,      /* the passed in socket argument did not match */
106   CURLM_UNKNOWN_OPTION,  /* curl_multi_setopt() with unsupported option */
107   CURLM_LAST
108 } CURLMcode;
109
110 typedef enum {
111   CURLMSG_NONE, /* first, not used */
112   CURLMSG_DONE, /* This easy handle has completed. 'result' contains
113                    the CURLcode of the transfer */
114   CURLMSG_LAST /* last, not used */
115 } CURLMSG;
116
117 struct CURLMsg {
118   CURLMSG msg;       /* what this message means */
119   CURL *easy_handle; /* the handle it concerns */
120   union {
121     void *whatever;    /* message-specific data */
122     CURLcode result;   /* return code for transfer */
123   } data;
124 };
125 typedef struct CURLMsg CURLMsg;
126
127 /*
128  * Name:    curl_multi_init()
129  *
130  * Desc:    inititalize multi-style curl usage
131  *
132  * Returns: a new CURLM handle to use in all 'curl_multi' functions.
133  */
134 CURL_EXTERN CURLM *curl_multi_init(void);
135
136 /*
137  * Name:    curl_multi_add_handle()
138  *
139  * Desc:    add a standard curl handle to the multi stack
140  *
141  * Returns: CURLMcode type, general multi error code.
142  */
143 CURL_EXTERN CURLMcode curl_multi_add_handle(CURLM *multi_handle,
144                                             CURL *curl_handle);
145
146  /*
147   * Name:    curl_multi_remove_handle()
148   *
149   * Desc:    removes a curl handle from the multi stack again
150   *
151   * Returns: CURLMcode type, general multi error code.
152   */
153 CURL_EXTERN CURLMcode curl_multi_remove_handle(CURLM *multi_handle,
154                                                CURL *curl_handle);
155
156  /*
157   * Name:    curl_multi_fdset()
158   *
159   * Desc:    Ask curl for its fd_set sets. The app can use these to select() or
160   *          poll() on. We want curl_multi_perform() called as soon as one of
161   *          them are ready.
162   *
163   * Returns: CURLMcode type, general multi error code.
164   */
165 CURL_EXTERN CURLMcode curl_multi_fdset(CURLM *multi_handle,
166                                        fd_set *read_fd_set,
167                                        fd_set *write_fd_set,
168                                        fd_set *exc_fd_set,
169                                        int *max_fd);
170
171  /*
172   * Name:    curl_multi_perform()
173   *
174   * Desc:    When the app thinks there's data available for curl it calls this
175   *          function to read/write whatever there is right now. This returns
176   *          as soon as the reads and writes are done. This function does not
177   *          require that there actually is data available for reading or that
178   *          data can be written, it can be called just in case. It returns
179   *          the number of handles that still transfer data in the second
180   *          argument's integer-pointer.
181   *
182   * Returns: CURLMcode type, general multi error code. *NOTE* that this only
183   *          returns errors etc regarding the whole multi stack. There might
184   *          still have occurred problems on invidual transfers even when this
185   *          returns OK.
186   */
187 CURL_EXTERN CURLMcode curl_multi_perform(CURLM *multi_handle,
188                                          int *running_handles);
189
190  /*
191   * Name:    curl_multi_cleanup()
192   *
193   * Desc:    Cleans up and removes a whole multi stack. It does not free or
194   *          touch any individual easy handles in any way. We need to define
195   *          in what state those handles will be if this function is called
196   *          in the middle of a transfer.
197   *
198   * Returns: CURLMcode type, general multi error code.
199   */
200 CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle);
201
202 /*
203  * Name:    curl_multi_info_read()
204  *
205  * Desc:    Ask the multi handle if there's any messages/informationals from
206  *          the individual transfers. Messages include informationals such as
207  *          error code from the transfer or just the fact that a transfer is
208  *          completed. More details on these should be written down as well.
209  *
210  *          Repeated calls to this function will return a new struct each
211  *          time, until a special "end of msgs" struct is returned as a signal
212  *          that there is no more to get at this point.
213  *
214  *          The data the returned pointer points to will not survive calling
215  *          curl_multi_cleanup().
216  *
217  *          The 'CURLMsg' struct is meant to be very simple and only contain
218  *          very basic informations. If more involved information is wanted,
219  *          we will provide the particular "transfer handle" in that struct
220  *          and that should/could/would be used in subsequent
221  *          curl_easy_getinfo() calls (or similar). The point being that we
222  *          must never expose complex structs to applications, as then we'll
223  *          undoubtably get backwards compatibility problems in the future.
224  *
225  * Returns: A pointer to a filled-in struct, or NULL if it failed or ran out
226  *          of structs. It also writes the number of messages left in the
227  *          queue (after this read) in the integer the second argument points
228  *          to.
229  */
230 CURL_EXTERN CURLMsg *curl_multi_info_read(CURLM *multi_handle,
231                                           int *msgs_in_queue);
232
233 /*
234  * Name:    curl_multi_strerror()
235  *
236  * Desc:    The curl_multi_strerror function may be used to turn a CURLMcode
237  *          value into the equivalent human readable error string.  This is
238  *          useful for printing meaningful error messages.
239  *
240  * Returns: A pointer to a zero-terminated error message.
241  */
242 CURL_EXTERN const char *curl_multi_strerror(CURLMcode);
243
244 /*
245  * Name:    curl_multi_socket() and
246  *          curl_multi_socket_all()
247  *
248  * Desc:    An alternative version of curl_multi_perform() that allows the
249  *          application to pass in one of the file descriptors that have been
250  *          detected to have "action" on them and let libcurl perform.
251  *          See man page for details.
252  */
253 #define CURL_POLL_NONE   0
254 #define CURL_POLL_IN     1
255 #define CURL_POLL_OUT    2
256 #define CURL_POLL_INOUT  3
257 #define CURL_POLL_REMOVE 4
258
259 #define CURL_SOCKET_TIMEOUT CURL_SOCKET_BAD
260
261 typedef int (*curl_socket_callback)(CURL *easy,      /* easy handle */
262                                     curl_socket_t s, /* socket */
263                                     int what,        /* see above */
264                                     void *userp,     /* private callback
265                                                         pointer */
266                                     void *socketp);  /* private socket
267                                                         pointer */
268
269 CURL_EXTERN CURLMcode curl_multi_socket(CURLM *multi_handle, curl_socket_t s);
270
271 CURL_EXTERN CURLMcode curl_multi_socket_all(CURLM *multi_handle);
272
273 /*
274  * Name:    curl_multi_timeout()
275  *
276  * Desc:    Returns the maximum number of milliseconds the app is allowed to
277  *          wait before curl_multi_socket() or curl_multi_perform() must be
278  *          called (to allow libcurl's timed events to take place).
279  *
280  * Returns: CURLM error code.
281  */
282 CURL_EXTERN CURLMcode curl_multi_timeout(CURLM *multi_handle,
283                                          long *milliseconds);
284
285 #undef CINIT /* re-using the same name as in curl.h */
286
287 #ifdef CURL_ISOCPP
288 #define CINIT(name,type,number) CURLMOPT_ ## name = CURLOPTTYPE_ ## type + number
289 #else
290 /* The macro "##" is ISO C, we assume pre-ISO C doesn't support it. */
291 #define LONG          CURLOPTTYPE_LONG
292 #define OBJECTPOINT   CURLOPTTYPE_OBJECTPOINT
293 #define FUNCTIONPOINT CURLOPTTYPE_FUNCTIONPOINT
294 #define OFF_T         CURLOPTTYPE_OFF_T
295 #define CINIT(name,type,number) CURLMOPT_/**/name = type + number
296 #endif
297
298 typedef enum {
299   /* This is the socket callback function pointer */
300   CINIT(SOCKETFUNCTION, FUNCTIONPOINT, 1),
301
302   /* This is the argument passed to the socket callback */
303   CINIT(SOCKETDATA, OBJECTPOINT, 2),
304
305   CURLMOPT_LASTENTRY /* the last unused */
306 } CURLMoption;
307
308
309 /*
310  * Name:    curl_multi_setopt()
311  *
312  * Desc:    Sets options for the multi handle.
313  *
314  * Returns: CURLM error code.
315  */
316 CURL_EXTERN CURLMcode curl_multi_setopt(CURLM *multi_handle,
317                                         CURLMoption option, ...);
318
319
320 /*
321  * Name:    curl_multi_assign()
322  *
323  * Desc:    This function sets an association in the multi handle between the
324  *          given socket and a private pointer of the application. This is
325  *          (only) useful for curl_multi_socket uses.
326  *
327  * Returns: CURLM error code.
328  */
329 CURL_EXTERN CURLMcode curl_multi_assign(CURLM *multi_handle,
330                                         curl_socket_t sockfd, void *sockp);
331
332 #ifdef __cplusplus
333 } /* end of extern "C" */
334 #endif
335
336 #endif