asyncio/lib.rs
1// Copyright 2017 Haruhiko Uchida
2//
3// The software is released under the MIT license. see LICENSE.txt
4// https://github.com/harre-orz/rust_asio/blob/master/LICENSE.txt
5
6//! The asyncio is Asynchronous Input/Output library, that made based on [boost::asio](http://www.boost.org/doc/libs/1_62_0/doc/html/boost_asio.html) c++ library.
7//!
8//! # Usage
9//! This crate is on [github](https://github.com/harre-orz/rust_asio "github") and can be used by adding asyncio to the dependencies in your project's Cargo.toml.
10//!
11//! ```toml
12//! [dependencies]
13//! rust_asio = "*"
14//! ```
15//!
16//! And this in your crate root:
17//!
18//! ```
19//! extern crate asyncio;
20//! ```
21//!
22//! For example, TCP asynchronous connection code:
23//!
24//! ```
25//! use asyncio::*;
26//! use asyncio::ip::*;
27//! use asyncio::socket_base::*;
28//!
29//! use std::io;
30//! use std::sync::{Arc, Mutex};
31//!
32//! fn on_accept(sv: Arc<Mutex<TcpListener>>, res: io::Result<(TcpSocket, TcpEndpoint)>) {
33//! match res {
34//! Ok((soc, ep)) => { /* do something */ },
35//! Err(err) => panic!("{}", err),
36//! }
37//! }
38//!
39//! fn on_connect(cl: Arc<Mutex<TcpSocket>>, res: io::Result<()>) {
40//! match res {
41//! Ok(_) => { /* do something */ },
42//! Err(err) => panic!("{}", err),
43//! }
44//! }
45//!
46//! fn main() {
47//! let ctx = &IoContext::new().unwrap();
48//!
49//! let ep = TcpEndpoint::new(IpAddrV4::loopback(), 12345);
50//! let sv = TcpListener::new(ctx, ep.protocol()).unwrap();
51//! sv.set_option(ReuseAddr::new(true)).unwrap();
52//! sv.bind(&ep).unwrap();
53//! sv.listen().unwrap();
54//! let sv = Arc::new(Mutex::new(sv));
55//! sv.lock().unwrap().async_accept(wrap(on_accept, &sv));
56//!
57//! let cl = Arc::new(Mutex::new(TcpSocket::new(ctx, ep.protocol()).unwrap()));
58//! cl.lock().unwrap().async_connect(&ep, wrap(on_connect, &cl));
59//!
60//! ctx.run();
61//! }
62//! ```
63//!
64//! For example, TCP connection with coroutine code:
65//!
66//! ```
67//! use asyncio::*;
68//! use asyncio::ip::*;
69//! use asyncio::socket_base::*;
70//!
71//! fn main() {
72//! let ctx = &IoContext::new().unwrap();
73//!
74//! let ep = TcpEndpoint::new(IpAddrV4::loopback(), 12345);
75//! let mut sv = TcpListener::new(ctx, ep.protocol()).unwrap();
76//! sv.set_option(ReuseAddr::new(true)).unwrap();
77//! sv.bind(&ep).unwrap();
78//! sv.listen().unwrap();
79//!
80//! IoContext::spawn(ctx, move |co| {
81//! let (soc, ep) = sv.async_accept(co.wrap()).unwrap();
82//! /* do something */
83//! });
84//!
85//! IoContext::spawn(ctx, move |co| {
86//! let mut cl = TcpSocket::new(co.as_ctx(), ep.protocol()).unwrap();
87//! cl.async_connect(&ep, co.wrap()).unwrap();
88//! /* do something */
89//! });
90//!
91//! ctx.run();
92//! }
93//! ```
94//!
95
96#![allow(dead_code)]
97
98#[macro_use] extern crate bitflags;
99#[macro_use] extern crate lazy_static;
100extern crate kernel32;
101extern crate winapi;
102extern crate libc;
103extern crate ws2_32;
104extern crate errno;
105#[cfg(feature = "context")] extern crate context;
106#[cfg(feature = "termios")] extern crate termios;
107#[cfg(feature = "openssl")] extern crate openssl;
108#[cfg(feature = "openssl-sys")] extern crate openssl_sys;
109#[cfg(feature = "test")] extern crate test;
110
111macro_rules! libc_try {
112 ($expr:expr) => (
113 match unsafe { $expr } {
114 rc if rc >= 0 => rc,
115 _ => return Err(::std::io::Error::last_os_error()),
116 })
117}
118
119macro_rules! libc_unwrap {
120 ($expr:expr) => (
121 match unsafe { $expr } {
122 rc if rc >= 0 => rc,
123 _ => panic!("{}", ::std::io::Error::last_os_error()),
124 }
125 )
126}
127
128#[cfg(debug_assertions)]
129macro_rules! libc_ign {
130 ($expr:expr) => (
131 if unsafe { $expr } < 0 {
132 panic!("{}", ::std::io::Error::last_os_error());
133 }
134 )
135}
136
137#[cfg(not(debug_assertions))]
138macro_rules! libc_ign {
139 ($expr:expr) => (
140 let _ = unsafe { $expr };
141 )
142}
143
144mod unsafe_cell;
145
146mod prelude;
147pub use self::prelude::*;
148
149mod ffi;
150pub use self::ffi::{RawFd, AsRawFd};
151
152mod error;
153
154pub mod socket_base;
155
156mod buffers;
157pub use self::buffers::StreamBuf;
158
159mod core;
160pub use self::core::{IoContext, AsIoContext, IoContextWork, Socket};
161
162mod async;
163pub use self::async::{Handler, Strand, StrandImmutable, wrap};
164#[cfg(feature = "context")] pub use self::async::Coroutine;
165
166mod reactive_io;
167
168mod streams;
169pub use self::streams::{Stream, MatchCondition,
170 //read_until, write_until, async_read_until, async_write_until
171};
172
173mod stream_socket;
174pub use self::stream_socket::StreamSocket;
175
176mod dgram_socket;
177pub use self::dgram_socket::DgramSocket;
178
179mod raw_socket;
180pub use self::raw_socket::RawSocket;
181
182mod seq_packet_socket;
183pub use self::seq_packet_socket::SeqPacketSocket;
184
185mod socket_listener;
186pub use self::socket_listener::SocketListener;
187
188pub mod ip;
189
190mod from_str;
191
192#[cfg(unix)] pub mod local;
193
194#[cfg(unix)] pub mod posix;
195
196pub mod generic;
197
198#[cfg(feature = "termios")] pub mod serial_port;
199
200//#[cfg(feature = "openssl")] pub mod ssl;
201
202#[cfg(target_os = "linux")] mod signal_set;
203#[cfg(target_os = "linux")] pub use self::signal_set::{Signal, SignalSet, raise};
204
205pub mod clock;
206
207mod waitable_timer;
208pub use self::waitable_timer::{WaitableTimer, SteadyTimer, SystemTimer};