Skip to main content

Connection

Struct Connection 

Source
pub struct Connection { /* private fields */ }
Expand description

Connection — CDP JSON-RPC 命令收发 + 事件分发核心。

持有 Box<dyn Transport>,提供:

  • send_command(method, params) → 构造 JSON-RPC request → 通过 Transport 发送 → 返回 result
  • send_command_with_session(method, params, session_id) → 带子 session 的命令
  • recv_event() → 从 Transport 读取一个事件
  • on_event(method, handler) → 注册事件 handler
  • off_event(method, listener_id) → 移除事件 handler
  • close() → 关闭 Transport

@trace REQ-BAO-API-001 [level:library]

Implementations§

Source§

impl Connection

Source

pub fn new( transport: Box<dyn Transport>, config: ConnectionConfig, ) -> Connection

从 Transport 构造 Connection。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn from_transport(transport: Box<dyn Transport>) -> Connection

从 Transport 构造 Connection(使用默认配置)。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn config(&self) -> &ConnectionConfig

取配置引用。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn transport_kind(&self) -> TransportKind

Transport 类型。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn is_closed(&self) -> bool

是否已关闭。

Source

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]

Source

pub fn send_command_with_session( &mut self, method: &str, params: Value, session_id: Option<&str>, ) -> Result<Value, CdpError>

发送 CDP 命令(带可选 session_id),返回响应 result。

内部流程:

  1. 分配 JSON-RPC request ID
  2. 调用 Transport::send_command(method, params, session_id)
  3. 返回 result 字段或错误
§错误
  • CdpError::ConnectionClosed: Transport 已关闭
  • CdpError::ProtocolError: 远端返回 JSON-RPC error
  • CdpError::Timeout: 命令超时

@trace REQ-BAO-API-001 [level:library]

Source

pub fn recv_event(&mut self) -> Result<Option<CdpEvent>, CdpError>

从 Transport 接收一个事件。

无事件时返回 Ok(None)(超时)。 收到事件后自动分发到已注册的 handler。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn drain_events(&mut self) -> Result<Vec<CdpEvent>, CdpError>

接收并分发所有待处理事件(非阻塞轮询直到超时返回 None)。

@trace REQ-BAO-API-001 [level:library]

Source

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]

Source

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]

Source

pub fn off_event(&mut self, method: &str, listener_id: u64) -> bool

移除事件 handler。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn remove_all_event_handlers(&mut self, method: &str)

移除指定 method 的所有 handler。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn event_handler_count(&self, method: &str) -> usize

返回指定 method 的 handler 数量。

Source

pub fn close(&mut self) -> Result<(), CdpError>

关闭 Connection(关闭底层 Transport)。

幂等:重复调用安全。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn transport_mut(&mut self) -> &mut dyn Transport

获取底层 Transport 的可变引用(高级用法)。

Trait Implementations§

Source§

impl Debug for Connection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsCtxPtr for T
where T: ?Sized,

Source§

fn as_ctx_ptr(&self) -> *mut Self
where 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> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> Filterable for T

Source§

fn filterable( self, filter_name: &'static str, ) -> RequestFilterDataProvider<T, fn(DataRequest<'_>) -> bool>

Creates a filterable data provider with the given name for debugging. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> MaybeBoxed<Box<T>> for T

Source§

fn maybe_boxed(self) -> Box<T>

Convert
Source§

impl<T> MaybeBoxed<T> for T

Source§

fn maybe_boxed(self) -> T

Convert
Source§

impl<T> MaybeSendSync for T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Send for T
where T: ?Sized,

Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

impl<T> Sync for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more