Remove TcpListener::pending_accept field

No need for oneshot shenanigans as now we'll always have the `Handle` available
to us regardless of what thread we're on to associate a new socket
This commit is contained in:
Alex Crichton 2017-12-05 08:17:08 -08:00
parent 7c768fc046
commit 329bca15a6

View File

@ -6,7 +6,6 @@ use std::time::Duration;
use bytes::{Buf, BufMut}; use bytes::{Buf, BufMut};
use futures::stream::Stream; use futures::stream::Stream;
use futures::sync::oneshot;
use futures::{Future, Poll, Async}; use futures::{Future, Poll, Async};
use iovec::IoVec; use iovec::IoVec;
use mio; use mio;
@ -20,7 +19,6 @@ use reactor::{Handle, PollEvented};
/// various forms of processing. /// various forms of processing.
pub struct TcpListener { pub struct TcpListener {
io: PollEvented<mio::net::TcpListener>, io: PollEvented<mio::net::TcpListener>,
pending_accept: Option<oneshot::Receiver<io::Result<(TcpStream, SocketAddr)>>>,
} }
/// Stream returned by the `TcpListener::incoming` function representing the /// Stream returned by the `TcpListener::incoming` function representing the
@ -59,17 +57,6 @@ impl TcpListener {
/// future's task. It's recommended to only call this from the /// future's task. It's recommended to only call this from the
/// implementation of a `Future::poll`, if necessary. /// implementation of a `Future::poll`, if necessary.
pub fn accept(&mut self) -> io::Result<(TcpStream, SocketAddr)> { pub fn accept(&mut self) -> io::Result<(TcpStream, SocketAddr)> {
loop {
if let Some(mut pending) = self.pending_accept.take() {
match pending.poll().expect("shouldn't be canceled") {
Async::NotReady => {
self.pending_accept = Some(pending);
return Err(io::ErrorKind::WouldBlock.into())
},
Async::Ready(r) => return r,
}
}
if let Async::NotReady = self.io.poll_read() { if let Async::NotReady = self.io.poll_read() {
return Err(io::Error::new(io::ErrorKind::WouldBlock, "not ready")) return Err(io::Error::new(io::ErrorKind::WouldBlock, "not ready"))
} }
@ -79,13 +66,12 @@ impl TcpListener {
if e.kind() == io::ErrorKind::WouldBlock { if e.kind() == io::ErrorKind::WouldBlock {
self.io.need_read(); self.io.need_read();
} }
return Err(e) Err(e)
}, },
Ok((sock, addr)) => { Ok((sock, addr)) => {
let handle = self.io.handle(); let handle = self.io.handle();
let io = try!(PollEvented::new(sock, &handle)); let io = try!(PollEvented::new(sock, &handle));
return Ok((TcpStream { io: io }, addr)) Ok((TcpStream { io: io }, addr))
}
} }
} }
} }
@ -127,7 +113,7 @@ impl TcpListener {
fn new(listener: mio::net::TcpListener, handle: &Handle) fn new(listener: mio::net::TcpListener, handle: &Handle)
-> io::Result<TcpListener> { -> io::Result<TcpListener> {
let io = try!(PollEvented::new(listener, handle)); let io = try!(PollEvented::new(listener, handle));
Ok(TcpListener { io: io, pending_accept: None }) Ok(TcpListener { io: io })
} }
/// Test whether this socket is ready to be read or not. /// Test whether this socket is ready to be read or not.