Add poll_accept method

This commit is contained in:
Aode (lion) 2022-03-05 13:19:36 -06:00
parent 9fe3325a26
commit 8364de60d1

View file

@ -64,7 +64,7 @@ struct Flush<'a, T: AsFd + 'static> {
} }
struct Accept<'a> { struct Accept<'a> {
io: &'a Arc<TcpListener>, io: &'a mut Async<TcpListener>,
} }
struct Bind { struct Bind {
@ -90,13 +90,32 @@ impl Async<TcpListener> {
.map(Async::new) .map(Async::new)
} }
pub async fn accept(&mut self) -> Result<(Async<TcpStream>, Option<SocketAddrAny>)> { pub fn poll_accept(
let (stream, addr) = Accept { &mut self,
io: self.io.ensure_arc(), cx: &mut Context<'_>,
} ) -> Poll<Result<(Async<TcpStream>, Option<SocketAddrAny>)>> {
.await?; let poll = self
.io
.as_ref()
.try_accept()
.map(|nonblock| nonblock.map(|(stream, addr)| (Async::new(stream), addr)));
Ok((Async::new(stream), addr)) poll_nonblocking!(poll);
ReactorRef::with(|mut reactor| {
reactor.register(
Arc::clone(self.io.ensure_arc()),
cx.waker().clone(),
Readiness::read() | Readiness::hangup(),
);
})
.unwrap();
Poll::Pending
}
pub async fn accept(&mut self) -> Result<(Async<TcpStream>, Option<SocketAddrAny>)> {
Accept { io: self }.await
} }
} }
@ -468,21 +487,10 @@ where
} }
impl<'a> Future for Accept<'a> { impl<'a> Future for Accept<'a> {
type Output = Result<(TcpStream, Option<SocketAddrAny>)>; type Output = Result<(Async<TcpStream>, Option<SocketAddrAny>)>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
poll_nonblocking!(self.io.try_accept()); self.get_mut().io.poll_accept(cx)
ReactorRef::with(|mut reactor| {
reactor.register(
Arc::clone(self.io),
cx.waker().clone(),
Readiness::read() | Readiness::hangup(),
);
})
.unwrap();
Poll::Pending
} }
} }