Discovery 发现
darra::opcua::Discovery 是静态类, 不需要 Session 即可调用. 用于"连接前先探测":
子页跳转
- 列出某 Server 全部 Endpoint 请参考 GetEndpoints.
- 通过 LDS 列出域内全部 Server 请参考 FindServers.
头文件
#include <darra/opcua/discovery.hpp>
using namespace darra::opcua;
两个方法都是 class Discovery 的静态方法, 原型:
static std::vector<EndpointDescription> GetEndpoints(std::string const& endpoint_url, uint32_t timeout_ms = 10000);
static std::vector<ApplicationDescription> FindServers(std::string const& endpoint_url, uint32_t timeout_ms = 10000);
endpoint_url 传空串抛 Exception(..., Status::DarraInvalidArgument); 其余失败抛 Exception 并带原始状态码.
用法模式
#include <darra/opcua/client.hpp>
#include <darra/opcua/discovery.hpp>
using namespace darra::opcua;
Stack::Init();
// 1. 先 GetEndpoints 看服务端支持哪些组合
auto endpoints = Discovery::GetEndpoints("opc.tcp://server:4840");
// 2. 挑一个匹配的 (例如 SignAndEncrypt + Username)
EndpointDescription const* picked = nullptr;
for (auto const& ep : endpoints) {
if (ep.security_mode == MessageSecurityMode::SignAndEncrypt) {
for (auto const& t : ep.user_tokens) {
if (t.token_type == UserTokenType::Username) {
picked = &ep;
break;
}
}
if (picked) break;
}
}
if (!picked) throw std::runtime_error("服务端没有 SignAndEncrypt + Username 的 Endpoint");
// 3. 用挑出来的 Endpoint 真正连接
ConnectionConfig cfg;
cfg.endpoint_url = picked->endpoint_url;
cfg.security_mode = picked->security_mode;
cfg.security_policy_uri = picked->security_policy_uri;
cfg.user_token_type = UserTokenType::Username; // 不设会默认 Anonymous
cfg.username = "operator";
cfg.password = "secret";
cfg.client_cert_path = "client.pfx"; // PFX 文件 (内含私钥)
cfg.client_key_path = "123456"; // 上面那个 PFX 的密码 (字段名历史遗留, 不是私钥文件路径)
Session s(cfg);
s.Connect();