open62541pp 0.19.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
method/server_method_async.cpp
#include <chrono>
#include <iostream>
#include <thread>
int main() {
opcua::Server server;
const opcua::NodeId objectsId{opcua::ObjectId::ObjectsFolder};
const opcua::NodeId methodId{1, 1000};
const auto result = opcua::services::addMethod(
server,
objectsId,
methodId,
"Greet",
const auto& name = input.at(0).scalar<opcua::String>();
const auto greeting = std::string{"Hello "}.append(name);
output.at(0) = greeting;
},
{{"name", {"en-US", "your name"}, opcua::DataTypeId::String, opcua::ValueRank::Scalar}},
{{"greeting", {"en-US", "greeting"}, opcua::DataTypeId::String, opcua::ValueRank::Scalar}},
opcua::ReferenceTypeId::HasComponent
);
result.code().throwIfBad();
// Enable async operation for the method node
// This will queue the method call operations to be processed in a worker thread
opcua::useAsyncOperation(server, methodId, true);
// Process async operations in a separate worker thread
std::thread workerThread{[&server]() {
while (true) {
const auto operation = opcua::getAsyncOperation(server);
if (operation.has_value()) {
opcua::runAsyncOperation(server, operation.value());
std::cout << "Async operation processed\n";
} else {
std::this_thread::sleep_for(std::chrono::milliseconds{10});
}
}
}};
server.run();
workerThread.join();
}
UA_NodeId wrapper class.
Definition types.hpp:641
High-level server class.
Definition server.hpp:142
void run()
Run the server's main loop. This method will block until Server::stop is called.
View to a contiguous sequence of objects, similar to std::span in C++20.
Definition span.hpp:29
constexpr reference at(size_t index) const
Access element by index with bounds checking.
Definition span.hpp:114
UA_MethodAttributes wrapper class.
Definition types.hpp:483
Result< NodeId > addMethod(T &connection, const NodeId &parentId, const NodeId &id, std::string_view browseName, MethodCallback callback, Span< const Argument > inputArguments, Span< const Argument > outputArguments, const MethodAttributes &attributes, const NodeId &referenceType) noexcept
Add method.