BrowseCrawler
概述
BFS 队列遍历整棵 AddressSpace 子树, 双重保护 (max_depth + max_nodes)。C 版是回调式: 每发现一个节点调一次 on_node, 爬完函数返回, 不返回结果数组。
API
| 函数 | 类别 | 读写 | 说明 |
|---|---|---|---|
| DarraUa_CrawlerConfig_Init(&cfg) | 构造 | — | 填默认配置 |
| DarraUa_Session_CrawlAddressSpace(session, &cfg, on_node, ctx, &total) | 方法 | 读 | 同步 BFS, 阻塞调用 |
#define UA_CRAWLER_DEFAULT_MAX_DEPTH 10
#define UA_CRAWLER_DEFAULT_MAX_NODES 100000
typedef struct {
const char* root_node_id; /* NULL = "i=85" (ObjectsFolder) */
uint32_t max_depth; /* 0 = 用默认 10 */
uint32_t max_nodes; /* 0 = 用默认 100000 */
DarraUa_NodeClass node_class_filter; /* UA_NODE_CLASS_UNSPECIFIED = 不过滤 */
} DarraUa_CrawlerConfig;
typedef struct {
const char* node_id;
const char* display_name;
const char* browse_name;
DarraUa_NodeClass node_class;
const char* parent_node_id; /* root 节点为 NULL */
uint32_t depth; /* root = 0 */
} DarraUa_CrawlNode;
/* 返回 1 = 继续, 0 = 立即取消 */
typedef int32_t (*DarraUa_OnCrawlNode)(
const DarraUa_CrawlNode* node,
uint32_t running_count,
void* user_context);
DarraUa_Status DarraUa_Session_CrawlAddressSpace(
DarraUa_SessionHandle session,
const DarraUa_CrawlerConfig* cfg, /* NULL = 全默认 */
DarraUa_OnCrawlNode on_node, /* 必须非 NULL */
void* user_context,
uint32_t* out_total); /* 可为 NULL */
| 参数 | 默认 | 说明 |
|---|---|---|
| session | — | 已连接会话句柄 (DarraUa_SessionHandle) |
| cfg | NULL → 全默认 | 爬取配置 |
| on_node | — | 节点回调, 必须非 NULL |
| user_context | — | 回调用户上下文 |
| out_total | 可 NULL | 输出: 总抓到的节点数 |
节点字段生命周期: DarraUa_CrawlNode 里所有字符串字段都是 borrowed, 生命周期只在回调内部; 要保留请回调里 strdup。
取消: 回调返回 0 立即取消, 函数返回 UA_STATUS_GOOD。out_total 仍给出已抓到的数量。
代码示例
#include <opcua.h>
#include <opcua_browse_crawler.h>
#include <stdio.h>
typedef struct {
uint32_t shown;
int32_t cancel;
} CrawlCtx;
static int32_t on_node(const DarraUa_CrawlNode* node,
uint32_t running_count, void* ctx)
{
CrawlCtx* c = (CrawlCtx*)ctx;
printf("\r已抓 %u 个, 当前 %s", running_count,
node->node_id ? node->node_id : "");
/* 只缓存变量节点 (不影响 BFS 下钻) */
if (node->node_class == UA_NODE_CLASS_VARIABLE) {
/* 字符串是借用的, 要保留必须自己拷贝 */
cache_put(node->node_id, node->browse_name);
c->shown++;
}
/* 用户点了"取消"就返回 0 */
return c->cancel ? 0 : 1;
}
int main(void)
{
DarraUa_SessionConfig scfg;
DarraUa_SessionConfig_Init(&scfg);
scfg.endpoint_url = "opc.tcp://localhost:4840";
/* ... DarraUa_Session_Create + DarraUa_Session_Connect 略 ... */
DarraUa_CrawlerConfig cfg;
DarraUa_CrawlerConfig_Init(&cfg); /* 深度 10 / 节点 100000 / 不过滤 */
cfg.root_node_id = "i=85"; /* ObjectsFolder */
cfg.max_depth = 8;
cfg.max_nodes = 50000;
cfg.node_class_filter = UA_NODE_CLASS_UNSPECIFIED;
CrawlCtx ctx = { 0, 0 };
uint32_t total = 0;
DarraUa_Status st = DarraUa_Session_CrawlAddressSpace(
session, &cfg, on_node, &ctx, &total);
printf("\n共 %u 节点 (变量 %u), status=0x%08X\n",
total, ctx.shown, st);
/* 无结果数组要释放: 节点在回调里就地消费 */
return 0;
}
最佳实践
- 限定 root, 不要从 i=84 (Root) 开始
- 用
node_class_filter减少回调次数 (注意: 过滤只是不写回调, BFS 仍会下钻所有 hierarchical 子节点) - 启动时一次性爬
- 回调返回 0 实现取消, 不用外部标志位轮询
- 拿到的
DarraUa_CrawlNode字符串要留就得拷贝
跨语言对照
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| BrowseCrawler | BrowseCrawler | BrowseCrawler | BrowseCrawler | BrowseCrawler | DarraUa_CrawlerConfig + DarraUa_Session_CrawlAddressSpace |
| CrawlAsync | crawl_async | crawlAsync | CrawlAsync | crawl_async | DarraUa_Session_CrawlAddressSpace |