From 7bf1e76329a8bdb3faa56afe051fa013d1723b2b Mon Sep 17 00:00:00 2001 From: jusax23 Date: Mon, 2 Oct 2023 00:24:13 +0200 Subject: [PATCH] state, fixes --- js/quad-ws.js | 12 ++++++------ src/lib.rs | 22 +++++++++++++++++++++- src/pc.rs | 18 +++++++++++++++++- src/wasm.rs | 25 +++++++++++++++---------- 4 files changed, 59 insertions(+), 18 deletions(-) diff --git a/js/quad-ws.js b/js/quad-ws.js index dca3256..aff48ba 100644 --- a/js/quad-ws.js +++ b/js/quad-ws.js @@ -21,8 +21,8 @@ let ws = {}; let i = 0; const WS_NOT_EXISTING = -1; -const WS_CREATING = 0; -const WS_OPEN = 1; +const WS_DISCONNECTED = 0; +const WS_CONNECTED = 1; const WS_CLOSED = 2; function ws_open(ptr, len) { @@ -32,15 +32,15 @@ function ws_open(ptr, len) { try { conn = { socket: new WebSocket(url), - state: 0, + state: WS_DISCONNECTED, received: [] }; } catch (error) { return -1; } - + conn.socket.onopen = (s, e) => { - conn.state = WS_OPEN; + conn.state = WS_CONNECTED; }; conn.socket.onmessage = (s, e) => { conn.received.push(new Uint8Array(e.data.arraybuffer())); @@ -63,7 +63,7 @@ function ws_write(id, ptr, len) { for (let i = 0; i < len; i++) { data[i] = data_in[i]; }*/ - if (ws[id] != null) { + if (ws[id] != null && ws[id].socket.readyState == WebSocket.OPEN) { ws[id].socket.send(data); return true; } diff --git a/src/lib.rs b/src/lib.rs index d294f93..6a90d3d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,13 @@ pub mod pc; #[cfg(not(target_arch = "wasm32"))] use pc::*; +#[repr(i32)] +pub enum QuadWsState { + WsNotExisting = -1, + WsDisconnected = 0, + WsConnected = 1, + WsClosed = 2, +} pub struct QuadWs { channel: WsChannnel, } @@ -23,7 +30,20 @@ impl QuadWs { pub fn close(&mut self) { ws_close_rust(&mut self.channel) } - pub fn read(&mut self) -> Option>{ + pub fn read(&mut self) -> Option> { ws_read_rust(&mut self.channel) } + pub fn connected(&mut self) -> bool { + if let QuadWsState::WsConnected = self.state(){ + return true; + } + false + } + pub fn state(&mut self) -> QuadWsState { + let state = ws_state_rust(&mut self.channel); + if state < 0 || state > 2 { + return QuadWsState::WsClosed; + } + unsafe { ::std::mem::transmute(state) } + } } diff --git a/src/pc.rs b/src/pc.rs index 46c4cba..7e78ad5 100644 --- a/src/pc.rs +++ b/src/pc.rs @@ -103,14 +103,30 @@ pub fn ws_read_rust(socket: &mut WsChannnel) -> Option> { return None; } -pub fn ws_close_rust(socket: &WsChannnel) { +pub fn ws_close_rust(socket: &mut WsChannnel) { match &socket.client { WsClient::None => {} WsClient::Insecure(client) => { client.shutdown().unwrap(); + socket.client = WsClient::None; } WsClient::Secure(client) => { client.shutdown().unwrap(); + socket.client = WsClient::None; } } } + +pub fn ws_state_rust(socket: &mut WsChannnel) -> i32{ + match &socket.client { + WsClient::None => { + 2 + } + WsClient::Insecure(_) => { + 1 + } + WsClient::Secure(_) => { + 1 + } + } +} \ No newline at end of file diff --git a/src/wasm.rs b/src/wasm.rs index 76716db..8a72e9c 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -11,32 +11,37 @@ extern "C" { fn ws_close(id: WsChannnel); } -pub fn ws_open_rust(url: String) -> Result { +pub fn ws_open_rust(url: String) -> Option { let url = CString::new(url).unwrap(); let socket_id = unsafe { ws_open(url.as_ptr(), url.as_bytes().len() as u32) }; if socket_id < 0 { - Err(()) + None } else { - Ok(socket_id) + Some(socket_id) } } -pub fn ws_write_rust(socket: WsChannnel, data: Vec) -> bool { +pub fn ws_write_rust(socket: &mut WsChannnel, data: Vec) -> bool { let buf = data.as_slice(); - let succ = unsafe { ws_write(socket, buf.as_ptr(), buf.len() as u32) }; + let succ = unsafe { ws_write(*socket, buf.as_ptr(), buf.len() as u32) }; succ } -pub fn ws_read_rust(socket: WsChannnel) -> Option> { - let available = unsafe { ws_available(socket) }; +pub fn ws_read_rust(socket: &mut WsChannnel) -> Option> { + let available = unsafe { ws_available(*socket) }; if available < 0 { return None; } let mut buffer = vec![0; available as usize]; - unsafe { ws_read(socket, buffer.as_mut_ptr(), available as u32) }; + unsafe { ws_read(*socket, buffer.as_mut_ptr(), available as u32) }; return Some(buffer); } -pub fn ws_close_rust(socket: WsChannnel) { - unsafe { ws_close(socket) }; +pub fn ws_close_rust(socket: &mut WsChannnel) { + unsafe { ws_close(*socket) }; } + +pub fn ws_state_rust(socket: &mut WsChannnel) -> i32{ + let state = unsafe { ws_state(*socket) }; + return state; +} \ No newline at end of file