跳到主要内容

BrowseWithPaging

browse 只返回 Vec<Reference> 列表, 丢弃 ContinuationPoint, 适合"已知小节点"场景. 大节点 (子节点 100+) 用 browse_with_paging 显式拿到 BrowsePage.

前置阅读

签名

pub fn Session::browse_with_paging(
&self,
node_id: &str,
filter: NodeClass,
) -> Result<BrowsePage, OpcUaError>;

用法

use darra_opcua::NodeClass;

let page = s.browse_with_paging("ns=2;s=BigFolder", NodeClass::Unspecified)?;
println!("Got {} children, more = {}",
page.references.len(),
page.continuation_point.is_some());

if let Some(cp) = &page.continuation_point {
let next = s.browse_next(cp, false)?;
println!("Next page: {} more children", next.references.len());
}

何时用 browse vs browse_with_paging

场景API
已知节点子数 ≤ 50, 不关心分页browse(...)
不确定子数, 安全起见browse_with_paging(...) + 循环 browse_next
100+ 子节点的大节点browse_with_paging(...) 必选
一次 Browse 多个节点browse_many(...) (注意当前不返回 ContinuationPoint)
一行式自动翻完所有页browse_all(...)

完整模板

use darra_opcua::{Session, NodeClass, OpcUaError, Reference};

pub fn browse_all_paged(
s: &Session, node_id: &str, filter: NodeClass,
) -> Result<Vec<Reference>, OpcUaError> {
let mut page = s.browse_with_paging(node_id, filter)?;
let mut all = page.references;
while let Some(cp) = page.continuation_point.take() {
if cp.is_empty() { break; }
page = s.browse_next(&cp, false)?;
all.extend(page.references);
}
Ok(all)
}

自动翻页 browse_all

注意: 上面这个等价于 SDK 内置的 Session::browse_all(...), 推荐直接用:

pub fn browse_all(
&self, node_id: &str, filter: NodeClass, max_pages: usize,
) -> Result<Vec<Reference>, OpcUaError>;
let all = s.browse_all("ns=2;s=BigFolder", NodeClass::Unspecified, 100)?;

max_pages 是安全上限, 防止服务端异常持续返回 CP 导致死循环; 达到上限后自动 release 当前 CP.

下一步