3 个版本

使用旧的 Rust 2015

0.1.2 2018 年 4 月 15 日
0.1.1 2018 年 4 月 15 日
0.1.0 2018 年 4 月 14 日

#20 in #ice

MIT 许可证

26KB
811

是用于编写针对 Ice 应用程序的运行时,Ice 是一个高效、可靠且异步的平台,用于在 WebAssembly 中构建现代后端应用程序。

在较高层次上,ia(代表“Ice App”)提供了一些主要组件(基于底层 Ice Core 引擎)

  • 异步 TCP 服务器和客户端
  • 文件 I/O
  • 计时器(由于 Ice 的一个错误目前无法使用)

异步 API 基于 futures,同时提供基于回调的低级 API。

示例

一个简单的 TCP 代理,将 127.0.01:1111 转发到 127.0.01: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
});

请参阅 simpleproxy 了解完整代码和项目布局。

依赖项

~53KB