Path: blob/master/cpp/linux/curl/multi.h
644 views
#ifndef __CURL_MULTI_H1#define __CURL_MULTI_H2/***************************************************************************3* _ _ ____ _4* Project ___| | | | _ \| |5* / __| | | | |_) | |6* | (__| |_| | _ <| |___7* \___|\___/|_| \_\_____|8*9* Copyright (C) 1998 - 2017, Daniel Stenberg, <[email protected]>, et al.10*11* This software is licensed as described in the file COPYING, which12* you should have received as part of this distribution. The terms13* are also available at https://curl.haxx.se/docs/copyright.html.14*15* You may opt to use, copy, modify, merge, publish, distribute and/or sell16* copies of the Software, and permit persons to whom the Software is17* furnished to do so, under the terms of the COPYING file.18*19* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY20* KIND, either express or implied.21*22***************************************************************************/23/*24This is an "external" header file. Don't give away any internals here!2526GOALS2728o Enable a "pull" interface. The application that uses libcurl decides where29and when to ask libcurl to get/send data.3031o Enable multiple simultaneous transfers in the same thread without making it32complicated for the application.3334o Enable the application to select() on its own file descriptors and curl's35file descriptors simultaneous easily.3637*/3839/*40* This header file should not really need to include "curl.h" since curl.h41* itself includes this file and we expect user applications to do #include42* <curl/curl.h> without the need for especially including multi.h.43*44* For some reason we added this include here at one point, and rather than to45* break existing (wrongly written) libcurl applications, we leave it as-is46* but with this warning attached.47*/48#include "curl.h"4950#ifdef __cplusplus51extern "C" {52#endif5354#if defined(BUILDING_LIBCURL) || defined(CURL_STRICTER)55typedef struct Curl_multi CURLM;56#else57typedef void CURLM;58#endif5960typedef enum {61CURLM_CALL_MULTI_PERFORM = -1, /* please call curl_multi_perform() or62curl_multi_socket*() soon */63CURLM_OK,64CURLM_BAD_HANDLE, /* the passed-in handle is not a valid CURLM handle */65CURLM_BAD_EASY_HANDLE, /* an easy handle was not good/valid */66CURLM_OUT_OF_MEMORY, /* if you ever get this, you're in deep sh*t */67CURLM_INTERNAL_ERROR, /* this is a libcurl bug */68CURLM_BAD_SOCKET, /* the passed in socket argument did not match */69CURLM_UNKNOWN_OPTION, /* curl_multi_setopt() with unsupported option */70CURLM_ADDED_ALREADY, /* an easy handle already added to a multi handle was71attempted to get added - again */72CURLM_RECURSIVE_API_CALL, /* an api function was called from inside a73callback */74CURLM_LAST75} CURLMcode;7677/* just to make code nicer when using curl_multi_socket() you can now check78for CURLM_CALL_MULTI_SOCKET too in the same style it works for79curl_multi_perform() and CURLM_CALL_MULTI_PERFORM */80#define CURLM_CALL_MULTI_SOCKET CURLM_CALL_MULTI_PERFORM8182/* bitmask bits for CURLMOPT_PIPELINING */83#define CURLPIPE_NOTHING 0L84#define CURLPIPE_HTTP1 1L85#define CURLPIPE_MULTIPLEX 2L8687typedef enum {88CURLMSG_NONE, /* first, not used */89CURLMSG_DONE, /* This easy handle has completed. 'result' contains90the CURLcode of the transfer */91CURLMSG_LAST /* last, not used */92} CURLMSG;9394struct CURLMsg {95CURLMSG msg; /* what this message means */96CURL *easy_handle; /* the handle it concerns */97union {98void *whatever; /* message-specific data */99CURLcode result; /* return code for transfer */100} data;101};102typedef struct CURLMsg CURLMsg;103104/* Based on poll(2) structure and values.105* We don't use pollfd and POLL* constants explicitly106* to cover platforms without poll(). */107#define CURL_WAIT_POLLIN 0x0001108#define CURL_WAIT_POLLPRI 0x0002109#define CURL_WAIT_POLLOUT 0x0004110111struct curl_waitfd {112curl_socket_t fd;113short events;114short revents; /* not supported yet */115};116117/*118* Name: curl_multi_init()119*120* Desc: inititalize multi-style curl usage121*122* Returns: a new CURLM handle to use in all 'curl_multi' functions.123*/124CURL_EXTERN CURLM *curl_multi_init(void);125126/*127* Name: curl_multi_add_handle()128*129* Desc: add a standard curl handle to the multi stack130*131* Returns: CURLMcode type, general multi error code.132*/133CURL_EXTERN CURLMcode curl_multi_add_handle(CURLM *multi_handle,134CURL *curl_handle);135136/*137* Name: curl_multi_remove_handle()138*139* Desc: removes a curl handle from the multi stack again140*141* Returns: CURLMcode type, general multi error code.142*/143CURL_EXTERN CURLMcode curl_multi_remove_handle(CURLM *multi_handle,144CURL *curl_handle);145146/*147* Name: curl_multi_fdset()148*149* Desc: Ask curl for its fd_set sets. The app can use these to select() or150* poll() on. We want curl_multi_perform() called as soon as one of151* them are ready.152*153* Returns: CURLMcode type, general multi error code.154*/155CURL_EXTERN CURLMcode curl_multi_fdset(CURLM *multi_handle,156fd_set *read_fd_set,157fd_set *write_fd_set,158fd_set *exc_fd_set,159int *max_fd);160161/*162* Name: curl_multi_wait()163*164* Desc: Poll on all fds within a CURLM set as well as any165* additional fds passed to the function.166*167* Returns: CURLMcode type, general multi error code.168*/169CURL_EXTERN CURLMcode curl_multi_wait(CURLM *multi_handle,170struct curl_waitfd extra_fds[],171unsigned int extra_nfds,172int timeout_ms,173int *ret);174175/*176* Name: curl_multi_perform()177*178* Desc: When the app thinks there's data available for curl it calls this179* function to read/write whatever there is right now. This returns180* as soon as the reads and writes are done. This function does not181* require that there actually is data available for reading or that182* data can be written, it can be called just in case. It returns183* the number of handles that still transfer data in the second184* argument's integer-pointer.185*186* Returns: CURLMcode type, general multi error code. *NOTE* that this only187* returns errors etc regarding the whole multi stack. There might188* still have occurred problems on individual transfers even when189* this returns OK.190*/191CURL_EXTERN CURLMcode curl_multi_perform(CURLM *multi_handle,192int *running_handles);193194/*195* Name: curl_multi_cleanup()196*197* Desc: Cleans up and removes a whole multi stack. It does not free or198* touch any individual easy handles in any way. We need to define199* in what state those handles will be if this function is called200* in the middle of a transfer.201*202* Returns: CURLMcode type, general multi error code.203*/204CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle);205206/*207* Name: curl_multi_info_read()208*209* Desc: Ask the multi handle if there's any messages/informationals from210* the individual transfers. Messages include informationals such as211* error code from the transfer or just the fact that a transfer is212* completed. More details on these should be written down as well.213*214* Repeated calls to this function will return a new struct each215* time, until a special "end of msgs" struct is returned as a signal216* that there is no more to get at this point.217*218* The data the returned pointer points to will not survive calling219* curl_multi_cleanup().220*221* The 'CURLMsg' struct is meant to be very simple and only contain222* very basic information. If more involved information is wanted,223* we will provide the particular "transfer handle" in that struct224* and that should/could/would be used in subsequent225* curl_easy_getinfo() calls (or similar). The point being that we226* must never expose complex structs to applications, as then we'll227* undoubtably get backwards compatibility problems in the future.228*229* Returns: A pointer to a filled-in struct, or NULL if it failed or ran out230* of structs. It also writes the number of messages left in the231* queue (after this read) in the integer the second argument points232* to.233*/234CURL_EXTERN CURLMsg *curl_multi_info_read(CURLM *multi_handle,235int *msgs_in_queue);236237/*238* Name: curl_multi_strerror()239*240* Desc: The curl_multi_strerror function may be used to turn a CURLMcode241* value into the equivalent human readable error string. This is242* useful for printing meaningful error messages.243*244* Returns: A pointer to a zero-terminated error message.245*/246CURL_EXTERN const char *curl_multi_strerror(CURLMcode);247248/*249* Name: curl_multi_socket() and250* curl_multi_socket_all()251*252* Desc: An alternative version of curl_multi_perform() that allows the253* application to pass in one of the file descriptors that have been254* detected to have "action" on them and let libcurl perform.255* See man page for details.256*/257#define CURL_POLL_NONE 0258#define CURL_POLL_IN 1259#define CURL_POLL_OUT 2260#define CURL_POLL_INOUT 3261#define CURL_POLL_REMOVE 4262263#define CURL_SOCKET_TIMEOUT CURL_SOCKET_BAD264265#define CURL_CSELECT_IN 0x01266#define CURL_CSELECT_OUT 0x02267#define CURL_CSELECT_ERR 0x04268269typedef int (*curl_socket_callback)(CURL *easy, /* easy handle */270curl_socket_t s, /* socket */271int what, /* see above */272void *userp, /* private callback273pointer */274void *socketp); /* private socket275pointer */276/*277* Name: curl_multi_timer_callback278*279* Desc: Called by libcurl whenever the library detects a change in the280* maximum number of milliseconds the app is allowed to wait before281* curl_multi_socket() or curl_multi_perform() must be called282* (to allow libcurl's timed events to take place).283*284* Returns: The callback should return zero.285*/286typedef int (*curl_multi_timer_callback)(CURLM *multi, /* multi handle */287long timeout_ms, /* see above */288void *userp); /* private callback289pointer */290291CURL_EXTERN CURLMcode curl_multi_socket(CURLM *multi_handle, curl_socket_t s,292int *running_handles);293294CURL_EXTERN CURLMcode curl_multi_socket_action(CURLM *multi_handle,295curl_socket_t s,296int ev_bitmask,297int *running_handles);298299CURL_EXTERN CURLMcode curl_multi_socket_all(CURLM *multi_handle,300int *running_handles);301302#ifndef CURL_ALLOW_OLD_MULTI_SOCKET303/* This macro below was added in 7.16.3 to push users who recompile to use304the new curl_multi_socket_action() instead of the old curl_multi_socket()305*/306#define curl_multi_socket(x,y,z) curl_multi_socket_action(x,y,0,z)307#endif308309/*310* Name: curl_multi_timeout()311*312* Desc: Returns the maximum number of milliseconds the app is allowed to313* wait before curl_multi_socket() or curl_multi_perform() must be314* called (to allow libcurl's timed events to take place).315*316* Returns: CURLM error code.317*/318CURL_EXTERN CURLMcode curl_multi_timeout(CURLM *multi_handle,319long *milliseconds);320321#undef CINIT /* re-using the same name as in curl.h */322323#ifdef CURL_ISOCPP324#define CINIT(name,type,num) CURLMOPT_ ## name = CURLOPTTYPE_ ## type + num325#else326/* The macro "##" is ISO C, we assume pre-ISO C doesn't support it. */327#define LONG CURLOPTTYPE_LONG328#define OBJECTPOINT CURLOPTTYPE_OBJECTPOINT329#define FUNCTIONPOINT CURLOPTTYPE_FUNCTIONPOINT330#define OFF_T CURLOPTTYPE_OFF_T331#define CINIT(name,type,number) CURLMOPT_/**/name = type + number332#endif333334typedef enum {335/* This is the socket callback function pointer */336CINIT(SOCKETFUNCTION, FUNCTIONPOINT, 1),337338/* This is the argument passed to the socket callback */339CINIT(SOCKETDATA, OBJECTPOINT, 2),340341/* set to 1 to enable pipelining for this multi handle */342CINIT(PIPELINING, LONG, 3),343344/* This is the timer callback function pointer */345CINIT(TIMERFUNCTION, FUNCTIONPOINT, 4),346347/* This is the argument passed to the timer callback */348CINIT(TIMERDATA, OBJECTPOINT, 5),349350/* maximum number of entries in the connection cache */351CINIT(MAXCONNECTS, LONG, 6),352353/* maximum number of (pipelining) connections to one host */354CINIT(MAX_HOST_CONNECTIONS, LONG, 7),355356/* maximum number of requests in a pipeline */357CINIT(MAX_PIPELINE_LENGTH, LONG, 8),358359/* a connection with a content-length longer than this360will not be considered for pipelining */361CINIT(CONTENT_LENGTH_PENALTY_SIZE, OFF_T, 9),362363/* a connection with a chunk length longer than this364will not be considered for pipelining */365CINIT(CHUNK_LENGTH_PENALTY_SIZE, OFF_T, 10),366367/* a list of site names(+port) that are blacklisted from368pipelining */369CINIT(PIPELINING_SITE_BL, OBJECTPOINT, 11),370371/* a list of server types that are blacklisted from372pipelining */373CINIT(PIPELINING_SERVER_BL, OBJECTPOINT, 12),374375/* maximum number of open connections in total */376CINIT(MAX_TOTAL_CONNECTIONS, LONG, 13),377378/* This is the server push callback function pointer */379CINIT(PUSHFUNCTION, FUNCTIONPOINT, 14),380381/* This is the argument passed to the server push callback */382CINIT(PUSHDATA, OBJECTPOINT, 15),383384CURLMOPT_LASTENTRY /* the last unused */385} CURLMoption;386387388/*389* Name: curl_multi_setopt()390*391* Desc: Sets options for the multi handle.392*393* Returns: CURLM error code.394*/395CURL_EXTERN CURLMcode curl_multi_setopt(CURLM *multi_handle,396CURLMoption option, ...);397398399/*400* Name: curl_multi_assign()401*402* Desc: This function sets an association in the multi handle between the403* given socket and a private pointer of the application. This is404* (only) useful for curl_multi_socket uses.405*406* Returns: CURLM error code.407*/408CURL_EXTERN CURLMcode curl_multi_assign(CURLM *multi_handle,409curl_socket_t sockfd, void *sockp);410411412/*413* Name: curl_push_callback414*415* Desc: This callback gets called when a new stream is being pushed by the416* server. It approves or denies the new stream.417*418* Returns: CURL_PUSH_OK or CURL_PUSH_DENY.419*/420#define CURL_PUSH_OK 0421#define CURL_PUSH_DENY 1422423struct curl_pushheaders; /* forward declaration only */424425CURL_EXTERN char *curl_pushheader_bynum(struct curl_pushheaders *h,426size_t num);427CURL_EXTERN char *curl_pushheader_byname(struct curl_pushheaders *h,428const char *name);429430typedef int (*curl_push_callback)(CURL *parent,431CURL *easy,432size_t num_headers,433struct curl_pushheaders *headers,434void *userp);435436#ifdef __cplusplus437} /* end of extern "C" */438#endif439440#endif441442443