This header files provides basic support for an RPC server and client.
To support RPCs in a server, every supported RPC command needs to
be defined and registered.
EVRPC_HEADER(SendCommand, Request, Reply);
SendCommand is the name of the RPC command. Request is the name of
a structure generated by event_rpcgen.py. It contains all parameters
relating to the SendCommand RPC. The server needs to fill in the Reply
structure. Reply is the name of a structure generated by event_rpcgen.py. It
contains the answer to the RPC.
To register an RPC with an HTTP server, you need to first create
an RPC base with:
struct evrpc_base *base = evrpc_init(http);
A specific RPC can then be registered with
EVRPC_REGISTER(base, SendCommand, Request, Reply, FunctionCB,
arg);
when the server receives an appropriately formatted RPC, the user
callback is invoked. The callback needs to fill in the reply structure.
void FunctionCB(EVRPC_STRUCT(SendCommand)* rpc, void *arg);
To send the reply, call EVRPC_REQUEST_DONE(rpc);
See the regression test for an example.
Value:
int evrpc_send_request_##rpcname(struct evrpc_pool *pool, struct reqstruct *request, struct rplystruct *reply, void (*cb)(struct evrpc_status *, struct reqstruct *, struct rplystruct *, void *cbarg), void *cbarg) { return evrpc_send_request_generic(pool, request, reply, (void (*)(struct evrpc_status *, void *, void *, void *))cb, cbarg, #rpcname, (void (*)(struct evbuffer *, void *))reqstruct##_marshal, (void (*)(void *))rplystruct##_clear, (int (*)(void *, struct evbuffer *))rplystruct##_unmarshal); }
Generates the code for receiving and sending an RPC message.
EVRPC_GENERATE is used to create the code corresponding to sending and
receiving a particular RPC message
Parameters:
rpcname the name of the RPC
reqstruct the name of the RPC request structure
replystruct the name of the RPC reply structure
See also:
EVRPC_HEADER()
Value:
EVRPC_STRUCT(rpcname) { struct evrpc_hook_meta *hook_meta; struct reqstruct* request; struct rplystruct* reply; struct evrpc* rpc; struct evhttp_request* http_req; struct evbuffer* rpc_data; }; int evrpc_send_request_##rpcname(struct evrpc_pool *, struct reqstruct *, struct rplystruct *, void (*)(struct evrpc_status *, struct reqstruct *, struct rplystruct *, void *cbarg), void *);
Creates the definitions and prototypes for an RPC. You need to use
EVRPC_HEADER to create structures and function prototypes needed by the
server and client implementation. The structures have to be defined in an
.rpc file and converted to source code via event_rpcgen.py
Parameters:
rpcname the name of the RPC
reqstruct the name of the RPC request structure
replystruct the name of the RPC reply structure
See also:
EVRPC_GENERATE()
Value:
evrpc_make_request_ctx(pool, request, reply, #rpcname, (void (*)(struct evbuffer *, void *))reqstruct##_marshal, (void (*)(void *))rplystruct##_clear, (int (*)(void *, struct evbuffer *))rplystruct##_unmarshal, (void (*)(struct evrpc_status *, void *, void *, void *))cb, cbarg)
Creates a context structure that contains rpc specific
information. EVRPC_MAKE_CTX is used to populate a RPC specific context that
contains information about marshaling the RPC data types.
Parameters:
rpcname the name of the RPC
reqstruct the name of the RPC request structure
replystruct the name of the RPC reply structure
pool the evrpc_pool over which to make the request
request a pointer to the RPC request structure object
reply a pointer to the RPC reply structure object
cb the callback function to call when the RPC has completed
cbarg the argument to supply to the callback
launches an RPC and sends it to the server EVRPC_MAKE_REQUEST() is used
by the client to send an RPC to the server.
Parameters:
name the name of the RPC
pool the evrpc_pool that contains the connection objects over which the
request should be sent.
request a pointer to the RPC request structure - it contains the data to
be sent to the server.
reply a pointer to the RPC reply structure. It is going to be filled if
the request was answered successfully
cb the callback to invoke when the RPC request has been answered
cbarg an additional argument to be passed to the client
Returns:
0 on success, -1 on failure
Value:
evrpc_register_generic(base, #name, (void (*)(struct evrpc_req_generic *, void *))callback, cbarg, (void *(*)(void *))request##_new, NULL, (void (*)(void *))request##_free, (int (*)(void *, struct evbuffer *))request##_unmarshal, (void *(*)(void *))reply##_new, NULL, (void (*)(void *))reply##_free, (int (*)(void *))reply##_complete, (void (*)(struct evbuffer *, void *))reply##_marshal)
register RPCs with the HTTP Server registers a new RPC with the
HTTP server, each RPC needs to have a unique name under which it can be
identified.
Parameters:
base the evrpc_base structure in which the RPC
should be registered.
name the name of the RPC
request the name of the RPC request structure
reply the name of the RPC reply structure
callback the callback that should be invoked when the RPC is received.
The callback has the following prototype void
(callback)(EVRPC_STRUCT(Message) rpc, void *arg)
cbarg an additional parameter that can be passed to the callback. The
parameter can be used to carry around state.
Value:
do { struct evrpc_req_generic *req_ = (struct evrpc_req_generic *)(rpc_req); \
evrpc_request_done(req_); } while (0)
Creates the reply to an RPC request. EVRPC_REQUEST_DONE is used to
answer a request; the reply is expected to have been filled in. The request
and reply pointers become invalid after this call has finished.
Parameters:
rpc_req the rpc request structure provided to the
server callback
Provides access to the HTTP request object underlying an RPC. Access to the
underlying http object; can be used to look at headers or for getting the
remote ip address
Parameters:
rpc_req the rpc request structure provided to the
server callback
Returns:
an struct evhttp_request object that can be inspected for
HTTP headers or sender information.
The type of a specific RPC Message.
Parameters:
rpcname the name of the RPC message
Unregisters an already registered RPC.
Parameters:
base the evrpc_base object from which to
unregister an RPC
name the name of the rpc to unregister
Returns:
-1 on error or 0 when successful.
See also:
EVRPC_REGISTER()
Assigns a value to the member in the message.
Parameters:
msg the message to which to assign a value
member the name of the member variable
value the value to assign
Assigns a value to the member in the message.
Parameters:
msg the message to which to assign a value
member the name of the member variable
value the value to assign
len the length of the value
Returns the value for a member.
Parameters:
msg the message from which to get the value
member the name of the member variable
pvalue a pointer to the variable to hold the value
Returns:
0 on success, -1 otherwise.
Returns the value for a member.
Parameters:
msg the message from which to get the value
member the name of the member variable
pvalue a pointer to the variable to hold the value
plen a pointer to the length of the value
Returns:
0 on success, -1 otherwise.
Determines if the member has been set in the message.
Parameters:
msg the message to inspect
member the member variable to test for presences
Returns:
1 if it's present or 0 otherwise.
Deprecated alias for EVRPC_INPUT. Not available on windows, where it conflicts
with platform headers.
Deprecated alias for EVRPC_OUTPUT. Not available on windows, where it conflicts
with platform headers.
adds a processing hook to either an rpc base or rpc pool If a hook returns
TERMINATE, the processing is aborted. On CONTINUE, the request is immediately
processed after the hook returns. If the hook returns PAUSE, request
processing stops until evrpc_resume_request() has been called.
The add functions return handles that can be used for removing
hooks.
Parameters:
vbase a pointer to either struct evrpc_base or
struct evrpc_pool
hook_type either INPUT or OUTPUT
cb the callback to call when the hook is activated
cb_arg an additional argument for the callback
Returns:
a handle to the hook so it can be removed later
See also:
evrpc_remove_hook()
Frees the evrpc base. For now, you are responsible for making sure that no rpcs
are ongoing.
Parameters:
base the evrpc_base object to be freed
See also:
evrpc_init
adds meta data to request evrpc_hook_add_meta() allows hooks to add meta
data to a request. for a client request, the meta data can be inserted by an
outgoing request hook and retrieved by the incoming request hook.
Parameters:
ctx the context provided to the hook call
key a NUL-terminated c-string
data the data to be associated with the key
data_size the size of the data
retrieves meta data previously associated evrpc_hook_find_meta() can be
used to retrieve meta data associated to a request by a previous hook.
Parameters:
ctx the context provided to the hook call
key a NUL-terminated c-string
data pointer to a data pointer that will contain the retrieved data
data_size pointer to the size of the data
Returns:
0 on success or -1 on failure
returns the connection object associated with the request
Parameters:
ctx the context provided to the hook call
Returns:
a pointer to the evhttp_connection object
Creates a new rpc base from which RPC requests can be received.
Parameters:
server a pointer to an existing HTTP server
Returns:
a newly allocated evrpc_base struct
See also:
evrpc_free()
Makes an RPC request based on the provided context. This is a low-level function
and should not be used directly unless a custom context object is provided.
Use EVRPC_MAKE_REQUEST() instead.
Parameters:
ctx a context from EVRPC_MAKE_CTX()
Returns:
0 on success, -1 otherwise.
See also:
EVRPC_MAKE_REQUEST(),
EVRPC_MAKE_CTX()
Adds a connection over which rpc can be dispatched to the pool. The connection
object must have been newly created.
Parameters:
pool the pool to which to add the connection
evcon the connection to add to the pool.
frees an rpc connection pool
Parameters:
pool a pointer to an evrpc_pool allocated via
evrpc_pool_new()
See also:
evrpc_pool_new()
creates an rpc connection pool a pool has a number of connections associated
with it. rpc requests are always made via a pool.
Parameters:
base a pointer to an struct event_based object;
can be left NULL in singled-threaded applications
Returns:
a newly allocated struct evrpc_pool object
See also:
evrpc_pool_free()
Removes a connection from the pool. The connection object must have been newly
created.
Parameters:
pool the pool from which to remove the connection
evcon the connection to remove from the pool.
Sets the timeout in secs after which a request has to complete. The RPC is
completely aborted if it does not complete by then. Setting the timeout to 0
means that it never timeouts and can be used to implement callback type RPCs.
Any connection already in the pool will be updated with the new
timeout. Connections added to the pool after set_timeout has be called
receive the pool timeout only if no timeout has been set for the connection
itself.
Parameters:
pool a pointer to a struct evrpc_pool object
timeout_in_secs the number of seconds after which a request should
timeout and a failure be returned to the callback.
Function for registering a generic RPC with the RPC base. Do not call this
function directly, use EVRPC_REGISTER() instead.
See also:
EVRPC_REGISTER()
Low level function for registering an RPC with a server. Use
EVRPC_REGISTER() instead.
See also:
EVRPC_REGISTER()
removes a previously added hook
Parameters:
vbase a pointer to either struct evrpc_base or
struct evrpc_pool
hook_type either INPUT or OUTPUT
handle a handle returned by evrpc_add_hook()
Returns:
1 on success or 0 on failure
See also:
evrpc_add_hook()
resume a paused request
Parameters:
vbase a pointer to either struct evrpc_base or
struct evrpc_pool
ctx the context pointer provided to the original hook call
Function for sending a generic RPC request. Do not call this function directly,
use EVRPC_MAKE_REQUEST() instead.
See also:
EVRPC_MAKE_REQUEST()