pub struct Connection { /* private fields */ }Expand description
Connection — CDP JSON-RPC 命令收发 + 事件分发核心。
持有 Box<dyn Transport>,提供:
send_command(method, params)→ 构造 JSON-RPC request → 通过 Transport 发送 → 返回 resultsend_command_with_session(method, params, session_id)→ 带子 session 的命令recv_event()→ 从 Transport 读取一个事件on_event(method, handler)→ 注册事件 handleroff_event(method, listener_id)→ 移除事件 handlerclose()→ 关闭 Transport
@trace REQ-BAO-API-001 [level:library]
Implementations§
Source§impl Connection
impl Connection
Sourcepub fn new(
transport: Box<dyn Transport>,
config: ConnectionConfig,
) -> Connection
pub fn new( transport: Box<dyn Transport>, config: ConnectionConfig, ) -> Connection
从 Transport 构造 Connection。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn from_transport(transport: Box<dyn Transport>) -> Connection
pub fn from_transport(transport: Box<dyn Transport>) -> Connection
从 Transport 构造 Connection(使用默认配置)。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn config(&self) -> &ConnectionConfig
pub fn config(&self) -> &ConnectionConfig
取配置引用。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn transport_kind(&self) -> TransportKind
pub fn transport_kind(&self) -> TransportKind
Transport 类型。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn send_command(
&mut self,
method: &str,
params: Value,
) -> Result<Value, CdpError>
pub fn send_command( &mut self, method: &str, params: Value, ) -> Result<Value, CdpError>
发送 CDP 命令(无 session_id),返回响应 result。
等价于 send_command_with_session(method, params, None)。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn send_command_with_session(
&mut self,
method: &str,
params: Value,
session_id: Option<&str>,
) -> Result<Value, CdpError>
pub fn send_command_with_session( &mut self, method: &str, params: Value, session_id: Option<&str>, ) -> Result<Value, CdpError>
发送 CDP 命令(带可选 session_id),返回响应 result。
内部流程:
- 分配 JSON-RPC request ID
- 调用
Transport::send_command(method, params, session_id) - 返回
result字段或错误
§错误
CdpError::ConnectionClosed: Transport 已关闭CdpError::ProtocolError: 远端返回 JSON-RPC errorCdpError::Timeout: 命令超时
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn recv_event(&mut self) -> Result<Option<CdpEvent>, CdpError>
pub fn recv_event(&mut self) -> Result<Option<CdpEvent>, CdpError>
从 Transport 接收一个事件。
无事件时返回 Ok(None)(超时)。
收到事件后自动分发到已注册的 handler。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn drain_events(&mut self) -> Result<Vec<CdpEvent>, CdpError>
pub fn drain_events(&mut self) -> Result<Vec<CdpEvent>, CdpError>
接收并分发所有待处理事件(非阻塞轮询直到超时返回 None)。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn on_event(
&mut self,
method: &str,
handler: Arc<dyn Fn(CdpEvent) + Sync + Send>,
) -> u64
pub fn on_event( &mut self, method: &str, handler: Arc<dyn Fn(CdpEvent) + Sync + Send>, ) -> u64
注册事件 handler,返回 listener ID(可用于 off_event)。
handler 在 recv_event / drain_events 收到匹配 method 的事件时被调用。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn once_event(
&mut self,
method: &str,
handler: Arc<dyn Fn(CdpEvent) + Sync + Send>,
) -> u64
pub fn once_event( &mut self, method: &str, handler: Arc<dyn Fn(CdpEvent) + Sync + Send>, ) -> u64
注册一次性事件 handler(触发后自动移除)。
handler 在首次匹配到 method 的事件后被调用并自动从注册表移除, 后续相同 method 的事件不再触发此 handler。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn off_event(&mut self, method: &str, listener_id: u64) -> bool
pub fn off_event(&mut self, method: &str, listener_id: u64) -> bool
移除事件 handler。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn remove_all_event_handlers(&mut self, method: &str)
pub fn remove_all_event_handlers(&mut self, method: &str)
移除指定 method 的所有 handler。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn event_handler_count(&self, method: &str) -> usize
pub fn event_handler_count(&self, method: &str) -> usize
返回指定 method 的 handler 数量。
Sourcepub fn close(&mut self) -> Result<(), CdpError>
pub fn close(&mut self) -> Result<(), CdpError>
关闭 Connection(关闭底层 Transport)。
幂等:重复调用安全。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn transport_mut(&mut self) -> &mut dyn Transport
pub fn transport_mut(&mut self) -> &mut dyn Transport
获取底层 Transport 的可变引用(高级用法)。
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Connection
impl !Sync for Connection
impl !UnwindSafe for Connection
impl Freeze for Connection
impl Send for Connection
impl Unpin for Connection
impl UnsafeUnpin for Connection
Blanket Implementations§
Source§impl<T> AsCtxPtr for Twhere
T: ?Sized,
impl<T> AsCtxPtr for Twhere
T: ?Sized,
Source§fn as_ctx_ptr(&self) -> *mut Selfwhere
Self: Sized,
fn as_ctx_ptr(&self) -> *mut Selfwhere
Self: Sized,
self’s address as *mut Self for deferred-task / scopeguard /
ref_guard ctx slots. The closures/trampolines deref it as shared
(&*p) — every method they reach is &self post-R-2, so no write
provenance is required; the *mut spelling is purely to match the
existing DerefOnDrop / HasAutoFlush / RefCount ABI.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Filterable for T
impl<T> Filterable for T
Source§fn filterable(
self,
filter_name: &'static str,
) -> RequestFilterDataProvider<T, fn(DataRequest<'_>) -> bool>
fn filterable( self, filter_name: &'static str, ) -> RequestFilterDataProvider<T, fn(DataRequest<'_>) -> bool>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more