Created
December 22, 2016 06:20
-
-
Save TheWaWaR/9b4c9adfec8b0bf17d844494917e3beb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
extern crate libc; | |
extern crate threadpool; | |
extern crate futures; | |
extern crate tokio_core; | |
use std::env; | |
use std::net::SocketAddr; | |
// use std::sync::mpsc::{channel, Receiver}; | |
// use threadpool::ThreadPool; | |
use futures::Future; | |
use futures::stream::Stream; | |
use tokio_core::io::{copy, Io}; | |
use tokio_core::net::TcpListener; | |
use tokio_core::reactor::Core; | |
// fn start_loop(rx: Receiver<u32>) {} | |
fn main() { | |
let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string()); | |
let addr = addr.parse::<SocketAddr>().unwrap(); | |
let mut core = Core::new().unwrap(); | |
let handle = core.handle(); | |
let socket = TcpListener::bind(&addr, &handle).unwrap(); | |
println!("Listening on: {}", addr); | |
let done = socket.incoming().for_each(move |(socket, addr)| { | |
let (reader, writer) = socket.split(); | |
let amt = copy(reader, writer); | |
let msg = amt.then(move |result| { | |
match result { | |
Ok(amt) => println!("wrote {} bytes to {}", amt, addr), | |
Err(e) => println!("error on {}: {}", addr, e), | |
} | |
Ok(()) | |
}); | |
handle.spawn(msg); | |
Ok(()) | |
}); | |
let mut pid; | |
unsafe { | |
pid = libc::fork(); | |
pid = if pid == 0 { libc::getpid() } else { pid }; | |
} | |
println!("[Pid]: {}", pid); | |
core.run(done).unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment