multi.h 12 KB

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