pub struct FramedWriter<W: AsyncWrite> { /* private fields */ }Expand description
A writer which writes to a writeable sink in a framed manner. Frames written
by this writer can be read using a FramedReader.
Implementations§
Source§impl<W: AsyncWrite> FramedWriter<W>
impl<W: AsyncWrite> FramedWriter<W>
Sourcepub fn new(sink: W) -> Self
pub fn new(sink: W) -> Self
Creates a new framed writer which writes to the given writable sink.
Sourcepub async fn write_frame(&mut self, frame: &[u8]) -> Result<(), WriteFrameError>
pub async fn write_frame(&mut self, frame: &[u8]) -> Result<(), WriteFrameError>
Writes a single frame to the writeable sink.
The framed writer uses a BufWriter under the hood, which means that
written frames may not immediately be sent, but may stay buffered until
the buffer is full enough, at which point all written frames will be
flushed.
If you need the frame to be flushed immediately, you can use
flush.
§Cancel Safety
This function is not cancel safe, and so it shouldn’t be used as one
of the branches of tokio::select, because it may cause data loss when
cancelled.
If you want to write a frame in a cancel safe way, use
write_frame_cancel_safe.
Sourcepub async fn write_frame_cancel_safe(
&mut self,
frame: &[u8],
) -> Result<(), WriteFrameError>
pub async fn write_frame_cancel_safe( &mut self, frame: &[u8], ) -> Result<(), WriteFrameError>
Writes a single frame to the writeable sink in a cancel safe manner.
The framed writer uses a BufWriter under the hood, which means that
written frames may not immediately be sent, but may stay buffered until
the buffer is full enough, at which point all written frames will be
flushed.
If you need the frame to be flushed immediately, you can use
flush.
This is more expensive than write_frame,
but provides cancel safety.
§Cancel Safety
This function is cancel safe, and can be used as one of the branches of
tokio::select without causing any data loss.
Sourcepub async fn flush(&mut self) -> Result<(), WriteFrameError>
pub async fn flush(&mut self) -> Result<(), WriteFrameError>
Flushes the framed writer, ensuring that any buffered data reaches its destination.