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, Mutex};

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

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

fn main() {
  let ctx = &IoContext::new().unwrap();

  let ep = TcpEndpoint::new(IpAddrV4::loopback(), 12345);
  let sv = TcpListener::new(ctx, ep.protocol()).unwrap();
  sv.set_option(ReuseAddr::new(true)).unwrap();
  sv.bind(&ep).unwrap();
  sv.listen().unwrap();
  let sv = Arc::new(Mutex::new(sv));
  sv.lock().unwrap().async_accept(wrap(on_accept, &sv));

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

  ctx.run();
}

For example, TCP connection with coroutine code:

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

fn main() {
  let ctx = &IoContext::new().unwrap();

  let ep = TcpEndpoint::new(IpAddrV4::loopback(), 12345);
  let mut sv = TcpListener::new(ctx, ep.protocol()).unwrap();
  sv.set_option(ReuseAddr::new(true)).unwrap();
  sv.bind(&ep).unwrap();
  sv.listen().unwrap();

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

  IoContext::spawn(ctx, move |co| {
    let mut cl = TcpSocket::new(co.as_ctx(), ep.protocol()).unwrap();
    cl.async_connect(&ep, co.wrap()).unwrap();
    /* do something */
  });

  ctx.run();
}

Modules

clock
generic
ip
local
posix
socket_base

Structs

Coroutine

Context object that represents the currently executing coroutine.

DgramSocket

Provides a datagram-oriented socket.

IoContext
IoContextWork

The class to delaying until the stop of IoContext 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.

Traits

AsIoContext
AsRawFd

A trait to extract the raw unix file descriptor from an underlying object.

Endpoint
GetSocketOption
Handler
IoControl
MatchCondition
Protocol
SetSocketOption
SockAddr
Socket
SocketOption
Stream

Functions

wrap

Provides a Arc handler to asynchronous operation.

Type Definitions

RawFd

Raw file descriptors.

SteadyTimer

The monotonic clock's timer.

SystemTimer

The system clock's timer.