open62541++ 0.13.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
Asynchronous model

Open62541++ adapts the well-proven asynchronous model of (Boost) Asio. A key goal of Asio's asynchronous model is to support multiple composition mechanisms. This is achieved via completion tokens.

Completion tokens

Open62541++ accepts completion tokens as the final argument of asynchronous operations.

// Function signature of the completion handler: void(opcua::Result<opcua::Variant>&)
template <typename CompletionToken = opcua::DefaultCompletionToken>
opcua::Client& client,
const opcua::NodeId& id,
CompletionToken&& token = opcua::DefaultCompletionToken()
);
High-level client class.
Definition Client.h:60
UA_NodeId wrapper class.
Definition NodeId.h:36
auto readValueAsync(Client &connection, const NodeId &id, CompletionToken &&token=DefaultCompletionToken())
Asynchronously read the AttributeId::Value attribute of a node.
Future completion token type.
Definition async.h:54

Following completion tokens can be used (and described in more detail below):

Callback function

If the user passes a function object as the completion token, the asynchronous operation behaves as previously described: the operation begins, and when the operation completes the result is passed to the callback. The callback function must match the expected signature:

void(opcua::Result<T>); // for void and non-void, trivially-copyable value types
void(opcua::Result<T>&); // for non-void value types
The template class Result encapsulates a StatusCode and optionally a value.
Definition Result.h:53
client,
id,
// ...
}
);

The callback is executed within the client's or server's event loop. Please make sure not to block the event loop. Waiting for asynchronous results within the callback will block the further execution of the event loop.

Future completion token

The special token opcua::useFuture can be passed as completion token to return a future object std::future<opcua::Result<T>>.

std::future<opcua::Result<opcua::Variant>> future = opcua::services::readValueAsync(client, id, opcua::useFuture);
auto value = future.get().value(); // throws an exception if an error occurred
constexpr UseFutureToken useFuture
Future completion token object.
Definition async.h:60

Deferred completion token

The token opcua::useDeferred is used to indicate that an asynchronous operation should return a function object to lazily launch the operation.

// start operation with provided completion token
func([](auto&& result) {
// ...
});
constexpr UseDeferredToken useDeferred
Deferred completion token object.
Definition async.h:90

Detached completion token

The token opcua::useDetached is used to indicate that an asynchronous operation is detached. That is, there is no completion handler waiting for the operation's result.

// no way to check if the operation succeeded...
static Variant fromScalar(T &&value)
Create Variant from scalar value.
Definition Variant.h:53
auto writeValueAsync(Client &connection, const NodeId &id, const Variant &value, CompletionToken &&token=DefaultCompletionToken())
Asynchronously write the AttributeId::Value attribute of a node.

Custom completion token

The opcua::AsyncResult trait is a customization point to define user-defined completion tokens via template specialization:

struct PrintResultToken {};
namespace opcua {
template <typename T>
struct AsyncResult<PrintResultToken, T> {
template <typename Initiation, typename CompletionHandler, typename... Args>
static void initiate(Initiation&& initiation, PrintResultToken, Args&&... args) {
std::invoke(
std::forward<Initiation>(initiation),
[](opcua::Result<T>& result) {
std::cout << "Async operation completed: code=" << result.code() << ", value=" << result.value() << std::endl;
},
std::forward<Args>(args)...
);
}
};
}
constexpr T & value() &
Get the value of the Result.
Definition Result.h:158
constexpr StatusCode code() const noexcept
Get the StatusCode of the Result.
Definition Result.h:136
static void initiate(Initiation &&initiation, CompletionHandler &&handler, Args &&... args)
Definition async.h:29

The trait's opcua::AsyncResult::initiate member function is called with three arguments:

  1. A function object that launches the async operation (initiating function)
  2. A concrete completion handler with the signature void(opcua::Result<T>) or void(opcua::Result<T>&)
  3. Any additional arguments for the function object

Please have a look at implementations in async.h for further details.