Skip to content

Instantly share code, notes, and snippets.

@jcsoo
Created September 1, 2016 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcsoo/1b5b7e7c395927099da057cefdf34bf6 to your computer and use it in GitHub Desktop.
Save jcsoo/1b5b7e7c395927099da057cefdf34bf6 to your computer and use it in GitHub Desktop.
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