MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
evrpc.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006 Niels Provos <provos@citi.umich.edu>
3  * All rights reserved.
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 _EVRPC_H_
28 #define _EVRPC_H_
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
69 struct evbuffer;
70 struct event_base;
71 struct evrpc_req_generic;
72 
73 /* Encapsulates a request */
74 struct evrpc {
75  TAILQ_ENTRY(evrpc) next;
76 
77  /* the URI at which the request handler lives */
78  const char* uri;
79 
80  /* creates a new request structure */
81  void *(*request_new)(void);
82 
83  /* frees the request structure */
84  void (*request_free)(void *);
85 
86  /* unmarshals the buffer into the proper request structure */
87  int (*request_unmarshal)(void *, struct evbuffer *);
88 
89  /* creates a new reply structure */
90  void *(*reply_new)(void);
91 
92  /* creates a new reply structure */
93  void (*reply_free)(void *);
94 
95  /* verifies that the reply is valid */
96  int (*reply_complete)(void *);
97 
98  /* marshals the reply into a buffer */
99  void (*reply_marshal)(struct evbuffer*, void *);
100 
101  /* the callback invoked for each received rpc */
102  void (*cb)(struct evrpc_req_generic *, void *);
103  void *cb_arg;
104 
105  /* reference for further configuration */
106  struct evrpc_base *base;
107 };
108 
113 #define EVRPC_STRUCT(rpcname) struct evrpc_req__##rpcname
114 
115 struct evhttp_request;
116 struct evrpc_status;
117 
118 /* We alias the RPC specific structs to this voided one */
120  /* the unmarshaled request object */
121  void *request;
122 
123  /* the empty reply object that needs to be filled in */
124  void *reply;
125 
126  /*
127  * the static structure for this rpc; that can be used to
128  * automatically unmarshal and marshal the http buffers.
129  */
130  struct evrpc *rpc;
131 
132  /*
133  * the http request structure on which we need to answer.
134  */
135  struct evhttp_request* http_req;
136 
137  /*
138  * callback to reply and finish answering this rpc
139  */
140  void (*done)(struct evrpc_req_generic* rpc);
141 };
142 
154 #define EVRPC_HEADER(rpcname, reqstruct, rplystruct) \
155 EVRPC_STRUCT(rpcname) { \
156  struct reqstruct* request; \
157  struct rplystruct* reply; \
158  struct evrpc* rpc; \
159  struct evhttp_request* http_req; \
160  void (*done)(struct evrpc_status *, \
161  struct evrpc* rpc, void *request, void *reply); \
162 }; \
163 int evrpc_send_request_##rpcname(struct evrpc_pool *, \
164  struct reqstruct *, struct rplystruct *, \
165  void (*)(struct evrpc_status *, \
166  struct reqstruct *, struct rplystruct *, void *cbarg), \
167  void *);
168 
179 #define EVRPC_GENERATE(rpcname, reqstruct, rplystruct) \
180 int evrpc_send_request_##rpcname(struct evrpc_pool *pool, \
181  struct reqstruct *request, struct rplystruct *reply, \
182  void (*cb)(struct evrpc_status *, \
183  struct reqstruct *, struct rplystruct *, void *cbarg), \
184  void *cbarg) { \
185  struct evrpc_status status; \
186  struct evrpc_request_wrapper *ctx; \
187  ctx = (struct evrpc_request_wrapper *) \
188  malloc(sizeof(struct evrpc_request_wrapper)); \
189  if (ctx == NULL) \
190  goto error; \
191  ctx->pool = pool; \
192  ctx->evcon = NULL; \
193  ctx->name = strdup(#rpcname); \
194  if (ctx->name == NULL) { \
195  free(ctx); \
196  goto error; \
197  } \
198  ctx->cb = (void (*)(struct evrpc_status *, \
199  void *, void *, void *))cb; \
200  ctx->cb_arg = cbarg; \
201  ctx->request = (void *)request; \
202  ctx->reply = (void *)reply; \
203  ctx->request_marshal = (void (*)(struct evbuffer *, void *))reqstruct##_marshal; \
204  ctx->reply_clear = (void (*)(void *))rplystruct##_clear; \
205  ctx->reply_unmarshal = (int (*)(void *, struct evbuffer *))rplystruct##_unmarshal; \
206  return (evrpc_make_request(ctx)); \
207 error: \
208  memset(&status, 0, sizeof(status)); \
209  status.error = EVRPC_STATUS_ERR_UNSTARTED; \
210  (*(cb))(&status, request, reply, cbarg); \
211  return (-1); \
212 }
213 
223 #define EVRPC_REQUEST_HTTP(rpc_req) (rpc_req)->http_req
224 
233 #define EVRPC_REQUEST_DONE(rpc_req) do { \
234  struct evrpc_req_generic *_req = (struct evrpc_req_generic *)(rpc_req); \
235  _req->done(_req); \
236 } while (0)
237 
238 
239 /* Takes a request object and fills it in with the right magic */
240 #define EVRPC_REGISTER_OBJECT(rpc, name, request, reply) \
241  do { \
242  (rpc)->uri = strdup(#name); \
243  if ((rpc)->uri == NULL) { \
244  fprintf(stderr, "failed to register object\n"); \
245  exit(1); \
246  } \
247  (rpc)->request_new = (void *(*)(void))request##_new; \
248  (rpc)->request_free = (void (*)(void *))request##_free; \
249  (rpc)->request_unmarshal = (int (*)(void *, struct evbuffer *))request##_unmarshal; \
250  (rpc)->reply_new = (void *(*)(void))reply##_new; \
251  (rpc)->reply_free = (void (*)(void *))reply##_free; \
252  (rpc)->reply_complete = (int (*)(void *))reply##_complete; \
253  (rpc)->reply_marshal = (void (*)(struct evbuffer*, void *))reply##_marshal; \
254  } while (0)
255 
256 struct evrpc_base;
257 struct evhttp;
258 
259 /* functions to start up the rpc system */
260 
267 struct evrpc_base *evrpc_init(struct evhttp *server);
268 
277 void evrpc_free(struct evrpc_base *base);
278 
295 #define EVRPC_REGISTER(base, name, request, reply, callback, cbarg) \
296  do { \
297  struct evrpc* rpc = (struct evrpc *)calloc(1, sizeof(struct evrpc)); \
298  EVRPC_REGISTER_OBJECT(rpc, name, request, reply); \
299  evrpc_register_rpc(base, rpc, \
300  (void (*)(struct evrpc_req_generic*, void *))callback, cbarg); \
301  } while (0)
302 
303 int evrpc_register_rpc(struct evrpc_base *, struct evrpc *,
304  void (*)(struct evrpc_req_generic*, void *), void *);
305 
314 #define EVRPC_UNREGISTER(base, name) evrpc_unregister_rpc(base, #name)
315 
316 int evrpc_unregister_rpc(struct evrpc_base *base, const char *name);
317 
318 /*
319  * Client-side RPC support
320  */
321 
322 struct evrpc_pool;
323 struct evhttp_connection;
324 
328 struct evrpc_status {
329 #define EVRPC_STATUS_ERR_NONE 0
330 #define EVRPC_STATUS_ERR_TIMEOUT 1
331 #define EVRPC_STATUS_ERR_BADPAYLOAD 2
332 #define EVRPC_STATUS_ERR_UNSTARTED 3
333 #define EVRPC_STATUS_ERR_HOOKABORTED 4
334  int error;
335 
336  /* for looking at headers or other information */
337  struct evhttp_request *http_req;
338 };
339 
341  TAILQ_ENTRY(evrpc_request_wrapper) next;
342 
343  /* pool on which this rpc request is being made */
344  struct evrpc_pool *pool;
345 
346  /* connection on which the request is being sent */
347  struct evhttp_connection *evcon;
348 
349  /* event for implementing request timeouts */
350  struct event ev_timeout;
351 
352  /* the name of the rpc */
353  char *name;
354 
355  /* callback */
356  void (*cb)(struct evrpc_status*, void *request, void *reply, void *arg);
357  void *cb_arg;
358 
359  void *request;
360  void *reply;
361 
362  /* unmarshals the buffer into the proper request structure */
363  void (*request_marshal)(struct evbuffer *, void *);
364 
365  /* removes all stored state in the reply */
366  void (*reply_clear)(void *);
367 
368  /* marshals the reply into a buffer */
369  int (*reply_unmarshal)(void *, struct evbuffer*);
370 };
371 
387 #define EVRPC_MAKE_REQUEST(name, pool, request, reply, cb, cbarg) \
388  evrpc_send_request_##name(pool, request, reply, cb, cbarg)
389 
390 int evrpc_make_request(struct evrpc_request_wrapper *);
391 
402 struct evrpc_pool *evrpc_pool_new(struct event_base *base);
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  */
413 void evrpc_pool_add_connection(struct evrpc_pool *,
414  struct evhttp_connection *);
415 
431 void evrpc_pool_set_timeout(struct evrpc_pool *pool, int timeout_in_secs);
432 
441 };
442 
443 #ifndef WIN32
444 
446 #define INPUT EVRPC_INPUT
447 
449 #define OUTPUT EVRPC_OUTPUT
450 #endif
451 
465 void *evrpc_add_hook(void *vbase,
466  enum EVRPC_HOOK_TYPE hook_type,
467  int (*cb)(struct evhttp_request *, struct evbuffer *, void *),
468  void *cb_arg);
469 
478 int evrpc_remove_hook(void *vbase,
479  enum EVRPC_HOOK_TYPE hook_type,
480  void *handle);
481 
482 #ifdef __cplusplus
483 }
484 #endif
485 
486 #endif /* _EVRPC_H_ */