diff --git a/examples/deno/wt_server_test.ts b/examples/deno/wt_server_test.ts index 27a8070..51fe6af 100644 --- a/examples/deno/wt_server_test.ts +++ b/examples/deno/wt_server_test.ts @@ -11,7 +11,6 @@ async function sleep(msec: number) { Deno.test({ name: "Server startup/close" }, async () => { //generate a certificate - sleep(2); const server = new WebTransportServer("https://localhost:4433", { certFile: "./certs/localhost.crt", diff --git a/mod/server.ts b/mod/server.ts index 7ea828f..cb22272 100644 --- a/mod/server.ts +++ b/mod/server.ts @@ -100,17 +100,19 @@ export class WebTransportServer extends EventEmitter { console.info("[JS] SERVER CLOSE CALLED"); //free all the connections this.connections.forEach((conn, id) => { - if (conn.state != "closed" && this.#SRV_PTR && conn.pointer) { - window.WTLIB.symbols.proc_client_close( - conn.pointer, - ); + if (conn.state != "closed" && conn.pointer) { + // window.WTLIB.symbols.proc_client_close( + // conn.pointer, + // ); window.WTLIB.symbols.free_conn(conn.pointer); conn.state = "closed"; } this.connections.delete(id); }); if (this.#SRV_PTR) { - await window.WTLIB.symbols.proc_server_close(this.#SRV_PTR); + await window.WTLIB.symbols.proc_server_close_clients(this.#SRV_PTR); + window.WTLIB.symbols.proc_server_close(this.#SRV_PTR); + window.WTLIB.symbols.free_server(this.#SRV_PTR); } this.#CONNECTION_CB.close(); this.#SRV_PTR = undefined; diff --git a/mod/symbols.ts b/mod/symbols.ts index c0caaeb..4b994c4 100644 --- a/mod/symbols.ts +++ b/mod/symbols.ts @@ -22,6 +22,11 @@ export const symbols = { nonblocking: false, }, proc_server_close: { + parameters: ["pointer"], + result: "usize", + nonblocking: false, + }, + proc_server_close_clients: { parameters: ["pointer"], result: "usize", nonblocking: true, diff --git a/src/server.rs b/src/server.rs index 99c87ac..e15c1b8 100644 --- a/src/server.rs +++ b/src/server.rs @@ -127,6 +127,17 @@ pub unsafe extern "C" fn proc_server_listen( RUNTIME.spawn(async move { loop { + match server.state { + Some(true) => {} + Some(false) => { + println!("Server state is false, exiting"); + return; + } + None => { + println!("Server state is None, exiting"); + return; + } + } match server.handle_sess_in().await { Ok(conn) => { cb(conn); @@ -186,19 +197,33 @@ pub extern "C" fn proc_server_client_path( #[no_mangle] pub unsafe extern "C" fn proc_server_close(server_ptr: *mut WebTransportServer) -> usize { assert!(!server_ptr.is_null()); - println!("SERVER CLOSE CALLED"); let server = &mut *server_ptr; - server.state = Some(false); let endpoint = server.server.as_mut(); match endpoint { - Some(endpoint) => RUNTIME.block_on(async move { - endpoint.wait_idle().await; - }), + Some(endpoint) => { + endpoint.close(30, b"Server closing"); + } None => println!("Error closing server"), } 0 } - +#[no_mangle] +pub unsafe extern "C" fn proc_server_close_clients(server_ptr: *mut WebTransportServer) -> usize { + assert!(!server_ptr.is_null()); + println!("CLIENT CLOSE CALLED"); + let server = &mut *server_ptr; + server.state = Some(false); + let endpoint = server.server.as_mut(); + match endpoint { + Some(endpoint) => { + RUNTIME.block_on(async move { + endpoint.wait_idle().await; + }); + } + None => println!("Error closing clients connections"), + } + 0 +} //free all above once #[no_mangle] pub unsafe extern "C" fn free_all_server(_a: *mut WebTransportServer, _c: *mut Runtime) {}