CppWAMP
C++11 client library for the WAMP protocol
Connectors

wamp::Connector is the abstract base class for objects that can establish client transport endpoints.

The library currently supports two types of connectors:

Connector Factory Declared in For Transport
connector<TCodec>(AnyExecutor, TcpHost) <cppwamp/tcp.hpp> TCP raw socket
connector<TCodec>(AnyExecutor, UdsPath) <cppwamp/uds.hpp> Unix domain socket

When creating a connector, you must specify which codec class (aka serializer) to use for encoding WAMP messages. The following codecs are currently supported:

Codec Declared in
wamp::Json <cppwamp/json.hpp>
wamp::Msgpack <cppwamp/msgpack.hpp>
wamp::Cbor <cppwamp/cbor.hpp>

Connectors are created on the heap using the wamp::connector factory function as shown in the examples below. wamp::connector returns a std::shared_ptr to the newly created wamp::Connector.

After you have created one or more connectors, you pass them to the session API. The session object will then use these connectors while establishing a transport connection to the router.

Creating a TCP Raw Socket Connector with the Json codec

#include <cppwamp/json.hpp>
#include <cppwamp/tcp.hpp>
using namespace wamp;
AsioContext ioctx; // AsioContext is an alias to boost::asio::io_context
auto tcp = connector<Json>(
ioctx, // The I/O context to use for asynchronous operations
"127.0.0.1", // Host address
8001 // Port number (could also be a service name string)
)
);
// Create a `Session` object which shall later use the above connector
// when establishing a transport connection.
auto session = Session::create(tcp);

Creating a Unix Domain Socket Connector with the Msgpack codec

#include <cppwamp/uds.hpp>
using namespace wamp;
AsioContext ioctx; // AsioContext is an alias to boost::asio::io_context
auto uds = connector<Msgpack>(
ioctx, // I/O context to use for asynchronous operations
UdsPath("path/to/uds") // Path name of the Unix domain socket
);
// Create a `Session` object which shall later use the above connector when
// establishing a transport connection.
auto session = Session::create(uds);

Specifying Extra Socket Options

The wamp::TcpHost object allows you to specify extra socket options. See the reference documentation for more details.

Likewise, the wamp::UdsPath object also allows you to specify extra socket options.

This snippet below shows how to create a TCP connector with a specified maximum incoming message length, as well as with Nagle's algorithm disabled:

using namespace wamp;
auto tcp = connector<Json>(
ioctx,
TcpHost("127.0.0.1", 8001)
.withMaxRxLength(RawsockMaxLength::MB_1)
.withOptions(TcpOptions.withNoDelay())
);

Combining Connectors

More than one Connector can be passed to a session object. In such cases, the session will successively attempt to establish a transport connection with each of the connectors until one succeeds. This allows you to specify "fallback" transports if the primary one fails to establish.

To achieve this, you must pass a std::vector of Connector shared pointers while creating session objects. The following example uses two TcpConnector objects, each specifying a different serializer:

#include <cppwamp/json.hpp>
#include <cppwamp/tcp.hpp>
using namespace wamp;
auto tcpJson = connector<Json>(ioctx, TcpHost("localhost", 8001));
auto tcpPack = connector<Msgpack>(ioctx, TcpHost("localhost", 8001));
// Create a `Session` object which shall later use both of the above
// connectors (in succession) when establishing a transport connection.
auto session = Session::create({tcpJson, tcpPack});

Next: Sessions

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::UdsPath
Contains a Unix domain socket path, as well as other socket options.
Definition: udspath.hpp:95
uds.hpp
Contains facilities for creating Unix domain socket transport connectors.
wamp::TcpHost
Contains TCP host address information, as well as other socket options.
Definition: tcphost.hpp:103
msgpack.hpp
Contains the Msgpack codec.
wamp
Definition: anyhandler.hpp:36
wamp::TcpOptions::withNoDelay
TcpOptions & withNoDelay(bool enabled=true)
Adds the TCP_NODELAY socket option.
Definition: tcphost.ipp:35
wamp::RawsockMaxLength::MB_1
@ MB_1
1 megabyte
json.hpp
Contains the JSON codec.
tcp.hpp
Contains facilities for creating TCP transport connectors.
wamp::TcpOptions
Contains options for the TCP transport.
Definition: tcphost.hpp:37
wamp::AsioContext
boost::asio::io_context AsioContext
Queues and runs I/O completion handlers.
Definition: asiodefs.hpp:34