Crate asyncio [] [src]

The asyncio is Asynchronous Input/Output library, that made based on boost::asio c++ library.

Usage

This crate is on github and can be used by adding asyncio to the dependencies in your project's Cargo.toml.

[dependencies]
rust_asio = "*"

And this in your crate root:

extern crate asyncio;

For example, TCP asynchronous connection code:

use asyncio::*;
use asyncio::ip::*;
use asyncio::socket_base::*;
use std::io;
use std::sync::Arc;

fn on_accept(sv: Arc<TcpListener>, res: io::Result<(TcpSocket, TcpEndpoint)>) {
  match res {
    Ok((soc, ep)) => { /* do something */ },
    Err(err) => panic!("{}", err),
  }
}

fn on_connect(cl: Arc<TcpSocket>, res: io::Result<()>) {
  match res {
    Ok(_) => { /* do something */ },
    Err(err) => panic!("{}", err),
  }
}

fn main() {
  let io = &IoService::new();

  let ep = TcpEndpoint::new(Tcp::v4(), 12345);
  let sv = Arc::new(TcpListener::new(io, ep.protocol()).unwrap());
  sv.set_option(ReuseAddr::new(true)).unwrap();
  sv.bind(&ep).unwrap();
  sv.listen().unwrap();
  sv.async_accept(wrap(on_accept, &sv));

  let cl = Arc::new(TcpSocket::new(io, ep.protocol()).unwrap());
  cl.async_connect(&ep, wrap(on_connect, &cl));

  io.run();
}

For example, TCP connection with coroutine code:

use asyncio::*;
use asyncio::ip::*;
use asyncio::socket_base::*;

fn main() {
  let io = &IoService::new();

  let ep = TcpEndpoint::new(Tcp::v4(), 12345);
  let sv = TcpListener::new(io, ep.protocol()).unwrap();
  sv.set_option(ReuseAddr::new(true)).unwrap();
  sv.bind(&ep).unwrap();
  sv.listen().unwrap();

  IoService::spawn(io, move |co| {
    let (soc, ep) = sv.async_accept(co.wrap()).unwrap();
    /* do something */
  });

  IoService::spawn(io, move |co| {
    let cl = TcpSocket::new(co.io_service(), ep.protocol()).unwrap();
    cl.async_connect(&ep, co.wrap()).unwrap();
    /* do something */
  });

  io.run();
}

Modules

clock
generic
ip
local
posix
serial_port
socket_base

Structs

Coroutine

Context object that represents the currently executing coroutine.

DgramSocket

Provides a datagram-oriented socket.

IoService

Provides core I/O functionality.

IoServiceWork

The class to delaying until the stop of IoService is dropped.

RawSocket

Provides a raw-oriented socket.

SeqPacketSocket

Provides a sequenced packet socket.

SocketListener

Provides an ability to accept new connections.

Strand

Provides serialized data and handler execution.

StrandImmutable

Provides immutable data and handler execution.

StreamBuf

Automatically resizing buffer.

StreamSocket

Provides a stream-oriented socket.

WaitableTimer

Provides waitable timer functionality.

Enums

Shutdown

Possible values which can be passed to the shutdown method.

Traits

Endpoint
FromRawFd
GetSocketOption
Handler
IoControl
IoObject

Traits to the associated with IoService.

MatchCondition
Protocol
SetSocketOption
SockAddr
SocketOption
Stream

Functions

async_read_until
async_write_until
read_until
wrap

Provides a Arc handler to asynchronous operation.

write_until

Type Definitions

SteadyTimer

The monotonic clock's timer.

SystemTimer

The system clock's timer.