From fe39cd6b7fcc373e89df3357ce8d0c26c709744d Mon Sep 17 00:00:00 2001 From: "Join.G" Date: Mon, 17 Jun 2024 18:44:06 +0800 Subject: [PATCH] feat: support websocket configure by adding the configure item --- boot.json | 1 + crates/wasi/src/emulator.rs | 8 +++++--- crates/wasi/src/setting.rs | 5 ++++- crates/wasi/src/ws_thr.rs | 13 ++++++++++--- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/boot.json b/boot.json index 63c70f1..c1e4ab2 100644 --- a/boot.json +++ b/boot.json @@ -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"], diff --git a/crates/wasi/src/emulator.rs b/crates/wasi/src/emulator.rs index aff0188..b57838b 100644 --- a/crates/wasi/src/emulator.rs +++ b/crates/wasi/src/emulator.rs @@ -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) }; + let store: StoreT = unsafe { + Weak::from_raw(store_cl as *const Store) + }; let mut jit_worker = JitWorker { sender: rs_tx, recv: rx, @@ -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(); }); diff --git a/crates/wasi/src/setting.rs b/crates/wasi/src/setting.rs index 19f2bbe..82cc8c9 100644 --- a/crates/wasi/src/setting.rs +++ b/crates/wasi/src/setting.rs @@ -20,6 +20,7 @@ pub struct Setting { pub(crate) tun_netmask: Option, pub(crate) tun_ether_addr: Option, 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, @@ -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, } @@ -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"] { diff --git a/crates/wasi/src/ws_thr.rs b/crates/wasi/src/ws_thr.rs index c23f8dc..aab9009 100644 --- a/crates/wasi/src/ws_thr.rs +++ b/crates/wasi/src/ws_thr.rs @@ -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)>)>; + pub(crate) struct WsThread { - sender: Sender<(Receiver<(u16, BusData)>, Sender<(u16, Vec)>)>, + sender: SenderType, + port: u32, } impl WsThread { - pub fn new(sender: Sender<(Receiver<(u16, BusData)>, Sender<(u16, Vec)>)>) -> Self { + pub fn new( + sender: SenderType, + port: u32, + ) -> Self { Self { sender, + port, } } @@ -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;