From 2e584228901db1fd57d68676b4978389fcbecff2 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 5 Dec 2017 08:48:17 -0800 Subject: [PATCH] Add TcpListener::accept_std as a method This should allow configuration over what reactor accepted streams go on to by giving back a libstd-bound object that can then be used later in conjunction with `TcpStream::from_std`. --- Cargo.toml | 2 +- src/net/tcp.rs | 26 ++++++++++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d34347991..70c02a405 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ appveyor = { repository = "alexcrichton/tokio" } [dependencies] bytes = "0.4" log = "0.3" -mio = "0.6.10" +mio = "0.6.11" slab = "0.4" iovec = "0.1" tokio-io = "0.1" diff --git a/src/net/tcp.rs b/src/net/tcp.rs index e94db0dab..1c0c88d05 100644 --- a/src/net/tcp.rs +++ b/src/net/tcp.rs @@ -57,21 +57,35 @@ impl TcpListener { /// future's task. It's recommended to only call this from the /// implementation of a `Future::poll`, if necessary. pub fn accept(&mut self) -> io::Result<(TcpStream, SocketAddr)> { + let (stream, addr) = self.accept_std()?; + let stream = TcpStream::from_std(stream, self.io.handle())?; + Ok((stream, addr)) + } + + /// Attempt to accept a connection and create a new connected `TcpStream` if + /// successful. + /// + /// This function is the asme as `accept` above except that it returns a + /// `std::net::TcpStream` instead of a `tokio::net::TcpStream`. This in turn + /// can then allow for the TCP stream to be assoiated with a different + /// reactor than the one this `TcpListener` is associated with. + /// + /// # Panics + /// + /// This function will panic for the same reasons as `accept`, notably if + /// called outside the context of a future. + pub fn accept_std(&mut self) -> io::Result<(net::TcpStream, SocketAddr)> { if let Async::NotReady = self.io.poll_read() { return Err(io::Error::new(io::ErrorKind::WouldBlock, "not ready")) } - match self.io.get_ref().accept() { + match self.io.get_ref().accept_std() { + Ok(pair) => Ok(pair), Err(e) => { if e.kind() == io::ErrorKind::WouldBlock { self.io.need_read()?; } Err(e) - }, - Ok((sock, addr)) => { - let handle = self.io.handle(); - let io = try!(PollEvented::new(sock, &handle)); - Ok((TcpStream { io: io }, addr)) } } }