libevent
rpc.h
Go to the documentation of this file.
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_
28 #define _EVENT2_RPC_H_
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
76 #define EVTAG_HAS(msg, member) \
77  ((msg)->member##_set == 1)
78 
79 #ifndef _EVENT2_RPC_COMPAT_H_
80 
88 #define EVTAG_ASSIGN(msg, member, value) \
89  (*(msg)->base->member##_assign)((msg), (value))
90 
98 #define EVTAG_ASSIGN_WITH_LEN(msg, member, value, len) \
99  (*(msg)->base->member##_assign)((msg), (value), (len))
100 
108 #define EVTAG_GET(msg, member, pvalue) \
109  (*(msg)->base->member##_get)((msg), (pvalue))
110 
119 #define EVTAG_GET_WITH_LEN(msg, member, pvalue, plen) \
120  (*(msg)->base->member##_get)((msg), (pvalue), (plen))
121 
122 #endif /* _EVENT2_RPC_COMPAT_H_ */
123 
127 #define EVTAG_ARRAY_ADD_VALUE(msg, member, value) \
128  (*(msg)->base->member##_add)((msg), (value))
129 
132 #define EVTAG_ARRAY_ADD(msg, member) \
133  (*(msg)->base->member##_add)(msg)
134 
137 #define EVTAG_ARRAY_GET(msg, member, offset, pvalue) \
138  (*(msg)->base->member##_get)((msg), (offset), (pvalue))
139 
142 #define EVTAG_ARRAY_LEN(msg, member) ((msg)->member##_length)
143 
144 
145 struct evbuffer;
146 struct event_base;
147 struct evrpc_req_generic;
148 struct evrpc_request_wrapper;
149 struct evrpc;
150 
155 #define EVRPC_STRUCT(rpcname) struct evrpc_req__##rpcname
156 
157 struct evhttp_request;
158 struct evrpc_status;
159 struct evrpc_hook_meta;
160 
172 #define EVRPC_HEADER(rpcname, reqstruct, rplystruct) \
173 EVRPC_STRUCT(rpcname) { \
174  struct evrpc_hook_meta *hook_meta; \
175  struct reqstruct* request; \
176  struct rplystruct* reply; \
177  struct evrpc* rpc; \
178  struct evhttp_request* http_req; \
179  struct evbuffer* rpc_data; \
180 }; \
181 int evrpc_send_request_##rpcname(struct evrpc_pool *, \
182  struct reqstruct *, struct rplystruct *, \
183  void (*)(struct evrpc_status *, \
184  struct reqstruct *, struct rplystruct *, void *cbarg), \
185  void *);
186 
187 struct evrpc_pool;
188 
190 struct evrpc_request_wrapper *evrpc_make_request_ctx(
191  struct evrpc_pool *pool, void *request, void *reply,
192  const char *rpcname,
193  void (*req_marshal)(struct evbuffer*, void *),
194  void (*rpl_clear)(void *),
195  int (*rpl_unmarshal)(void *, struct evbuffer *),
196  void (*cb)(struct evrpc_status *, void *, void *, void *),
197  void *cbarg);
198 
213 #define EVRPC_MAKE_CTX(rpcname, reqstruct, rplystruct, \
214  pool, request, reply, cb, cbarg) \
215  evrpc_make_request_ctx(pool, request, reply, \
216  #rpcname, \
217  (void (*)(struct evbuffer *, void *))reqstruct##_marshal, \
218  (void (*)(void *))rplystruct##_clear, \
219  (int (*)(void *, struct evbuffer *))rplystruct##_unmarshal, \
220  (void (*)(struct evrpc_status *, void *, void *, void *))cb, \
221  cbarg)
222 
233 #define EVRPC_GENERATE(rpcname, reqstruct, rplystruct) \
234  int evrpc_send_request_##rpcname(struct evrpc_pool *pool, \
235  struct reqstruct *request, struct rplystruct *reply, \
236  void (*cb)(struct evrpc_status *, \
237  struct reqstruct *, struct rplystruct *, void *cbarg), \
238  void *cbarg) { \
239  return evrpc_send_request_generic(pool, request, reply, \
240  (void (*)(struct evrpc_status *, void *, void *, void *))cb, \
241  cbarg, \
242  #rpcname, \
243  (void (*)(struct evbuffer *, void *))reqstruct##_marshal, \
244  (void (*)(void *))rplystruct##_clear, \
245  (int (*)(void *, struct evbuffer *))rplystruct##_unmarshal); \
246 }
247 
257 #define EVRPC_REQUEST_HTTP(rpc_req) (rpc_req)->http_req
258 
260 void evrpc_request_done(struct evrpc_req_generic *req);
261 
263 void *evrpc_get_request(struct evrpc_req_generic *req);
264 void *evrpc_get_reply(struct evrpc_req_generic *req);
265 
274 #define EVRPC_REQUEST_DONE(rpc_req) do { \
275  struct evrpc_req_generic *_req = (struct evrpc_req_generic *)(rpc_req); \
276  evrpc_request_done(_req); \
277 } while (0)
278 
279 
280 struct evrpc_base;
281 struct evhttp;
282 
283 /* functions to start up the rpc system */
284 
291 struct evrpc_base *evrpc_init(struct evhttp *server);
292 
301 void evrpc_free(struct evrpc_base *base);
302 
319 #define EVRPC_REGISTER(base, name, request, reply, callback, cbarg) \
320  evrpc_register_generic(base, #name, \
321  (void (*)(struct evrpc_req_generic *, void *))callback, cbarg, \
322  (void *(*)(void *))request##_new, NULL, \
323  (void (*)(void *))request##_free, \
324  (int (*)(void *, struct evbuffer *))request##_unmarshal, \
325  (void *(*)(void *))reply##_new, NULL, \
326  (void (*)(void *))reply##_free, \
327  (int (*)(void *))reply##_complete, \
328  (void (*)(struct evbuffer *, void *))reply##_marshal)
329 
337 int evrpc_register_rpc(struct evrpc_base *, struct evrpc *,
338  void (*)(struct evrpc_req_generic*, void *), void *);
339 
348 #define EVRPC_UNREGISTER(base, name) evrpc_unregister_rpc((base), #name)
349 
350 int evrpc_unregister_rpc(struct evrpc_base *base, const char *name);
351 
352 /*
353  * Client-side RPC support
354  */
355 
356 struct evhttp_connection;
357 struct evrpc_status;
358 
374 #define EVRPC_MAKE_REQUEST(name, pool, request, reply, cb, cbarg) \
375  evrpc_send_request_##name((pool), (request), (reply), (cb), (cbarg))
376 
388 int evrpc_make_request(struct evrpc_request_wrapper *ctx);
389 
400 struct evrpc_pool *evrpc_pool_new(struct event_base *base);
406 void evrpc_pool_free(struct evrpc_pool *pool);
407 
416 void evrpc_pool_add_connection(struct evrpc_pool *pool,
417  struct evhttp_connection *evcon);
418 
427 void evrpc_pool_remove_connection(struct evrpc_pool *pool,
428  struct evhttp_connection *evcon);
429 
445 void evrpc_pool_set_timeout(struct evrpc_pool *pool, int timeout_in_secs);
446 
455 };
456 
457 #ifndef WIN32
458 
460 #define INPUT EVRPC_INPUT
461 
463 #define OUTPUT EVRPC_OUTPUT
464 #endif
465 
474 };
475 
492 void *evrpc_add_hook(void *vbase,
493  enum EVRPC_HOOK_TYPE hook_type,
494  int (*cb)(void *, struct evhttp_request *, struct evbuffer *, void *),
495  void *cb_arg);
496 
505 int evrpc_remove_hook(void *vbase,
506  enum EVRPC_HOOK_TYPE hook_type,
507  void *handle);
508 
514 int
515 evrpc_resume_request(void *vbase, void *ctx, enum EVRPC_HOOK_RESULT res);
516 
528 void evrpc_hook_add_meta(void *ctx, const char *key,
529  const void *data, size_t data_size);
530 
541 int evrpc_hook_find_meta(void *ctx, const char *key,
542  void **data, size_t *data_size);
543 
550 struct evhttp_connection *evrpc_hook_get_connection(void *ctx);
551 
559 int evrpc_send_request_generic(struct evrpc_pool *pool,
560  void *request, void *reply,
561  void (*cb)(struct evrpc_status *, void *, void *, void *),
562  void *cb_arg,
563  const char *rpcname,
564  void (*req_marshal)(struct evbuffer *, void *),
565  void (*rpl_clear)(void *),
566  int (*rpl_unmarshal)(void *, struct evbuffer *));
567 
575 int
576 evrpc_register_generic(struct evrpc_base *base, const char *name,
577  void (*callback)(struct evrpc_req_generic *, void *), void *cbarg,
578  void *(*req_new)(void *), void *req_new_arg, void (*req_free)(void *),
579  int (*req_unmarshal)(void *, struct evbuffer *),
580  void *(*rpl_new)(void *), void *rpl_new_arg, void (*rpl_free)(void *),
581  int (*rpl_complete)(void *),
582  void (*rpl_marshal)(struct evbuffer *, void *));
583 
585 struct evrpc_pool* evrpc_request_get_pool(struct evrpc_request_wrapper *ctx);
586 void evrpc_request_set_pool(struct evrpc_request_wrapper *ctx,
587  struct evrpc_pool *pool);
588 void evrpc_request_set_cb(struct evrpc_request_wrapper *ctx,
589  void (*cb)(struct evrpc_status*, void *request, void *reply, void *arg),
590  void *cb_arg);
591 
592 #ifdef __cplusplus
593 }
594 #endif
595 
596 #endif /* _EVENT2_RPC_H_ */
int evrpc_hook_find_meta(void *ctx, const char *key, void **data, size_t *data_size)
retrieves meta data previously associated
int evrpc_register_generic(struct evrpc_base *base, const char *name, void(*callback)(struct evrpc_req_generic *, void *), void *cbarg, void *(*req_new)(void *), void *req_new_arg, void(*req_free)(void *), int(*req_unmarshal)(void *, struct evbuffer *), void *(*rpl_new)(void *), void *rpl_new_arg, void(*rpl_free)(void *), int(*rpl_complete)(void *), void(*rpl_marshal)(struct evbuffer *, void *))
Function for registering a generic RPC with the RPC base.
int evrpc_remove_hook(void *vbase, enum EVRPC_HOOK_TYPE hook_type, void *handle)
removes a previously added hook
pause processing request until resumed
Definition: rpc.h:473
void evrpc_pool_set_timeout(struct evrpc_pool *pool, int timeout_in_secs)
Sets the timeout in secs after which a request has to complete.
struct evrpc_request_wrapper * evrpc_make_request_ctx(struct evrpc_pool *pool, void *request, void *reply, const char *rpcname, void(*req_marshal)(struct evbuffer *, void *), void(*rpl_clear)(void *), int(*rpl_unmarshal)(void *, struct evbuffer *), void(*cb)(struct evrpc_status *, void *, void *, void *), void *cbarg)
use EVRPC_GENERATE instead
void * evrpc_get_request(struct evrpc_req_generic *req)
accessors for request and reply
indicates the rpc should be terminated
Definition: rpc.h:471
int evrpc_register_rpc(struct evrpc_base *, struct evrpc *, void(*)(struct evrpc_req_generic *, void *), void *)
Low level function for registering an RPC with a server.
apply the function to an output hook
Definition: rpc.h:454
void evrpc_pool_add_connection(struct evrpc_pool *pool, struct evhttp_connection *evcon)
Adds a connection over which rpc can be dispatched to the pool.
void evrpc_free(struct evrpc_base *base)
Frees the evrpc base.
struct evrpc_base * evrpc_init(struct evhttp *server)
Creates a new rpc base from which RPC requests can be received.
continue processing the rpc
Definition: rpc.h:472
struct evrpc_pool * evrpc_pool_new(struct event_base *base)
creates an rpc connection pool
EVRPC_HOOK_TYPE
Hooks for changing the input and output of RPCs; this can be used to implement compression, authentication, encryption, ...
Definition: rpc.h:452
An evbuffer is an opaque data type for efficiently buffering data to be sent or received on the netwo...
Definition: buffer.h:95
void evrpc_request_done(struct evrpc_req_generic *req)
completes the server response to an rpc request
void evrpc_pool_remove_connection(struct evrpc_pool *pool, struct evhttp_connection *evcon)
Removes a connection from the pool.
struct evrpc_pool * evrpc_request_get_pool(struct evrpc_request_wrapper *ctx)
accessors for obscure and undocumented functionality
struct evhttp_connection * evrpc_hook_get_connection(void *ctx)
returns the connection object associated with the request
void evrpc_hook_add_meta(void *ctx, const char *key, const void *data, size_t data_size)
adds meta data to request
void * evrpc_add_hook(void *vbase, enum EVRPC_HOOK_TYPE hook_type, int(*cb)(void *, struct evhttp_request *, struct evbuffer *, void *), void *cb_arg)
adds a processing hook to either an rpc base or rpc pool
int evrpc_resume_request(void *vbase, void *ctx, enum EVRPC_HOOK_RESULT res)
resume a paused request
void evrpc_pool_free(struct evrpc_pool *pool)
frees an rpc connection pool
int evrpc_send_request_generic(struct evrpc_pool *pool, void *request, void *reply, void(*cb)(struct evrpc_status *, void *, void *, void *), void *cb_arg, const char *rpcname, void(*req_marshal)(struct evbuffer *, void *), void(*rpl_clear)(void *), int(*rpl_unmarshal)(void *, struct evbuffer *))
Function for sending a generic RPC request.
int evrpc_make_request(struct evrpc_request_wrapper *ctx)
Makes an RPC request based on the provided context.
Structure to hold information and state for a Libevent dispatch loop.
Definition: event.h:213
apply the function to an input hook
Definition: rpc.h:453
EVRPC_HOOK_RESULT
Return value from hook processing functions.
Definition: rpc.h:470