pub struct MessageStream<S> { /* private fields */ }net only.Expand description
A whole-message reader/writer over a byte stream (anything Read + Write, e.g. a
TcpStream). It owns the stream and keeps a read buffer, so read_message and
write_message exchange #[bin] values — and one MessageStream serves both
directions on a single connection, no try_clone needed.
Implementations§
Source§impl<S> MessageStream<S>
impl<S> MessageStream<S>
Sourcepub fn new(inner: S) -> Self
pub fn new(inner: S) -> Self
Wrap a stream with an unbounded read buffer. On an untrusted peer prefer
bounded — a stream that never completes a frame would otherwise
grow the buffer without bound.
Sourcepub fn bounded(inner: S, cap: usize) -> Self
pub fn bounded(inner: S, cap: usize) -> Self
Wrap a stream with a bounded read buffer: a peer that streams bytes which never
complete a message can only grow the buffer to cap bytes before
read_message fails with
ErrorKind::BufferFull, rather than consuming memory
without bound. The bounded counterpart to new for untrusted streams.
Sourcepub fn get_mut(&mut self) -> &mut S
pub fn get_mut(&mut self) -> &mut S
Mutably borrow the underlying stream (e.g. to set a timeout).
Sourcepub fn into_inner(self) -> S
pub fn into_inner(self) -> S
Recover the underlying stream (any buffered-but-unparsed bytes are dropped).
Source§impl<S: Read> MessageStream<S>
impl<S: Read> MessageStream<S>
Sourcepub fn read_message<T: BitDecode + BitEncode>(&mut self) -> Result<T, BitError>
pub fn read_message<T: BitDecode + BitEncode>(&mut self) -> Result<T, BitError>
Read exactly one #[bin] message, pulling more bytes from the stream as needed and
keeping any trailing bytes for the next call. The message’s own byte/bit order is honored
(via BitBuf::pull).
§Errors
A codec BitError for a malformed message, or an I/O error — an EOF mid-stream (a
closed connection) surfaces as an Io(UnexpectedEof) error, so a read loop ends on Err.