pub struct FrameReader<R> { /* private fields */ }Expand description
A reader of frames, object chunks, and pull bodies on a stream.
A frame takes three reads and a chunk takes two, so a caller wraps a raw socket or pipe in a buffered reader. A buffered reader that passes a large read through to the stream does not copy the object bytes a second time.
The futures of the reader are not cancel-safe. A call that is dropped before it completes can leave part of a frame or of a chunk length read. The next call then reads from the middle of it. After a canceled call, the caller must drop the reader.
Implementations§
Source§impl<R: AsyncRead + Unpin> FrameReader<R>
impl<R: AsyncRead + Unpin> FrameReader<R>
Sourcepub fn new(inner: R) -> Self
pub fn new(inner: R) -> Self
Creates a reader with the limit MIN_FRAME_LIMIT.
Sourcepub fn set_limit(&mut self, limit: u32)
pub fn set_limit(&mut self, limit: u32)
Sets the frame and chunk limit of the reader.
The value is clamped to MIN_FRAME_LIMIT..=MAX_FRAME_LIMIT.
A client sets a read limit of its own here, for example
MAX_FRAME, and gives the max-frame of the server to its
FrameWriter alone. The frame rules give
the reason.
Sourcepub async fn read_message(&mut self) -> Result<Option<Message>>
pub async fn read_message(&mut self) -> Result<Option<Message>>
Reads the next message.
Ok(None) is an end of file at a frame boundary. After an
ObjectHeader, and after a GetReply whose found is true, the
caller must read the body to its end before the next message. It reads
the body with read_object_data or
object_body.
§Errors
Error::Protocolif the body of the previous message is not read to its end.Error::Protocolif the frame length is 0, if the kind byte is not a known kind, or if the body does not decode.Error::LimitExceededif the frame length is more than the limit. The reader checks the length before it reads or allocates the body.Error::Ioof kindUnexpectedEofif the stream ends inside a frame.Error::Ioif a read of the stream fails.
Sourcepub fn object_body(&mut self) -> ObjectBody<'_, R>
pub fn object_body(&mut self) -> ObjectBody<'_, R>
Returns an ObjectBody that reads the current object or pull body.
Sourcepub async fn read_object_data(&mut self, buf: &mut [u8]) -> Result<ObjectRead>
pub async fn read_object_data(&mut self, buf: &mut [u8]) -> Result<ObjectRead>
Reads bytes of the current object or pull body into buf.
A chunk longer than buf arrives over several calls, so buf bounds
the memory. Inside a body, an empty buf reads nothing and gives
ObjectRead::Data(0).
If the call meets the abandon marker, it reads the message that must
follow the marker. In an object of the push, an Abort message gives
ObjectRead::Abandoned.
§Errors
Error::Protocolif the reader is not inside a body.Error::LimitExceededif a chunk length is more than the limit.- In a body of the pull, the
Errorvariant of the code in theErrormessage after the marker, for exampleError::Internal. Error::Protocolif a message of another kind follows the marker.Error::Ioof kindUnexpectedEofif the stream ends inside a chunk, or after the marker and before the next message.Error::Ioif a read of the stream fails.- The errors of
read_messagefor the message after the marker.
Sourcepub fn get_ref(&self) -> &R
pub fn get_ref(&self) -> &R
Returns a reference to the underlying stream.
If the stream is a buffered reader, a caller can look at the bytes in the buffer with no read. An example is a check for a whole frame in the buffer.
Sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Returns the underlying stream.