Skip to main content

MessageParser

Struct MessageParser 

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

消息解析器

Implementations§

Source§

impl MessageParser

Source

pub fn new( format: SerializationFormat, compression: CompressionAlgorithm, encryption: EncryptionAlgorithm, ) -> Self

创建新的消息解析器

Source

pub fn with_custom_format( format_name: &str, compression: CompressionAlgorithm, encryption: EncryptionAlgorithm, ) -> Self

创建使用自定义序列化格式的解析器

§参数
  • format_name: 自定义序列化格式名称(必须在注册表中注册)
  • compression: 压缩算法
  • encryption: 加密算法
§示例
use flare_core::common::message::MessageParser;
use flare_core::common::compression::CompressionAlgorithm;
use flare_core::common::encryption::EncryptionAlgorithm;

// 创建使用自定义格式的解析器
let parser = MessageParser::with_custom_format(
    "messagepack",
    CompressionAlgorithm::None,
    EncryptionAlgorithm::None,
);
Source

pub fn new_with_format_compression( format: SerializationFormat, compression: CompressionAlgorithm, ) -> Self

创建使用指定格式和压缩的解析器

Source

pub fn protobuf() -> Self

创建使用 Protobuf 格式的解析器

Source

pub fn json() -> Self

创建使用 JSON 格式的解析器

Source

pub fn default_format(&self) -> SerializationFormat

获取默认序列化格式

Source

pub fn default_compression(&self) -> CompressionAlgorithm

获取默认压缩算法

Source

pub fn default_encryption(&self) -> EncryptionAlgorithm

获取默认加密算法

Source

pub fn parse(&self, data: &[u8]) -> Result<Frame>

解析消息(自动检测格式、压缩和加密)

处理流程:解密 -> 解压缩 -> 反序列化 默认使用容错模式(解密失败时尝试作为未加密数据处理)

Source

pub fn parse_with_fallback( &self, data: &[u8], allow_fallback: bool, ) -> Result<Frame>

解析消息(支持容错标记)

§参数
  • data: 要解析的原始数据
  • allow_fallback: 如果为 true,启用容错模式:
    • 解密失败时尝试作为未加密数据处理
    • 解压缩失败时尝试作为未压缩数据处理
    • 反序列化失败时尝试所有序列化格式
    • 如果为 false,严格模式:任何步骤失败都直接返回错误
§处理流程

解密(根据 allow_fallback 决定是否容错) -> 解压缩(根据 allow_fallback 决定是否容错) -> 反序列化(根据 allow_fallback 决定是否容错)

Source

pub fn parse_with_format( &self, data: &[u8], format: SerializationFormat, ) -> Result<Frame>

根据指定格式解析消息

Source

pub fn serialize(&self, frame: &Frame) -> Result<Vec<u8>>

序列化消息(使用默认格式、压缩和加密)

Source

pub fn serialize_with_format( &self, frame: &Frame, format: SerializationFormat, compression: CompressionAlgorithm, encryption: EncryptionAlgorithm, ) -> Result<Vec<u8>>

序列化消息(指定格式、压缩和加密)

处理流程:序列化 -> 压缩 -> 加密

Source

pub fn serialize_with_format_compression( &self, frame: &Frame, format: SerializationFormat, compression: CompressionAlgorithm, ) -> Result<Vec<u8>>

序列化消息(指定格式和压缩,使用默认加密)

Source

pub fn get_compression_from_frame(frame: &Frame) -> CompressionAlgorithm

从 Frame 的 metadata 中读取压缩算法

Source

pub fn should_compress_payload( payload_len: usize, compression: &CompressionAlgorithm, ) -> bool

判断序列化后的 payload 是否必须按协商结果压缩。

Source

pub fn get_format_from_frame(frame: &Frame) -> Option<SerializationFormat>

从 Frame 的 metadata 中读取序列化格式

Source

pub fn get_encryption_from_frame(frame: &Frame) -> EncryptionAlgorithm

从 Frame 的 metadata 中读取加密算法

Trait Implementations§

Source§

impl Clone for MessageParser

Source§

fn clone(&self) -> MessageParser

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

impl Debug for MessageParser

Source§

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

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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> 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> 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 = 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