2016-09-02 11:07:52 -07:00
|
|
|
//! `Future`-powered I/O at the core of Tokio
|
2016-07-30 17:53:12 -07:00
|
|
|
//!
|
2017-12-05 16:55:25 +01:00
|
|
|
//! This crate uses the [`futures`] crate to provide an event loop ("reactor
|
2017-10-24 17:20:46 -07:00
|
|
|
//! core") which can be used to drive I/O like TCP and UDP. All asynchronous I/O
|
2017-12-05 16:55:25 +01:00
|
|
|
//! is powered by the [`mio`] crate.
|
|
|
|
|
//!
|
|
|
|
|
//! [`futures`]: ../futures/index.html
|
|
|
|
|
//! [`mio`]: ../mio/index.html
|
2016-09-02 11:07:52 -07:00
|
|
|
//!
|
|
|
|
|
//! The concrete types provided in this crate are relatively bare bones but are
|
|
|
|
|
//! intended to be the essential foundation for further projects needing an
|
|
|
|
|
//! event loop. In this crate you'll find:
|
|
|
|
|
//!
|
2017-12-05 16:55:25 +01:00
|
|
|
//! * TCP, both streams and listeners.
|
|
|
|
|
//! * UDP sockets.
|
|
|
|
|
//! * An event loop to run futures.
|
2016-09-02 11:07:52 -07:00
|
|
|
//!
|
|
|
|
|
//! More functionality is likely to be added over time, but otherwise the crate
|
2017-12-05 16:55:25 +01:00
|
|
|
//! is intended to be flexible, with the [`PollEvented`] type accepting any
|
|
|
|
|
//! type that implements [`mio::Evented`]. For example, the [`tokio-uds`] crate
|
|
|
|
|
//! uses [`PollEvented`] to provide support for Unix domain sockets.
|
|
|
|
|
//!
|
|
|
|
|
//! [`PollEvented`]: ./reactor/struct.PollEvented.html
|
|
|
|
|
//! [`mio::Evented`]: ../mio/event/trait.Evented.html
|
|
|
|
|
//! [`tokio-uds`]: https://crates.io/crates/tokio-uds
|
2016-09-02 11:07:52 -07:00
|
|
|
//!
|
|
|
|
|
//! Some other important tasks covered by this crate are:
|
|
|
|
|
//!
|
|
|
|
|
//! * All I/O is futures-aware. If any action in this crate returns "not ready"
|
|
|
|
|
//! or "would block", then the current future task is scheduled to receive a
|
|
|
|
|
//! notification when it would otherwise make progress.
|
|
|
|
|
//!
|
2017-01-11 09:14:50 -08:00
|
|
|
//! You can find more extensive documentation in terms of tutorials at
|
|
|
|
|
//! [https://tokio.rs](https://tokio.rs).
|
|
|
|
|
//!
|
2016-09-02 11:07:52 -07:00
|
|
|
//! # Examples
|
|
|
|
|
//!
|
|
|
|
|
//! A simple TCP echo server:
|
|
|
|
|
//!
|
|
|
|
|
//! ```no_run
|
|
|
|
|
//! extern crate futures;
|
2017-10-24 16:30:16 -07:00
|
|
|
//! extern crate tokio;
|
2017-08-16 19:17:12 +08:00
|
|
|
//! extern crate tokio_io;
|
2016-09-02 11:07:52 -07:00
|
|
|
//!
|
2017-12-11 21:29:18 -06:00
|
|
|
//! use futures::prelude::*;
|
2017-08-16 19:17:12 +08:00
|
|
|
//! use tokio_io::AsyncRead;
|
|
|
|
|
//! use tokio_io::io::copy;
|
2017-10-24 16:30:16 -07:00
|
|
|
//! use tokio::net::TcpListener;
|
2016-09-02 11:07:52 -07:00
|
|
|
//!
|
|
|
|
|
//! fn main() {
|
2017-12-05 16:55:25 +01:00
|
|
|
//! // Bind the server's socket.
|
2017-01-10 10:02:15 -08:00
|
|
|
//! let addr = "127.0.0.1:12345".parse().unwrap();
|
2017-12-12 18:32:50 -06:00
|
|
|
//! let listener = TcpListener::bind(&addr)
|
2017-12-05 16:55:25 +01:00
|
|
|
//! .expect("unable to bind TCP listener");
|
2017-01-10 10:02:15 -08:00
|
|
|
//!
|
|
|
|
|
//! // Pull out a stream of sockets for incoming connections
|
2018-02-21 07:42:22 -08:00
|
|
|
//! let server = listener.incoming()
|
|
|
|
|
//! .map_err(|e| println!("accept failed = {:?}", e))
|
|
|
|
|
//! .for_each(|sock| {
|
|
|
|
|
//! // Split up the reading and writing parts of the
|
|
|
|
|
//! // socket.
|
|
|
|
|
//! let (reader, writer) = sock.split();
|
|
|
|
|
//!
|
|
|
|
|
//! // A future that echos the data and returns how
|
|
|
|
|
//! // many bytes were copied...
|
|
|
|
|
//! let bytes_copied = copy(reader, writer);
|
|
|
|
|
//!
|
|
|
|
|
//! // ... after which we'll print what happened.
|
|
|
|
|
//! let handle_conn = bytes_copied.map(|amt| {
|
|
|
|
|
//! println!("wrote {:?} bytes", amt)
|
|
|
|
|
//! }).map_err(|err| {
|
|
|
|
|
//! eprintln!("IO error {:?}", err)
|
|
|
|
|
//! });
|
|
|
|
|
//!
|
|
|
|
|
//! // Spawn the future as a concurrent task.
|
|
|
|
|
//! tokio::spawn(handle_conn)
|
2017-01-10 10:02:15 -08:00
|
|
|
//! });
|
|
|
|
|
//!
|
2018-02-21 07:42:22 -08:00
|
|
|
//! // Start the Tokio runtime
|
|
|
|
|
//! tokio::run(server);
|
2016-09-02 11:07:52 -07:00
|
|
|
//! }
|
|
|
|
|
//! ```
|
2016-07-30 17:53:12 -07:00
|
|
|
|
2018-02-09 14:30:32 -08:00
|
|
|
#![doc(html_root_url = "https://docs.rs/tokio/0.1.1")]
|
2016-07-30 17:53:12 -07:00
|
|
|
#![deny(missing_docs)]
|
2018-01-26 10:12:19 -08:00
|
|
|
#![deny(warnings)]
|
2017-12-06 17:19:21 +01:00
|
|
|
#![warn(missing_debug_implementations)]
|
2016-07-30 17:53:12 -07:00
|
|
|
|
2017-02-05 17:06:57 -08:00
|
|
|
extern crate bytes;
|
|
|
|
|
#[macro_use]
|
2016-07-30 17:53:12 -07:00
|
|
|
extern crate futures;
|
2017-02-05 17:06:57 -08:00
|
|
|
extern crate iovec;
|
2016-07-30 17:53:12 -07:00
|
|
|
extern crate mio;
|
|
|
|
|
extern crate slab;
|
2017-10-24 16:41:39 -07:00
|
|
|
#[macro_use]
|
2017-02-05 17:06:57 -08:00
|
|
|
extern crate tokio_io;
|
2018-02-21 07:42:22 -08:00
|
|
|
extern crate tokio_executor;
|
|
|
|
|
extern crate tokio_threadpool;
|
2016-07-30 17:53:12 -07:00
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
|
extern crate log;
|
|
|
|
|
|
2018-02-06 07:26:21 -08:00
|
|
|
pub mod executor;
|
2016-09-02 11:07:52 -07:00
|
|
|
pub mod net;
|
|
|
|
|
pub mod reactor;
|
2018-02-21 07:42:22 -08:00
|
|
|
pub mod runtime;
|
|
|
|
|
|
|
|
|
|
pub use executor::spawn;
|
|
|
|
|
pub use runtime::run;
|
2018-02-28 09:03:13 -08:00
|
|
|
|
|
|
|
|
mod atomic_task;
|