Skip to content

Commit

Permalink
[Lib] Use WasmEdge as runner and pass clippy test
Browse files Browse the repository at this point in the history
Signed-off-by: KernelErr <[email protected]>
  • Loading branch information
KernelErr authored and hydai committed Feb 13, 2022
1 parent 460e9a6 commit ee7ae9f
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 52 deletions.
5 changes: 4 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[build]
target="wasm32-wasi"
target="wasm32-wasi"

[target.wasm32-wasi]
runner = "wasmedge"
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasmedge_wasi_socket"
version = "0.3.0"
version = "0.3.1"
authors = [ "Yi <[email protected]>" ]
edition = "2021"
license = "Apache-2.0"
Expand All @@ -14,8 +14,5 @@ keywords = ["http", "tcp", "socket", "webassembly", "wasmedge"]
name = "wasmedge_wasi_socket"
path = "src/lib.rs"

[features]
std = []

[dependencies]
libc = "0.2.3"
25 changes: 13 additions & 12 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# WasmEdge WASI Socket

Here are some examples for running network socket programs in wasmedge. The applications are written in Rust.
Here are some examples for running network socket programs in WasmEdge. The applications are written in Rust.

## Prerequisites

Expand All @@ -10,16 +10,24 @@ You need to install [Rust](https://www.rust-lang.org/tools/install) and [WasmEdg

[See here](http_client/README.md)

## An non-blocking HTTP client example

[See here](nonblock_http_client)

## An HTTP server example

[See here](http_server/README.md)

## An HTTP server example with poll

[See here](poll_http_server)

## TCP Stream Example with WasmEdge

This is a example of using wasmedge as a socket client.
This is a example of using WasmEdge as a socket client.

```
rustup run nightly cargo build --example tcp_stream --target wasm32-wasi
cargo run --example tcp_stream
```

Set up a server on your localhost with [ncat](https://nmap.org/ncat).
Expand All @@ -28,7 +36,7 @@ Set up a server on your localhost with [ncat](https://nmap.org/ncat).
ncat -kvlp 1234
```

Copy wasm into wasmedge directory and run it. Wasmedge would send message "hello" to a server at `localhost:1234`.
Copy wasm into WasmEdge directory and run it. WasmEdge would send message "hello" to a server at `localhost:1234`.

```
$ cp <path-to-wasmedge_wasi_socket>/target/wasm32-unknown-unknown/debug/examples/tcp_stream.wasm <path-to-wasmedge>
Expand All @@ -54,14 +62,7 @@ hello
This is a example of using wasmedge as a socket server.

```
rustup run nightly cargo build --example tcp_listener --target wasm32-wasi
```

Copy wasm into wasmedge directory and run it. This should setup a tcp listener at `localhost:1234` in wasmedge.

```
cp <path-to-wasmedge_wasi_socket>/target/wasm32-unknown-unknown/debug/examples/tcp_listener.wasm <path-to-wasmedge>
./wasmedge --env PORT=1234 ./tcp_listener.wasm
cargo run --example tcp_listener
```

Set up a client on your localhost with [ncat](https://nmap.org/ncat).
Expand Down
42 changes: 19 additions & 23 deletions examples/get_addrinfo.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
#[cfg(not(feature = "std"))]
use wasmedge_wasi_socket::WasiAddrinfo;

fn main() {
#[cfg(not(feature = "std"))]
{
let node = String::from("google.com");
let service = String::from("http");
let hints: WasiAddrinfo = WasiAddrinfo::default();
let mut sockaddr = Vec::new();
let mut sockbuff = Vec::new();
let mut ai_canonname = Vec::new();
let addrinfo = WasiAddrinfo::get_addrinfo(
&node,
&service,
&hints,
10,
&mut sockaddr,
&mut sockbuff,
&mut ai_canonname,
)
.unwrap();
for i in 0..addrinfo.len() {
if addrinfo[i].ai_addrlen.ne(&0) {
println!("{:?}", sockbuff[i]);
}
let node = String::from("google.com");
let service = String::from("http");
let hints: WasiAddrinfo = WasiAddrinfo::default();
let mut sockaddr = Vec::new();
let mut sockbuff = Vec::new();
let mut ai_canonname = Vec::new();
let addrinfo = WasiAddrinfo::get_addrinfo(
&node,
&service,
&hints,
10,
&mut sockaddr,
&mut sockbuff,
&mut ai_canonname,
)
.unwrap();
for i in 0..addrinfo.len() {
if addrinfo[i].ai_addrlen.ne(&0) {
println!("{:?}", sockbuff[i]);
}
}
}
3 changes: 0 additions & 3 deletions examples/http_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use bytecodec::DecodeExt;
use httpcodec::{HttpVersion, ReasonPhrase, Request, RequestDecoder, Response, StatusCode};
use std::io::{Read, Write};
#[cfg(feature = "std")]
use std::net::{Shutdown, TcpListener, TcpStream};
#[cfg(not(feature = "std"))]
use wasmedge_wasi_socket::{Shutdown, TcpListener, TcpStream};

fn handle_http(req: Request<String>) -> bytecodec::Result<Response<String>> {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub struct WasiSockaddr {
}

impl WasiSockaddr {
pub fn new(family: AddressFamily, sa_data: &mut Vec<u8>) -> WasiSockaddr {
pub fn new(family: AddressFamily, sa_data: &mut [u8]) -> WasiSockaddr {
WasiSockaddr {
family,
sa_data_len: 14,
Expand Down
8 changes: 5 additions & 3 deletions src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::AsRawFd;
use std::{collections::HashMap, os::wasi::prelude::*};

trait AsPollFd {
fn push_sub(&self, subs: &mut Vec<Subscription>);
fn push_sub(&self) -> Vec<Subscription>;
}

/// Associates readiness events with FD
Expand Down Expand Up @@ -106,7 +106,7 @@ impl Poll {
}
let mut subs = Vec::with_capacity(self.selector.poll_fds.len());
for fd in self.selector.poll_fds.values() {
fd.push_sub(&mut subs);
subs.append(&mut fd.push_sub());
}
let mut oevents = vec![
WasiEvent {
Expand Down Expand Up @@ -201,7 +201,8 @@ pub struct PollFd {
}

impl AsPollFd for PollFd {
fn push_sub(&self, subs: &mut Vec<Subscription>) {
fn push_sub(&self) -> Vec<Subscription> {
let mut subs = Vec::new();
match self.interest {
Interest::Read => {
subs.push(Subscription {
Expand Down Expand Up @@ -254,5 +255,6 @@ impl AsPollFd for PollFd {
});
}
}
subs
}
}
6 changes: 1 addition & 5 deletions src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use core::mem::MaybeUninit;
pub type Size = usize;
pub type Filesize = u64;
pub type Timestamp = u64;

#[repr(transparent)]
#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Clockid(u32);
Expand Down Expand Up @@ -243,11 +244,6 @@ impl fmt::Display for Errno {
}
}

#[cfg(feature = "std")]
extern crate std;
#[cfg(feature = "std")]
impl std::error::Error for Errno {}

pub type Rights = u64;

pub type Fd = u32;
Expand Down

0 comments on commit ee7ae9f

Please sign in to comment.