|
| 1 | +//! Simple file-reader TCP server with framed stream. |
| 2 | +//! |
| 3 | +//! Using the following command: |
| 4 | +//! |
| 5 | +//! ```sh |
| 6 | +//! nc 127.0.0.1 8080 |
| 7 | +//! ``` |
| 8 | +//! |
| 9 | +//! Follow the prompt and enter a file path, relative or absolute. |
| 10 | +
|
| 11 | +use std::io; |
| 12 | + |
| 13 | +use actix_codec::{Framed, LinesCodec}; |
| 14 | +use actix_rt::net::TcpStream; |
| 15 | +use actix_server::Server; |
| 16 | +use actix_service::{fn_service, ServiceFactoryExt as _}; |
| 17 | +use futures_util::{SinkExt as _, StreamExt as _}; |
| 18 | +use tokio::{fs::File, io::AsyncReadExt as _}; |
| 19 | + |
| 20 | +async fn run() -> io::Result<()> { |
| 21 | + env_logger::init_from_env(env_logger::Env::default().default_filter_or("info")); |
| 22 | + |
| 23 | + let addr = ("127.0.0.1", 8080); |
| 24 | + log::info!("starting server on port: {}", &addr.0); |
| 25 | + |
| 26 | + // Bind socket address and start worker(s). By default, the server uses the number of physical |
| 27 | + // CPU cores as the worker count. For this reason, the closure passed to bind needs to return |
| 28 | + // a service *factory*; so it can be created once per worker. |
| 29 | + Server::build() |
| 30 | + .bind("file-reader", addr, move || { |
| 31 | + fn_service(move |stream: TcpStream| async move { |
| 32 | + // set up codec to use with I/O resource |
| 33 | + let mut framed = Framed::new(stream, LinesCodec::default()); |
| 34 | + |
| 35 | + loop { |
| 36 | + // prompt for file name |
| 37 | + framed.send("Type file name to return:").await?; |
| 38 | + |
| 39 | + // wait for next line |
| 40 | + match framed.next().await { |
| 41 | + Some(Ok(line)) => { |
| 42 | + match File::open(line).await { |
| 43 | + Ok(mut file) => { |
| 44 | + // read file into String buffer |
| 45 | + let mut buf = String::new(); |
| 46 | + file.read_to_string(&mut buf).await?; |
| 47 | + |
| 48 | + // send String into framed object |
| 49 | + framed.send(buf).await?; |
| 50 | + |
| 51 | + // break out of loop and |
| 52 | + break; |
| 53 | + } |
| 54 | + Err(err) => { |
| 55 | + log::error!("{}", err); |
| 56 | + framed |
| 57 | + .send("File not found or not readable. Try again.") |
| 58 | + .await?; |
| 59 | + continue; |
| 60 | + } |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + // not being able to read a line from the stream is unrecoverable |
| 65 | + Some(Err(err)) => return Err(err), |
| 66 | + |
| 67 | + // This EOF won't be hit. |
| 68 | + None => continue, |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // close connection after file has been copied to TCP stream |
| 73 | + Ok(()) |
| 74 | + }) |
| 75 | + .map_err(|err| log::error!("Service Error: {:?}", err)) |
| 76 | + })? |
| 77 | + .workers(2) |
| 78 | + .run() |
| 79 | + .await |
| 80 | +} |
| 81 | + |
| 82 | +#[tokio::main] |
| 83 | +async fn main() -> io::Result<()> { |
| 84 | + run().await?; |
| 85 | + Ok(()) |
| 86 | +} |
| 87 | + |
| 88 | +// alternatively: |
| 89 | +// #[actix_rt::main] |
| 90 | +// async fn main() -> io::Result<()> { |
| 91 | +// run().await?; |
| 92 | +// Ok(()) |
| 93 | +// } |
0 commit comments