evrpc-internal.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 EVRPC_INTERNAL_H_INCLUDED_
  28. #define EVRPC_INTERNAL_H_INCLUDED_
  29. #include "event2/http.h"
  30. #include "http-internal.h"
  31. struct evrpc;
  32. struct evrpc_request_wrapper;
  33. #define EVRPC_URI_PREFIX "/.rpc."
  34. struct evrpc_hook {
  35. TAILQ_ENTRY(evrpc_hook) next;
  36. /* returns EVRPC_TERMINATE; if the rpc should be aborted.
  37. * a hook is is allowed to rewrite the evbuffer
  38. */
  39. int (*process)(void *, struct evhttp_request *,
  40. struct evbuffer *, void *);
  41. void *process_arg;
  42. };
  43. TAILQ_HEAD(evrpc_hook_list, evrpc_hook);
  44. /*
  45. * this is shared between the base and the pool, so that we can reuse
  46. * the hook adding functions; we alias both evrpc_pool and evrpc_base
  47. * to this common structure.
  48. */
  49. struct evrpc_hook_ctx;
  50. TAILQ_HEAD(evrpc_pause_list, evrpc_hook_ctx);
  51. struct evrpc_hooks_ {
  52. /* hooks for processing outbound and inbound rpcs */
  53. struct evrpc_hook_list in_hooks;
  54. struct evrpc_hook_list out_hooks;
  55. struct evrpc_pause_list pause_requests;
  56. };
  57. #define input_hooks common.in_hooks
  58. #define output_hooks common.out_hooks
  59. #define paused_requests common.pause_requests
  60. struct evrpc_base {
  61. struct evrpc_hooks_ common;
  62. /* the HTTP server under which we register our RPC calls */
  63. struct evhttp* http_server;
  64. /* a list of all RPCs registered with us */
  65. TAILQ_HEAD(evrpc_list, evrpc) registered_rpcs;
  66. };
  67. struct evrpc_req_generic;
  68. void evrpc_reqstate_free_(struct evrpc_req_generic* rpc_state);
  69. /* A pool for holding evhttp_connection objects */
  70. struct evrpc_pool {
  71. struct evrpc_hooks_ common;
  72. struct event_base *base;
  73. struct evconq connections;
  74. int timeout;
  75. TAILQ_HEAD(evrpc_requestq, evrpc_request_wrapper) (requests);
  76. };
  77. struct evrpc_hook_ctx {
  78. TAILQ_ENTRY(evrpc_hook_ctx) next;
  79. void *ctx;
  80. void (*cb)(void *, enum EVRPC_HOOK_RESULT);
  81. };
  82. struct evrpc_meta {
  83. TAILQ_ENTRY(evrpc_meta) next;
  84. char *key;
  85. void *data;
  86. size_t data_size;
  87. };
  88. TAILQ_HEAD(evrpc_meta_list, evrpc_meta);
  89. struct evrpc_hook_meta {
  90. struct evrpc_meta_list meta_data;
  91. struct evhttp_connection *evcon;
  92. };
  93. /* allows association of meta data with a request */
  94. static void evrpc_hook_associate_meta_(struct evrpc_hook_meta **pctx,
  95. struct evhttp_connection *evcon);
  96. /* creates a new meta data store */
  97. static struct evrpc_hook_meta *evrpc_hook_meta_new_(void);
  98. /* frees the meta data associated with a request */
  99. static void evrpc_hook_context_free_(struct evrpc_hook_meta *ctx);
  100. /* the server side of an rpc */
  101. /* We alias the RPC specific structs to this voided one */
  102. struct evrpc_req_generic {
  103. /*
  104. * allows association of meta data via hooks - needs to be
  105. * synchronized with evrpc_request_wrapper
  106. */
  107. struct evrpc_hook_meta *hook_meta;
  108. /* the unmarshaled request object */
  109. void *request;
  110. /* the empty reply object that needs to be filled in */
  111. void *reply;
  112. /*
  113. * the static structure for this rpc; that can be used to
  114. * automatically unmarshal and marshal the http buffers.
  115. */
  116. struct evrpc *rpc;
  117. /*
  118. * the http request structure on which we need to answer.
  119. */
  120. struct evhttp_request* http_req;
  121. /*
  122. * Temporary data store for marshaled data
  123. */
  124. struct evbuffer* rpc_data;
  125. };
  126. /* the client side of an rpc request */
  127. struct evrpc_request_wrapper {
  128. /*
  129. * allows association of meta data via hooks - needs to be
  130. * synchronized with evrpc_req_generic.
  131. */
  132. struct evrpc_hook_meta *hook_meta;
  133. TAILQ_ENTRY(evrpc_request_wrapper) next;
  134. /* pool on which this rpc request is being made */
  135. struct evrpc_pool *pool;
  136. /* connection on which the request is being sent */
  137. struct evhttp_connection *evcon;
  138. /* the actual request */
  139. struct evhttp_request *req;
  140. /* event for implementing request timeouts */
  141. struct event ev_timeout;
  142. /* the name of the rpc */
  143. char *name;
  144. /* callback */
  145. void (*cb)(struct evrpc_status*, void *request, void *reply, void *arg);
  146. void *cb_arg;
  147. void *request;
  148. void *reply;
  149. /* unmarshals the buffer into the proper request structure */
  150. void (*request_marshal)(struct evbuffer *, void *);
  151. /* removes all stored state in the reply */
  152. void (*reply_clear)(void *);
  153. /* marshals the reply into a buffer */
  154. int (*reply_unmarshal)(void *, struct evbuffer*);
  155. };
  156. #endif /* EVRPC_INTERNAL_H_INCLUDED_ */