99
101
/* the callback invoked for each received rpc */
100
102
void (*cb)(struct evrpc_req_generic *, void *);
105
/* reference for further configuration */
106
struct evrpc_base *base;
109
/** The type of a specific RPC Message
111
* @param rpcname the name of the RPC message
104
113
#define EVRPC_STRUCT(rpcname) struct evrpc_req__##rpcname
106
115
struct evhttp_request;
131
140
void (*done)(struct evrpc_req_generic* rpc);
143
/** Creates the definitions and prototypes for an RPC
135
145
* You need to use EVRPC_HEADER to create structures and function prototypes
136
* needed by the server and client implementation.
146
* needed by the server and client implementation. The structures have to be
147
* defined in an .rpc file and converted to source code via event_rpcgen.py
149
* @param rpcname the name of the RPC
150
* @param reqstruct the name of the RPC request structure
151
* @param replystruct the name of the RPC reply structure
152
* @see EVRPC_GENERATE()
138
154
#define EVRPC_HEADER(rpcname, reqstruct, rplystruct) \
139
155
EVRPC_STRUCT(rpcname) { \
140
156
struct reqstruct* request; \
141
157
struct rplystruct* reply; \
142
158
struct evrpc* rpc; \
159
struct evhttp_request* http_req; \
143
160
void (*done)(struct evrpc_status *, \
144
161
struct evrpc* rpc, void *request, void *reply); \
149
166
struct reqstruct *, struct rplystruct *, void *cbarg), \
169
/** Generates the code for receiving and sending an RPC message
171
* EVRPC_GENERATE is used to create the code corresponding to sending
172
* and receiving a particular RPC message
174
* @param rpcname the name of the RPC
175
* @param reqstruct the name of the RPC request structure
176
* @param replystruct the name of the RPC reply structure
177
* @see EVRPC_HEADER()
152
179
#define EVRPC_GENERATE(rpcname, reqstruct, rplystruct) \
153
180
int evrpc_send_request_##rpcname(struct evrpc_pool *pool, \
154
181
struct reqstruct *request, struct rplystruct *reply, \
214
/** Provides access to the HTTP request object underlying an RPC
216
* Access to the underlying http object; can be used to look at headers or
217
* for getting the remote ip address
219
* @param rpc_req the rpc request structure provided to the server callback
220
* @return an struct evhttp_request object that can be inspected for
221
* HTTP headers or sender information.
223
#define EVRPC_REQUEST_HTTP(rpc_req) (rpc_req)->http_req
225
/** Creates the reply to an RPC request
189
227
* EVRPC_REQUEST_DONE is used to answer a request; the reply is expected
190
228
* to have been filled in. The request and reply pointers become invalid
191
229
* after this call has finished.
231
* @param rpc_req the rpc request structure provided to the server callback
193
233
#define EVRPC_REQUEST_DONE(rpc_req) do { \
194
234
struct evrpc_req_generic *_req = (struct evrpc_req_generic *)(rpc_req); \
219
259
/* functions to start up the rpc system */
261
/** Creates a new rpc base from which RPC requests can be received
263
* @param server a pointer to an existing HTTP server
264
* @return a newly allocated evrpc_base struct
220
267
struct evrpc_base *evrpc_init(struct evhttp *server);
222
/* frees the base - for now, you are responsible for making sure that no rpcs are ongoing */
223
void evrpc_free(struct evrpc_base *);
270
* Frees the evrpc base
272
* For now, you are responsible for making sure that no rpcs are ongoing.
274
* @param base the evrpc_base object to be freed
277
void evrpc_free(struct evrpc_base *base);
225
/* this macro is used to register RPCs with the HTTP Server */
279
/** register RPCs with the HTTP Server
281
* registers a new RPC with the HTTP server, each RPC needs to have
282
* a unique name under which it can be identified.
284
* @param base the evrpc_base structure in which the RPC should be
286
* @param name the name of the RPC
287
* @param request the name of the RPC request structure
288
* @param reply the name of the RPC reply structure
289
* @param callback the callback that should be invoked when the RPC
290
* is received. The callback has the following prototype
291
* void (*callback)(EVRPC_STRUCT(Message)* rpc, void *arg)
292
* @param cbarg an additional parameter that can be passed to the callback.
293
* The parameter can be used to carry around state.
226
295
#define EVRPC_REGISTER(base, name, request, reply, callback, cbarg) \
228
297
struct evrpc* rpc = (struct evrpc *)calloc(1, sizeof(struct evrpc)); \
234
303
int evrpc_register_rpc(struct evrpc_base *, struct evrpc *,
235
304
void (*)(struct evrpc_req_generic*, void *), void *);
237
/* Takes the named RPCs and tried to unregister it */
307
* Unregisters an already registered RPC
309
* @param base the evrpc_base object from which to unregister an RPC
310
* @param name the name of the rpc to unregister
311
* @return -1 on error or 0 when successful.
312
* @see EVRPC_REGISTER()
238
314
#define EVRPC_UNREGISTER(base, name) evrpc_unregister_rpc(base, #name)
240
int evrpc_unregister_rpc(struct evrpc_base *, const char *name);
316
int evrpc_unregister_rpc(struct evrpc_base *base, const char *name);
243
319
* Client-side RPC support
246
322
struct evrpc_pool;
247
323
struct evhttp_connection;
326
* provides information about the completed RPC request.
249
328
struct evrpc_status {
250
329
#define EVRPC_STATUS_ERR_NONE 0
251
330
#define EVRPC_STATUS_ERR_TIMEOUT 1
252
331
#define EVRPC_STATUS_ERR_BADPAYLOAD 2
253
332
#define EVRPC_STATUS_ERR_UNSTARTED 3
333
#define EVRPC_STATUS_ERR_HOOKABORTED 4
336
/* for looking at headers or other information */
337
struct evhttp_request *http_req;
257
340
struct evrpc_request_wrapper {
286
369
int (*reply_unmarshal)(void *, struct evbuffer*);
372
/** launches an RPC and sends it to the server
374
* EVRPC_MAKE_REQUEST() is used by the client to send an RPC to the server.
376
* @param name the name of the RPC
377
* @param pool the evrpc_pool that contains the connection objects over which
378
* the request should be sent.
379
* @param request a pointer to the RPC request structure - it contains the
380
* data to be sent to the server.
381
* @param reply a pointer to the RPC reply structure. It is going to be filled
382
* if the request was answered successfully
383
* @param cb the callback to invoke when the RPC request has been answered
384
* @param cbarg an additional argument to be passed to the client
385
* @return 0 on success, -1 on failure
289
387
#define EVRPC_MAKE_REQUEST(name, pool, request, reply, cb, cbarg) \
290
388
evrpc_send_request_##name(pool, request, reply, cb, cbarg)
292
390
int evrpc_make_request(struct evrpc_request_wrapper *);
392
/** creates an rpc connection pool
295
394
* a pool has a number of connections associated with it.
296
395
* rpc requests are always made via a pool.
298
struct evrpc_pool *evrpc_pool_new(void);
299
void evrpc_pool_free(struct evrpc_pool *);
397
* @param base a pointer to an struct event_based object; can be left NULL
398
* in singled-threaded applications
399
* @return a newly allocated struct evrpc_pool object
400
* @see evrpc_pool_free()
402
struct evrpc_pool *evrpc_pool_new(struct event_base *base);
403
/** frees an rpc connection pool
405
* @param pool a pointer to an evrpc_pool allocated via evrpc_pool_new()
406
* @see evrpc_pool_new()
408
void evrpc_pool_free(struct evrpc_pool *pool);
410
* adds a connection over which rpc can be dispatched. the connection
411
* object must have been newly created.
300
413
void evrpc_pool_add_connection(struct evrpc_pool *,
301
414
struct evhttp_connection *);
304
417
* Sets the timeout in secs after which a request has to complete. The
305
418
* RPC is completely aborted if it does not complete by then. Setting
306
419
* the timeout to 0 means that it never timeouts and can be used to
310
423
* timeout. Connections added to the pool after set_timeout has be
311
424
* called receive the pool timeout only if no timeout has been set
312
425
* for the connection itself.
314
void evrpc_pool_set_timeout(struct evrpc_pool *, int timeout_in_secs);
427
* @param pool a pointer to a struct evrpc_pool object
428
* @param timeout_in_secs the number of seconds after which a request should
429
* timeout and a failure be returned to the callback.
431
void evrpc_pool_set_timeout(struct evrpc_pool *pool, int timeout_in_secs);
434
* Hooks for changing the input and output of RPCs; this can be used to
435
* implement compression, authentication, encryption, ...
438
enum EVRPC_HOOK_TYPE {
439
INPUT, /**< apply the function to an input hook */
440
OUTPUT /**< apply the function to an output hook */
443
/** adds a processing hook to either an rpc base or rpc pool
445
* If a hook returns -1, the processing is aborted.
447
* The add functions return handles that can be used for removing hooks.
449
* @param vbase a pointer to either struct evrpc_base or struct evrpc_pool
450
* @param hook_type either INPUT or OUTPUT
451
* @param cb the callback to call when the hook is activated
452
* @param cb_arg an additional argument for the callback
453
* @return a handle to the hook so it can be removed later
454
* @see evrpc_remove_hook()
456
void *evrpc_add_hook(void *vbase,
457
enum EVRPC_HOOK_TYPE hook_type,
458
int (*cb)(struct evhttp_request *, struct evbuffer *, void *),
461
/** removes a previously added hook
463
* @param vbase a pointer to either struct evrpc_base or struct evrpc_pool
464
* @param hook_type either INPUT or OUTPUT
465
* @param handle a handle returned by evrpc_add_hook()
466
* @return 1 on success or 0 on failure
467
* @see evrpc_add_hook()
469
int evrpc_remove_hook(void *vbase,
470
enum EVRPC_HOOK_TYPE hook_type,
316
473
#ifdef __cplusplus