Skip to main content

VersionedNegotiator

Struct VersionedNegotiator 

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

版本感知的子协议协商器。

相比 SubProtocolRegistry,支持:

  • 协议元数据(版本、优先级、描述)
  • 按优先级排序选取(而非严格按客户端顺序)
  • 协商统计跟踪
  • 详细的协商结果(NegotiationOutcome

Implementations§

Source§

impl VersionedNegotiator

Source

pub fn new() -> Self

创建空的协商器

Source

pub fn register(&mut self, metadata: ProtocolMetadata)

注册一个带元数据的协议

Source

pub fn register_simple(&mut self, name: &str, version: &str)

便捷注册:仅指定名称与版本,优先级默认为 0

Source

pub fn unregister(&mut self, header_value: &str) -> bool

注销指定协议

Source

pub fn contains(&self, header_value: &str) -> bool

判断指定协议是否已注册

Source

pub fn len(&self) -> usize

获取已注册协议数量

Source

pub fn is_empty(&self) -> bool

是否为空

Source

pub fn stats(&self) -> NegotiationStats

获取协商统计快照

Source

pub fn registered_protocols(&self) -> Vec<String>

获取所有已注册协议的 header 值列表(按字母序排序)

Source

pub fn protocols_by_priority(&self) -> Vec<&ProtocolMetadata>

按优先级降序返回已注册协议的元数据。 优先级相同时按 header_value 字母序排列。

Source

pub fn negotiate(&mut self, client_protocols: &[String]) -> NegotiationOutcome

执行协商:从客户端请求的协议列表中选取最佳匹配。

选取规则:

  1. 客户端列表为空 -> NegotiationOutcome::NotRequested
  2. 筛选客户端列表中服务端也支持的协议
  3. 从候选中按优先级降序选取第一个
  4. 无候选 -> NegotiationOutcome::NoMatch

每次调用会更新协商统计。

Source

pub fn clear(&mut self)

清空所有注册的协议与统计

Trait Implementations§

Source§

impl Default for VersionedNegotiator

Source§

fn default() -> Self

Returns the “default value” for a type. 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> 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, 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.