Safe API — Never Throw Exception Variants
Overview
The UI main thread / batch jobs must not be interrupted by a darra::opcua::Exception thrown on an RPC failure, so the SDK provides a Safe variant for each common call — every exception is folded into a Status inside Result<T>, which the caller inspects.
All five entries are under the namespace darra::opcuaFree Functions(safe_api.hpp), the first parameter is passed to Session&, which is not a member method of Session.
Corresponding sections of the standard: Browse Part 4 §5.8, Read §5.10.2, Write §5.10.4, Call§5.11.2, HistoryRead Part 11§6.5.
API
| Function | Category | Reading and Writing | Return |
|---|---|---|---|
| BrowseSafe | Browse | Read | Result<std::vector<Reference>> |
| ReadSafe | Read | Read | Result<DataValue> |
| WriteSafe | Write | Write | Status(Returns the status code directly, without a Result) |
| CallSafe | Methods | Write | Result<std::vector<Variant>> |
| ReadHistorySafe | History | Read | Result<std::vector<DataValue>> |
namespace darra::opcua {
/// Generic result package. When status != Good, value is default-constructed.
template <typename T>
struct Result {
Status status = Status::Bad;
T value{};
explicit operator bool() const; // status == Status::Good
static Result<T> Ok(T v);
static Result<T> Err(Status s);
};
/// DataValue There is no default constructor; this is a specialized case: the default value is an empty handle (owns=false).
template <>
struct Result<DataValue> {
Status status = Status::Bad;
DataValue value = DataValue(nullptr, /*owns=*/false);
explicit operator bool() const;
};
Result<std::vector<Reference>> BrowseSafe(Session& s, std::string const& nid,
NodeClass filter = NodeClass::Unspecified);
Result<DataValue> ReadSafe (Session& s, std::string const& nid,
AttributeId attr = AttributeId::Value);
Status WriteSafe(Session& s, std::string const& nid,
Variant const& value,
AttributeId attr = AttributeId::Value);
Result<std::vector<Variant>> CallSafe (Session& s, std::string const& obj_id,
std::string const& mtd_id,
std::vector<Variant> const& inputs);
Result<std::vector<DataValue>> ReadHistorySafe(Session& s, std::string const& nid,
int64_t start_ft, int64_t end_ft,
uint32_t max_values = 0,
bool return_bounds = false);
}
| Parameters | Default | Note |
|---|---|---|
| s | — | Connected Session |
| nid / obj_id / mtd_id | — | NodeId String |
| filter | NodeClass::Unspecified | Browse NodeClass Filter |
| attr | AttributeId::Value | Property Id |
| value | — | Variant const& |
| inputs | — | std::vector<Variant> const& |
| start_ft / end_ft | — | FileTime(UTC 100ns since 1601) |
| max_values | 0 | 0 = No limit |
| return_bounds | false |
How to Interpret Return Values:if (r) Equivalent to r.status == Status::Good.Result Using the two r.value fields r.status +, Nor.references / r.outputs / r.values These scenario-based field names—all five Safe rules use the same template Result<T> .ReadSafe The r.value is an object DataValue (not a pointer), and retrieving its value requires going through another layer like .Value().AsString() this.
Code Example
#include <darra/opcua/client.hpp>
#include <darra/opcua/safe_api.hpp>
#include <iostream>
#include <string>
#include <vector>
using namespace darra::opcua;
Session s("opc.tcp://localhost:4840");
s.Connect();
// 1) Browse - Deleting a node should not cause an error for the entire tree
auto br = BrowseSafe(s, "ns=2;s=Boiler1");
if (!br) {
std::cout << "Browse 失败 " << Stack::StatusName(static_cast<uint32_t>(br.status)) << "\n";
} else {
for (auto const& r : br.value) std::cout << r.browse_name << "\n";
}
// 2) Batch Polling—A single failure does not interrupt the process
for (auto const& nid : node_list) {
auto rr = ReadSafe(s, nid);
if (rr) {
std::cout << nid << " = " << rr.value.Value().AsString() << "\n";
} else {
std::cout << nid << " = <" << Stack::StatusName(static_cast<uint32_t>(rr.status)) << ">\n";
}
}
// 3) Batch parameter assignment — This is Variant "move-only"; use the setter, not Variant(val)
for (auto const& [nid, val] : setpoints) {
Variant v;
v.SetDouble(val);
Status rc = WriteSafe(s, nid, v);
if (rc != Status::Good)
std::cout << "写 " << nid << " 失败 " << Stack::StatusName(static_cast<uint32_t>(rc)) << "\n";
}
// 4) Calling the Method
auto cr = CallSafe(s, "ns=2;s=Robot", "ns=2;s=Robot.Home", {});
if (!cr) std::cerr << "Home 失败: " << Stack::StatusName(static_cast<uint32_t>(cr.status)) << "\n";
// 5) History
auto hr = ReadHistorySafe(s, "ns=2;s=Temp", t0, t1, 5000);
Relationship to the Standard Version
Normal Read / Write / Browse / Call / On ReadHistory failureThrow darra::opcua::Exception(Use ex.status_code() to get the source code). The Safe variant wraps these calls in a layer try/catch, writes Exception::status() into Result::status,catch (...) and falls back to Status::Bad.
The SDK has no wrapper for "auto-reconnect and retry on session failure" — reconnection is governed by auto_reconnect / reconnect_max_retries / reconnect_delay_ms in ConnectionConfig; see Automatic Reconnection.
Best Practices
- On the UI main thread always use
reconnect_delay_ms— a C++ exception escaping into the Qt event loop will crash the process. - Safe for Batch Processing / Inspection Cycles
- Continue using the standard version for the business critical path, and have exceptions explicitly throw errors.
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| BrowseSafe | browse_safe | browseSafe | BrowseSafe | browse_safe | DarraUa_Session_BrowseSafe |
| ReadSafe | read_safe | readSafe | ReadSafe | read_safe | DarraUa_Session_ReadSafe |
| WriteSafe | write_safe | writeSafe | WriteSafe | write_safe | DarraUa_Session_WriteSafe |
| CallSafe | call_safe | callSafe | CallSafe | call_safe | DarraUa_Session_CallSafe |
| ReadHistorySafe | read_history_safe | readHistorySafe | ReadHistorySafe | read_history_safe | DarraUa_Session_ReadHistorySafe |
C++ uses free functions (first parameter Session&); C# and Python use methods on session objects that return tuples or; Result Java returns BrowseResult / CallResult /, etc HistoryResult .Dedicated Result Class; Rust returns a tuple (StatusCode, 值).