Skip to content

Commit

Permalink
Alignment of final API set for v0 and minor changes to readme. (#33)
Browse files Browse the repository at this point in the history
* update: remove the debug bool in _water_init to match final API set of v0, and make logging default

* update: add m1 test in git action

* update: change macos-latest to macos-12 in action
  • Loading branch information
erikziyunchi authored Feb 3, 2024
1 parent b06c432 commit 3e542ee
Show file tree
Hide file tree
Showing 19 changed files with 22 additions and 50 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ jobs:
- name: Lint
run: cargo clippy --workspace --all-targets --verbose --all-features

test_amd64:
test_amd64_and_m1:
name: Test and Build WASM
needs: format
# needs: [format, lint]
strategy:
fail-fast: true
matrix:
os: [ "ubuntu-latest", "macos-latest" ]
os: [ "ubuntu-latest", "macos-12", "macos-14" ]

runs-on: ${{ matrix.os }}

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ If you quoted or used our work in your own project/paper/research, please cite o

The repo contains 2 main components:
1. A Rust crate [`water`](https://github.com/erikziyunchi/water-rs/tree/main/crates/water) runtime library used to interact with `.wasm` WebAssembly Transport Modules(WATM).
2. A Rust crate [`water_wasm`](https://github.com/erikziyunchi/water-rs/tree/main/crates/wasm) for WATM-development where developers can easily create their own `.wasm`.
2. A Rust crate [`watm_v0`](https://github.com/erikziyunchi/water-rs/tree/main/crates/watm_v0) for the v0 WATM-development where developers can easily create their own `.wasm` (the general version of `watm` is still under development).


Also include examples for demonstration of usage:
1. A standalone cli tool which can be used to load a `.wasm` WATM directly and run it. See [water-rs/tree/main/examples/clients/cli](https://github.com/erikziyunchi/water-rs/tree/main/examples/clients/cli).
1. A standalone [`cli`](https://github.com/erikziyunchi/water-rs/tree/main/examples/clients/cli) tool which is compatible with both `v0` and `v1_preview`, and can run all different roles with all of our example WATM binaries.
2. A few examples WATM implementations with `water_wasm` crate, see [water-rs/tree/main/examples/water_bins](https://github.com/erikziyunchi/water-rs/tree/main/examples/water_bins).
3. Examples of using the above WATM examples with our `water` library, see [tests](https://github.com/erikziyunchi/water-rs/tree/main/tests/tests) for usage.

Expand Down
5 changes: 2 additions & 3 deletions crates/water/src/runtime/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl H2O<Host> {
}

/// This function is called when the host wants to call _init() in WASM
pub fn _init(&mut self, debug: bool) -> Result<(), anyhow::Error> {
pub fn _init(&mut self, _debug: bool) -> Result<(), anyhow::Error> {
info!("[HOST] WATERCore calling _init from WASM...");

let store_lock_result = self.store.lock();
Expand All @@ -235,9 +235,8 @@ impl H2O<Host> {
};

// check if we need to pass in any arguments / configs later
let params = vec![Val::I32(debug as i32); init_fn.ty(&*store).params().len()];
let mut res = vec![Val::I64(0); init_fn.ty(&*store).results().len()];
match init_fn.call(&mut *store, &params, &mut res) {
match init_fn.call(&mut *store, &[], &mut res) {
Ok(_) => {}
Err(e) => return Err(anyhow::Error::msg(format!("init function failed: {}", e))),
}
Expand Down
Binary file modified examples/clients/cli/demo_wasm/echo_client.wasm
Binary file not shown.
Binary file modified examples/clients/cli/demo_wasm/plain.wasm
Binary file not shown.
Binary file modified examples/clients/cli/demo_wasm/reverse.wasm
Binary file not shown.
Binary file modified examples/clients/cli/demo_wasm/ss_client_wasm.wasm
Binary file not shown.
Binary file modified examples/water_bins/echo_client/echo_client.wasm
Binary file not shown.
17 changes: 3 additions & 14 deletions examples/water_bins/echo_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,10 @@ lazy_static! {
// static ref CONN: Mutex<Connection> = Mutex::new(Connection::new());
}

#[cfg(target_family = "wasm")]
#[export_name = "_water_init"]
pub fn _init(debug: bool) {
if debug {
tracing_subscriber::fmt().with_max_level(Level::INFO).init();
}

info!("[WASM] running in _init");
}

#[cfg(not(target_family = "wasm"))]
pub fn _init(debug: bool) {
if debug {
tracing_subscriber::fmt().with_max_level(Level::INFO).init();
}
pub fn _init() {
// default to have logging enabled
tracing_subscriber::fmt().with_max_level(Level::INFO).init();

info!("[WASM] running in _init");
}
Expand Down
Binary file modified examples/water_bins/plain_v0/plain.wasm
Binary file not shown.
16 changes: 6 additions & 10 deletions examples/water_bins/plain_v0/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use tokio::{
};
use v0plus::ConnPair;

use tracing::{info, Level};
// use tracing::{info, Level};

const READ_BUFFER_SIZE: usize = 1024; // 1KB is shorter than common MTU but longer than common TCP MSS

Expand All @@ -30,11 +30,7 @@ pub static VERSION: i32 = v0plus::VERSION;

// version-independent API
#[export_name = "_water_init"]
pub fn _init(debug: bool) -> i32 {
if debug {
tracing_subscriber::fmt().with_max_level(Level::INFO).init();
}

pub fn _init() -> i32 {
// do all the initializing work here AND pull config from host
sleep(Duration::from_millis(10)); // sleep for 10ms
error::Error::None.i32()
Expand Down Expand Up @@ -149,7 +145,7 @@ pub fn _cancel_with(fd: i32) -> i32 {
/// WASM Entry point here
#[export_name = "_water_worker"]
pub fn _worker() -> i32 {
info!("[WATM plain] worker: start");
println!("[WATM plain] worker: start");

// borrow CANCEL as &mut AsyncFdConn
let mut cancel = CANCEL.lock().unwrap();
Expand Down Expand Up @@ -222,7 +218,7 @@ async fn bidi_worker(
src: &mut common::AsyncFdConn,
cancel: &mut common::AsyncFdConn,
) -> std::io::Result<()> {
info!("[WATM plain] bidi_worker: start");
println!("[WATM plain] bidi_worker: start");

// upgrade to AsyncFdConn
dst.tokio_upgrade().expect("dst upgrade failed");
Expand Down Expand Up @@ -252,7 +248,7 @@ async fn bidi_worker(
return Err(e);
}

info!("[WATM plain] dst read {:?}", &dst_buf[0..n]);
println!("[WATM plain] dst read {:?}", &dst_buf[0..n]);
}
Err(e) => {
println!("Error reading from dst: {:?}", e);
Expand All @@ -271,7 +267,7 @@ async fn bidi_worker(
return Err(e);
}

info!("[WATM plain] src read {:?}", &src_buf[0..n]);
println!("[WATM plain] src read {:?}", &src_buf[0..n]);
}
Err(e) => {
println!("Error reading from src: {:?}", e);
Expand Down
Binary file modified examples/water_bins/reverse_v0/reverse.wasm
Binary file not shown.
8 changes: 3 additions & 5 deletions examples/water_bins/reverse_v0/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use tokio::{
};
use v0plus::ConnPair;

use tracing::Level;
// use tracing::Level;

const READ_BUFFER_SIZE: usize = 1024; // 1KB is shorter than common MTU but longer than common TCP MSS

Expand All @@ -30,10 +30,8 @@ pub static VERSION: i32 = v0plus::VERSION;

// version-independent API
#[export_name = "_water_init"]
pub fn _init(debug: bool) -> i32 {
if debug {
tracing_subscriber::fmt().with_max_level(Level::INFO).init();
}
pub fn _init() -> i32 {
// tracing_subscriber::fmt().with_max_level(Level::INFO).init();

// do all the initializing work here AND pull config from host
sleep(Duration::from_millis(10)); // sleep for 10ms
Expand Down
17 changes: 3 additions & 14 deletions examples/water_bins/ss_client_wasm_v1/src/water.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,10 @@ use bytes::{BufMut, BytesMut};
use shadowsocks_crypto::v1::openssl_bytes_to_key;
use std::sync::Arc;

#[cfg(target_family = "wasm")]
#[export_name = "_water_init"]
pub fn _init(debug: bool) {
if debug {
tracing_subscriber::fmt().with_max_level(Level::INFO).init();
}

info!("[WASM] running in _init");
}

#[cfg(not(target_family = "wasm"))]
pub fn _init(debug: bool) {
if debug {
tracing_subscriber::fmt().with_max_level(Level::INFO).init();
}
pub fn _init() {
// default to have logging enabled
tracing_subscriber::fmt().with_max_level(Level::INFO).init();

info!("[WASM] running in _init");
}
Expand Down
Binary file modified examples/water_bins/ss_client_wasm_v1/ss_client_wasm.wasm
Binary file not shown.
Binary file modified tests/test_wasm/echo_client.wasm
Binary file not shown.
Binary file modified tests/test_wasm/plain.wasm
Binary file not shown.
Binary file modified tests/test_wasm/reverse.wasm
Binary file not shown.
Binary file modified tests/test_wasm/ss_client_wasm.wasm
Binary file not shown.

0 comments on commit 3e542ee

Please sign in to comment.