Last active
May 27, 2018 18:28
-
-
Save pavel-mukhanov/7b07cf027cd59aa3ca8d3b7a414404c3 to your computer and use it in GitHub Desktop.
tokio love
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate tokio_io; | |
extern crate tokio_core; | |
extern crate futures; | |
use tokio_core::reactor::Core; | |
use std::net::SocketAddr; | |
use tokio_core::net::TcpListener; | |
use tokio_core::net::TcpStream; | |
use futures::Future; | |
use futures::Stream; | |
use std::error::Error as StdError; | |
#[test] | |
fn test_tcp_listener() { | |
let mut core = Core::new().unwrap(); | |
let handle = core.handle(); | |
let addr: SocketAddr = "127.0.0.1:8000".parse().unwrap(); | |
let handle_cloned = handle.clone(); | |
let fut_stream = TcpListener::bind(&addr, &handle_cloned).unwrap(); | |
let fut = fut_stream.incoming() | |
.for_each(move |(stream, _)| { | |
println!("connected!"); | |
Ok(()) | |
}) | |
.map_err(log_error); | |
handle.spawn(fut); | |
let stream = TcpStream::connect(&addr, &handle_cloned); | |
let res = core.run(stream); | |
} | |
pub fn log_error<E: StdError>(err: E) { | |
println!("An error occurred: {}", err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment