How do you access peer SocketAddr and provide your own executor?
danneu opened this issue · 0 comments
danneu commented
For example, here's the familiar hyper/tokio code for when you want to bring your own reactor and grab the connection's socket address:
struct MyService {
peer: SocketAddr
}
fn main() {
let mut core = Core::new().unwrap();
let handle = core.handle();
let addr = "127.0.0.1:3000".parse().unwrap();
let http = Http::new();
let listener = tokio_core::net::TcpListener::bind(&addr, &handle).unwrap();
let factory = move |peer| {
MyService { peer }
};
let future = listener.incoming().for_each(move |(socket, peer)| {
let conn = http.serve_connection(socket, factory(peer))
.map(|_| ())
.map_err(|e| eprintln!("server connection error: {}", e));
handle.spawn(conn);
Ok(())
});
println!("Listening on http://localhost:3000");
core.run(future).unwrap()
}
However, I've been unable to figure out how to collaborate the structs and traits in tokio-tls to recreate that.
The only thing the example demonstrates is the trivial case where you don't have your own executor nor do you want to access the client socket address.
let acceptor = TlsAcceptor::builder(pfx).unwrap().build().unwrap();
let proto = proto::Server::new(Http::new(), acceptor);
let srv = TcpServer::new(proto, https.host);
srv.serve(move || Ok(MyService(ctx)));
I can see the sort of code I wrote in my first snippet inside the tokio-tls::proto
module, but the types are a bit advanced.
Could somebody provide an example? I'd love to update the examples/ folder once I figure something out.