stream/
stream.rs

1use work_pool::WorkPool;
2use std::net::{SocketAddr, TcpListener, TcpStream};
3use std::sync::Arc;
4
5fn handle(stream: Arc<TcpStream>) {
6    println!("Got connection");
7
8    drop(stream);
9}
10
11fn main() {
12    let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
13    let listener = TcpListener::bind(addr).expect("Failed to bind to port 8080");
14
15    // Setting the threads to 0 will let the system choose the number
16    // of threads. Setting the buffer length to None will default the
17    // buffer length to a reasonable amount.
18    let mut pool = WorkPool::new(0, None).expect("Failed to build work pool");
19    pool.set_executor_and_start(|stream| { handle(stream); });
20
21    println!("Bound to port 8080 - Send a request!");
22    println!("Press Ctrl+C to quit ...");
23
24    for stream in listener.incoming() {
25        match stream {
26            Ok(s) => pool.dispatch(Arc::new(s)),
27            Err(e) => eprintln!("failed to get incomign stream {:?}", e),
28        }
29    }
30}