From d742d0252ef6af3617f333e6f31be9559130ded7 Mon Sep 17 00:00:00 2001 From: Matous Hybl Date: Thu, 4 Nov 2021 13:34:13 +0100 Subject: [PATCH] Add TCP listen. --- embassy-net/src/tcp_socket.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/embassy-net/src/tcp_socket.rs b/embassy-net/src/tcp_socket.rs index 2433771c..bb1b626e 100644 --- a/embassy-net/src/tcp_socket.rs +++ b/embassy-net/src/tcp_socket.rs @@ -58,6 +58,26 @@ impl<'a> TcpSocket<'a> { .await } + pub async fn listen(&mut self, local_endpoint: T) -> Result<()> + where + T: Into, + { + self.with(|s| s.listen(local_endpoint))?; + + futures::future::poll_fn(|cx| { + self.with(|s| match s.state() { + TcpState::Closed | TcpState::TimeWait => Poll::Ready(Err(Error::Unaddressable)), + TcpState::Listen => Poll::Ready(Ok(())), + TcpState::SynSent | TcpState::SynReceived => { + s.register_send_waker(cx.waker()); + Poll::Pending + } + _ => Poll::Ready(Ok(())), + }) + }) + .await + } + pub fn set_timeout(&mut self, duration: Option) { self.with(|s| s.set_timeout(duration)) }