Update examples to std-future

This commit is contained in:
Gurwinder Singh
2019-08-15 08:26:48 +05:30
committed by Sean McArthur
parent 517077c698
commit 0a4bd393ec
3 changed files with 111 additions and 133 deletions

View File

@@ -1,3 +1,10 @@
fn main() {
// Enable the below code once tokio_rustls moves to std::future
}
/*
#![feature(async_await)]
use h2::client;
use futures::*;
@@ -10,10 +17,12 @@ use tokio_rustls::ClientConfigExt;
use webpki::DNSNameRef;
use std::net::ToSocketAddrs;
use std::error::Error;
const ALPN_H2: &str = "h2";
pub fn main() {
#[tokio::main]
pub async fn main() -> Result<(), Box<dyn Error>> {
let _ = env_logger::try_init();
let tls_client_config = std::sync::Arc::new({
@@ -33,14 +42,9 @@ pub fn main() {
println!("ADDR: {:?}", addr);
let tcp = TcpStream::connect(&addr);
let tcp = TcpStream::connect(&addr).await?;
let dns_name = DNSNameRef::try_from_ascii_str("http2.akamai.com").unwrap();
let tcp = tcp.then(move |res| {
let tcp = res.unwrap();
tls_client_config
.connect_async(dns_name, tcp)
.then(|res| {
let res = tls_client_config.connect_async(dns_name, tcp).await;
let tls = res.unwrap();
{
let (_, session) = tls.get_ref();
@@ -49,10 +53,7 @@ pub fn main() {
}
println!("Starting client handshake");
client::handshake(tls)
})
.then(|res| {
let (mut client, h2) = res.unwrap();
let (mut client, h2) = client::handshake(tls).await?;
let request = Request::builder()
.method(Method::GET)
@@ -61,21 +62,10 @@ pub fn main() {
.unwrap();
let (response, _) = client.send_request(request, true).unwrap();
let stream = response.and_then(|response| {
let (_, body) = response.into_parts();
body.for_each(|chunk| {
println!("RX: {:?}", chunk);
Ok(())
})
});
h2.join(stream)
})
})
.map_err(|e| eprintln!("ERROR: {:?}", e))
.map(|((), ())| ());
tokio::run(tcp);
let (_, mut body) = response.await?.into_parts();
while let Some(chunk) = body.next().await {
println!("RX: {:?}", chunk?);
}
Ok(())
}
*/

View File

@@ -1,8 +1,14 @@
#![feature(async_await)]
use futures::{ready, Stream};
use h2::client;
use h2::RecvStream;
use http::{HeaderMap, Request};
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use futures::*;
use http::*;
use std::error::Error;
use tokio::net::TcpStream;
@@ -12,41 +18,37 @@ struct Process {
}
impl Future for Process {
type Item = ();
type Error = h2::Error;
type Output = Result<(), h2::Error>;
fn poll(&mut self) -> Poll<(), h2::Error> {
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
loop {
if self.trailers {
let trailers = try_ready!(self.body.poll_trailers());
let trailers = ready!(self.body.poll_trailers(cx));
println!("GOT TRAILERS: {:?}", trailers);
return Ok(().into());
return Poll::Ready(Ok(()));
} else {
match try_ready!(self.body.poll()) {
Some(chunk) => {
match ready!(Pin::new(&mut self.body).poll_next(cx)) {
Some(Ok(chunk)) => {
println!("GOT CHUNK = {:?}", chunk);
},
}
Some(Err(e)) => return Poll::Ready(Err(e)),
None => {
self.trailers = true;
},
}
}
}
}
}
}
pub fn main() {
#[tokio::main]
pub async fn main() -> Result<(), Box<dyn Error>> {
let _ = env_logger::try_init();
let tcp = TcpStream::connect(&"127.0.0.1:5928".parse().unwrap());
let tcp = tcp.then(|res| {
let tcp = res.unwrap();
client::handshake(tcp)
}).then(|res| {
let (mut client, h2) = res.unwrap();
let tcp = TcpStream::connect(&"127.0.0.1:5928".parse().unwrap()).await?;
let (mut client, h2) = client::handshake(tcp).await?;
println!("sending request");
@@ -64,10 +66,13 @@ pub fn main() {
stream.send_trailers(trailers).unwrap();
// Spawn a task to run the conn...
tokio::spawn(h2.map_err(|e| println!("GOT ERR={:?}", e)));
tokio::spawn(async move {
if let Err(e) = h2.await {
println!("GOT ERR={:?}", e);
}
});
response
.and_then(|response| {
let response = response.await?;
println!("GOT RESPONSE: {:?}", response);
// Get the body
@@ -77,11 +82,6 @@ pub fn main() {
body,
trailers: false,
}
})
.map_err(|e| {
println!("GOT ERR={:?}", e);
})
});
tokio::run(tcp);
.await?;
Ok(())
}

View File

@@ -1,62 +1,50 @@
#![feature(async_await)]
use h2::server;
use bytes::*;
use futures::*;
use http::*;
use http::{Response, StatusCode};
use tokio::net::TcpListener;
use tokio::net::{TcpListener, TcpStream};
use std::error::Error;
pub fn main() {
#[tokio::main]
pub async fn main() -> Result<(), Box<dyn Error>> {
let _ = env_logger::try_init();
let listener = TcpListener::bind(&"127.0.0.1:5928".parse().unwrap()).unwrap();
println!("listening on {:?}", listener.local_addr());
let mut incoming = listener.incoming();
let server = listener.incoming().for_each(move |socket| {
// let socket = io_dump::Dump::to_stdout(socket);
while let Some(socket) = incoming.next().await {
tokio::spawn(async move {
if let Err(e) = handle(socket).await {
println!(" -> err={:?}", e);
}
});
}
let connection = server::handshake(socket)
.and_then(|conn| {
Ok(())
}
async fn handle(socket: io::Result<TcpStream>) -> Result<(), Box<dyn Error>> {
let mut connection = server::handshake(socket?).await?;
println!("H2 connection bound");
conn.for_each(|(request, mut respond)| {
while let Some(result) = connection.next().await {
let (request, mut respond) = result?;
println!("GOT request: {:?}", request);
let response = Response::builder().status(StatusCode::OK).body(()).unwrap();
let mut send = match respond.send_response(response, false) {
Ok(send) => send,
Err(e) => {
println!(" error respond; err={:?}", e);
return Ok(());
}
};
let mut send = respond.send_response(response, false)?;
println!(">>>> sending data");
if let Err(e) = send.send_data(Bytes::from_static(b"hello world"), true) {
println!(" -> err={:?}", e);
send.send_data(Bytes::from_static(b"hello world"), true)?;
}
Ok(())
})
})
.and_then(|_| {
println!("~~~~~~~~~~~~~~~~~~~~~~~~~~~ H2 connection CLOSE !!!!!! ~~~~~~~~~~~");
Ok(())
})
.then(|res| {
if let Err(e) = res {
println!(" -> err={:?}", e);
}
Ok(())
});
tokio::spawn(Box::new(connection));
Ok(())
})
.map_err(|e| eprintln!("accept error: {}", e));
tokio::run(server);
}