summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: 6985add4dca273d8a1f21b797393ff996be100d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//! `Future`-powered I/O at the core of Tokio
//!
//! This crate uses the [`futures`] crate to provide an event loop ("reactor
//! core") which can be used to drive I/O like TCP and UDP. All asynchronous I/O
//! is powered by the [`mio`] crate.
//!
//! [`futures`]: ../futures/index.html
//! [`mio`]: ../mio/index.html
//!
//! 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:
//!
//! * TCP, both streams and listeners.
//! * UDP sockets.
//! * An event loop to run futures.
//!
//! More functionality is likely to be added over time, but otherwise the crate
//! 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
//!
//! 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.
//!
//! You can find more extensive documentation in terms of tutorials at
//! [https://tokio.rs](https://tokio.rs).
//!
//! # Examples
//!
//! A simple TCP echo server:
//!
//! ```no_run
//! extern crate futures;
//! extern crate futures_cpupool;
//! extern crate tokio;
//! extern crate tokio_io;
//!
//! use futures::{Future, Stream};
//! use futures::future::Executor;
//! use futures_cpupool::CpuPool;
//! use tokio_io::AsyncRead;
//! use tokio_io::io::copy;
//! use tokio::net::TcpListener;
//! use tokio::reactor::Reactor;
//!
//! fn main() {
//!     // Create the event loop that will drive this server.
//!     let mut core = Reactor::new().unwrap();
//!     let handle = core.handle();
//!
//!     let pool = CpuPool::new_num_cpus();
//!
//!     // Bind the server's socket.
//!     let addr = "127.0.0.1:12345".parse().unwrap();
//!     let listener = TcpListener::bind(&addr, &handle)
//!         .expect("unable to bind TCP listener");
//!
//!     // Pull out a stream of sockets for incoming connections
//!     let server = listener.incoming().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.
//!         pool.execute(handle_conn).unwrap();
//!
//!         Ok(())
//!     });
//!
//!     // Spin up the server on the event loop.
//!     core.run(server).unwrap();
//! }
//! ```

#![doc(html_root_url = "https://docs.rs/tokio-core/0.1")]
#![deny(missing_docs)]
#![deny(warnings)]

extern crate bytes;
#[macro_use]
extern crate futures;
extern crate iovec;
extern crate mio;
extern crate slab;
#[macro_use]
extern crate tokio_io;

#[macro_use]
extern crate log;

pub mod net;
pub mod reactor;