diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/echo.rs | 37 |
1 files changed, 0 insertions, 37 deletions
diff --git a/examples/echo.rs b/examples/echo.rs deleted file mode 100644 index 644627a..0000000 --- a/examples/echo.rs +++ /dev/null @@ -1,37 +0,0 @@ -//! Echo example. -//! Use `nc 127.0.0.1 30000` to connect. - -use futures::StreamExt; -use mini_rust_runtime::executor::Executor; -use mini_rust_runtime::tcp::TcpListener; -use tokio::io::{AsyncReadExt, AsyncWriteExt}; - -fn main() { - let ex = Executor::new(); - ex.block_on(serve); -} - -async fn serve() { - let mut listener = TcpListener::bind("127.0.0.1:30000").unwrap(); - while let Some(ret) = listener.next().await { - if let Ok((mut stream, addr)) = ret { - println!("accept a new connection from {} successfully", addr); - let f = async move { - let mut buf = [0; 4096]; - loop { - match stream.read(&mut buf).await { - Ok(n) => { - if n == 0 || stream.write_all(&buf[..n]).await.is_err() { - return; - } - } - Err(_) => { - return; - } - } - } - }; - Executor::spawn(f); - } - } -} |
