Skip to content

Commit

Permalink
Merge pull request #71 from blocklessnetwork/feature/ws-setting
Browse files Browse the repository at this point in the history
feat: support websocket configure by adding the configure item
  • Loading branch information
Joinhack authored Jun 17, 2024
2 parents 26ccb4c + fe39cd6 commit 16aba52
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions boot.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"bios_file": "arch/seabios.bin",
"vga_bios_file": "arch/vgabios.bin",
"wasm_file": "target/v86.wasm",
"ws_port": 8081,
"memory_size": 134217728,
"vga_memory_size": 8388608,
"cmdline": ["tsc=reliable mitigations=off random.trust_cpu=on"],
Expand Down
8 changes: 5 additions & 3 deletions crates/wasi/src/emulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ impl InnerEmulator {
.unwrap();
std::thread::spawn(move || {
let table = table;
let store: StoreT = unsafe { Weak::from_raw(store_cl as *const Store<Emulator>) };
let store: StoreT = unsafe {
Weak::from_raw(store_cl as *const Store<Emulator>)
};
let mut jit_worker = JitWorker {
sender: rs_tx,
recv: rx,
Expand All @@ -107,13 +109,13 @@ impl InnerEmulator {
self.table = Some(table);
self.jit_result_rx = Some(rs_rx);
self.bus = Some(BUS::new(store.clone()));

let ws_port = self.setting.ws_port;
//tx rx for term adapater
let (tx, rs) = channel(1);
std::thread::Builder::new()
.name("ws thread".to_string())
.spawn(move || {
let ws_thr = WsThread::new(tx);
let ws_thr = WsThread::new(tx, ws_port);
ws_thr.start();
});

Expand Down
5 changes: 4 additions & 1 deletion crates/wasi/src/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct Setting {
pub(crate) tun_netmask: Option<String>,
pub(crate) tun_ether_addr: Option<String>,
pub(crate) vga_memory_size: u32,
pub(crate) ws_port: u32,
pub(crate) memory_size: u32,
pub(crate) log_mask: u32,
pub(crate) fast_boot: bool,
Expand All @@ -42,6 +43,7 @@ impl Setting {
bzimage_file: None,
vga_bios_file: None,
tun_ether_addr: None,
ws_port: 8081,
vga_memory_size: 8 * 1024 * 1024,
memory_size: 128 * 1024 * 1024,
}
Expand All @@ -55,7 +57,8 @@ impl Setting {
setting.bios_file = setting_obj["bios_file"].as_str().map(|s| s.into());
setting.vga_bios_file = setting_obj["vga_bios_file"].as_str().map(|s| s.into());
setting.wasm_file = setting_obj["wasm_file"].as_str().map(|s| s.into());
setting.memory_size = setting_obj["memory_size"].as_u32().unwrap_or(128 * 1024 * 1024);
setting.wasm_file = setting_obj["wasm_file"].as_str().map(|s| s.into());
setting.ws_port = setting_obj["ws_port"].as_u32().unwrap_or(8081);
setting.vga_memory_size = setting_obj["vga_memory_size"].as_u32().unwrap_or( 8 * 1024 * 1024);
if setting_obj["cmdline"].is_array() {
setting.cmdline = match setting_obj["cmdline"] {
Expand Down
13 changes: 10 additions & 3 deletions crates/wasi/src/ws_thr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ use std::net::SocketAddr;
use tokio_tungstenite::{accept_async, tungstenite::{Error, Message}};
use tokio_tungstenite::WebSocketStream;

type SenderType = Sender<(Receiver<(u16, BusData)>, Sender<(u16, Vec<u8>)>)>;

pub(crate) struct WsThread {
sender: Sender<(Receiver<(u16, BusData)>, Sender<(u16, Vec<u8>)>)>,
sender: SenderType,
port: u32,
}

impl WsThread {
pub fn new(sender: Sender<(Receiver<(u16, BusData)>, Sender<(u16, Vec<u8>)>)>) -> Self {
pub fn new(
sender: SenderType,
port: u32,
) -> Self {
Self {
sender,
port,
}
}

Expand Down Expand Up @@ -109,7 +116,7 @@ impl WsThread {
.build()
.unwrap();
runtime.block_on(async move {
let listener = TcpListener::bind("127.0.0.1:9002").await.unwrap();
let listener = TcpListener::bind(format!("0.0.0.0:{}", self.port)).await.unwrap();
while let Ok((stream, _)) = listener.accept().await {
let peer_addr = stream.peer_addr().expect("connected streams should have a peer address");
self.accept_connection(peer_addr, stream).await;
Expand Down

0 comments on commit 16aba52

Please sign in to comment.