Created
September 1, 2016 19:40
-
-
Save jcsoo/1b5b7e7c395927099da057cefdf34bf6 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
extern crate futures; | |
extern crate tokio_proto; | |
#[macro_use] | |
extern crate tokio_core; | |
use std::io::{self, Read}; | |
use futures::{Future, Poll}; | |
use tokio_proto::server; | |
use tokio_core::{Loop, TcpStream}; | |
struct Connection { | |
stream: TcpStream, | |
buf: Box<[u8]>, | |
} | |
impl Connection { | |
fn new(stream: TcpStream) -> Connection { | |
let buf = vec![0; 1024]; | |
Connection { | |
stream: stream, | |
buf: buf.into_boxed_slice(), | |
} | |
} | |
} | |
impl Future for Connection { | |
type Item = (); | |
type Error = io::Error; | |
fn poll(&mut self) -> Poll<(), io::Error> { | |
loop { | |
let n = try_nb!(self.stream.read(&mut self.buf)); | |
println!("read {} bytes", n); | |
if n == 0 { | |
// Socket closed, shutdown | |
return Poll::Ok(()) | |
} | |
} | |
Poll::NotReady | |
} | |
} | |
fn main() { | |
let mut lp = Loop::new().unwrap(); | |
// Launch the server | |
server::listen(lp.handle(), | |
"0.0.0.0:3245".parse().unwrap(), | |
|stream| Ok(Connection::new(stream))); | |
// Run the reactor | |
lp.run(futures::empty::<(), ()>()).unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment