From 34b1bc9c830010909f1cbaade66494444caf5b74 Mon Sep 17 00:00:00 2001 From: quininer Date: Thu, 28 Nov 2019 00:11:02 +0800 Subject: [PATCH] Update example --- README.md | 4 ++-- examples/client/Cargo.toml | 6 +++--- examples/client/src/main.rs | 24 ++++++++++++++---------- examples/server/Cargo.toml | 4 ++-- examples/server/src/main.rs | 35 +++++++++++++++++------------------ tests/test.rs | 2 +- 6 files changed, 39 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index c851af3..70ab646 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,8 @@ config.root_store.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS); let config = TlsConnector::from(Arc::new(config)); let dnsname = DNSNameRef::try_from_ascii_str("www.rust-lang.org").unwrap(); -TcpStream::connect(&addr) - .and_then(move |socket| config.connect(dnsname, socket)) +let stream = TcpStream::connect(&addr).await?; +let mut stream = config.connect(dnsname, stream).await?; // ... ``` diff --git a/examples/client/Cargo.toml b/examples/client/Cargo.toml index feec249..4e29373 100644 --- a/examples/client/Cargo.toml +++ b/examples/client/Cargo.toml @@ -5,9 +5,9 @@ authors = ["quininer "] edition = "2018" [dependencies] -futures = { package = "futures-preview", version = "0.3.0-alpha.16", features = ["io-compat"] } -romio = "0.3.0-alpha.8" +futures-util = "0.3" +tokio = { version = "0.2", features = [ "net", "io-util", "rt-threaded" ] } structopt = "0.2" tokio-rustls = { path = "../.." } -webpki-roots = "0.16" +webpki-roots = "0.18" tokio-stdin-stdout = "0.1" diff --git a/examples/client/src/main.rs b/examples/client/src/main.rs index 6416db2..8f130d2 100644 --- a/examples/client/src/main.rs +++ b/examples/client/src/main.rs @@ -1,16 +1,14 @@ -#![feature(async_await)] - use std::io; use std::fs::File; use std::path::PathBuf; use std::sync::Arc; use std::net::ToSocketAddrs; use std::io::BufReader; +use futures_util::future; use structopt::StructOpt; -use romio::TcpStream; -use futures::prelude::*; -use futures::executor; -use futures::compat::{ AsyncRead01CompatExt, AsyncWrite01CompatExt }; +use tokio::runtime; +use tokio::net::TcpStream; +use tokio::io::{ AsyncWriteExt, copy, split }; use tokio_rustls::{ TlsConnector, rustls::ClientConfig, webpki::DNSNameRef }; use tokio_stdin_stdout::{ stdin as tokio_stdin, stdout as tokio_stdout }; @@ -46,6 +44,10 @@ fn main() -> io::Result<()> { domain ); + let mut runtime = runtime::Builder::new() + .basic_scheduler() + .enable_io() + .build()?; let mut config = ClientConfig::new(); if let Some(cafile) = &options.cafile { let mut pem = BufReader::new(File::open(cafile)?); @@ -58,6 +60,8 @@ fn main() -> io::Result<()> { let fut = async { let stream = TcpStream::connect(&addr).await?; + + // TODO tokio-compat let (mut stdin, mut stdout) = (tokio_stdin(0).compat(), tokio_stdout(0).compat()); let domain = DNSNameRef::try_from_ascii_str(&domain) @@ -66,14 +70,14 @@ fn main() -> io::Result<()> { let mut stream = connector.connect(domain, stream).await?; stream.write_all(content.as_bytes()).await?; - let (mut reader, mut writer) = stream.split(); + let (mut reader, mut writer) = split(stream); future::try_join( - reader.copy_into(&mut stdout), - stdin.copy_into(&mut writer) + copy(&mut reader, &mut stdout), + copy(&mut stdin, &mut writer) ).await?; Ok(()) }; - executor::block_on(fut) + runtime.block_on(fut) } diff --git a/examples/server/Cargo.toml b/examples/server/Cargo.toml index 9da4423..cd42662 100644 --- a/examples/server/Cargo.toml +++ b/examples/server/Cargo.toml @@ -5,7 +5,7 @@ authors = ["quininer "] edition = "2018" [dependencies] -futures = { package = "futures-preview", version = "0.3.0-alpha.16" } -romio = "0.3.0-alpha.8" +futures-util = "0.3" +tokio = { version = "0.2", features = [ "net", "io-util", "rt-threaded" ] } structopt = "0.2" tokio-rustls = { path = "../.." } diff --git a/examples/server/src/main.rs b/examples/server/src/main.rs index a2a3b13..1fcb0e4 100644 --- a/examples/server/src/main.rs +++ b/examples/server/src/main.rs @@ -1,15 +1,13 @@ -#![feature(async_await)] - use std::fs::File; use std::sync::Arc; use std::net::ToSocketAddrs; use std::path::{ PathBuf, Path }; use std::io::{ self, BufReader }; +use futures_util::future::TryFutureExt; use structopt::StructOpt; -use futures::task::SpawnExt; -use futures::prelude::*; -use futures::executor; -use romio::TcpListener; +use tokio::runtime; +use tokio::net::TcpListener; +use tokio::io::{ AsyncWriteExt, copy, split }; use tokio_rustls::rustls::{ Certificate, NoClientAuth, PrivateKey, ServerConfig }; use tokio_rustls::rustls::internal::pemfile::{ certs, rsa_private_keys }; use tokio_rustls::TlsAcceptor; @@ -53,27 +51,30 @@ fn main() -> io::Result<()> { let mut keys = load_keys(&options.key)?; let flag_echo = options.echo; - let mut pool = executor::ThreadPool::new()?; + let mut runtime = runtime::Builder::new() + .threaded_scheduler() + .enable_io() + .build()?; + let handle = runtime.handle().clone(); let mut config = ServerConfig::new(NoClientAuth::new()); config.set_single_cert(certs, keys.remove(0)) .map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?; let acceptor = TlsAcceptor::from(Arc::new(config)); let fut = async { - let mut listener = TcpListener::bind(&addr)?; - let mut incoming = listener.incoming(); + let mut listener = TcpListener::bind(&addr).await?; - while let Some(stream) = incoming.next().await { + loop { + let (stream, peer_addr) = listener.accept().await?; let acceptor = acceptor.clone(); let fut = async move { - let stream = stream?; - let peer_addr = stream.peer_addr()?; let mut stream = acceptor.accept(stream).await?; if flag_echo { - let (mut reader, mut writer) = stream.split(); - let n = reader.copy_into(&mut writer).await?; + let (mut reader, mut writer) = split(stream); + let n = copy(&mut reader, &mut writer).await?; + writer.flush().await?; println!("Echo: {} - {}", peer_addr, n); } else { stream.write_all( @@ -90,11 +91,9 @@ fn main() -> io::Result<()> { Ok(()) as io::Result<()> }; - pool.spawn(fut.unwrap_or_else(|err| eprintln!("{:?}", err))).unwrap(); + handle.spawn(fut.unwrap_or_else(|err| eprintln!("{:?}", err))); } - - Ok(()) }; - executor::block_on(fut) + runtime.block_on(fut) } diff --git a/tests/test.rs b/tests/test.rs index d0d30f4..9b98688 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -7,8 +7,8 @@ use futures_util::future::TryFutureExt; use lazy_static::lazy_static; use tokio::prelude::*; use tokio::runtime; +use tokio::io::{ copy, split }; use tokio::net::{ TcpListener, TcpStream }; -use tokio::io::{copy, split}; use rustls::{ ServerConfig, ClientConfig }; use rustls::internal::pemfile::{ certs, rsa_private_keys }; use tokio_rustls::{ TlsConnector, TlsAcceptor };