Planeshift

multi.h

Go to the documentation of this file.
00001 #ifndef __CURL_MULTI_H
00002 #define __CURL_MULTI_H
00003 /***************************************************************************
00004  *                                  _   _ ____  _
00005  *  Project                     ___| | | |  _ \| |
00006  *                             / __| | | | |_) | |
00007  *                            | (__| |_| |  _ <| |___
00008  *                             \___|\___/|_| \_\_____|
00009  *
00010  * Copyright (C) 1998 - 2007, Daniel Stenberg, <[email protected]>, et al.
00011  *
00012  * This software is licensed as described in the file COPYING, which
00013  * you should have received as part of this distribution. The terms
00014  * are also available at http://curl.haxx.se/docs/copyright.html.
00015  *
00016  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00017  * copies of the Software, and permit persons to whom the Software is
00018  * furnished to do so, under the terms of the COPYING file.
00019  *
00020  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00021  * KIND, either express or implied.
00022  *
00023  * $Id: multi.h 8795 2013-09-14 06:15:05Z joelyon $
00024  ***************************************************************************/
00025 /*
00026   This is an "external" header file. Don't give away any internals here!
00027 
00028   GOALS
00029 
00030   o Enable a "pull" interface. The application that uses libcurl decides where
00031     and when to ask libcurl to get/send data.
00032 
00033   o Enable multiple simultaneous transfers in the same thread without making it
00034     complicated for the application.
00035 
00036   o Enable the application to select() on its own file descriptors and curl's
00037     file descriptors simultaneous easily.
00038 
00039 */
00040 
00041 /*
00042  * This header file should not really need to include "curl.h" since curl.h
00043  * itself includes this file and we expect user applications to do #include
00044  * <curl/curl.h> without the need for especially including multi.h.
00045  *
00046  * For some reason we added this include here at one point, and rather than to
00047  * break existing (wrongly written) libcurl applications, we leave it as-is
00048  * but with this warning attached.
00049  */
00050 #include "curl.h"
00051 
00052 #ifdef  __cplusplus
00053 extern "C" {
00054 #endif
00055 
00056 typedef void CURLM;
00057 
00058 typedef enum {
00059   CURLM_CALL_MULTI_PERFORM = -1, /* please call curl_multi_perform() or
00060                                     curl_multi_socket*() soon */
00061   CURLM_OK,
00062   CURLM_BAD_HANDLE,      /* the passed-in handle is not a valid CURLM handle */
00063   CURLM_BAD_EASY_HANDLE, /* an easy handle was not good/valid */
00064   CURLM_OUT_OF_MEMORY,   /* if you ever get this, you're in deep sh*t */
00065   CURLM_INTERNAL_ERROR,  /* this is a libcurl bug */
00066   CURLM_BAD_SOCKET,      /* the passed in socket argument did not match */
00067   CURLM_UNKNOWN_OPTION,  /* curl_multi_setopt() with unsupported option */
00068   CURLM_LAST
00069 } CURLMcode;
00070 
00071 /* just to make code nicer when using curl_multi_socket() you can now check
00072    for CURLM_CALL_MULTI_SOCKET too in the same style it works for
00073    curl_multi_perform() and CURLM_CALL_MULTI_PERFORM */
00074 #define CURLM_CALL_MULTI_SOCKET CURLM_CALL_MULTI_PERFORM
00075 
00076 typedef enum {
00077   CURLMSG_NONE, /* first, not used */
00078   CURLMSG_DONE, /* This easy handle has completed. 'result' contains
00079                    the CURLcode of the transfer */
00080   CURLMSG_LAST /* last, not used */
00081 } CURLMSG;
00082 
00083 struct CURLMsg {
00084   CURLMSG msg;       /* what this message means */
00085   CURL *easy_handle; /* the handle it concerns */
00086   union {
00087     void *whatever;    /* message-specific data */
00088     CURLcode result;   /* return code for transfer */
00089   } data;
00090 };
00091 typedef struct CURLMsg CURLMsg;
00092 
00093 /*
00094  * Name:    curl_multi_init()
00095  *
00096  * Desc:    inititalize multi-style curl usage
00097  *
00098  * Returns: a new CURLM handle to use in all 'curl_multi' functions.
00099  */
00100 CURL_EXTERN CURLM *curl_multi_init(void);
00101 
00102 /*
00103  * Name:    curl_multi_add_handle()
00104  *
00105  * Desc:    add a standard curl handle to the multi stack
00106  *
00107  * Returns: CURLMcode type, general multi error code.
00108  */
00109 CURL_EXTERN CURLMcode curl_multi_add_handle(CURLM *multi_handle,
00110                                             CURL *curl_handle);
00111 
00112  /*
00113   * Name:    curl_multi_remove_handle()
00114   *
00115   * Desc:    removes a curl handle from the multi stack again
00116   *
00117   * Returns: CURLMcode type, general multi error code.
00118   */
00119 CURL_EXTERN CURLMcode curl_multi_remove_handle(CURLM *multi_handle,
00120                                                CURL *curl_handle);
00121 
00122  /*
00123   * Name:    curl_multi_fdset()
00124   *
00125   * Desc:    Ask curl for its fd_set sets. The app can use these to select() or
00126   *          poll() on. We want curl_multi_perform() called as soon as one of
00127   *          them are ready.
00128   *
00129   * Returns: CURLMcode type, general multi error code.
00130   */
00131 CURL_EXTERN CURLMcode curl_multi_fdset(CURLM *multi_handle,
00132                                        fd_set *read_fd_set,
00133                                        fd_set *write_fd_set,
00134                                        fd_set *exc_fd_set,
00135                                        int *max_fd);
00136 
00137  /*
00138   * Name:    curl_multi_perform()
00139   *
00140   * Desc:    When the app thinks there's data available for curl it calls this
00141   *          function to read/write whatever there is right now. This returns
00142   *          as soon as the reads and writes are done. This function does not
00143   *          require that there actually is data available for reading or that
00144   *          data can be written, it can be called just in case. It returns
00145   *          the number of handles that still transfer data in the second
00146   *          argument's integer-pointer.
00147   *
00148   * Returns: CURLMcode type, general multi error code. *NOTE* that this only
00149   *          returns errors etc regarding the whole multi stack. There might
00150   *          still have occurred problems on invidual transfers even when this
00151   *          returns OK.
00152   */
00153 CURL_EXTERN CURLMcode curl_multi_perform(CURLM *multi_handle,
00154                                          int *running_handles);
00155 
00156  /*
00157   * Name:    curl_multi_cleanup()
00158   *
00159   * Desc:    Cleans up and removes a whole multi stack. It does not free or
00160   *          touch any individual easy handles in any way. We need to define
00161   *          in what state those handles will be if this function is called
00162   *          in the middle of a transfer.
00163   *
00164   * Returns: CURLMcode type, general multi error code.
00165   */
00166 CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle);
00167 
00168 /*
00169  * Name:    curl_multi_info_read()
00170  *
00171  * Desc:    Ask the multi handle if there's any messages/informationals from
00172  *          the individual transfers. Messages include informationals such as
00173  *          error code from the transfer or just the fact that a transfer is
00174  *          completed. More details on these should be written down as well.
00175  *
00176  *          Repeated calls to this function will return a new struct each
00177  *          time, until a special "end of msgs" struct is returned as a signal
00178  *          that there is no more to get at this point.
00179  *
00180  *          The data the returned pointer points to will not survive calling
00181  *          curl_multi_cleanup().
00182  *
00183  *          The 'CURLMsg' struct is meant to be very simple and only contain
00184  *          very basic informations. If more involved information is wanted,
00185  *          we will provide the particular "transfer handle" in that struct
00186  *          and that should/could/would be used in subsequent
00187  *          curl_easy_getinfo() calls (or similar). The point being that we
00188  *          must never expose complex structs to applications, as then we'll
00189  *          undoubtably get backwards compatibility problems in the future.
00190  *
00191  * Returns: A pointer to a filled-in struct, or NULL if it failed or ran out
00192  *          of structs. It also writes the number of messages left in the
00193  *          queue (after this read) in the integer the second argument points
00194  *          to.
00195  */
00196 CURL_EXTERN CURLMsg *curl_multi_info_read(CURLM *multi_handle,
00197                                           int *msgs_in_queue);
00198 
00199 /*
00200  * Name:    curl_multi_strerror()
00201  *
00202  * Desc:    The curl_multi_strerror function may be used to turn a CURLMcode
00203  *          value into the equivalent human readable error string.  This is
00204  *          useful for printing meaningful error messages.
00205  *
00206  * Returns: A pointer to a zero-terminated error message.
00207  */
00208 CURL_EXTERN const char *curl_multi_strerror(CURLMcode);
00209 
00210 /*
00211  * Name:    curl_multi_socket() and
00212  *          curl_multi_socket_all()
00213  *
00214  * Desc:    An alternative version of curl_multi_perform() that allows the
00215  *          application to pass in one of the file descriptors that have been
00216  *          detected to have "action" on them and let libcurl perform.
00217  *          See man page for details.
00218  */
00219 #define CURL_POLL_NONE   0
00220 #define CURL_POLL_IN     1
00221 #define CURL_POLL_OUT    2
00222 #define CURL_POLL_INOUT  3
00223 #define CURL_POLL_REMOVE 4
00224 
00225 #define CURL_SOCKET_TIMEOUT CURL_SOCKET_BAD
00226 
00227 #define CURL_CSELECT_IN   0x01
00228 #define CURL_CSELECT_OUT  0x02
00229 #define CURL_CSELECT_ERR  0x04
00230 
00231 typedef int (*curl_socket_callback)(CURL *easy,      /* easy handle */
00232                                     curl_socket_t s, /* socket */
00233                                     int what,        /* see above */
00234                                     void *userp,     /* private callback
00235                                                         pointer */
00236                                     void *socketp);  /* private socket
00237                                                         pointer */
00238 /*
00239  * Name:    curl_multi_timer_callback
00240  *
00241  * Desc:    Called by libcurl whenever the library detects a change in the
00242  *          maximum number of milliseconds the app is allowed to wait before
00243  *          curl_multi_socket() or curl_multi_perform() must be called
00244  *          (to allow libcurl's timed events to take place).
00245  *
00246  * Returns: The callback should return zero.
00247  */
00248 typedef int (*curl_multi_timer_callback)(CURLM *multi,    /* multi handle */
00249                                          long timeout_ms, /* see above */
00250                                          void *userp);    /* private callback
00251                                                              pointer */
00252 
00253 CURL_EXTERN CURLMcode curl_multi_socket(CURLM *multi_handle, curl_socket_t s,
00254                                         int *running_handles);
00255 
00256 CURL_EXTERN CURLMcode curl_multi_socket_action(CURLM *multi_handle,
00257                                                curl_socket_t s,
00258                                                int ev_bitmask,
00259                                                int *running_handles);
00260 
00261 CURL_EXTERN CURLMcode curl_multi_socket_all(CURLM *multi_handle,
00262                                             int *running_handles);
00263 
00264 #ifndef CURL_ALLOW_OLD_MULTI_SOCKET
00265 /* This macro below was added in 7.16.3 to push users who recompile to use
00266    the new curl_multi_socket_action() instead of the old curl_multi_socket()
00267 */
00268 #define curl_multi_socket(x,y,z) curl_multi_socket_action(x,y,0,z)
00269 #endif
00270 
00271 /*
00272  * Name:    curl_multi_timeout()
00273  *
00274  * Desc:    Returns the maximum number of milliseconds the app is allowed to
00275  *          wait before curl_multi_socket() or curl_multi_perform() must be
00276  *          called (to allow libcurl's timed events to take place).
00277  *
00278  * Returns: CURLM error code.
00279  */
00280 CURL_EXTERN CURLMcode curl_multi_timeout(CURLM *multi_handle,
00281                                          long *milliseconds);
00282 
00283 #undef CINIT /* re-using the same name as in curl.h */
00284 
00285 #ifdef CURL_ISOCPP
00286 #define CINIT(name,type,num) CURLMOPT_ ## name = CURLOPTTYPE_ ## type + num
00287 #else
00288 /* The macro "##" is ISO C, we assume pre-ISO C doesn't support it. */
00289 #define LONG          CURLOPTTYPE_LONG
00290 #define OBJECTPOINT   CURLOPTTYPE_OBJECTPOINT
00291 #define FUNCTIONPOINT CURLOPTTYPE_FUNCTIONPOINT
00292 #define OFF_T         CURLOPTTYPE_OFF_T
00293 #define CINIT(name,type,number) CURLMOPT_name = type + number
00294 #endif
00295 
00296 typedef enum {
00297   /* This is the socket callback function pointer */
00298   CINIT(SOCKETFUNCTION, FUNCTIONPOINT, 1),
00299 
00300   /* This is the argument passed to the socket callback */
00301   CINIT(SOCKETDATA, OBJECTPOINT, 2),
00302 
00303     /* set to 1 to enable pipelining for this multi handle */
00304   CINIT(PIPELINING, LONG, 3),
00305 
00306    /* This is the timer callback function pointer */
00307   CINIT(TIMERFUNCTION, FUNCTIONPOINT, 4),
00308 
00309   /* This is the argument passed to the timer callback */
00310   CINIT(TIMERDATA, OBJECTPOINT, 5),
00311 
00312   /* maximum number of entries in the connection cache */
00313   CINIT(MAXCONNECTS, LONG, 6),
00314 
00315   CURLMOPT_LASTENTRY /* the last unused */
00316 } CURLMoption;
00317 
00318 
00319 /*
00320  * Name:    curl_multi_setopt()
00321  *
00322  * Desc:    Sets options for the multi handle.
00323  *
00324  * Returns: CURLM error code.
00325  */
00326 CURL_EXTERN CURLMcode curl_multi_setopt(CURLM *multi_handle,
00327                                         CURLMoption option, ...);
00328 
00329 
00330 /*
00331  * Name:    curl_multi_assign()
00332  *
00333  * Desc:    This function sets an association in the multi handle between the
00334  *          given socket and a private pointer of the application. This is
00335  *          (only) useful for curl_multi_socket uses.
00336  *
00337  * Returns: CURLM error code.
00338  */
00339 CURL_EXTERN CURLMcode curl_multi_assign(CURLM *multi_handle,
00340                                         curl_socket_t sockfd, void *sockp);
00341 
00342 #ifdef __cplusplus
00343 } /* end of extern "C" */
00344 #endif
00345 
00346 #endif