1
0
Fork 0

libasync: simplify TcpStream::accept()

This commit is contained in:
Astro 2020-04-01 23:01:48 +02:00
parent 90e9a7db02
commit 0618642d3f
1 changed files with 8 additions and 25 deletions

View File

@ -93,33 +93,16 @@ impl TcpStream {
/// connection attempts stall any more new connections. Use /// connection attempts stall any more new connections. Use
/// `listen()` with a backlog instead. /// `listen()` with a backlog instead.
pub async fn accept(port: u16, rx_bufsize: usize, tx_bufsize: usize) -> Self { pub async fn accept(port: u16, rx_bufsize: usize, tx_bufsize: usize) -> Self {
struct Accept {
stream: Option<TcpStream>,
}
impl Future for Accept {
type Output = TcpStream;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let is_active = self.stream.as_ref()
.map(|s| s.with_socket(|s| s.is_active()))
.unwrap_or(true);
if is_active {
Poll::Ready(self.stream.take().unwrap())
} else {
Sockets::register_waker(cx.waker().clone());
//asm::sev();
Poll::Pending
}
}
}
let stream = Self::new(rx_bufsize, tx_bufsize); let stream = Self::new(rx_bufsize, tx_bufsize);
stream.with_socket(|mut s| s.listen(port)).expect("listen"); stream.with_socket(|mut s| s.listen(port)).expect("listen");
Accept { poll_stream!(&stream, (), |socket| {
stream: Some(stream), if socket.is_active() {
}.await Poll::Ready(())
} else {
Poll::Pending
}
}).await;
stream
} }
/// Probe the receive buffer /// Probe the receive buffer