CppWAMP
C++11 client library for the WAMP protocol
Asynchronous Callbacks

The previous tutorials have used stackful routines to demonstrate CppWAMP. Traditional "callback" handler functions can also be used with wamp::Session.

For asynchronous operations that can fail, Session expects a handler taking an wamp::ErrorOr<T> as a parameter. ErrorOr<T> makes it difficult for handlers to ignore error conditions when accessing the result of an asynchronous operation via its value method.

The following example shows how to establish a connection using callback handler functions:

#include <cppwamp/json.hpp>
#include <cppwamp/tcp.hpp>
using namespace wamp;
int main()
{
AsioContext ioctx;
auto tcp = connector<Json>(ioctx, TcpHost("localhost", 8001));
auto session = Session::create(ioctx, tcp);
// Asynchronously connect to the router, using a lambda function for
// the handler
session->connect( [](ErrorOr<size_t> result)
{
// Obtain the result of the connect operation, which is the index of
// the connector used to establish the transport connection.
// ErrorOr::get throws an error::Failure exception if the
// operation failed.
auto index = result.value();
});
ioctx.run();
return 0;
}

Instead of letting wamp::ErrorOr::get throw an exception upon failure, wamp::ErrorOr::operator bool() and wamp::ErrorOr::errorCode can be used to check the error status of an asynchronous operation:

using namespace wamp;
session->connect( [](ErrorOr<size_t> index)
{
if (index)
{
std::cout << "Connected via transport #" << (index.value()+1) << "\n";
// Continue as normal...
}
else
{
std::cerr << "Failure connecting: " << index.error() << "\n";
}
});

The following example shows how to call member functions within asynchronous handlers, and how to chain one asynchronous operation after another:

using namespace wamp;
class App
{
public:
explicit App(wamp::Connector::Ptr connector)
: session_(wamp::Session::create(connector))
{}
void start()
{
session_->connect( [this](wamp::AsyncHandler<size_t> index)
{
index.value(); // throws if connection failed
this->join();
});
}
private:
void join()
{
session_->join(Realm("somerealm"),
{
sessionInfo_ = info.value(); // throws if join failed
// Continue with other asynchronous operations...
}
);
}
wamp::SessionInfo sessionInfo_;
};

With this asynchronous style of programming, it is not immediately obvious how the program control flows. Couroutines are therefore useful in implementing asynchronous logic in a more synchronous, sequential manner.

For complete examples using the asynchronous API see:


Next: User Type Conversions

wamp::Session::create
static Ptr create(AnyIoExecutor exec, const Connector::Ptr &connector)
Creates a new Session instance.
Definition: session.ipp:22
session.hpp
Contains the asynchronous session API used by a client peer in WAMP applications.
wamp::Realm
Realm URI and other options contained within WAMP HELLO messages.
Definition: peerdata.hpp:59
wamp::Connector::Ptr
std::shared_ptr< Connector > Ptr
Shared pointer to a Connector.
Definition: connector.hpp:43
wamp::ErrorOr::error
error_type & error()
Unchecked access of the stored error.
Definition: erroror.hpp:292
wamp::Session::Ptr
std::shared_ptr< Session > Ptr
Shared pointer to a Session.
Definition: session.hpp:117
wamp::TcpHost
Contains TCP host address information, as well as other socket options.
Definition: tcphost.hpp:103
wamp::ErrorOr::value
value_type & value() &
Checked access of the stored value.
Definition: erroror.hpp:257
wamp
Definition: anyhandler.hpp:36
json.hpp
Contains the JSON codec.
tcp.hpp
Contains facilities for creating TCP transport connectors.
wamp::AsyncHandler
std::function< void(ErrorOr< T >)> AsyncHandler
Type alias for a handler taking an ErrorOr<T> parameter.
Definition: erroror.hpp:495
wamp::ErrorOr
Minimalistic implementation of std::expected<T, std::error_code>
Definition: erroror.hpp:128
wamp::Session
Session API used by a client peer in WAMP applications.
Definition: session.hpp:110
wamp::SessionInfo
Session information contained within WAMP WELCOME messages.
Definition: peerdata.hpp:97
wamp::AsioContext
boost::asio::io_context AsioContext
Queues and runs I/O completion handlers.
Definition: asiodefs.hpp:34