* Change send_reset to take &mut self. While calling this function is the last thing that should be done with the instance, the intent of the h2 library is not to be used directly by users, but to be used as an implementation detail by other libraries. Requiring `self` on `send_reset` is pretty annoying when calling the function from inside a `Future` implementation. Also, all the other fns on the type take `&mut self`. * Remove the P: Peer generic from internals * Split out `Respond` from `server::Stream` This new type is used to send HTTP responses to the client as well as reserve streams for push promises. * Remove unused `Send` helper. This could be brought back later when the API becomes stable. * Unite `client` and `server` types * Remove `B` generic from internal proto structs This is a first step in removing the `B` generic from public API types that do not strictly require it. Currently, all public API types must be generic over `B` even if they do not actually interact with the send data frame type. The first step in removing this is to remove `B` as a generic on all internal types. * Remove `Buffer<B>` from inner stream state This is the next step in removing the `B` generic from all public API types. The send buffer is the only type that requires `B`. It has now been extracted from the rest of the stream state. The strategy used in this PR requires an additional `Arc` and `Mutex`, but this is not a fundamental requirement. The additional overhead can be avoided with a little bit of unsafe code. However, this optimization should not be made until it is proven that it is required. * Remove `B` generic from `Body` + `ReleaseCapacity` This commit actually removes the generic from these two public API types. Also note, that removing the generic requires that `B: 'static`. This is because there is no more generic on `Body` and `ReleaseCapacity` and the compiler must be able to ensure that `B` outlives all `Body` and `ReleaseCapacity` handles. In practice, in an async world, passing a non 'static `B` is never going to happen. * Remove generic from `ResponseFuture` This change also makes generic free types `Send`. The original strategy of using a trait object meant that those handles could not be `Send`. The solution was to avoid using the send buffer when canceling a stream. This is done by transitioning the stream state to `Canceled`, a new `Cause` variant. * Simplify Send::send_reset Now that implicit cancelation goes through a separate path, the send_reset function can be simplified. * Export types common to client & server at root * Rename Stream -> SendStream, Body -> RecvStream * Implement send_reset on server::Respond
73 lines
2.0 KiB
Rust
73 lines
2.0 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 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(())
|
|
});
|
|
|
|
handle.spawn(connection);
|
|
Ok(())
|
|
});
|
|
|
|
core.run(server).unwrap();
|
|
}
|