From f1a4db44c4c3f591df378190a6604d4547a37c25 Mon Sep 17 00:00:00 2001 From: kbleeke Date: Wed, 22 Feb 2023 13:57:40 +0100 Subject: [PATCH 1/2] Implement flush for TcpSocket --- embassy-net/src/tcp.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/embassy-net/src/tcp.rs b/embassy-net/src/tcp.rs index d46bd4dbf..b9e494fc4 100644 --- a/embassy-net/src/tcp.rs +++ b/embassy-net/src/tcp.rs @@ -254,10 +254,19 @@ impl<'d> TcpIo<'d> { .await } - #[allow(unused)] async fn flush(&mut self) -> Result<(), Error> { - poll_fn(move |_| { - Poll::Ready(Ok(())) // TODO: Is there a better implementation for this? + poll_fn(move |cx| { + self.with_mut(|s, _| { + // If there are outstanding send operations, register for wake up and wait + // smoltcp issues wake-ups when octets are dequeued from the send buffer + if s.send_queue() > 0 { + s.register_send_waker(cx.waker()); + Poll::Pending + // No outstanding sends, socket is flushed + } else { + Poll::Ready(Ok(())) + } + }) }) .await } From 035de6f3ff7bae31dce25b55e822f75b46ebe047 Mon Sep 17 00:00:00 2001 From: kbleeke Date: Wed, 22 Feb 2023 14:45:17 +0100 Subject: [PATCH 2/2] embassy-net: add flush to TcpSocket and TcpWriter as an inherent method --- embassy-net/src/tcp.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/embassy-net/src/tcp.rs b/embassy-net/src/tcp.rs index b9e494fc4..c3d8764b0 100644 --- a/embassy-net/src/tcp.rs +++ b/embassy-net/src/tcp.rs @@ -63,6 +63,10 @@ impl<'a> TcpWriter<'a> { pub async fn write(&mut self, buf: &[u8]) -> Result { self.io.write(buf).await } + + pub async fn flush(&mut self) -> Result<(), Error> { + self.io.flush().await + } } impl<'a> TcpSocket<'a> { @@ -146,6 +150,10 @@ impl<'a> TcpSocket<'a> { self.io.write(buf).await } + pub async fn flush(&mut self) -> Result<(), Error> { + self.io.flush().await + } + pub fn set_timeout(&mut self, duration: Option) { self.io.with_mut(|s, _| s.set_timeout(duration)) }