Files
h2/examples/server.rs
Eliza Weisman 2e3dcf602c StreamRef sends RST_STREAM on drop (#109)
This PR modifies the `Drop` implementation for `StreamRef` to reset the underlying stream if it is the last reference to that stream. Since both `Stream` and `Body` are internally just a `StreamRef`, this means they will both reset the stream on drop; thus, this closes #100.

The assertion that the store no longer contains the dropped stream ID at the end of the `Drop` method  had to be removed, as the stream has to be reset from inside of a `transition` block (which now manages releasing that ID for us), and the `transition` closure moves the value of `stream`, making the assertion no longer possible.

Modifications to some of the tests in `flow_control.rs` were also necessary, in order to prevent `StreamRef`s from being dropped too early.
2017-10-05 18:05:18 -05: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_core;
use h2::server::Server;
use bytes::*;
use futures::*;
use http::*;
use tokio_core::net::TcpListener;
use tokio_core::reactor;
pub fn main() {
let _ = env_logger::init();
let mut core = reactor::Core::new().unwrap();
let handle = core.handle();
let listener = TcpListener::bind(&"127.0.0.1:5928".parse().unwrap(), &handle).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 stream)| {
println!("GOT request: {:?}", request);
let response = Response::builder().status(StatusCode::OK).body(()).unwrap();
if let Err(e) = stream.send_response(response, false) {
println!(" error responding; err={:?}", e);
}
println!(">>>> sending data");
if let Err(e) = stream.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(())
});
handle.spawn(connection);
Ok(())
});
core.run(server).unwrap();
}