1#ifndef __CURL_MULTI_H 2#define __CURL_MULTI_H 3/*************************************************************************** 4 * _ _ ____ _ 5 * Project ___| | | | _ \| | 6 * / __| | | | |_) | | 7 * | (__| |_| | _ <| |___ 8 * \___|\___/|_| \_\_____| 9 * 10 * Copyright (C) 1998 - 2012, 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#include <Availability.h> 52 53#ifdef __cplusplus 54extern "C" { 55#endif 56 57typedef void CURLM; 58 59typedef enum { 60 CURLM_CALL_MULTI_PERFORM = -1, /* please call curl_multi_perform() or 61 curl_multi_socket*() soon */ 62 CURLM_OK, 63 CURLM_BAD_HANDLE, /* the passed-in handle is not a valid CURLM handle */ 64 CURLM_BAD_EASY_HANDLE, /* an easy handle was not good/valid */ 65 CURLM_OUT_OF_MEMORY, /* if you ever get this, you're in deep sh*t */ 66 CURLM_INTERNAL_ERROR, /* this is a libcurl bug */ 67 CURLM_BAD_SOCKET, /* the passed in socket argument did not match */ 68 CURLM_UNKNOWN_OPTION, /* curl_multi_setopt() with unsupported option */ 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 77typedef 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 84struct 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}; 92typedef 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 101struct 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 */ 114CURL_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 */ 123CURL_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 */ 133CURL_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 */ 145CURL_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__OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0) 160CURL_EXTERN CURLMcode curl_multi_wait(CURLM *multi_handle, 161 struct curl_waitfd extra_fds[], 162 unsigned int extra_nfds, 163 int timeout_ms, 164 int *ret); 165 166 /* 167 * Name: curl_multi_perform() 168 * 169 * Desc: When the app thinks there's data available for curl it calls this 170 * function to read/write whatever there is right now. This returns 171 * as soon as the reads and writes are done. This function does not 172 * require that there actually is data available for reading or that 173 * data can be written, it can be called just in case. It returns 174 * the number of handles that still transfer data in the second 175 * argument's integer-pointer. 176 * 177 * Returns: CURLMcode type, general multi error code. *NOTE* that this only 178 * returns errors etc regarding the whole multi stack. There might 179 * still have occurred problems on invidual transfers even when this 180 * returns OK. 181 */ 182CURL_EXTERN CURLMcode curl_multi_perform(CURLM *multi_handle, 183 int *running_handles); 184 185 /* 186 * Name: curl_multi_cleanup() 187 * 188 * Desc: Cleans up and removes a whole multi stack. It does not free or 189 * touch any individual easy handles in any way. We need to define 190 * in what state those handles will be if this function is called 191 * in the middle of a transfer. 192 * 193 * Returns: CURLMcode type, general multi error code. 194 */ 195CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle); 196 197/* 198 * Name: curl_multi_info_read() 199 * 200 * Desc: Ask the multi handle if there's any messages/informationals from 201 * the individual transfers. Messages include informationals such as 202 * error code from the transfer or just the fact that a transfer is 203 * completed. More details on these should be written down as well. 204 * 205 * Repeated calls to this function will return a new struct each 206 * time, until a special "end of msgs" struct is returned as a signal 207 * that there is no more to get at this point. 208 * 209 * The data the returned pointer points to will not survive calling 210 * curl_multi_cleanup(). 211 * 212 * The 'CURLMsg' struct is meant to be very simple and only contain 213 * very basic informations. If more involved information is wanted, 214 * we will provide the particular "transfer handle" in that struct 215 * and that should/could/would be used in subsequent 216 * curl_easy_getinfo() calls (or similar). The point being that we 217 * must never expose complex structs to applications, as then we'll 218 * undoubtably get backwards compatibility problems in the future. 219 * 220 * Returns: A pointer to a filled-in struct, or NULL if it failed or ran out 221 * of structs. It also writes the number of messages left in the 222 * queue (after this read) in the integer the second argument points 223 * to. 224 */ 225CURL_EXTERN CURLMsg *curl_multi_info_read(CURLM *multi_handle, 226 int *msgs_in_queue); 227 228/* 229 * Name: curl_multi_strerror() 230 * 231 * Desc: The curl_multi_strerror function may be used to turn a CURLMcode 232 * value into the equivalent human readable error string. This is 233 * useful for printing meaningful error messages. 234 * 235 * Returns: A pointer to a zero-terminated error message. 236 */ 237CURL_EXTERN const char *curl_multi_strerror(CURLMcode); 238 239/* 240 * Name: curl_multi_socket() and 241 * curl_multi_socket_all() 242 * 243 * Desc: An alternative version of curl_multi_perform() that allows the 244 * application to pass in one of the file descriptors that have been 245 * detected to have "action" on them and let libcurl perform. 246 * See man page for details. 247 */ 248#define CURL_POLL_NONE 0 249#define CURL_POLL_IN 1 250#define CURL_POLL_OUT 2 251#define CURL_POLL_INOUT 3 252#define CURL_POLL_REMOVE 4 253 254#define CURL_SOCKET_TIMEOUT CURL_SOCKET_BAD 255 256#define CURL_CSELECT_IN 0x01 257#define CURL_CSELECT_OUT 0x02 258#define CURL_CSELECT_ERR 0x04 259 260typedef int (*curl_socket_callback)(CURL *easy, /* easy handle */ 261 curl_socket_t s, /* socket */ 262 int what, /* see above */ 263 void *userp, /* private callback 264 pointer */ 265 void *socketp); /* private socket 266 pointer */ 267/* 268 * Name: curl_multi_timer_callback 269 * 270 * Desc: Called by libcurl whenever the library detects a change in the 271 * maximum number of milliseconds the app is allowed to wait before 272 * curl_multi_socket() or curl_multi_perform() must be called 273 * (to allow libcurl's timed events to take place). 274 * 275 * Returns: The callback should return zero. 276 */ 277typedef int (*curl_multi_timer_callback)(CURLM *multi, /* multi handle */ 278 long timeout_ms, /* see above */ 279 void *userp); /* private callback 280 pointer */ 281 282CURL_EXTERN CURLMcode curl_multi_socket(CURLM *multi_handle, curl_socket_t s, 283 int *running_handles); 284 285CURL_EXTERN CURLMcode curl_multi_socket_action(CURLM *multi_handle, 286 curl_socket_t s, 287 int ev_bitmask, 288 int *running_handles); 289 290CURL_EXTERN CURLMcode curl_multi_socket_all(CURLM *multi_handle, 291 int *running_handles); 292 293#ifndef CURL_ALLOW_OLD_MULTI_SOCKET 294/* This macro below was added in 7.16.3 to push users who recompile to use 295 the new curl_multi_socket_action() instead of the old curl_multi_socket() 296*/ 297#define curl_multi_socket(x,y,z) curl_multi_socket_action(x,y,0,z) 298#endif 299 300/* 301 * Name: curl_multi_timeout() 302 * 303 * Desc: Returns the maximum number of milliseconds the app is allowed to 304 * wait before curl_multi_socket() or curl_multi_perform() must be 305 * called (to allow libcurl's timed events to take place). 306 * 307 * Returns: CURLM error code. 308 */ 309CURL_EXTERN CURLMcode curl_multi_timeout(CURLM *multi_handle, 310 long *milliseconds); 311 312#undef CINIT /* re-using the same name as in curl.h */ 313 314#ifdef CURL_ISOCPP 315#define CINIT(name,type,num) CURLMOPT_ ## name = CURLOPTTYPE_ ## type + num 316#else 317/* The macro "##" is ISO C, we assume pre-ISO C doesn't support it. */ 318#define LONG CURLOPTTYPE_LONG 319#define OBJECTPOINT CURLOPTTYPE_OBJECTPOINT 320#define FUNCTIONPOINT CURLOPTTYPE_FUNCTIONPOINT 321#define OFF_T CURLOPTTYPE_OFF_T 322#define CINIT(name,type,number) CURLMOPT_/**/name = type + number 323#endif 324 325typedef enum { 326 /* This is the socket callback function pointer */ 327 CINIT(SOCKETFUNCTION, FUNCTIONPOINT, 1), 328 329 /* This is the argument passed to the socket callback */ 330 CINIT(SOCKETDATA, OBJECTPOINT, 2), 331 332 /* set to 1 to enable pipelining for this multi handle */ 333 CINIT(PIPELINING, LONG, 3), 334 335 /* This is the timer callback function pointer */ 336 CINIT(TIMERFUNCTION, FUNCTIONPOINT, 4), 337 338 /* This is the argument passed to the timer callback */ 339 CINIT(TIMERDATA, OBJECTPOINT, 5), 340 341 /* maximum number of entries in the connection cache */ 342 CINIT(MAXCONNECTS, LONG, 6), 343 344 /* maximum number of (pipelining) connections to one host */ 345 CINIT(MAX_HOST_CONNECTIONS, LONG, 7), 346 347 /* maximum number of requests in a pipeline */ 348 CINIT(MAX_PIPELINE_LENGTH, LONG, 8), 349 350 /* a connection with a content-length longer than this 351 will not be considered for pipelining */ 352 CINIT(CONTENT_LENGTH_PENALTY_SIZE, OFF_T, 9), 353 354 /* a connection with a chunk length longer than this 355 will not be considered for pipelining */ 356 CINIT(CHUNK_LENGTH_PENALTY_SIZE, OFF_T, 10), 357 358 /* a list of site names(+port) that are blacklisted from 359 pipelining */ 360 CINIT(PIPELINING_SITE_BL, OBJECTPOINT, 11), 361 362 /* a list of server types that are blacklisted from 363 pipelining */ 364 CINIT(PIPELINING_SERVER_BL, OBJECTPOINT, 12), 365 366 /* maximum number of open connections in total */ 367 CINIT(MAX_TOTAL_CONNECTIONS, LONG, 13), 368 369 CURLMOPT_LASTENTRY /* the last unused */ 370} CURLMoption; 371 372 373/* 374 * Name: curl_multi_setopt() 375 * 376 * Desc: Sets options for the multi handle. 377 * 378 * Returns: CURLM error code. 379 */ 380CURL_EXTERN CURLMcode curl_multi_setopt(CURLM *multi_handle, 381 CURLMoption option, ...); 382 383 384/* 385 * Name: curl_multi_assign() 386 * 387 * Desc: This function sets an association in the multi handle between the 388 * given socket and a private pointer of the application. This is 389 * (only) useful for curl_multi_socket uses. 390 * 391 * Returns: CURLM error code. 392 */ 393CURL_EXTERN CURLMcode curl_multi_assign(CURLM *multi_handle, 394 curl_socket_t sockfd, void *sockp); 395 396#ifdef __cplusplus 397} /* end of extern "C" */ 398#endif 399 400#endif 401