Created
April 30, 2019 22:56
-
-
Save kpp/fc8de5220412b8278fb8c462fbb809f0 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
#![feature(async_await, await_macro)] | |
use std::io; | |
use std::net::SocketAddr; | |
use futures::executor; | |
use futures::io::AsyncReadExt; | |
use futures::StreamExt; | |
use romio::{TcpListener, TcpStream}; | |
fn main() { | |
let mut listener = TcpListener::bind(&"127.0.0.1:7878".parse().unwrap()).unwrap(); | |
let incoming = listener.incoming(); | |
println!("Listening on 127.0.0.1:7878"); | |
let process_connections = incoming | |
.filter_map(async move |s| s.ok()) // filter only Ok(TcpStream) | |
.filter_map(async move |s| s.peer_addr().ok().map(|a| (s, a))) // filter only with peer_addr | |
.for_each_concurrent(None, async move |(stream, addr)| { | |
let result = await!(echo_on(stream, addr)); | |
drop(result); // I don't care about errors | |
}); | |
executor::block_on(process_connections); | |
} | |
async fn echo_on(stream: TcpStream, addr: SocketAddr) -> io::Result<()> { | |
println!("Accepting stream from: {}", addr); | |
let (mut reader, mut writer) = stream.split(); | |
await!(reader.copy_into(&mut writer))?; | |
println!("Closing stream from: {}", addr); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment