3 releases

Uses old Rust 2015

0.1.2 Apr 15, 2018
0.1.1 Apr 15, 2018
0.1.0 Apr 14, 2018

#20 in #ice

MIT license

26KB
811 lines

The runtime for writing applications for Ice, an efficient, reliable and asynchronous platform for building modern backend applications in WebAssembly.

At a high level, ia (which stands for "Ice App") provides a few major components (based on the underlying Ice Core engine):

  • Asynchronous TCP server and client
  • File I/O
  • Timer (not working for now due to an Ice bug)

The asynchronous APIs are based on futures, while low-level callback-based APIs are also provided.

Examples

A simple TCP proxy that forwards 127.0.0.1:1111 to 127.0.0.1:80:

#![feature(proc_macro, generators)]

#[macro_use]
extern crate ia;
extern crate futures_await as futures;

use futures::prelude::*;
use ia::net::{TcpListener, TcpConnection};
use ia::error::IoResult;

#[async]
fn handle_connection(incoming: TcpConnection) -> IoResult<()> {
    #[async]
    fn forward(from: TcpConnection, to: TcpConnection) -> IoResult<()> {
        while let Ok(v) = await!(from.read(4096)) {
            if v.len() == 0 {
                break;
            }
            await!(to.write(v))?;
        }
        Ok(())
    }
    let proxied = await!(TcpConnection::connect("127.0.0.1:80"))?;
    ia::spawn(forward(proxied.clone(), incoming.clone()));
    await!(forward(incoming, proxied))?;

    Ok(())
}

#[async]
fn run_proxy() -> IoResult<()> {
    static LISTEN_ADDR: &'static str = "127.0.0.1:1111";
    let listener = TcpListener::new(LISTEN_ADDR);
    println!("Listening on {}", LISTEN_ADDR);

    #[async]
    for incoming in listener {
        ia::spawn(handle_connection(incoming));
    }

    Ok(())
}

app_init!({
    ia::spawn(run_proxy());
    0
});

See simpleproxy for the full code & project layout.

Dependencies

~53KB