From b4bcc6cf5ca69a8455751cf5d2d55a136892576c Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 18 Jun 2020 01:56:49 +0200 Subject: [PATCH] TcpStream: add send_slice() --- libasync/src/smoltcp/tcp_stream.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/libasync/src/smoltcp/tcp_stream.rs b/libasync/src/smoltcp/tcp_stream.rs index 9ea7d61..2a26fa1 100644 --- a/libasync/src/smoltcp/tcp_stream.rs +++ b/libasync/src/smoltcp/tcp_stream.rs @@ -200,6 +200,24 @@ impl TcpStream { Ok(()) } + /// Yields to wait for more buffer space + pub async fn send_slice(&self, mut data: &'_ [u8]) -> Result<()> { + while data.len() > 0 { + self.wait_can_send().await?; + + data = self.with_socket(|mut socket| { + socket.send(|buf| { + let len = buf.len().min(data.len()); + buf[..len].copy_from_slice(&data[..len]); + data = &data[len..]; + (len, data) + }) + })?; + } + + Ok(()) + } + /// Wait for all queued data to be sent and ACKed /// /// **Warning:** this may not work as immediately as expected! The