upgrade example/test to tokio

This commit is contained in:
quininer 2018-02-28 14:36:37 +08:00
parent 8aa3f3a14b
commit 8d6140a7b9
3 changed files with 28 additions and 31 deletions

View File

@ -22,7 +22,8 @@ webpki = "0.18.0-alpha"
tokio-proto = { version = "0.1.1", optional = true } tokio-proto = { version = "0.1.1", optional = true }
[dev-dependencies] [dev-dependencies]
tokio-core = "0.1.9" tokio-core = "0.1"
tokio = "0.1"
clap = "2.26" clap = "2.26"
webpki-roots = "0.14" webpki-roots = "0.14"

View File

@ -2,7 +2,7 @@ extern crate clap;
extern crate rustls; extern crate rustls;
extern crate futures; extern crate futures;
extern crate tokio_io; extern crate tokio_io;
extern crate tokio_core; extern crate tokio;
extern crate webpki_roots; extern crate webpki_roots;
extern crate tokio_rustls; extern crate tokio_rustls;
@ -14,8 +14,8 @@ use futures::{ Future, Stream };
use rustls::{ Certificate, NoClientAuth, PrivateKey, ServerConfig }; use rustls::{ Certificate, NoClientAuth, PrivateKey, ServerConfig };
use rustls::internal::pemfile::{ certs, rsa_private_keys }; use rustls::internal::pemfile::{ certs, rsa_private_keys };
use tokio_io::{ io, AsyncRead }; use tokio_io::{ io, AsyncRead };
use tokio_core::net::TcpListener; use tokio::net::TcpListener;
use tokio_core::reactor::Core; use tokio::executor::current_thread;
use clap::{ App, Arg }; use clap::{ App, Arg };
use tokio_rustls::ServerConfigExt; use tokio_rustls::ServerConfigExt;
@ -48,27 +48,28 @@ fn main() {
let key_file = matches.value_of("key").unwrap(); let key_file = matches.value_of("key").unwrap();
let flag_echo = matches.occurrences_of("echo") > 0; 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()); let mut config = ServerConfig::new(NoClientAuth::new());
config.set_single_cert(load_certs(cert_file), load_keys(key_file).remove(0)); config.set_single_cert(load_certs(cert_file), load_keys(key_file).remove(0));
let arc_config = Arc::new(config); let arc_config = Arc::new(config);
let socket = TcpListener::bind(&addr, &handle).unwrap(); let socket = TcpListener::bind(&addr).unwrap();
let done = socket.incoming() 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) let done = arc_config.accept_async(stream)
.and_then(|stream| { .and_then(|stream| {
let (reader, writer) = stream.split(); let (reader, writer) = stream.split();
io::copy(reader, writer) io::copy(reader, writer)
}) })
.map(move |(n, ..)| println!("Echo: {} - {}", n, addr)) .map(move |(n, ..)| println!("Echo: {} - {:?}", n, addr))
.map_err(move |err| println!("Error: {:?} - {}", err, addr)); .map_err(move |err| println!("Error: {:?} - {:?}", err, addr2));
handle.spawn(done); current_thread::spawn(done);
Ok(()) Ok(())
} else { } else {
let addr = stream.peer_addr().ok();
let addr2 = addr.clone();
let done = arc_config.accept_async(stream) let done = arc_config.accept_async(stream)
.and_then(|stream| io::write_all( .and_then(|stream| io::write_all(
stream, stream,
@ -79,12 +80,12 @@ fn main() {
Hello world!"[..] Hello world!"[..]
)) ))
.and_then(|(stream, _)| io::flush(stream)) .and_then(|(stream, _)| io::flush(stream))
.map(move |_| println!("Accept: {}", addr)) .map(move |_| println!("Accept: {:?}", addr))
.map_err(move |err| println!("Error: {:?} - {}", err, addr)); .map_err(move |err| println!("Error: {:?} - {:?}", err, addr2));
handle.spawn(done); current_thread::spawn(done);
Ok(()) Ok(())
}); });
core.run(done).unwrap(); current_thread::run(|_| current_thread::spawn(done.map_err(drop)));
} }

View File

@ -1,6 +1,6 @@
extern crate rustls; extern crate rustls;
extern crate futures; extern crate futures;
extern crate tokio_core; extern crate tokio;
extern crate tokio_io; extern crate tokio_io;
extern crate tokio_rustls; extern crate tokio_rustls;
extern crate webpki; extern crate webpki;
@ -11,8 +11,8 @@ use std::sync::Arc;
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
use std::net::{ SocketAddr, IpAddr, Ipv4Addr }; use std::net::{ SocketAddr, IpAddr, Ipv4Addr };
use futures::{ Future, Stream }; use futures::{ Future, Stream };
use tokio_core::reactor::Core; use tokio::executor::current_thread;
use tokio_core::net::{ TcpListener, TcpStream }; use tokio::net::{ TcpListener, TcpStream };
use tokio_io::io as aio; use tokio_io::io as aio;
use rustls::{ Certificate, PrivateKey, ServerConfig, ClientConfig }; use rustls::{ Certificate, PrivateKey, ServerConfig, ClientConfig };
use rustls::internal::pemfile::{ certs, rsa_private_keys }; use rustls::internal::pemfile::{ certs, rsa_private_keys };
@ -33,14 +33,12 @@ fn start_server(cert: Vec<Certificate>, rsa: PrivateKey) -> SocketAddr {
thread::spawn(move || { thread::spawn(move || {
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0); let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0);
let mut core = Core::new().unwrap(); let listener = TcpListener::bind(&addr).unwrap();
let handle = core.handle();
let listener = TcpListener::bind(&addr, &handle).unwrap();
send.send(listener.local_addr().unwrap()).unwrap(); send.send(listener.local_addr().unwrap()).unwrap();
let done = listener.incoming() let done = listener.incoming()
.for_each(|(stream, _)| { .for_each(move |stream| {
let done = config.accept_async(stream) let done = config.accept_async(stream)
.and_then(|stream| aio::read_exact(stream, vec![0; HELLO_WORLD.len()])) .and_then(|stream| aio::read_exact(stream, vec![0; HELLO_WORLD.len()]))
.and_then(|(stream, buf)| { .and_then(|(stream, buf)| {
@ -50,12 +48,13 @@ fn start_server(cert: Vec<Certificate>, rsa: PrivateKey) -> SocketAddr {
.map(drop) .map(drop)
.map_err(drop); .map_err(drop);
handle.spawn(done); current_thread::spawn(done);
Ok(()) Ok(())
}) })
.map(drop) .map(drop)
.map_err(drop); .map_err(drop);
core.run(done).unwrap();
current_thread::run(|_| current_thread::spawn(done));
}); });
recv.recv().unwrap() recv.recv().unwrap()
@ -70,11 +69,7 @@ fn start_client(addr: &SocketAddr, domain: &str,
} }
let config = Arc::new(config); let config = Arc::new(config);
let mut core = Core::new()?; let done = TcpStream::connect(addr)
let handle = core.handle();
#[allow(unreachable_code, unused_variables)]
let done = TcpStream::connect(addr, &handle)
.and_then(|stream| config.connect_async(domain, stream)) .and_then(|stream| config.connect_async(domain, stream))
.and_then(|stream| aio::write_all(stream, HELLO_WORLD)) .and_then(|stream| aio::write_all(stream, HELLO_WORLD))
.and_then(|(stream, _)| aio::read_exact(stream, vec![0; HELLO_WORLD.len()])) .and_then(|(stream, _)| aio::read_exact(stream, vec![0; HELLO_WORLD.len()]))
@ -83,7 +78,7 @@ fn start_client(addr: &SocketAddr, domain: &str,
Ok(()) Ok(())
}); });
core.run(done) done.wait()
} }