Skip to content

Commit

Permalink
add support of non-blocking system call accept
Browse files Browse the repository at this point in the history
  • Loading branch information
stlankes committed Sep 23, 2024
1 parent 4c1e545 commit 61f8812
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/fd/socket/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,20 @@ impl Socket {
self.with(|socket| match socket.state() {
tcp::State::Closed => {
let _ = socket.listen(self.port);
Poll::Ready(())
Poll::Ready(Ok(()))
}
tcp::State::Listen | tcp::State::Established => Poll::Ready(()),
tcp::State::Listen | tcp::State::Established => Poll::Ready(Ok(())),
_ => {
socket.register_recv_waker(cx.waker());
Poll::Pending
if self.is_nonblocking {
Poll::Ready(Err(io::Error::EAGAIN))
} else {
socket.register_recv_waker(cx.waker());
Poll::Pending
}
}
})
})
.await;
.await?;

future::poll_fn(|cx| {
self.with(|socket| {
Expand All @@ -314,8 +318,12 @@ impl Socket {
| tcp::State::FinWait1
| tcp::State::FinWait2 => Poll::Ready(Err(io::Error::EIO)),
_ => {
socket.register_recv_waker(cx.waker());
Poll::Pending
if self.is_nonblocking {
Poll::Ready(Err(io::Error::EAGAIN))
} else {
socket.register_recv_waker(cx.waker());
Poll::Pending
}
}
}
}
Expand Down Expand Up @@ -491,8 +499,8 @@ impl ObjectInterface for async_lock::RwLock<Socket> {
}

async fn accept(&self) -> io::Result<(Arc<dyn ObjectInterface>, Endpoint)> {
let (handle, endpoint) = self.write().await.accept().await?;
Ok((Arc::new(async_lock::RwLock::new(handle)), endpoint))
let (socket, endpoint) = self.write().await.accept().await?;
Ok((Arc::new(async_lock::RwLock::new(socket)), endpoint))
}

async fn getpeername(&self) -> io::Result<Option<Endpoint>> {
Expand Down

0 comments on commit 61f8812

Please sign in to comment.