Skip to main content

Constructor

darra::opcua::Session Two overloads are provided: a simplified constructor (common parameters + default inferences) and a fully configured constructor (ConnectionConfig all fields explicitly passed).

Prerequisite Reading
Session(std::string const& endpoint,
MessageSecurityMode mode = MessageSecurityMode::None,
std::string const& username = "",
std::string const& password = "");

Parameters​

CategoryPropertyTypeAccessDescription
Requiredendpointstd::string const&inOPC UA endpoint, in the form of opc.tcp://host:port
SecuritymodeMessageSecurityModeinNone / Sign / SignAndEncrypt, Default: None
Loginusernamestd::string const&inempty = Anonymous, non-empty = UsernameIdentityToken
passwordstd::string const&inUsed with username

Examples​

#include <darra/opcua/client.hpp>
using namespace darra::opcua;

// 1. Anonymous + Plaintext (Development Phase)
Session s1("opc.tcp://localhost:4840");

// 2. Username + Plaintext (Development Phase)
Session s2("opc.tcp://server:4840",
MessageSecurityMode::None, "operator", "secret");

Full Configuration Structure — ConnectionConfig​

struct ConnectionConfig {
std::string endpoint_url;
MessageSecurityMode security_mode = MessageSecurityMode::None;
std::string security_policy_uri; // Leave blank to select automatically based on mode
UserTokenType user_token_type = UserTokenType::Anonymous;
std::string username;
std::string password;
std::string client_cert_path; // PFX path (Sign / SignAndEncrypt required)
std::string client_key_path; // The password string of that PFX (not the path to a private key file)
std::string server_cert_path; // Path to the server certificate file (optional; if not specified, it defaults to None→Secure automatic upgrade)
uint32_t session_timeout_ms = 60000;
uint32_t request_timeout_ms = 5000;
uint32_t connect_timeout_ms = 10000;
uint32_t keepalive_interval_ms = 10000;
bool auto_reconnect = true;
uint32_t reconnect_max_retries = 3;
uint32_t reconnect_delay_ms = 2000;
bool auto_publish_on_connect = true;
};

explicit Session(ConnectionConfig const& cfg);

Field​

CategoryPropertyTypeAccessDescription
Requiredendpoint_urlstd::stringinendpoint URL
Securitysecurity_modeMessageSecurityModeinNone / Sign / SignAndEncrypt, Default: None
security_policy_uristd::stringinIf left blank, the mode is selected automatically (None → #None, others → #Basic256Sha256)
client_cert_pathstd::stringinClient PFX path (Sign / SignAndEncrypt required)
client_key_pathstd::stringinPFX Password (field reuse)
server_cert_pathstd::stringinPath to the server certificate file (optional); if not provided, a plaintext channel is first used to obtain the server's self-reported certificate via GetEndpoints, then the encryption is upgraded; the certificate is not stored on disk, and no fingerprint comparison is performed
Loginuser_token_typeUserTokenTypeinAnonymous / Username (Certificate / Issued enumeration values exist but are not implemented)
username / passwordstd::stringinIf the username is not empty, it is automatically converted to a Username token
Timeoutsession_timeout_msuint32_tinServer session timeout; default is 60,000 (60 seconds)
request_timeout_msuint32_tinSingle RPC timeout, default 5000 (5 seconds)
connect_timeout_msuint32_tinConnection establishment timeout; default is 10,000 (10 seconds)
Heartbeat and Reconnectionkeepalive_interval_msuint32_tinKeepAlive interval, default 10000 (0 = disabled)
auto_reconnectboolinEnable automatic reconnection; default is true
reconnect_max_retriesuint32_tinMaximum number of reconnection attempts; default is 3
reconnect_delay_msuint32_tinReconnection interval; default is 2000
Publicationsauto_publish_on_connectboolinAutomatically start the background publish thread after connecting; default is true

Example of a Complete Configuration​

ConnectionConfig cfg;
cfg.endpoint_url = "opc.tcp://server:4840";
cfg.security_mode = MessageSecurityMode::SignAndEncrypt;
cfg.username = "operator";
cfg.password = "secret";
cfg.client_cert_path = "C:/certs/client.pfx";
cfg.client_key_path = "123456"; // The password of the PFX shown above, not the path to a key file.
cfg.session_timeout_ms = 300000;
cfg.keepalive_interval_ms = 5000;
cfg.auto_reconnect = true;
cfg.reconnect_max_retries = 5;

Session s(cfg);
s.Connect();

Exception​

ExceptionReason
Exception("Session_Create failed", ...)C-layer stack initialization failed / Session creation failed
Exception("Connect failed", ...)Network, certificate, or token errors during subsequent Connect() calls

The constructor itself does not initiate a network connection; it only creates a Session resource handle. For the actual connection, see Connect.

RAII Resource Management​

{
Session s(cfg);
s.Connect();
// ... Using s ...
} // Leaving scope: s destructor → Disconnect + Close + Free handle

Session cannot be copied or moved (it only has a = delete copy constructor, no move constructor), so the only option is in-place construction: Stack objects, or std::make_unique<Session>(cfg) / std::make_shared<Session>(cfg). All parameters must be passed by reference (Session&), not by value.

Global Initialization​

Construction: Must be done before Session Stack::Init(), and before the program exits Stack::Shutdown(). Multiple calls: Safe Stack::Init():

int main() {
Stack::Init();
{
Session s("opc.tcp://localhost:4840");
s.Connect();
// ...
}
Stack::Shutdown();
return 0;
}

Next Step​