Files
tokio/src/bin/sink.rs
T

43 lines
1.2 KiB
Rust
Raw Normal View History

2016-07-30 17:53:12 -07:00
//! A small server that writes as many nul bytes on all connections it receives.
//!
//! There is no concurrency in this server, only one connection is written to at
//! a time.
#[macro_use]
extern crate futures;
2016-08-01 17:41:58 -07:00
extern crate futures_io;
2016-07-30 17:53:12 -07:00
extern crate futures_mio;
use std::env;
use std::net::SocketAddr;
use futures::Future;
use futures::stream::Stream;
2016-08-12 11:54:19 -07:00
use futures_io::IoFuture;
2016-07-30 17:53:12 -07:00
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 l = futures_mio::Loop::new().unwrap();
let server = l.handle().tcp_listen(&addr).and_then(|socket| {
socket.incoming().and_then(|(socket, addr)| {
println!("got a socket: {}", addr);
2016-08-17 09:29:05 -07:00
write(socket).or_else(|_| Ok(()))
2016-07-30 17:53:12 -07:00
}).for_each(|()| {
println!("lost the socket");
Ok(())
})
});
println!("Listenering on: {}", addr);
l.run(server).unwrap();
}
2016-08-17 09:29:05 -07:00
// TODO: this blows the stack...
2016-08-12 11:54:19 -07:00
fn write(socket: futures_mio::TcpStream) -> IoFuture<()> {
2016-08-17 09:29:05 -07:00
static BUF: &'static [u8] = &[0; 1 * 1024 * 1024];
futures_io::write_all(socket, BUF).and_then(|(socket, _)| {
2016-07-30 17:53:12 -07:00
write(socket)
}).boxed()
}