Session — darra::opcua::Session
Session This is the main Client class; one instance corresponds to one OPC UA Session. All access operations (Read / Write / Browse / Subscribe / Call / History) originate from this object. Cannot be copied or moved, Automatically disconnect and release resource handles.
#include <darra/opcua/client.hpp>
using namespace darra::opcua;
Subpage Redirect
- For structural and connection specifications, please refer to Constructor.
- For connecting/disconnecting, please refer to Connection Management.
- For single-item and batch read/write operations, please refer to Read / Write.
- For the unified channel for internal events, please refer to Events.
- For namespace lookup, please refer to Namespaces.
- For information on heartbeat and session keep-alive, please refer to KeepAlive.
Public Accessor
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Connections | EndpointUrl() | std::string const& | R | The endpoint URL specified at construction |
Config() | ConnectionConfig const& | R | Complete connection configuration (including SecurityMode etc.) | |
Handle() | DarraUa_SessionHandle | R | C-layer handle (for internal use) | |
| Status | State() | SessionState | R | Current session status (Disconnected / Connecting / Connected / Reconnecting / Closing / Failed) |
IsConnected() | bool | R | Equivalent to State() == Connected | |
| Child Objects | Nodes() | NodeCollection& | R | Lazy Load node accessor |
NamespacesRef() | Namespaces& | R | Namespace table | |
Events() | SessionEvents& | R | Session-level events (field-style std::function) | |
| Configuration | SetKeepAliveInterval(ms) | void | W | KeepAlive heartbeat interval (default 10000 ms, 0 = disabled) |
AutoPublish() / SetAutoPublish(bool) | bool | RW | Enables or disables the background publish thread (default: true) |
Public Methods
Connection Management
Connect()— Establish a connection (Hello → SecureChannel → Session → Activate)Disconnect()— Connection terminated voluntarily; the instance can reconnect~Session()— Automatic Disconnect + Release handle (RAII)
Heartbeat and Release
Publish(timeout_ms = 2000)— Manually trigger a Publish once (usually not necessary)StartAutoPublish() / StopAutoPublish()— Start or stop the background Publish thread
Read / Write
Read(node_id, attr = AttributeId::Value)— Read a Single Node's Specified AttributeReadMany(node_ids, attr)— Batch Read (Single Attribute)Write(node_id, variant, attr = Value)— Order Entry Node
Browse
Browse(node_id, filter = Unspecified)— Single-page viewBrowseMany(node_ids, filter)— Batch ViewBrowseWithPaging(node_id, filter)— Includes ContinuationPoint page-turning supportBrowseNext(continuation_point, release = false)— Continue to the next pageTranslateBrowsePaths(paths)— Batch parse the paths to NodeIdResolve(path)— Parsing a Single Absolute PathResolve(start_node_id, path)— Parsing a Single Relative PathRegisterNodes(node_ids)— Register for a Temporary High-Efficiency Plan NodeIdUnregisterNodes(registered_ids)— Cancel Temporary NodeId
Subscribe
CreateSubscription(interval_ms = 500.0)— Create a data subscriptionSubscribeEvents(node_id = "", existing = nullptr)— Subscribe to Events (Alarms / Conditions)TransferSubscriptions(sub_ids, send_initial)— Migrating Subscriptions Across Sessions
Method Call
Call(object_id, method_id, inputs)— Call the node OPC UA Method
History
ReadHistory(node_id, start_ft, end_ft, max_values = 0, return_bounds = false)— Reading Ancient HistoryReadHistoryModified(...)— Reading a history that includes revision recordsReadHistoryAtTime(...)— Read the interpolated value at a specified time pointReadHistoryProcessed(...)— Aggregation (Avg / Min / Max / TimeAverage / ...)ReadHistoryEvents(...)— Reading About Historical EventsUpdateHistory(...)— Insert / Replace / Update / Remove HistoryDeleteHistoryRange(...)— Delete by time period
Control
Cancel(request_handle)— Cancel Unfinished Requests
Copy / Move Semantics
Session Only the copy constructor and copy assignment operator for = delete have been declared, None Move construction (the destructor is user-declared; the compiler no longer implicitly generates a move). Neither copying nor moving passes compilation; only in-place construction and passing by reference are allowed:
Session s("opc.tcp://localhost:4840");
// Session s2 = s; // Compilation error: copy constructor deleted
// Session s2 = std::move(s); // Compilation error: No move constructor; falls back to the deleted copy constructor
Session& ref = s; // OK: Pass-by-reference
Complete Example
#include <darra/opcua/client.hpp>
#include <chrono>
#include <iostream>
#include <thread>
using namespace darra::opcua;
int main() {
Stack::Init();
{
Session s("opc.tcp://localhost:4840");
s.Events().on_connected = [](std::string const& url) {
std::cout << "Connected to " << url << "\n";
};
s.Events().on_disconnected = [](std::string const& url, Status reason) {
std::cout << "Disconnected from " << url
<< " reason=0x" << std::hex << static_cast<uint32_t>(reason) << "\n";
};
s.Connect();
// Read
auto dv = s.Read("ns=2;s=Temperature");
double t = 0; dv.Value().TryGetDouble(t);
std::cout << "T = " << t << " degC\n";
// Write
Variant v; v.SetDouble(42.0);
auto st = s.Write("ns=2;s=Setpoint", v);
std::cout << "Write status = 0x" << std::hex << static_cast<uint32_t>(st) << "\n";
// Subscribe
auto sub = s.CreateSubscription(500.0);
sub.AddNode("ns=2;s=Counter", [](DataValue const& dv) {
std::cout << dv.Value().AsString() << "\n";
});
// Methods
std::vector<Variant> args;
{ Variant a; a.SetInt32(3); args.push_back(std::move(a)); }
{ Variant b; b.SetInt32(4); args.push_back(std::move(b)); }
auto outs = s.Call("ns=2;s=Calc", "ns=2;s=Calc.Add", args);
for (auto const& o : outs) {
int32_t sum = 0; o.TryGetInt32(sum);
std::cout << "sum = " << sum << "\n";
}
std::this_thread::sleep_for(std::chrono::minutes(1));
} // sub / s is destroyed here — must occur before Stack::Shutdown()
Stack::Shutdown();
return 0;
}