跳到主要内容

服务端信息

概述​

OPC UA 标准节点 i=2256 (Server.ServerStatus) 暴露服务端运行时信息。SDK 不解 ExtensionObject,而是分别读 i=2257 / i=2258 / i=2259 / i=2261..i=2266 / i=2992 / i=2993 共 11 个子节点,容错装配成强类型 ServerStatus。

对应规范段: Part 5 §6.3。

API​

三个入口都是 darra::opcua 命名空间下的自由函数(server_info.hpp),第一个参数传 Session&,不是 Session 的成员方法。

函数类别读写说明
ReadServerStatus诊断读一次装配完整 ServerStatus(共 11 次 Read RPC)
ReadServerState诊断读只读 i=2259,单 RPC
ReadCurrentTimeFileTime诊断读只读 i=2258,单 RPC,返回 int64_t FileTime
namespace darra::opcua {

// 11 次 Read RPC 装配; 任一字段读失败用默认值, state 兜底 Unknown. 不抛.
ServerStatus ReadServerStatus(Session& s);

// 仅读 i=2259. 失败返回 ServerState::Unknown, 不抛.
ServerState ReadServerState(Session& s);

// 仅读 i=2258. 失败返回 0. 时间是 FileTime (UTC 100ns since 1601), 不是 time_point.
int64_t ReadCurrentTimeFileTime(Session& s);
}

相关结构 (ServerStatus 字段):

  • start_time_filetime (int64_t) — 启动时间, FileTime (UTC 100ns since 1601)
  • current_time_filetime (int64_t) — 当前时间, FileTime
  • state (ServerState) — 当前状态
  • build (BuildInfo) — 产品/版本
  • seconds_till_shutdown (uint32_t) — 距关停秒数
  • shutdown_reason (std::string) — 关停原因
  • ToString() — 拼成 State=..., Start=..., Now=..., Build=[...]

相关结构 (BuildInfo 字段):

  • product_uri / manufacturer_name / product_name / software_version / build_number (std::string)
  • build_date_filetime (int64_t) — 构建日期, FileTime, 0 = 未知
  • ToString() — 拼成 产品名 版本 (build 编号)

相关结构 (ServerState 枚举):

  • Running (0) — 正常
  • Failed (1) — 致命故障
  • NoConfiguration (2) — 缺配置
  • Suspended (3) — 被运维挂起
  • Shutdown (4) — 关停中
  • Test (5) — 测试模式
  • CommunicationFault (6) — 链路故障
  • Unknown (7) — SDK 兜底

代码示例​

#include <darra/opcua/client.hpp>
#include <darra/opcua/server_info.hpp>
#include <chrono>
#include <iostream>

using namespace darra::opcua;

Session s("opc.tcp://localhost:4840");
s.Connect();

// 1) 一次性装配 (共 11 次 Read RPC)
ServerStatus status = ReadServerStatus(s);
std::cout << status.ToString() << "\n";
std::cout << "Build = " << status.build.product_name << " "
<< status.build.software_version << "\n";

if (status.state != ServerState::Running)
std::cout << "非 Running, 倒计时 " << status.seconds_till_shutdown << "s\n";

// 2) 轻量心跳 (单 RPC)
if (ReadServerState(s) == ServerState::Running)
std::cout << "在线\n";

// 3) 时钟漂移 — 服务端返回 FileTime, 先换算再比
constexpr int64_t WIN_EPOCH_OFFSET = 116444736000000000LL;
int64_t server_ft = ReadCurrentTimeFileTime(s); // 0 = 读失败
if (server_ft != 0) {
auto now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
int64_t local_ft = WIN_EPOCH_OFFSET + now_ns / 100;
std::cout << "时钟差 = " << (local_ft - server_ft) / 10000 << " ms\n";
}

ServerStatus::start_time_filetime / current_time_filetime / build.build_date_filetime 都是 FileTime(UTC 100ns since 1601-01-01),与 Windows DateTime.ToFileTimeUtc 一致,不是 system_clock::time_point。

跨语言对照​

C#PythonJavaC++RustC
ReadServerStatusread_server_statusreadServerStatusReadServerStatusread_server_statusDarraUa_Session_ReadServerStatus
ReadServerStateread_server_statereadServerStateReadServerStateread_server_stateDarraUa_Session_ReadServerState
ReadCurrentTimeread_current_timereadCurrentTimeReadCurrentTimeFileTimeread_current_timeDarraUa_Session_ReadCurrentTime

C++ 这三个是自由函数,第一个参数是 Session&;C# / Python / Java / Rust 是会话对象上的方法。

下一步​