diff options
-rw-r--r-- | tokio-tcp/Cargo.toml | 2 | ||||
-rw-r--r-- | tokio-tcp/src/lib.rs | 1 | ||||
-rw-r--r-- | tokio-tcp/src/listener.rs | 18 | ||||
-rw-r--r-- | tokio-tcp/tests/tcp_accept.rs | 25 |
4 files changed, 46 insertions, 0 deletions
diff --git a/tokio-tcp/Cargo.toml b/tokio-tcp/Cargo.toml index f1c8722d..ef99c6b7 100644 --- a/tokio-tcp/Cargo.toml +++ b/tokio-tcp/Cargo.toml @@ -25,6 +25,7 @@ publish = false incoming = ["futures-core-preview"] [dependencies] +async-util = { git = "https://github.com/tokio-rs/async" } tokio-io = { version = "0.2.0", path = "../tokio-io" } tokio-reactor = { version = "0.2.0", path = "../tokio-reactor" } bytes = "0.4" @@ -38,3 +39,4 @@ futures-core-preview = { version = "0.3.0-alpha.16", optional = true } #env_logger = { version = "0.5", default-features = false } #net2 = "*" tokio = { version = "0.2.0", path = "../tokio" } +tokio-test = { version = "0.2.0", path = "../tokio-test" } diff --git a/tokio-tcp/src/lib.rs b/tokio-tcp/src/lib.rs index 23fb1f01..d50e4b6e 100644 --- a/tokio-tcp/src/lib.rs +++ b/tokio-tcp/src/lib.rs @@ -2,6 +2,7 @@ #![deny(missing_docs, missing_debug_implementations, rust_2018_idioms)] #![cfg_attr(test, deny(warnings))] #![doc(test(no_crate_inject, attr(deny(rust_2018_idioms))))] +#![feature(async_await)] //! TCP bindings for `tokio`. //! diff --git a/tokio-tcp/src/listener.rs b/tokio-tcp/src/listener.rs index c469a38f..50621d00 100644 --- a/tokio-tcp/src/listener.rs +++ b/tokio-tcp/src/listener.rs @@ -110,6 +110,24 @@ impl TcpListener { Poll::Ready(Ok((io, addr))) } + /// Accept a new incoming connection from this listener. + /// + /// This function will yield once a new TCP connection is established. When + /// established, the corresponding [`TcpStream`] and the remote peer's + /// address will be returned. + /// + /// [`TcpStream`]: ../struct.TcpStream.html + /// + /// # Examples + /// + /// ``` + /// unimplemented!(); + /// ``` + pub async fn accept(&mut self) -> io::Result<(TcpStream, SocketAddr)> { + use async_util::future::poll_fn; + poll_fn(|cx| self.poll_accept(cx)).await + } + /// Attempt to accept a connection and create a new connected `TcpStream` if /// successful. /// diff --git a/tokio-tcp/tests/tcp_accept.rs b/tokio-tcp/tests/tcp_accept.rs new file mode 100644 index 00000000..3a7d9f65 --- /dev/null +++ b/tokio-tcp/tests/tcp_accept.rs @@ -0,0 +1,25 @@ +#![deny(warnings, rust_2018_idioms)] +#![feature(async_await)] + +use tokio::net::{TcpListener, TcpStream}; +use tokio::sync::oneshot; +use tokio_test::assert_ok; + +#[tokio::test] +async fn accept() { + let addr = "127.0.0.1:0".parse().unwrap(); + let mut listener = assert_ok!(TcpListener::bind(&addr)); + let addr = listener.local_addr().unwrap(); + + let (tx, rx) = oneshot::channel(); + + tokio::spawn(async move { + let (socket, _) = assert_ok!(listener.accept().await); + assert_ok!(tx.send(socket)); + }); + + let cli = assert_ok!(TcpStream::connect(&addr).await); + let srv = assert_ok!(rx.await); + + assert_eq!(cli.local_addr().unwrap(), srv.peer_addr().unwrap()); +} |