Skip to content

Commit 4f274f4

Browse files
authored
Add flush method to ensure frame is correctly written (#88)
When the underlying stream is buffered (i.e: TlsStream<TcpStream>) calling write_frame is not enough to be guaranteed that data is pushed down to the last stream (i.e: on the network in case of TlsStream<TcpStream>). Currently there is no way to force flush the data on the stream, so write_frame call may never push the data. https://docs.rs/tokio-rustls/latest/tokio_rustls/index.html#why-do-i-need-to-call-poll_flush The commit introduce a flush method, to allow the user of the lib to ensure all data is correctly pushed down.
1 parent e912a92 commit 4f274f4

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,21 @@ impl<'f, S> WebSocketWrite<S> {
342342
{
343343
self.write_half.write_frame(&mut self.stream, frame).await
344344
}
345+
346+
pub async fn flush(&mut self) -> Result<(), WebSocketError>
347+
where
348+
S: AsyncWrite + Unpin,
349+
{
350+
flush(&mut self.stream).await
351+
}
352+
}
353+
354+
#[inline]
355+
async fn flush<S>(stream: &mut S) -> Result<(), WebSocketError>
356+
where
357+
S: AsyncWrite + Unpin,
358+
{
359+
stream.flush().await.map_err(|e| WebSocketError::IoError(e))
345360
}
346361

347362
/// WebSocket protocol implementation over an async stream.
@@ -495,6 +510,18 @@ impl<'f, S> WebSocket<S> {
495510
Ok(())
496511
}
497512

513+
/// Flushes the data from the underlying stream.
514+
///
515+
/// if the underlying stream is buffered (i.e: TlsStream<TcpStream>), it is needed to call flush
516+
/// to be sure that the written frame are correctly pushed down to the bottom stream/channel.
517+
///
518+
pub async fn flush(&mut self) -> Result<(), WebSocketError>
519+
where
520+
S: AsyncWrite + Unpin,
521+
{
522+
flush(&mut self.stream).await
523+
}
524+
498525
/// Reads a frame from the stream.
499526
///
500527
/// This method will unmask the frame payload. For fragmented frames, use `FragmentCollector::read_frame`.

0 commit comments

Comments
 (0)