Skip to main content

Connection

Struct Connection 

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

异步连接、Session、请求与订阅。 可 Clone 的连接访问句柄;最后公开访问 lease 消失会请求退出。

Implementations§

Source§

impl Connection

Source

pub fn from_transport<T: Transport>( transport: T, options: RuntimeOptions, ) -> Result<(Self, RawEventStream)>

接管已建立传输,同步安装初始 All 订阅,再启动唯一 Driver。 无 Tokio Handle 返回 NoRuntime;runtime 必须启用 time,否则 Tokio 可能 panic。

fn create<T: ydp_core::Transport>(transport: T) -> ydp_core::Result<()> {
let (connection, events) = ydp_core::Connection::from_transport(transport, Default::default())?;
drop(events); drop(connection); Ok(()) }
Source

pub fn key(&self) -> ConnectionKey

返回本地连接身份,不依赖 endpoint 字符串。

fn inspect(c: &ydp_core::Connection) { let _ = c.key(); }
Source

pub fn latest_receive_seq(&self) -> Option<ReceiveSeq>

读取本连接最近分配的入站接收序号,不消费事件,也不发送命令。

返回值在连接的短控制锁内与响应、事件的序号分配共享仲裁边界。 尚未处理任何合法入站 envelope 时返回 None;否则返回最近的 ReceiveSeq,包括已接收但尚未被调用者消费的响应或事件。 该值只表示本地接收进度,不证明订阅已消费、上层状态已归并或远端操作完成。

并发读取可能得到相同值;在同一连接上的先后读取不会倒退。 连接关闭后保留最后值;序号耗尽不会回绕。快照之后仍可能立即收到新消息, 因此调用者必须自行将此快照与自己的观察登记放在同一同步仲裁边界, 不能将它当作跨连接或浏览器因果屏障。此方法不取得新的资源许可, 不改变 Clone、Drop 或显式关闭的所有权规则。

§示例

使用没有入站消息的内存传输验证初值和关闭后的保留行为;无需浏览器。

let options = ydp_core::RuntimeOptions {
    close_timeout: std::time::Duration::ZERO,
    ..Default::default()
};
let (connection, events) = Connection::from_transport(Idle, options)?;
assert_eq!(connection.latest_receive_seq(), None);
connection.close().await;
assert_eq!(connection.latest_receive_seq(), None);
drop(events);
Source

pub fn root_session(&self) -> Session

创建根路由视图;不发送协议命令,不假定端点是浏览器。

fn inspect(c: &ydp_core::Connection) { assert!(c.root_session().id().is_none()); }
Source

pub fn adopt_session(&self, id: SessionId) -> Result<Session>

本地绑定 child ID;活跃 ID 复用记录,retired ID 拒绝,绝不自动 attach。

fn adopt(c: &ydp_core::Connection) -> ydp_core::Result<()> { let _ = c.adopt_session(ydp_core::SessionId::new("child".into())?)?; Ok(()) }
Source

pub fn raw_events(&self, options: SubscriptionOptions) -> Result<RawEventStream>

同步安装 All 订阅,覆盖未知 child;返回前即注册,不回放旧事件。

fn subscribe(c: &ydp_core::Connection) -> ydp_core::Result<()> { let _events = c.raw_events(Default::default())?; Ok(()) }
Source

pub fn state(&self) -> ConnectionState

返回本地生命周期快照。

fn inspect(c: &ydp_core::Connection) { let _ = c.state(); }
Source

pub async fn close(&self) -> CloseReport

首次 poll 关门并加入共享退出;丢弃本 Future 不撤销退出。

async fn close(c: &ydp_core::Connection) { let report = c.close().await; assert!(report.driver_resources_released); }
Source

pub async fn closed(&self) -> CloseReport

等待 Driver 释放资源,不主动发起关闭;报告不含用户仍持有的数据。

async fn observe(c: &ydp_core::Connection) { let _ = c.closed().await; }
Source§

impl Connection

Source

pub async fn connect( endpoint: &str, options: ConnectOptions, ) -> Result<(Self, RawEventStream)>

异步连接完整 ws/wss URL,成功返回连接和已经注册的初始 All 事件流。 握手前校验选项、头字段和 CA;失败不重试、不跟随 HTTP 重定向。 认证头只发给指定 endpoint,Debug 不输出头值或 URL。 需要 Tokio runtime 启用 IO 与 time;缺 Handle 返回 NoRuntime,禁用 driver 属调用方配置错误,Tokio 可能 panic。丢弃此 Future 会销毁尚未交付的传输。 关闭 rustls feature 后只接受 ws,附加 CA 配置也会被拒绝。

let (connection, events) = ydp_core::Connection::connect(
    "ws://127.0.0.1:9222/devtools/browser/example",
    ydp_core::ConnectOptions::default(),
).await?;
drop(events);
let report = connection.close().await;
assert!(report.driver_resources_released);

Trait Implementations§

Source§

impl Clone for Connection

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. 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> 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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.