CppWAMP
C++11 client library for the WAMP protocol
|
Except for establishing the underlying transport, WAMP client operations are performed via the wamp::Session class.
Asynchronous operations in wamp::Session accept a generic completion token, which can either be:
std::function
) taking a wamp::ErrorOr parameter,yield_context
(for stackful coroutines),use_awaitable
(for C++20 coroutines)use_future
(for std::future
)The outcome of a Session
asynchronous operation is emitted via wamp::ErrorOr<T>, where T
is the actual underlying result type. An ErrorOr
can contain either the actual result, or a std::error_code
if the operation failed. The ErrorOr::get
method is used to retrieve the underlying result value, throwing a wamp::error::Failure
exception if there was an error.
ErrorOr
is similar to the proposed std:expected
, which was not known to the CppWAMP author when this library was initially developed.
Because it makes things easier to demonstrate, the following tutorial pages will use Boost stackful couroutines. Using asynchronous callbacks is covered in Asynchronous Callbacks.
When passing a yield_context
to a Session
asynchronous operation, the operation will block within the coroutine until it is completed (or failed). While the operation is blocking, control is yielded to other pending asynchronous operations via the I/O context.
The ErrorOr
result of a Session
coroutine operation is emitted via the the member function's return value.
A yield_context
is obtained via boost::asio::spawn.
Once one or more wamp::Connector objects have been created, they are passed to the create factory method of the session API. create
then returns a std::shared_ptr
to the newly created wamp::Session object.
After the session object is created, the connect operation is used to establish a transport connection to a WAMP router. The join operation is then used to join a WAMP realm on the router.
The following example shows how to spawn a coroutine, and how a yield_context
is passed to the connect
and join
operations.
Note that while a coroutine operation is suspended, it yields control so that other asynchronous operations can be executed via the I/O context.
Also note the value()
method being invoked on the join
result to obtain the underlying wamp::SessionInfo. If there was an error during the join operation, value()
would throw a wamp::error::Failure exception.
Even though we're not interested in the result of the connect
operation in this example, the value()
method is invoked anyway so that an exception is thrown if there was a failure in the connect
operation.
If failure exceptions are not desired, the bool
operator of the ErrorOr
result can used used to check for errors:
To gracefully end a WAMP session, the leave and disconnect operations are used:
To abruptly end a WAMP session, you can skip the leave
operation and just disconnect
. This may be useful when handling error conditions. Alternatively, you can let the session shared_ptr
reference count drop to zero, and the session will abruptly terminate the connection in its destructor:
All pending coroutine operations can be aborted by dropping the client connection via Session::disconnect. Pending post-join operations can be also be aborted via Session::leave. Operations aborted in this manner will contain an error code in their returned wamp::ErrorOr result. There is currently no way to abort a single coroutine operation without dropping the connection or leaving the realm.
wamp::Session
can generate log events at runtime, for warnings and for inbound/outbound WAMP messages. CppWAMP is not opinionated about where the log events should go, so it provides the setWarningHandler and setTraceHandler methods that allow you to register handlers for these log events.
Warnings are generated when there are problems detected that don't prevent CppWAMP from performing its job.
Traces contain a stringified version of the WAMP protocol messages. They can be useful for troubleshooting without having to set up a network analyzer.
Both warning and trace handlers must be callable entities with the signature:
The handlers are posted via the executor that was passed to the session's create
method.
With these handlers you may, for example, log the messages to the console.
CppWAMP supports the AUTHENTICATE
and CHALLENGE
messages used for WAMP authentication. Algorithms for computing the cryptographic signatures needed for the WAMP-CRA and WAMP-SCRAM methods are currently not provided by CppWAMP. CppWAMP users must therefore compute these themselves using other cryptographic libraries. Once computed, the signatures can be passed to CppWAMP via wamp::Authentication.
To enable authentication, you must set the challenge handler via wamp::Session::setChallengeHandler. The handler must be a callable entity with the following signature:
Within the challenge handler, you must compute the cryptographic signature (or ticket string) and send it back via wamp::Challenge::authenticate.
When later performing the session join operation, you must set the authentication ID via wamp::Realm::withAuthId, as well as the desired/supported authentication methods via wamp::Realm::withAuthMethods.
The following example shows how to perform ticket authentication.
wamp::Session::setStateChangeHandler can be used to register a function that's called whenever the session's wamp::SessionState changes. This can be used, for example, to attempt reconnection when the session is disconnected due to a network failure.
Next: Error Handling