Created
January 30, 2019 15:52
-
-
Save snawaz/992006ffa4587c5da416739dc0e78a52 to your computer and use it in GitHub Desktop.
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
// Start the application | |
tokio::run(lazy(|| { | |
let addr = "127.0.0.1:0".parse().unwrap(); | |
let listener = TcpListener::bind(&addr).unwrap(); | |
// Create the channel that is used to communicate with the | |
// background task. | |
let (tx, rx) = mpsc::channel(1_024); | |
// Spawn the background task: | |
tokio::spawn(bg_task(rx)); | |
listener.incoming().for_each(move |socket| { | |
// An inbound socket has been received. | |
// | |
// Spawn a new task to process the socket | |
tokio::spawn({ | |
// Each spawned task will have a clone of the sender handle. | |
let tx = tx.clone(); | |
// In this example, "hello world" will be written to the | |
// socket followed by the socket being closed. | |
io::read_to_end(socket, vec![]) | |
// Drop the socket | |
.and_then(move |(_, buf)| { | |
tx.send(buf.len()) | |
.map_err(|_| io::ErrorKind::Other.into()) | |
}) | |
.map(|_| ()) | |
// Write any error to STDOUT | |
.map_err(|e| println!("socket error = {:?}", e)) | |
}); | |
// Receive the next inbound socket | |
Ok(()) | |
}) | |
.map_err(|e| println!("listener error = {:?}", e)) | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment