From 8d6140a7b981c7877fb18ed135204c9836604fb2 Mon Sep 17 00:00:00 2001 From: quininer Date: Wed, 28 Feb 2018 14:36:37 +0800 Subject: [PATCH] upgrade example/test to tokio --- Cargo.toml | 3 ++- examples/server.rs | 31 ++++++++++++++++--------------- tests/test.rs | 25 ++++++++++--------------- 3 files changed, 28 insertions(+), 31 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7c6acd6..151320b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,8 @@ webpki = "0.18.0-alpha" tokio-proto = { version = "0.1.1", optional = true } [dev-dependencies] -tokio-core = "0.1.9" +tokio-core = "0.1" +tokio = "0.1" clap = "2.26" webpki-roots = "0.14" diff --git a/examples/server.rs b/examples/server.rs index 9d407b9..a450393 100644 --- a/examples/server.rs +++ b/examples/server.rs @@ -2,7 +2,7 @@ extern crate clap; extern crate rustls; extern crate futures; extern crate tokio_io; -extern crate tokio_core; +extern crate tokio; extern crate webpki_roots; extern crate tokio_rustls; @@ -14,8 +14,8 @@ use futures::{ Future, Stream }; use rustls::{ Certificate, NoClientAuth, PrivateKey, ServerConfig }; use rustls::internal::pemfile::{ certs, rsa_private_keys }; use tokio_io::{ io, AsyncRead }; -use tokio_core::net::TcpListener; -use tokio_core::reactor::Core; +use tokio::net::TcpListener; +use tokio::executor::current_thread; use clap::{ App, Arg }; use tokio_rustls::ServerConfigExt; @@ -48,27 +48,28 @@ fn main() { let key_file = matches.value_of("key").unwrap(); let flag_echo = matches.occurrences_of("echo") > 0; - let mut core = Core::new().unwrap(); - let handle = core.handle(); - let mut config = ServerConfig::new(NoClientAuth::new()); config.set_single_cert(load_certs(cert_file), load_keys(key_file).remove(0)); let arc_config = Arc::new(config); - let socket = TcpListener::bind(&addr, &handle).unwrap(); + let socket = TcpListener::bind(&addr).unwrap(); let done = socket.incoming() - .for_each(|(stream, addr)| if flag_echo { + .for_each(move |stream| if flag_echo { + let addr = stream.peer_addr().ok(); + let addr2 = addr.clone(); let done = arc_config.accept_async(stream) .and_then(|stream| { let (reader, writer) = stream.split(); io::copy(reader, writer) }) - .map(move |(n, ..)| println!("Echo: {} - {}", n, addr)) - .map_err(move |err| println!("Error: {:?} - {}", err, addr)); - handle.spawn(done); + .map(move |(n, ..)| println!("Echo: {} - {:?}", n, addr)) + .map_err(move |err| println!("Error: {:?} - {:?}", err, addr2)); + current_thread::spawn(done); Ok(()) } else { + let addr = stream.peer_addr().ok(); + let addr2 = addr.clone(); let done = arc_config.accept_async(stream) .and_then(|stream| io::write_all( stream, @@ -79,12 +80,12 @@ fn main() { Hello world!"[..] )) .and_then(|(stream, _)| io::flush(stream)) - .map(move |_| println!("Accept: {}", addr)) - .map_err(move |err| println!("Error: {:?} - {}", err, addr)); - handle.spawn(done); + .map(move |_| println!("Accept: {:?}", addr)) + .map_err(move |err| println!("Error: {:?} - {:?}", err, addr2)); + current_thread::spawn(done); Ok(()) }); - core.run(done).unwrap(); + current_thread::run(|_| current_thread::spawn(done.map_err(drop))); } diff --git a/tests/test.rs b/tests/test.rs index e66e2aa..baa22b2 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,6 +1,6 @@ extern crate rustls; extern crate futures; -extern crate tokio_core; +extern crate tokio; extern crate tokio_io; extern crate tokio_rustls; extern crate webpki; @@ -11,8 +11,8 @@ use std::sync::Arc; use std::sync::mpsc::channel; use std::net::{ SocketAddr, IpAddr, Ipv4Addr }; use futures::{ Future, Stream }; -use tokio_core::reactor::Core; -use tokio_core::net::{ TcpListener, TcpStream }; +use tokio::executor::current_thread; +use tokio::net::{ TcpListener, TcpStream }; use tokio_io::io as aio; use rustls::{ Certificate, PrivateKey, ServerConfig, ClientConfig }; use rustls::internal::pemfile::{ certs, rsa_private_keys }; @@ -33,14 +33,12 @@ fn start_server(cert: Vec, rsa: PrivateKey) -> SocketAddr { thread::spawn(move || { let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0); - let mut core = Core::new().unwrap(); - let handle = core.handle(); - let listener = TcpListener::bind(&addr, &handle).unwrap(); + let listener = TcpListener::bind(&addr).unwrap(); send.send(listener.local_addr().unwrap()).unwrap(); let done = listener.incoming() - .for_each(|(stream, _)| { + .for_each(move |stream| { let done = config.accept_async(stream) .and_then(|stream| aio::read_exact(stream, vec![0; HELLO_WORLD.len()])) .and_then(|(stream, buf)| { @@ -50,12 +48,13 @@ fn start_server(cert: Vec, rsa: PrivateKey) -> SocketAddr { .map(drop) .map_err(drop); - handle.spawn(done); + current_thread::spawn(done); Ok(()) }) .map(drop) .map_err(drop); - core.run(done).unwrap(); + + current_thread::run(|_| current_thread::spawn(done)); }); recv.recv().unwrap() @@ -70,11 +69,7 @@ fn start_client(addr: &SocketAddr, domain: &str, } let config = Arc::new(config); - let mut core = Core::new()?; - let handle = core.handle(); - - #[allow(unreachable_code, unused_variables)] - let done = TcpStream::connect(addr, &handle) + let done = TcpStream::connect(addr) .and_then(|stream| config.connect_async(domain, stream)) .and_then(|stream| aio::write_all(stream, HELLO_WORLD)) .and_then(|(stream, _)| aio::read_exact(stream, vec![0; HELLO_WORLD.len()])) @@ -83,7 +78,7 @@ fn start_client(addr: &SocketAddr, domain: &str, Ok(()) }); - core.run(done) + done.wait() }