pub struct PeerMessageCodec { /* private fields */ }Expand description
Length-delimited message codec with optional CRC32 integrity checking.
§Example
use ipfrs_network::message_codec::{PeerMessageCodec, CodecConfig};
let codec = PeerMessageCodec::new(CodecConfig::default());
let wire = codec.encode(b"hello").expect("encode");
let payload = codec.decode(&wire).expect("decode");
assert_eq!(payload, b"hello");Implementations§
Source§impl PeerMessageCodec
impl PeerMessageCodec
Sourcepub fn new(config: CodecConfig) -> Self
pub fn new(config: CodecConfig) -> Self
Create a new codec with the given configuration.
Sourcepub fn encode(&self, payload: &[u8]) -> Result<Vec<u8>, CodecError>
pub fn encode(&self, payload: &[u8]) -> Result<Vec<u8>, CodecError>
Encode a payload into wire format.
Returns the complete frame: [4-byte length BE][payload][optional 4-byte CRC32].
§Errors
Returns CodecError::MessageTooLarge if payload.len() exceeds
config.max_message_size.
Sourcepub fn encode_with_metadata(
&self,
payload: &[u8],
) -> Result<EncodedMessage, CodecError>
pub fn encode_with_metadata( &self, payload: &[u8], ) -> Result<EncodedMessage, CodecError>
Encode a payload and return an EncodedMessage with metadata.
Sourcepub fn decode(&self, data: &[u8]) -> Result<Vec<u8>, CodecError>
pub fn decode(&self, data: &[u8]) -> Result<Vec<u8>, CodecError>
Decode a wire-format frame and return the payload bytes.
§Errors
CodecError::BufferTooSmallifdatais shorter than the length prefix.CodecError::InvalidLengthif the frame is truncated or the length prefix is inconsistent with the buffer size.CodecError::MessageTooLargeif the decoded length exceeds the configured maximum.CodecError::ChecksumMismatchif checksums are enabled and the stored checksum doesn’t match the computed one.
Sourcepub fn estimate_encoded_size(&self, payload_size: usize) -> usize
pub fn estimate_encoded_size(&self, payload_size: usize) -> usize
Estimate the total encoded size for a payload of the given length.
Returns 4 + payload_size + (4 if checksum enabled).
Sourcepub fn decode_length(&self, data: &[u8]) -> Result<usize, CodecError>
pub fn decode_length(&self, data: &[u8]) -> Result<usize, CodecError>
Peek at the length prefix without performing a full decode.
Returns the payload length encoded in the first 4 bytes.
§Errors
CodecError::BufferTooSmallifdatahas fewer than 4 bytes.CodecError::InvalidLengthif the decoded length is zero but additional bytes suggest corruption (currently not enforced — zero-length payloads are valid).
Sourcepub fn stats(&self) -> CodecStats
pub fn stats(&self) -> CodecStats
Return cumulative codec statistics.
Sourcepub fn config(&self) -> &CodecConfig
pub fn config(&self) -> &CodecConfig
Return a reference to the current configuration.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for PeerMessageCodec
impl !RefUnwindSafe for PeerMessageCodec
impl !Sync for PeerMessageCodec
impl Send for PeerMessageCodec
impl Unpin for PeerMessageCodec
impl UnsafeUnpin for PeerMessageCodec
impl UnwindSafe for PeerMessageCodec
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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>
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 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>
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