Crate asyncio [] [src]

The asyncio is Asynchronous Input/Output 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 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 sv = Arc::new(TcpListener::new(io, Tcp::v4()).unwrap());
  sv.set_option(ReuseAddr::new(true)).unwrap();
  let ep = TcpEndpoint::new(IpAddrV4::any(), 12345);
  sv.bind(&ep).unwrap();
  sv.listen().unwrap();
  sv.async_accept(wrap(on_accept, &sv));

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

  io.run();
}

Warnings

If use asynchronous function, MUST be wrapping in Arc, Strand or Coroutine.

Examples

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

fn good_example(soc: Arc<TcpListener>) {
  soc.async_accept(wrap(|soc: Arc<TcpListener>, res: io::Result<(TcpSocket, TcpEndpoint)>| {
    // OK
  }, &soc));
}

fn bad_example(soc: TcpListener, dummy: Arc<TcpListener>) {
  soc.async_accept(wrap(|soc: Arc<TcpListener>, res: io::Result<(TcpSocket, TcpEndpoint)>| {
    // Segmentation fault
  }, &dummy));
}

Examples

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

struct Client {
  soc: TcpSocket,
  buf: [u8; 256],
}

fn good_example(mut cl: Strand<Client>) {
  let buf = &mut cl.get().buf;

  cl.soc.async_read_some(buf, cl.wrap(|cl: Strand<Client>, res: io::Result<usize>| {
    // OK
  }));

  cl.soc.async_read_some(buf, cl.wrap(|cl: Strand<Client>, res: io::Result<usize>| {
    // OK
  }));
}

unsafe impl IoObject for Client {
  fn io_service(&self) -> &IoService { self.soc.io_service() }
}

fn bad_example(mut cl: Arc<Client>) {
  use std::slice;
  let buf = unsafe { slice::from_raw_parts_mut(cl.buf.as_ptr() as *mut _, cl.buf.len()) };

  cl.soc.async_read_some(buf, wrap(|cl: Arc<Client>, res: io::Result<usize>| {
    // Occurred data race for buf
  }, &cl));

  cl.soc.async_read_some(buf, wrap(|cl: Arc<Client>, res: io::Result<usize>| {
    // Occurred data race for buf
  }, &cl));
}

Modules

clock
generic
ip
local
posix
serial_port
socket_base

Structs

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.

SignalSet

Provides a signal handing.

SocketListener

Provides an ability to accept new connections.

Strand
StreamBuf
StreamSocket

Provides a stream-oriented socket.

WaitableTimer

Provides waitable timer functionality.

Enums

Shutdown

Possible values which can be passed to the shutdown method.

Signal

A list specifying POSIX categories of signal.

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
raise
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.