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).
- For encryption parameters (SecurityMode / SecurityPolicyUri / certificate), see Security Encryption.
- See "Server-Side Pre-Scan" Discovery.
- For instructions on what to do after connecting, see Connection Management.
Simplified Construction (Recommended)
Session(std::string const& endpoint,
MessageSecurityMode mode = MessageSecurityMode::None,
std::string const& username = "",
std::string const& password = "");
Parameters
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Required | endpoint | std::string const& | in | OPC UA endpoint, in the form of opc.tcp://host:port |
| Security | mode | MessageSecurityMode | in | None / Sign / SignAndEncrypt, Default: None |
| Login | username | std::string const& | in | empty = Anonymous, non-empty = UsernameIdentityToken |
password | std::string const& | in | Used 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
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Required | endpoint_url | std::string | in | endpoint URL |
| Security | security_mode | MessageSecurityMode | in | None / Sign / SignAndEncrypt, Default: None |
security_policy_uri | std::string | in | If left blank, the mode is selected automatically (None → #None, others → #Basic256Sha256) | |
client_cert_path | std::string | in | Client PFX path (Sign / SignAndEncrypt required) | |
client_key_path | std::string | in | PFX Password (field reuse) | |
server_cert_path | std::string | in | Path 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 | |
| Login | user_token_type | UserTokenType | in | Anonymous / Username (Certificate / Issued enumeration values exist but are not implemented) |
username / password | std::string | in | If the username is not empty, it is automatically converted to a Username token | |
| Timeout | session_timeout_ms | uint32_t | in | Server session timeout; default is 60,000 (60 seconds) |
request_timeout_ms | uint32_t | in | Single RPC timeout, default 5000 (5 seconds) | |
connect_timeout_ms | uint32_t | in | Connection establishment timeout; default is 10,000 (10 seconds) | |
| Heartbeat and Reconnection | keepalive_interval_ms | uint32_t | in | KeepAlive interval, default 10000 (0 = disabled) |
auto_reconnect | bool | in | Enable automatic reconnection; default is true | |
reconnect_max_retries | uint32_t | in | Maximum number of reconnection attempts; default is 3 | |
reconnect_delay_ms | uint32_t | in | Reconnection interval; default is 2000 | |
| Publications | auto_publish_on_connect | bool | in | Automatically 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
| Exception | Reason |
|---|---|
| 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;
}