Skip to content

Instantly share code, notes, and snippets.

@michaelbeaumont
Last active February 1, 2017 08:15
Show Gist options
  • Save michaelbeaumont/d96f3b5c57a759a60bd668b30ee47796 to your computer and use it in GitHub Desktop.
Save michaelbeaumont/d96f3b5c57a759a60bd668b30ee47796 to your computer and use it in GitHub Desktop.
use std::io;
use std::net;
use native_tls::TlsConnector;
use futures::{Future, sink, Sink};
use futures::future::ok;
use tokio_core::io::{Io, Framed};
use tokio_core::reactor::Handle;
use tokio_core::net::TcpStream;
use tokio_proto::BindClient;
use tokio_proto::TcpClient;
use tokio_proto::streaming::{Message, Body};
use tokio_proto::streaming::multiplex::ClientProto;
use tokio_proto::util::client_proxy;
use tokio_tls::proto;
use http;
use codec;
use frame;
pub struct Http2Proto;
impl<T: Io + 'static> ClientProto<T> for Http2Proto {
type Request = http::Message;
type RequestBody = Vec<u8>;
type Response = http::Message;
type ResponseBody = Vec<u8>;
type Error = codec::CodecError;
type Transport =
codec::Http2Transport<sink::Buffer<Framed<T, codec::FrameCodec>>>;
type BindTransport = Result<Self::Transport, io::Error>;
fn bind_transport(&self, io: T) -> Self::BindTransport {
Ok(codec::Http2Transport::new(
io.framed(codec::FrameCodec{}).buffer(10)
))
}
}
pub struct Client {
inner: client_proxy::ClientProxy<
Message<http::Message, Body<Vec<u8>, codec::CodecError>>,
Message<http::Message, Body<Vec<u8>, codec::CodecError>>,
codec::CodecError>
}
impl Client {
pub fn connect(addr: &net::SocketAddr, handle: &Handle) -> Box<Future<Item=Client, Error=io::Error>> {
let client = TcpClient::new(Http2Proto)
.connect(addr, handle)
.map(|client_service| {
Client { inner: client_service }
});
Box::new(client)
}
pub fn connect_tls(addr: &net::SocketAddr, hostname: &str, handle: &Handle) { //Box<Future<Item=Client, Error=io::Error>> {
TcpStream::connect(addr, handle).and_then(|io| {
let connector = TlsConnector::builder().unwrap().build().unwrap();
let tlsProto: proto::Client<Http2Proto> =
proto::Client::new(
Http2Proto,
connector,
"google.com"
);
let f = tlsProto.bind_client(handle, io);
ok(())
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment