Skip to main content

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​

CategoryPropertyTypeAccessDescription
ConnectionsEndpointUrl()std::string const&RThe endpoint URL specified at construction
Config()ConnectionConfig const&RComplete connection configuration (including SecurityMode etc.)
Handle()DarraUa_SessionHandleRC-layer handle (for internal use)
StatusState()SessionStateRCurrent session status (Disconnected / Connecting / Connected / Reconnecting / Closing / Failed)
IsConnected()boolREquivalent to State() == Connected
Child ObjectsNodes()NodeCollection&RLazy Load node accessor
NamespacesRef()Namespaces&RNamespace table
Events()SessionEvents&RSession-level events (field-style std::function)
ConfigurationSetKeepAliveInterval(ms)voidWKeepAlive heartbeat interval (default 10000 ms, 0 = disabled)
AutoPublish() / SetAutoPublish(bool)boolRWEnables 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​

Browse​

Subscribe​

Method Call​

History​

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;
}

Next Step​