CppWAMP
C++11 client library for the WAMP protocol
|
A slot is a callable target that is called in response to a signal (the signal being the call invocation in this case). The term slot, borrowed from Qt's signals and slots, is used to distinguish a call handler from an asynchronous operation handler.
A callable target can be:
std::bind
),A call slot represents an RPC handler that does not have any expectations on the invocation payload arguments. If necessary, the payload arguments must be checked during runtime, in the body of the call handler.
wamp::Session::enroll expects a call slot with the following signature:
where:
The following table summarizes the different types of call slots that can be used with the library:
Slot Type | Wrapper Function | Slot Signature |
---|---|---|
Simple Call Slot | wamp::simpleRpc<TResult, TArgs...> | TResult function(TArgs...) |
Simple Coroutine Call Slot | wamp::simpleCoroRpc<TResult, TArgs...> | TResult function(TArgs..., Yield) |
Unpacked Call Slot | wamp::unpackedRpc<TArgs...> | Outcome function(Invocation, TArgs...) |
Unpacked Coroutine Call Slot | wamp::unpackedCoroRpc<TArgs...> | Outcome function(Invocation, TArgs..., Yield) |
where Yield
represents the type boost::asio::yield_context
.
A simple call slot represents an RPC handler that expects one or more payload arguments having specific, static types. The wamp::simpleRpc function can be used when registering such call slots. It takes a simple call slot, and converts it into a regular call slot that can be passed to wamp::Session::enroll.
wamp::simpleRpc
expects a call slot with the following signature:
where
TResult
is a static return type that will be automatically converted to a single wamp::Result payload argument and then returned as a wamp::Outcome. This return type may be void
if the RPC is not expected to return a value.TArgs...
matches the template parameter pack that was passed to wamp::simpleRpc
.Examples of simple call slots are:
The above slots can be registered as follows:
where handler
is the asynchronous handler (or coroutine yield context) for the enroll operation itself.
Whenever a wamp::Session dispatches an RPC invocation to the above slots, it automatically unpacks the invocation payload positional arguments, and passes them to the slot's argument list. If Session
cannot convert the invocation payload arguments to their target types, it automatically sends an ERROR
reply back to the router.
A simple coroutine call slot is like a regular simple call slot, except that it is executed within the context of a coroutine. This is useful for RPC handlers that need to perform asynchronous operations themselves. The wamp::simpleCoroRpc function can be used when registering such call slots. It takes a simple coroutine call slot, and converts it into a regular call slot that can be passed to wamp::Session::enroll.
wamp::simpleCoroRpc
expects a call slot with the following signature:
where
TResult
is a static return type that will be automatically converted to a single wamp::Result payload argument and then returned as a wamp::Outcome. This return type may be void
if the RPC is not expected to return a value.TArgs...
matches the template parameter pack that was passed to wamp::simpleCoroRpc
.boost::asio::yield_context
represents the RPC's coroutine context.Examples of simple coroutine call slots are:
The above slots can be registered as follows:
where handler
is the asynchronous handler (or coroutine yield context) for the enroll operation itself.
Whenever a wamp::Session dispatches an RPC invocation to the above slots, it spawns a new coroutine to be executed on wamp::Session::userIosvc(). It then automatically unpacks the invocation payload positional arguments, and passes them to the slot's argument list. If Session
cannot convert the invocation payload arguments to their target types, it automatically sends an ERROR
reply back to the router.
An unpacked call slot represents an RPC handler that expects one or more payload arguments having specific, static types. The wamp::unpackedRpc function can be used when registering such call slots. It takes an unpacked call slot, and converts it into a regular call slot that can be passed to wamp::Session::enroll.
wamp::unpackedRpc
expects a call slot with the following signature:
where
TArgs...
matches the template parameter pack that was passed to wamp::unpackedRpc
.YIELD
message back to the caller in a separate asynchronous task.YIELD
message back to the caller.Examples of unpacked call slots are:
The above slots can be registered as follows:
where handler
is the asynchronous handler (or coroutine yield context) for the enroll operation itself.
Whenever a wamp::Session dispatches an RPC invocation to the above slots, it automatically unpacks the invocation payload positional arguments, and passes them to the slot's argument list. If Session
cannot convert the invocation payload arguments to their target types, it automatically sends an ERROR
reply back to the router.
An unpacked coroutine call slot is like an unpacked call slot, except that the slot is executed within the context of a coroutine. The wamp::unpackedCoroRpc function can be used when registering such call slots.
wamp::unpackedCoroRpc
expects a call slot with the following signature:
where
TArgs...
matches the template parameter pack that was passed to wamp::unpackedRpc
.boost::asio::yield_context
represents the RPC's coroutine context.RPC call slots are required to return a wamp::Outcome object. Outcome
is a discriminated union (i.e. variant) that can contain either:
An RPC call slot can send a result back to the caller by returning a wamp::Result object:
If only positional Variant
arguments need to be returned, they can be returned directly via a braced initializer list:
If an empty result needs to be returned, an empty initializer list can be used:
An RPC call slot can send an error back to the caller by returning a wamp::Error object:
Instead of returning an Error
object via the Outcome
return parameter, a call slot can also throw an Error
object as an exception. The library will catch such exceptions and will send a corresponding ERROR
message back to the caller:
boost::asio::io_service::run
.A deferred outcome means that the call slot does not return anything, and that measures will be taken by the callee to manually yield a Result
or Error
object back to the caller (via wamp::Invocation::yield). This can be useful when the call result can only be determined in a different asynchronous context.
Example:
A wamp::ScopedRegistration object can be used to limit a registration's lifetime to a particular scope. When a ScopedRegistration
object is destroyed, it automatically unregisters the RPC. This helps in automating the lifetime management of RPC registrations using RAII techniques.
Here's an example illustrating how ScopedRegistration
can be used to manage the lifetime of several registrations: