KeepAlive Heartbeat
Session Includes a built-in KeepAlive background thread: It performs a ping by calling ( ServerStatus.CurrentTime i=2258) once per cycle, and the results are passed on_keepalive to the business side via.
- For Heartbeat Event Callbacks, see Events.
- For the automatic reconnection process, please see Connection Management.
Configuration
During construction, via ConnectionConfig.keepalive_interval_ms:
#include <darra/opcua/client.hpp>
ConnectionConfig cfg;
cfg.endpoint_url = "opc.tcp://server:4840";
cfg.keepalive_interval_ms = 5000; // Change it to 5 seconds
Session s(cfg);
Runtime changes:
s.SetKeepAliveInterval(5000); // Restart the heartbeat thread immediately
| Item | Value |
|---|---|
| Unit | milliseconds |
| Default | 10000 (10 seconds) |
| 0 | Disable (Connect() Do not start, SetKeepAliveInterval(0) Stop) |
| Stop Responding | Effective no later than 100 ms after the heartbeat stops |
Start-Stop System
| Timing | Behavior |
|---|---|
| After Connect() succeeds | keepalive_interval_ms > 0 Then start |
| Disconnect() / Destructor | Stop |
| SetKeepAliveInterval(ms) | When connected, stop first, then press to start a new cycle (ms=0: stop only, do not start) |
| During automatic reconnection | The thread never stops, Automatically skips the current scan when not connected; resumes normally once the session is restored |
Heartbeat Event Callback
Each detection triggers on_keepalive, Regardless of success or failure — the result is reported in the second parameter:
constexpr int64_t WIN_EPOCH_OFFSET = 116444736000000000LL;
s.Events().on_keepalive = [](int64_t server_time_ft, Status server_status) {
if (server_status != Status::Good) { // No data was read during this detection.
std::cerr << "KeepAlive 探测失败 0x" << std::hex
<< static_cast<uint32_t>(server_status) << "\n";
return;
}
time_t t = (server_time_ft - WIN_EPOCH_OFFSET) / 10000000;
std::cout << "KeepAlive @ " << t << "\n";
};
- Read success:
server_time_ft=i=2258theSourceTimestamp()(FileTime),server_status= that value'sStatusEnum() - Read failure:
server_time_ft= 0,server_status= status code returned by the read operation
on_communication_error The cat parameter is always "keepalive", and is triggered only when an exception is thrown during the detection process:
s.Events().on_communication_error = [](Status code, std::string const& cat, std::string const& msg) {
if (cat == "keepalive") {
std::cerr << "Heartbeat error: 0x" << std::hex << static_cast<uint32_t>(code)
<< " " << msg << "\n";
}
};
Relationship with SessionTimeout
ConnectionConfig.session_timeout_ms isThe client submits the application in CreateSession . The session keep-alive duration is set to the default value 60000 ms. The server can modify this value, and the value modified by the server is the one that takes effect.
The KeepAlive interval should be significantly shorter than the session timeout; otherwise, the server may close the inactive session first, and the client will not receive the session expiration status code until its next request.
| session_timeout_ms | keepalive_interval_ms |
|---|---|
| 60000 (default) | 10000 (default) |
| 30000 | 5000 |
Why is the detection target ServerStatus.CurrentTime
i=2258 (ServerStatus.CurrentTime) is a standard OPC Foundation node; all servers that comply with the specification have one, and reading it incurs minimal overhead. By default SDK, KeepAlive always reads this node; there are no configurable probe nodes.
Custom Heartbeat Logic
If you want to completely disable the SDK built-in heartbeat, implement your own:
#include <darra/opcua/client.hpp>
#include <chrono>
#include <thread>
ConnectionConfig cfg;
cfg.endpoint_url = "opc.tcp://server:4840";
cfg.keepalive_interval_ms = 0; // Disable SDK Built-in
Session s(cfg);
s.Connect();
// Creating a std::thread on your own
std::thread heartbeat([&]() {
while (s.IsConnected()) {
try {
auto dv = s.Read("i=2258"); // Or read "Business Heartbeat Node"
if (!dv.IsGood()) std::cerr << "heartbeat bad\n";
} catch (Exception const& ex) {
std::cerr << "heartbeat: " << ex.what() << "\n";
}
std::this_thread::sleep_for(std::chrono::seconds(5));
}
});
// At the end of the transaction: s.Disconnect(); heartbeat.join();
Overhead per detection
Each cycle corresponds to a single Read RPC (reading the Value of i=2258). The duration of the cycle directly determines the frequency of this RPC; select a value based on the local network conditions: 1–5 seconds for a local area network, and around 10 seconds for cross-subnet connections.