From 767b370c219b99849b5a56c8bf02a6ef896e7483 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Wed, 15 Aug 2018 05:54:24 -0600 Subject: [PATCH] Add tokio-tls echo example. (#541) Based on the current example on the front page of tokio.rs. --- tokio-tls/examples/echo.rs | 59 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 tokio-tls/examples/echo.rs diff --git a/tokio-tls/examples/echo.rs b/tokio-tls/examples/echo.rs new file mode 100644 index 000000000..b2fc221d5 --- /dev/null +++ b/tokio-tls/examples/echo.rs @@ -0,0 +1,59 @@ +// A tiny async TLS echo server with Tokio +extern crate native_tls; +extern crate tokio; +extern crate tokio_tls; + +use native_tls::Identity; +use tokio::io; +use tokio::net::TcpListener; +use tokio::prelude::*; + +fn main() { + // Bind the server's socket + let addr = "127.0.0.1:12345".parse().unwrap(); + let tcp = TcpListener::bind(&addr).unwrap(); + + // Create the TLS acceptor. + let der = include_bytes!("identity.p12"); + let cert = Identity::from_pkcs12(der, "mypass").unwrap(); + let tls_acceptor = tokio_tls::TlsAcceptor::from( + native_tls::TlsAcceptor::builder(cert).build().unwrap()); + + // Iterate incoming connections + let server = tcp.incoming().for_each(move |tcp| { + + // Accept the TLS connection. + let tls_accept = tls_acceptor.accept(tcp) + .and_then(move |tls| { + // Split up the read and write halves + let (reader, writer) = tls.split(); + + // Copy the data back to the client + let conn = io::copy(reader, writer) + // print what happened + .map(|(n, _, _)| { + println!("wrote {} bytes", n) + }) + // Handle any errors + .map_err(|err| { + println!("IO error {:?}", err) + }); + + // Spawn the future as a concurrent task + tokio::spawn(conn); + + Ok(()) + }) + .map_err(|err| { + println!("TLS accept error: {:?}", err); + }); + tokio::spawn(tls_accept); + + Ok(()) + }).map_err(|err| { + println!("server error {:?}", err); + }); + + // Start the runtime and spin up the server + tokio::run(server); +}