miniRPC places few restrictions on the application's arrangement of dispatcher threads. miniRPC provides several alternative interfaces for dispatching; the application can choose the one it needs, or can even alternate between them. An application can have as many dispatchers as it wants.
Because application callbacks execute only in dispatcher threads, the application can decide how long a callback can take to complete. For example, a server providing a compute-intensive procedure call can choose to do its computation directly within the procedure handler, and simply create a large number of dispatcher threads to ensure parallelism. Or, it can return quickly from the procedure handler, finish processing in a dedicated thread, and return the result asynchronously.
By default, miniRPC does not fire multiple events at a time for a given connection, so the application need not handle concurrency within each connection. However, the application can override this behavior if it wishes. Interfaces are also provided to request that miniRPC stop (or resume) dispatching events for a given connection.
Dispatcher threads are subject to the following constraints:
The application may dispatch using one or more of the following mechanisms:
Typedefs | |
typedef void *( | mrpc_accept_fn )(void *set_data, struct mrpc_connection *conn, struct sockaddr *from, socklen_t from_len) |
Event callback fired on arrival of a new connection. | |
typedef void( | mrpc_disconnect_fn )(void *conn_data, enum mrpc_disc_reason reason) |
Event callback fired on connection close. | |
typedef void( | mrpc_ioerr_fn )(void *conn_data, char *message) |
Event callback fired on I/O error. | |
Functions | |
int | mrpc_start_dispatch_thread (struct mrpc_conn_set *set) |
Start a dispatcher thread for a connection set. | |
void | mrpc_dispatcher_add (struct mrpc_conn_set *set) |
Notify miniRPC that the current thread will dispatch events for this connection set. | |
void | mrpc_dispatcher_remove (struct mrpc_conn_set *set) |
Notify miniRPC that the current thread will no longer dispatch events for this connection set. | |
int | mrpc_dispatch_loop (struct mrpc_conn_set *set) |
Dispatch events from this thread until the connection set is destroyed. | |
int | mrpc_dispatch (struct mrpc_conn_set *set, int max) |
Dispatch events from this thread and then return. | |
int | mrpc_get_event_fd (struct mrpc_conn_set *set) |
Obtain a file descriptor which will be readable when there are events to process. | |
int | mrpc_stop_events (struct mrpc_connection *conn) |
Disable event processing for a connection. | |
int | mrpc_start_events (struct mrpc_connection *conn) |
Re-enable event processing for a connection. | |
int | mrpc_release_event (void) |
Allow the current event handler to run in parallel with other handlers for the same connection. |
typedef void*( mrpc_accept_fn)(void *set_data, struct mrpc_connection *conn, struct sockaddr *from, socklen_t from_len) |
Event callback fired on arrival of a new connection.
set_data | The cookie associated with the connection set | |
conn | The handle to the newly-created connection | |
from | The address of the remote end of the connection | |
from_len | The length of the from structure |
from
is no longer valid after the callback returns.
typedef void( mrpc_disconnect_fn)(void *conn_data, enum mrpc_disc_reason reason) |
Event callback fired on connection close.
conn_data | The cookie associated with the connection | |
reason | The reason the connection was closed |
typedef void( mrpc_ioerr_fn)(void *conn_data, char *message) |
Event callback fired on I/O error.
conn_data | The cookie associated with the connection | |
message | A string describing the error |
message
is in a format suitable for logging. message
is no longer valid once the callback returns.This callback is fired only on unusual error conditions, generally caused by invalid data on the wire. If miniRPC can recover from such an error, it will do so; if not, it will automatically close the connection (and report this via the disconnect callback).
The application need not register an ioerr
callback unless it wishes to log such events. In most cases, the callback function will simply call the appropriate logging function and return. If the application is paranoid about servicing connections which may be in a dubious state, it may wish to close the affected connection (with mrpc_conn_close()) as well.
int mrpc_start_dispatch_thread | ( | struct mrpc_conn_set * | set | ) |
Start a dispatcher thread for a connection set.
set | The connection set |
Unlike with mrpc_dispatch() and mrpc_dispatch_loop(), the caller does not need to register the dispatcher thread with mrpc_dispatcher_add(). The background thread handles this for you.
void mrpc_dispatcher_add | ( | struct mrpc_conn_set * | set | ) |
Notify miniRPC that the current thread will dispatch events for this connection set.
set | The connection set |
void mrpc_dispatcher_remove | ( | struct mrpc_conn_set * | set | ) |
Notify miniRPC that the current thread will no longer dispatch events for this connection set.
set | The connection set |
int mrpc_dispatch_loop | ( | struct mrpc_conn_set * | set | ) |
Dispatch events from this thread until the connection set is destroyed.
set | The connection set |
int mrpc_dispatch | ( | struct mrpc_conn_set * | set, | |
int | max | |||
) |
Dispatch events from this thread and then return.
set | The connection set | |
max | The maximum number of events to dispatch, or 0 for no limit |
max
events have been processed, whichever comes first; if max
is 0, dispatch until there are no more events to process. The calling thread must call mrpc_dispatcher_add() before calling this function for the first time.If this function returns ENXIO, the connection set is being destroyed. The application must stop calling this function, and must call mrpc_dispatcher_remove() to indicate its intent to do so.
This function must not be called from an event handler.
int mrpc_get_event_fd | ( | struct mrpc_conn_set * | set | ) |
Obtain a file descriptor which will be readable when there are events to process.
set | The connection set |
The application must not read, write, or close the provided file descriptor. Once mrpc_dispatch() returns ENXIO, indicating that the connection set is being shut down, the application must stop polling on the descriptor.
int mrpc_stop_events | ( | struct mrpc_connection * | conn | ) |
Disable event processing for a connection.
conn | The connection |
The application may call this function more than once against the same connection. Event processing for the connection will not resume until the application makes the corresponding number of calls to mrpc_start_events().
If this function is called from an event handler for the same connection, and the handler has not called mrpc_release_event(), the application is guaranteed that no further events will be fired on the connection once the call returns. Otherwise, there is a window after the function returns in which further events may be fired.
int mrpc_start_events | ( | struct mrpc_connection * | conn | ) |
Re-enable event processing for a connection.
conn | The connection |
int mrpc_release_event | ( | void | ) |
Allow the current event handler to run in parallel with other handlers for the same connection.
This function indicates to miniRPC that the calling event handler should no longer block the handling of additional events on its associated connection. Note that this call is effective only for the current invocation of the calling event handler; it will have no effect on any other event.
Returning from an event handler implicitly calls this function. If called from outside an event handler, this function returns EINVAL and has no other effect.