state, fixes

This commit is contained in:
jusax23 2023-10-02 00:24:13 +02:00
parent d013ffebbb
commit 7bf1e76329
Signed by: jusax23
GPG key ID: 499E2AA870C1CD41
4 changed files with 59 additions and 18 deletions

View file

@ -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,7 +32,7 @@ function ws_open(ptr, len) {
try {
conn = {
socket: new WebSocket(url),
state: 0,
state: WS_DISCONNECTED,
received: []
};
} catch (error) {
@ -40,7 +40,7 @@ function ws_open(ptr, len) {
}
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;
}

View file

@ -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,
}
@ -26,4 +33,17 @@ impl QuadWs {
pub fn read(&mut self) -> Option<Vec<u8>> {
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) }
}
}

View file

@ -103,14 +103,30 @@ pub fn ws_read_rust(socket: &mut WsChannnel) -> Option<Vec<u8>> {
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
}
}
}

View file

@ -11,32 +11,37 @@ extern "C" {
fn ws_close(id: WsChannnel);
}
pub fn ws_open_rust(url: String) -> Result<WsChannnel, _> {
pub fn ws_open_rust(url: String) -> Option<WsChannnel> {
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<u8>) -> bool {
pub fn ws_write_rust(socket: &mut WsChannnel, data: Vec<u8>) -> 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<Vec<u8>> {
let available = unsafe { ws_available(socket) };
pub fn ws_read_rust(socket: &mut WsChannnel) -> Option<Vec<u8>> {
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;
}