Files
h2/examples/server.rs
Eliza Weisman 00ca534c4a Update examples to use new Tokio (#316)
The old `tokio-core` crate is deprecated in favour of the `tokio` crate,
which also provides the new multithreaded Tokio runtime. Although `h2`
is generic over the runtime, the examples do depend on `tokio`. 

This branch update the example code to use the new `tokio` library
rather than `tokio-core`. It also updates the examples in `RustDoc`.

For the most part, this was pretty trivial --- simply replacing
`handle.spawn(...)` with `tokio::spawn(...)` and `core.run(...)` with
`tokio::run(...)`. There were a couple of cases where error and item
types from spawned futures had to be mapped to `(), ()`. Alternatively,
this could have been avoided by using the single-threaded runtime and
calling `block_on` instead to await the value of those futures, but I
thought it was better to reduce the amount of tokio-specific code in the
examples.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2018-09-25 14:33:49 -07:00

70 lines
1.9 KiB
Rust

extern crate bytes;
extern crate env_logger;
extern crate futures;
extern crate h2;
extern crate http;
extern crate tokio;
use h2::server;
use bytes::*;
use futures::*;
use http::*;
use tokio::net::TcpListener;
pub fn main() {
let _ = env_logger::try_init();
let listener = TcpListener::bind(&"127.0.0.1:5928".parse().unwrap()).unwrap();
println!("listening on {:?}", listener.local_addr());
let server = listener.incoming().for_each(move |socket| {
// let socket = io_dump::Dump::to_stdout(socket);
let connection = server::handshake(socket)
.and_then(|conn| {
println!("H2 connection bound");
conn.for_each(|(request, mut respond)| {
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(());
}
};
println!(">>>> sending data");
if let Err(e) = send.send_data(Bytes::from_static(b"hello world"), true) {
println!(" -> err={:?}", e);
}
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);
}