rpc.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * Copyright (c) 2006-2007 Niels Provos <provos@citi.umich.edu>
  3. * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef EVENT2_RPC_H_INCLUDED_
  28. #define EVENT2_RPC_H_INCLUDED_
  29. /* For int types. */
  30. #include <event2/util.h>
  31. #include <event2/visibility.h>
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /** @file rpc.h
  36. *
  37. * This header files provides basic support for an RPC server and client.
  38. *
  39. * To support RPCs in a server, every supported RPC command needs to be
  40. * defined and registered.
  41. *
  42. * EVRPC_HEADER(SendCommand, Request, Reply);
  43. *
  44. * SendCommand is the name of the RPC command.
  45. * Request is the name of a structure generated by event_rpcgen.py.
  46. * It contains all parameters relating to the SendCommand RPC. The
  47. * server needs to fill in the Reply structure.
  48. * Reply is the name of a structure generated by event_rpcgen.py. It
  49. * contains the answer to the RPC.
  50. *
  51. * To register an RPC with an HTTP server, you need to first create an RPC
  52. * base with:
  53. *
  54. * struct evrpc_base *base = evrpc_init(http);
  55. *
  56. * A specific RPC can then be registered with
  57. *
  58. * EVRPC_REGISTER(base, SendCommand, Request, Reply, FunctionCB, arg);
  59. *
  60. * when the server receives an appropriately formatted RPC, the user callback
  61. * is invoked. The callback needs to fill in the reply structure.
  62. *
  63. * void FunctionCB(EVRPC_STRUCT(SendCommand)* rpc, void *arg);
  64. *
  65. * To send the reply, call EVRPC_REQUEST_DONE(rpc);
  66. *
  67. * See the regression test for an example.
  68. */
  69. /**
  70. Determines if the member has been set in the message
  71. @param msg the message to inspect
  72. @param member the member variable to test for presences
  73. @return 1 if it's present or 0 otherwise.
  74. */
  75. #define EVTAG_HAS(msg, member) \
  76. ((msg)->member##_set == 1)
  77. #ifndef EVENT2_RPC_COMPAT_H_INCLUDED_
  78. /**
  79. Assigns a value to the member in the message.
  80. @param msg the message to which to assign a value
  81. @param member the name of the member variable
  82. @param value the value to assign
  83. */
  84. #define EVTAG_ASSIGN(msg, member, value) \
  85. (*(msg)->base->member##_assign)((msg), (value))
  86. /**
  87. Assigns a value to the member in the message.
  88. @param msg the message to which to assign a value
  89. @param member the name of the member variable
  90. @param value the value to assign
  91. @param len the length of the value
  92. */
  93. #define EVTAG_ASSIGN_WITH_LEN(msg, member, value, len) \
  94. (*(msg)->base->member##_assign)((msg), (value), (len))
  95. /**
  96. Returns the value for a member.
  97. @param msg the message from which to get the value
  98. @param member the name of the member variable
  99. @param pvalue a pointer to the variable to hold the value
  100. @return 0 on success, -1 otherwise.
  101. */
  102. #define EVTAG_GET(msg, member, pvalue) \
  103. (*(msg)->base->member##_get)((msg), (pvalue))
  104. /**
  105. Returns the value for a member.
  106. @param msg the message from which to get the value
  107. @param member the name of the member variable
  108. @param pvalue a pointer to the variable to hold the value
  109. @param plen a pointer to the length of the value
  110. @return 0 on success, -1 otherwise.
  111. */
  112. #define EVTAG_GET_WITH_LEN(msg, member, pvalue, plen) \
  113. (*(msg)->base->member##_get)((msg), (pvalue), (plen))
  114. #endif /* EVENT2_RPC_COMPAT_H_INCLUDED_ */
  115. /**
  116. Adds a value to an array.
  117. */
  118. #define EVTAG_ARRAY_ADD_VALUE(msg, member, value) \
  119. (*(msg)->base->member##_add)((msg), (value))
  120. /**
  121. Allocates a new entry in the array and returns it.
  122. */
  123. #define EVTAG_ARRAY_ADD(msg, member) \
  124. (*(msg)->base->member##_add)(msg)
  125. /**
  126. Gets a variable at the specified offset from the array.
  127. */
  128. #define EVTAG_ARRAY_GET(msg, member, offset, pvalue) \
  129. (*(msg)->base->member##_get)((msg), (offset), (pvalue))
  130. /**
  131. Returns the number of entries in the array.
  132. */
  133. #define EVTAG_ARRAY_LEN(msg, member) ((msg)->member##_length)
  134. struct evbuffer;
  135. struct event_base;
  136. struct evrpc_req_generic;
  137. struct evrpc_request_wrapper;
  138. struct evrpc;
  139. /** The type of a specific RPC Message
  140. *
  141. * @param rpcname the name of the RPC message
  142. */
  143. #define EVRPC_STRUCT(rpcname) struct evrpc_req__##rpcname
  144. struct evhttp_request;
  145. struct evrpc_status;
  146. struct evrpc_hook_meta;
  147. /** Creates the definitions and prototypes for an RPC
  148. *
  149. * You need to use EVRPC_HEADER to create structures and function prototypes
  150. * needed by the server and client implementation. The structures have to be
  151. * defined in an .rpc file and converted to source code via event_rpcgen.py
  152. *
  153. * @param rpcname the name of the RPC
  154. * @param reqstruct the name of the RPC request structure
  155. * @param replystruct the name of the RPC reply structure
  156. * @see EVRPC_GENERATE()
  157. */
  158. #define EVRPC_HEADER(rpcname, reqstruct, rplystruct) \
  159. EVRPC_STRUCT(rpcname) { \
  160. struct evrpc_hook_meta *hook_meta; \
  161. struct reqstruct* request; \
  162. struct rplystruct* reply; \
  163. struct evrpc* rpc; \
  164. struct evhttp_request* http_req; \
  165. struct evbuffer* rpc_data; \
  166. }; \
  167. EVENT2_EXPORT_SYMBOL \
  168. int evrpc_send_request_##rpcname(struct evrpc_pool *, \
  169. struct reqstruct *, struct rplystruct *, \
  170. void (*)(struct evrpc_status *, \
  171. struct reqstruct *, struct rplystruct *, void *cbarg), \
  172. void *);
  173. struct evrpc_pool;
  174. /** use EVRPC_GENERATE instead */
  175. EVENT2_EXPORT_SYMBOL
  176. struct evrpc_request_wrapper *evrpc_make_request_ctx(
  177. struct evrpc_pool *pool, void *request, void *reply,
  178. const char *rpcname,
  179. void (*req_marshal)(struct evbuffer*, void *),
  180. void (*rpl_clear)(void *),
  181. int (*rpl_unmarshal)(void *, struct evbuffer *),
  182. void (*cb)(struct evrpc_status *, void *, void *, void *),
  183. void *cbarg);
  184. /** Creates a context structure that contains rpc specific information.
  185. *
  186. * EVRPC_MAKE_CTX is used to populate a RPC specific context that
  187. * contains information about marshaling the RPC data types.
  188. *
  189. * @param rpcname the name of the RPC
  190. * @param reqstruct the name of the RPC request structure
  191. * @param replystruct the name of the RPC reply structure
  192. * @param pool the evrpc_pool over which to make the request
  193. * @param request a pointer to the RPC request structure object
  194. * @param reply a pointer to the RPC reply structure object
  195. * @param cb the callback function to call when the RPC has completed
  196. * @param cbarg the argument to supply to the callback
  197. */
  198. #define EVRPC_MAKE_CTX(rpcname, reqstruct, rplystruct, \
  199. pool, request, reply, cb, cbarg) \
  200. evrpc_make_request_ctx(pool, request, reply, \
  201. #rpcname, \
  202. (void (*)(struct evbuffer *, void *))reqstruct##_marshal, \
  203. (void (*)(void *))rplystruct##_clear, \
  204. (int (*)(void *, struct evbuffer *))rplystruct##_unmarshal, \
  205. (void (*)(struct evrpc_status *, void *, void *, void *))cb, \
  206. cbarg)
  207. /** Generates the code for receiving and sending an RPC message
  208. *
  209. * EVRPC_GENERATE is used to create the code corresponding to sending
  210. * and receiving a particular RPC message
  211. *
  212. * @param rpcname the name of the RPC
  213. * @param reqstruct the name of the RPC request structure
  214. * @param replystruct the name of the RPC reply structure
  215. * @see EVRPC_HEADER()
  216. */
  217. #define EVRPC_GENERATE(rpcname, reqstruct, rplystruct) \
  218. int evrpc_send_request_##rpcname(struct evrpc_pool *pool, \
  219. struct reqstruct *request, struct rplystruct *reply, \
  220. void (*cb)(struct evrpc_status *, \
  221. struct reqstruct *, struct rplystruct *, void *cbarg), \
  222. void *cbarg) { \
  223. return evrpc_send_request_generic(pool, request, reply, \
  224. (void (*)(struct evrpc_status *, void *, void *, void *))cb, \
  225. cbarg, \
  226. #rpcname, \
  227. (void (*)(struct evbuffer *, void *))reqstruct##_marshal, \
  228. (void (*)(void *))rplystruct##_clear, \
  229. (int (*)(void *, struct evbuffer *))rplystruct##_unmarshal); \
  230. }
  231. /** Provides access to the HTTP request object underlying an RPC
  232. *
  233. * Access to the underlying http object; can be used to look at headers or
  234. * for getting the remote ip address
  235. *
  236. * @param rpc_req the rpc request structure provided to the server callback
  237. * @return an struct evhttp_request object that can be inspected for
  238. * HTTP headers or sender information.
  239. */
  240. #define EVRPC_REQUEST_HTTP(rpc_req) (rpc_req)->http_req
  241. /** completes the server response to an rpc request */
  242. EVENT2_EXPORT_SYMBOL
  243. void evrpc_request_done(struct evrpc_req_generic *req);
  244. /** accessors for request and reply */
  245. EVENT2_EXPORT_SYMBOL
  246. void *evrpc_get_request(struct evrpc_req_generic *req);
  247. EVENT2_EXPORT_SYMBOL
  248. void *evrpc_get_reply(struct evrpc_req_generic *req);
  249. /** Creates the reply to an RPC request
  250. *
  251. * EVRPC_REQUEST_DONE is used to answer a request; the reply is expected
  252. * to have been filled in. The request and reply pointers become invalid
  253. * after this call has finished.
  254. *
  255. * @param rpc_req the rpc request structure provided to the server callback
  256. */
  257. #define EVRPC_REQUEST_DONE(rpc_req) do { \
  258. struct evrpc_req_generic *req_ = (struct evrpc_req_generic *)(rpc_req); \
  259. evrpc_request_done(req_); \
  260. } while (0)
  261. struct evrpc_base;
  262. struct evhttp;
  263. /* functions to start up the rpc system */
  264. /** Creates a new rpc base from which RPC requests can be received
  265. *
  266. * @param server a pointer to an existing HTTP server
  267. * @return a newly allocated evrpc_base struct or NULL if an error occurred
  268. * @see evrpc_free()
  269. */
  270. EVENT2_EXPORT_SYMBOL
  271. struct evrpc_base *evrpc_init(struct evhttp *server);
  272. /**
  273. * Frees the evrpc base
  274. *
  275. * For now, you are responsible for making sure that no rpcs are ongoing.
  276. *
  277. * @param base the evrpc_base object to be freed
  278. * @see evrpc_init
  279. */
  280. EVENT2_EXPORT_SYMBOL
  281. void evrpc_free(struct evrpc_base *base);
  282. /** register RPCs with the HTTP Server
  283. *
  284. * registers a new RPC with the HTTP server, each RPC needs to have
  285. * a unique name under which it can be identified.
  286. *
  287. * @param base the evrpc_base structure in which the RPC should be
  288. * registered.
  289. * @param name the name of the RPC
  290. * @param request the name of the RPC request structure
  291. * @param reply the name of the RPC reply structure
  292. * @param callback the callback that should be invoked when the RPC
  293. * is received. The callback has the following prototype
  294. * void (*callback)(EVRPC_STRUCT(Message)* rpc, void *arg)
  295. * @param cbarg an additional parameter that can be passed to the callback.
  296. * The parameter can be used to carry around state.
  297. */
  298. #define EVRPC_REGISTER(base, name, request, reply, callback, cbarg) \
  299. evrpc_register_generic(base, #name, \
  300. (void (*)(struct evrpc_req_generic *, void *))callback, cbarg, \
  301. (void *(*)(void *))request##_new_with_arg, NULL, \
  302. (void (*)(void *))request##_free, \
  303. (int (*)(void *, struct evbuffer *))request##_unmarshal, \
  304. (void *(*)(void *))reply##_new_with_arg, NULL, \
  305. (void (*)(void *))reply##_free, \
  306. (int (*)(void *))reply##_complete, \
  307. (void (*)(struct evbuffer *, void *))reply##_marshal)
  308. /**
  309. Low level function for registering an RPC with a server.
  310. Use EVRPC_REGISTER() instead.
  311. @see EVRPC_REGISTER()
  312. */
  313. EVENT2_EXPORT_SYMBOL
  314. int evrpc_register_rpc(struct evrpc_base *, struct evrpc *,
  315. void (*)(struct evrpc_req_generic*, void *), void *);
  316. /**
  317. * Unregisters an already registered RPC
  318. *
  319. * @param base the evrpc_base object from which to unregister an RPC
  320. * @param name the name of the rpc to unregister
  321. * @return -1 on error or 0 when successful.
  322. * @see EVRPC_REGISTER()
  323. */
  324. #define EVRPC_UNREGISTER(base, name) evrpc_unregister_rpc((base), #name)
  325. EVENT2_EXPORT_SYMBOL
  326. int evrpc_unregister_rpc(struct evrpc_base *base, const char *name);
  327. /*
  328. * Client-side RPC support
  329. */
  330. struct evhttp_connection;
  331. struct evrpc_status;
  332. /** launches an RPC and sends it to the server
  333. *
  334. * EVRPC_MAKE_REQUEST() is used by the client to send an RPC to the server.
  335. *
  336. * @param name the name of the RPC
  337. * @param pool the evrpc_pool that contains the connection objects over which
  338. * the request should be sent.
  339. * @param request a pointer to the RPC request structure - it contains the
  340. * data to be sent to the server.
  341. * @param reply a pointer to the RPC reply structure. It is going to be filled
  342. * if the request was answered successfully
  343. * @param cb the callback to invoke when the RPC request has been answered
  344. * @param cbarg an additional argument to be passed to the client
  345. * @return 0 on success, -1 on failure
  346. */
  347. #define EVRPC_MAKE_REQUEST(name, pool, request, reply, cb, cbarg) \
  348. evrpc_send_request_##name((pool), (request), (reply), (cb), (cbarg))
  349. /**
  350. Makes an RPC request based on the provided context.
  351. This is a low-level function and should not be used directly
  352. unless a custom context object is provided. Use EVRPC_MAKE_REQUEST()
  353. instead.
  354. @param ctx a context from EVRPC_MAKE_CTX()
  355. @returns 0 on success, -1 otherwise.
  356. @see EVRPC_MAKE_REQUEST(), EVRPC_MAKE_CTX()
  357. */
  358. EVENT2_EXPORT_SYMBOL
  359. int evrpc_make_request(struct evrpc_request_wrapper *ctx);
  360. /** creates an rpc connection pool
  361. *
  362. * a pool has a number of connections associated with it.
  363. * rpc requests are always made via a pool.
  364. *
  365. * @param base a pointer to an struct event_based object; can be left NULL
  366. * in singled-threaded applications
  367. * @return a newly allocated struct evrpc_pool object or NULL if an error
  368. * occurred
  369. * @see evrpc_pool_free()
  370. */
  371. EVENT2_EXPORT_SYMBOL
  372. struct evrpc_pool *evrpc_pool_new(struct event_base *base);
  373. /** frees an rpc connection pool
  374. *
  375. * @param pool a pointer to an evrpc_pool allocated via evrpc_pool_new()
  376. * @see evrpc_pool_new()
  377. */
  378. EVENT2_EXPORT_SYMBOL
  379. void evrpc_pool_free(struct evrpc_pool *pool);
  380. /**
  381. * Adds a connection over which rpc can be dispatched to the pool.
  382. *
  383. * The connection object must have been newly created.
  384. *
  385. * @param pool the pool to which to add the connection
  386. * @param evcon the connection to add to the pool.
  387. */
  388. EVENT2_EXPORT_SYMBOL
  389. void evrpc_pool_add_connection(struct evrpc_pool *pool,
  390. struct evhttp_connection *evcon);
  391. /**
  392. * Removes a connection from the pool.
  393. *
  394. * The connection object must have been newly created.
  395. *
  396. * @param pool the pool from which to remove the connection
  397. * @param evcon the connection to remove from the pool.
  398. */
  399. EVENT2_EXPORT_SYMBOL
  400. void evrpc_pool_remove_connection(struct evrpc_pool *pool,
  401. struct evhttp_connection *evcon);
  402. /**
  403. * Sets the timeout in secs after which a request has to complete. The
  404. * RPC is completely aborted if it does not complete by then. Setting
  405. * the timeout to 0 means that it never timeouts and can be used to
  406. * implement callback type RPCs.
  407. *
  408. * Any connection already in the pool will be updated with the new
  409. * timeout. Connections added to the pool after set_timeout has be
  410. * called receive the pool timeout only if no timeout has been set
  411. * for the connection itself.
  412. *
  413. * @param pool a pointer to a struct evrpc_pool object
  414. * @param timeout_in_secs the number of seconds after which a request should
  415. * timeout and a failure be returned to the callback.
  416. */
  417. EVENT2_EXPORT_SYMBOL
  418. void evrpc_pool_set_timeout(struct evrpc_pool *pool, int timeout_in_secs);
  419. /**
  420. * Hooks for changing the input and output of RPCs; this can be used to
  421. * implement compression, authentication, encryption, ...
  422. */
  423. enum EVRPC_HOOK_TYPE {
  424. EVRPC_INPUT, /**< apply the function to an input hook */
  425. EVRPC_OUTPUT /**< apply the function to an output hook */
  426. };
  427. #ifndef _WIN32
  428. /** Deprecated alias for EVRPC_INPUT. Not available on windows, where it
  429. * conflicts with platform headers. */
  430. #define INPUT EVRPC_INPUT
  431. /** Deprecated alias for EVRPC_OUTPUT. Not available on windows, where it
  432. * conflicts with platform headers. */
  433. #define OUTPUT EVRPC_OUTPUT
  434. #endif
  435. /**
  436. * Return value from hook processing functions
  437. */
  438. enum EVRPC_HOOK_RESULT {
  439. EVRPC_TERMINATE = -1, /**< indicates the rpc should be terminated */
  440. EVRPC_CONTINUE = 0, /**< continue processing the rpc */
  441. EVRPC_PAUSE = 1 /**< pause processing request until resumed */
  442. };
  443. /** adds a processing hook to either an rpc base or rpc pool
  444. *
  445. * If a hook returns TERMINATE, the processing is aborted. On CONTINUE,
  446. * the request is immediately processed after the hook returns. If the
  447. * hook returns PAUSE, request processing stops until evrpc_resume_request()
  448. * has been called.
  449. *
  450. * The add functions return handles that can be used for removing hooks.
  451. *
  452. * @param vbase a pointer to either struct evrpc_base or struct evrpc_pool
  453. * @param hook_type either INPUT or OUTPUT
  454. * @param cb the callback to call when the hook is activated
  455. * @param cb_arg an additional argument for the callback
  456. * @return a handle to the hook so it can be removed later
  457. * @see evrpc_remove_hook()
  458. */
  459. EVENT2_EXPORT_SYMBOL
  460. void *evrpc_add_hook(void *vbase,
  461. enum EVRPC_HOOK_TYPE hook_type,
  462. int (*cb)(void *, struct evhttp_request *, struct evbuffer *, void *),
  463. void *cb_arg);
  464. /** removes a previously added hook
  465. *
  466. * @param vbase a pointer to either struct evrpc_base or struct evrpc_pool
  467. * @param hook_type either INPUT or OUTPUT
  468. * @param handle a handle returned by evrpc_add_hook()
  469. * @return 1 on success or 0 on failure
  470. * @see evrpc_add_hook()
  471. */
  472. EVENT2_EXPORT_SYMBOL
  473. int evrpc_remove_hook(void *vbase,
  474. enum EVRPC_HOOK_TYPE hook_type,
  475. void *handle);
  476. /** resume a paused request
  477. *
  478. * @param vbase a pointer to either struct evrpc_base or struct evrpc_pool
  479. * @param ctx the context pointer provided to the original hook call
  480. */
  481. EVENT2_EXPORT_SYMBOL
  482. int evrpc_resume_request(void *vbase, void *ctx, enum EVRPC_HOOK_RESULT res);
  483. /** adds meta data to request
  484. *
  485. * evrpc_hook_add_meta() allows hooks to add meta data to a request. for
  486. * a client request, the meta data can be inserted by an outgoing request hook
  487. * and retrieved by the incoming request hook.
  488. *
  489. * @param ctx the context provided to the hook call
  490. * @param key a NUL-terminated c-string
  491. * @param data the data to be associated with the key
  492. * @param data_size the size of the data
  493. */
  494. EVENT2_EXPORT_SYMBOL
  495. void evrpc_hook_add_meta(void *ctx, const char *key,
  496. const void *data, size_t data_size);
  497. /** retrieves meta data previously associated
  498. *
  499. * evrpc_hook_find_meta() can be used to retrieve meta data associated to a
  500. * request by a previous hook.
  501. * @param ctx the context provided to the hook call
  502. * @param key a NUL-terminated c-string
  503. * @param data pointer to a data pointer that will contain the retrieved data
  504. * @param data_size pointer to the size of the data
  505. * @return 0 on success or -1 on failure
  506. */
  507. EVENT2_EXPORT_SYMBOL
  508. int evrpc_hook_find_meta(void *ctx, const char *key,
  509. void **data, size_t *data_size);
  510. /**
  511. * returns the connection object associated with the request
  512. *
  513. * @param ctx the context provided to the hook call
  514. * @return a pointer to the evhttp_connection object or NULL if an error
  515. * occurred
  516. */
  517. EVENT2_EXPORT_SYMBOL
  518. struct evhttp_connection *evrpc_hook_get_connection(void *ctx);
  519. /**
  520. Function for sending a generic RPC request.
  521. Do not call this function directly, use EVRPC_MAKE_REQUEST() instead.
  522. @see EVRPC_MAKE_REQUEST()
  523. */
  524. EVENT2_EXPORT_SYMBOL
  525. int evrpc_send_request_generic(struct evrpc_pool *pool,
  526. void *request, void *reply,
  527. void (*cb)(struct evrpc_status *, void *, void *, void *),
  528. void *cb_arg,
  529. const char *rpcname,
  530. void (*req_marshal)(struct evbuffer *, void *),
  531. void (*rpl_clear)(void *),
  532. int (*rpl_unmarshal)(void *, struct evbuffer *));
  533. /**
  534. Function for registering a generic RPC with the RPC base.
  535. Do not call this function directly, use EVRPC_REGISTER() instead.
  536. @see EVRPC_REGISTER()
  537. */
  538. EVENT2_EXPORT_SYMBOL
  539. int evrpc_register_generic(struct evrpc_base *base, const char *name,
  540. void (*callback)(struct evrpc_req_generic *, void *), void *cbarg,
  541. void *(*req_new)(void *), void *req_new_arg, void (*req_free)(void *),
  542. int (*req_unmarshal)(void *, struct evbuffer *),
  543. void *(*rpl_new)(void *), void *rpl_new_arg, void (*rpl_free)(void *),
  544. int (*rpl_complete)(void *),
  545. void (*rpl_marshal)(struct evbuffer *, void *));
  546. /** accessors for obscure and undocumented functionality */
  547. EVENT2_EXPORT_SYMBOL
  548. struct evrpc_pool* evrpc_request_get_pool(struct evrpc_request_wrapper *ctx);
  549. EVENT2_EXPORT_SYMBOL
  550. void evrpc_request_set_pool(struct evrpc_request_wrapper *ctx,
  551. struct evrpc_pool *pool);
  552. EVENT2_EXPORT_SYMBOL
  553. void evrpc_request_set_cb(struct evrpc_request_wrapper *ctx,
  554. void (*cb)(struct evrpc_status*, void *request, void *reply, void *arg),
  555. void *cb_arg);
  556. #ifdef __cplusplus
  557. }
  558. #endif
  559. #endif /* EVENT2_RPC_H_INCLUDED_ */