Crate gjio [−] [src]
Asynchronous input and output.
Example
extern crate gj; extern crate gjio; use gj::{EventLoop, Promise}; use gjio::{AsyncRead, AsyncWrite, BufferPrefix, SocketStream}; fn echo(mut stream: SocketStream, buf: Vec<u8>) -> Promise<(), ::std::io::Error> { stream.try_read(buf, 1).then(move |(buf, n)| { if n == 0 { // EOF Promise::ok(()) } else { stream.write(BufferPrefix::new(buf, n)).then(move |prefix| { echo(stream, prefix.buf) }) } }) } fn main() { EventLoop::top_level(|wait_scope| -> Result<(), ::std::io::Error> { let mut event_port = try!(gjio::EventPort::new()); let network = event_port.get_network(); let mut listen_address = network.get_tcp_address( ::std::str::FromStr::from_str("127.0.0.1:0").unwrap()); let listener = try!(listen_address.listen()); let connect_address = network.get_tcp_address(try!(listener.local_addr())); let promise1 = listener.accept().then(move |stream| { echo(stream, vec![0;5]) // Tiny buffer just to be difficult }); let promise2 = connect_address.connect().then(move |mut stream| { stream.write(b"hello world").then(move |_| { stream.read(vec![0; 11], 11).map(|(buf, _)| { assert_eq!(buf, b"hello world"); Ok(()) }) }) }); let all = Promise::all(vec![promise1, promise2].into_iter()); try!(all.wait(wait_scope, &mut event_port)); Ok(()) }).expect("top level"); }
Structs
BufferPrefix |
Wrapper around an owned buffer, exposing some number of initial bytes. |
EventPort |
Source of events from the outside world. Implements |
Network |
Mediates the creation of async-enabled sockets. |
SocketAddress |
An address to which the application may connect or on which the application may listen. |
SocketListener |
A server socket that can accept connections. |
SocketStream |
A connected socket that allows reading and writing. |
Timer |
Allows scheduling of timeouts. |
Traits
AsyncRead |
A nonblocking input bytestream. |
AsyncWrite |
A nonblocking output bytestream. |