mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-22 00:00:11 +02:00
Renamed APIs * Loop => reactor::Core * LoopHandle => reactor::Handle * LoopPin => reactor::Pinned * TcpStream => net::TcpStream * TcpListener => net::TcpListener * UdpSocket => net::UdpSocket * Sender => channel::Sender * Receiver => channel::Receiver * Timeout => reactor::Timeout * ReadinessStream => reactor::PollEvented * All `LoopHandle` methods to construct objects are now free functions on the associated types, e.g. `LoopHandle::tcp_listen` is now `TcpListener::bind` * All APIs taking a `Handle` now take a `Handle` as the last argument * All future-returning APIs now return concrete types instead of trait objects Added APIs * io::Io trait -- Read + Write + ability to poll Removed without replacement: * AddSource * AddTimeout * IoToken * TimeoutToken Closes #3 Closes #6
46 lines
1.4 KiB
Rust
46 lines
1.4 KiB
Rust
//! 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.
|
|
|
|
extern crate env_logger;
|
|
extern crate futures;
|
|
extern crate tokio_core;
|
|
|
|
use std::env;
|
|
use std::iter;
|
|
use std::net::SocketAddr;
|
|
|
|
use futures::Future;
|
|
use futures::stream::{self, Stream};
|
|
use tokio_core::io::IoFuture;
|
|
use tokio_core::net::{TcpListener, TcpStream};
|
|
use tokio_core::reactor::Core;
|
|
|
|
fn main() {
|
|
env_logger::init().unwrap();
|
|
let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string());
|
|
let addr = addr.parse::<SocketAddr>().unwrap();
|
|
|
|
let mut l = Core::new().unwrap();
|
|
let server = TcpListener::bind(&addr, &l.handle()).and_then(|socket| {
|
|
socket.incoming().and_then(|(socket, addr)| {
|
|
println!("got a socket: {}", addr);
|
|
write(socket).or_else(|_| Ok(()))
|
|
}).for_each(|()| {
|
|
println!("lost the socket");
|
|
Ok(())
|
|
})
|
|
});
|
|
println!("Listenering on: {}", addr);
|
|
l.run(server).unwrap();
|
|
}
|
|
|
|
fn write(socket: TcpStream) -> IoFuture<()> {
|
|
static BUF: &'static [u8] = &[0; 64 * 1024];
|
|
let iter = iter::repeat(()).map(|()| Ok(()));
|
|
stream::iter(iter).fold(socket, |socket, ()| {
|
|
tokio_core::io::write_all(socket, BUF).map(|(socket, _)| socket)
|
|
}).map(|_| ()).boxed()
|
|
}
|