~ubuntu-branches/ubuntu/jaunty/transmission/jaunty-security

« back to all changes in this revision

Viewing changes to third-party/libevent/evrpc.h

  • Committer: Bazaar Package Importer
  • Author(s): Philipp Benner
  • Date: 2007-11-22 12:37:14 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20071122123714-b0xi4zxhgk5qwbmc
Tags: 0.93.dfsg-2
* Added missing build-dependency (python).
* debian/control: switching to Homepage, Vcs-Browser and Vcs-Svn official
  fields (Leo "costela" Antunes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
extern "C" {
32
32
#endif
33
33
 
34
 
/*
 
34
/** @file evrpc.h
 
35
 *
35
36
 * This header files provides basic support for an RPC server and client.
36
37
 *
37
38
 * To support RPCs in a server, every supported RPC command needs to be
66
67
 */
67
68
 
68
69
struct evbuffer;
 
70
struct event_base;
69
71
struct evrpc_req_generic;
70
72
 
71
73
/* Encapsulates a request */
99
101
        /* the callback invoked for each received rpc */
100
102
        void (*cb)(struct evrpc_req_generic *, void *);
101
103
        void *cb_arg;
 
104
 
 
105
        /* reference for further configuration */
 
106
        struct evrpc_base *base;
102
107
};
103
108
 
 
109
/** The type of a specific RPC Message
 
110
 *
 
111
 * @param rpcname the name of the RPC message
 
112
 */
104
113
#define EVRPC_STRUCT(rpcname) struct evrpc_req__##rpcname
105
114
 
106
115
struct evhttp_request;
131
140
        void (*done)(struct evrpc_req_generic* rpc); 
132
141
};
133
142
 
134
 
/*
 
143
/** Creates the definitions and prototypes for an RPC
 
144
 *
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
 
148
 *
 
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()
137
153
 */
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);          \
145
162
};                                                                   \
149
166
        struct reqstruct *, struct rplystruct *, void *cbarg),  \
150
167
    void *);
151
168
 
 
169
/** Generates the code for receiving and sending an RPC message
 
170
 *
 
171
 * EVRPC_GENERATE is used to create the code corresponding to sending
 
172
 * and receiving a particular RPC message
 
173
 *
 
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()
 
178
 */
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, \
184
211
        return (-1);                                                \
185
212
}
186
213
 
 
214
/** Provides access to the HTTP request object underlying an RPC
 
215
 *
 
216
 * Access to the underlying http object; can be used to look at headers or
 
217
 * for getting the remote ip address
 
218
 *
 
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.
 
222
 */
 
223
#define EVRPC_REQUEST_HTTP(rpc_req) (rpc_req)->http_req
187
224
 
188
 
/* 
 
225
/** Creates the reply to an RPC request
 
226
 * 
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.
 
230
 * 
 
231
 * @param rpc_req the rpc request structure provided to the server callback
192
232
 */
193
233
#define EVRPC_REQUEST_DONE(rpc_req) do { \
194
234
  struct evrpc_req_generic *_req = (struct evrpc_req_generic *)(rpc_req); \
217
257
struct evhttp;
218
258
 
219
259
/* functions to start up the rpc system */
 
260
 
 
261
/** Creates a new rpc base from which RPC requests can be received
 
262
 *
 
263
 * @param server a pointer to an existing HTTP server
 
264
 * @return a newly allocated evrpc_base struct
 
265
 * @see evrpc_free()
 
266
 */
220
267
struct evrpc_base *evrpc_init(struct evhttp *server);
221
268
 
222
 
/* frees the base - for now, you are responsible for making sure that no rpcs are ongoing */
223
 
void evrpc_free(struct evrpc_base *);
 
269
/** 
 
270
 * Frees the evrpc base
 
271
 *
 
272
 * For now, you are responsible for making sure that no rpcs are ongoing.
 
273
 *
 
274
 * @param base the evrpc_base object to be freed
 
275
 * @see evrpc_init
 
276
 */
 
277
void evrpc_free(struct evrpc_base *base);
224
278
 
225
 
/* this macro is used to register RPCs with the HTTP Server */
 
279
/** register RPCs with the HTTP Server
 
280
 *
 
281
 * registers a new RPC with the HTTP server, each RPC needs to have
 
282
 * a unique name under which it can be identified.
 
283
 *
 
284
 * @param base the evrpc_base structure in which the RPC should be
 
285
 *   registered.
 
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.
 
294
 */
226
295
#define EVRPC_REGISTER(base, name, request, reply, callback, cbarg) \
227
296
  do { \
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 *);
236
305
 
237
 
/* Takes the named RPCs and tried to unregister it */
 
306
/**
 
307
 * Unregisters an already registered RPC
 
308
 *
 
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()
 
313
 */
238
314
#define EVRPC_UNREGISTER(base, name) evrpc_unregister_rpc(base, #name)
239
315
 
240
 
int evrpc_unregister_rpc(struct evrpc_base *, const char *name);
 
316
int evrpc_unregister_rpc(struct evrpc_base *base, const char *name);
241
317
 
242
318
/*
243
319
 * Client-side RPC support
246
322
struct evrpc_pool;
247
323
struct evhttp_connection;
248
324
 
 
325
/** 
 
326
 * provides information about the completed RPC request.
 
327
 */
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
254
334
        int error;
 
335
 
 
336
        /* for looking at headers or other information */
 
337
        struct evhttp_request *http_req;
255
338
};
256
339
 
257
340
struct evrpc_request_wrapper {
286
369
        int (*reply_unmarshal)(void *, struct evbuffer*);
287
370
};
288
371
 
 
372
/** launches an RPC and sends it to the server
 
373
 *
 
374
 * EVRPC_MAKE_REQUEST() is used by the client to send an RPC to the server.
 
375
 *
 
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
 
386
 */
289
387
#define EVRPC_MAKE_REQUEST(name, pool, request, reply, cb, cbarg)       \
290
388
        evrpc_send_request_##name(pool, request, reply, cb, cbarg)
291
389
 
292
390
int evrpc_make_request(struct evrpc_request_wrapper *);
293
391
 
294
 
/* 
 
392
/** creates an rpc connection pool
 
393
 * 
295
394
 * a pool has a number of connections associated with it.
296
395
 * rpc requests are always made via a pool.
297
 
 */
298
 
struct evrpc_pool *evrpc_pool_new(void);
299
 
void evrpc_pool_free(struct evrpc_pool *);
 
396
 *
 
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()
 
401
 */
 
402
struct evrpc_pool *evrpc_pool_new(struct event_base *base);
 
403
/** frees an rpc connection pool
 
404
 *
 
405
 * @param pool a pointer to an evrpc_pool allocated via evrpc_pool_new()
 
406
 * @see evrpc_pool_new()
 
407
 */
 
408
void evrpc_pool_free(struct evrpc_pool *pool);
 
409
/*
 
410
 * adds a connection over which rpc can be dispatched.  the connection
 
411
 * object must have been newly created.
 
412
 */
300
413
void evrpc_pool_add_connection(struct evrpc_pool *, 
301
414
    struct evhttp_connection *);
302
415
 
303
 
/*
 
416
/**
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.
313
 
 */
314
 
void evrpc_pool_set_timeout(struct evrpc_pool *, int timeout_in_secs);
 
426
 *
 
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.
 
430
 */
 
431
void evrpc_pool_set_timeout(struct evrpc_pool *pool, int timeout_in_secs);
 
432
 
 
433
/**
 
434
 * Hooks for changing the input and output of RPCs; this can be used to
 
435
 * implement compression, authentication, encryption, ...
 
436
 */
 
437
 
 
438
enum EVRPC_HOOK_TYPE {
 
439
        INPUT,          /**< apply the function to an input hook */
 
440
        OUTPUT          /**< apply the function to an output hook */
 
441
};
 
442
 
 
443
/** adds a processing hook to either an rpc base or rpc pool
 
444
 *
 
445
 * If a hook returns -1, the processing is aborted.
 
446
 *
 
447
 * The add functions return handles that can be used for removing hooks.
 
448
 *
 
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()
 
455
 */
 
456
void *evrpc_add_hook(void *vbase,
 
457
    enum EVRPC_HOOK_TYPE hook_type,
 
458
    int (*cb)(struct evhttp_request *, struct evbuffer *, void *),
 
459
    void *cb_arg);
 
460
 
 
461
/** removes a previously added hook
 
462
 *
 
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()
 
468
 */
 
469
int evrpc_remove_hook(void *vbase,
 
470
    enum EVRPC_HOOK_TYPE hook_type,
 
471
    void *handle);
315
472
 
316
473
#ifdef __cplusplus
317
474
}