Skip to content

Commit

Permalink
Fixed gracefull server close
Browse files Browse the repository at this point in the history
  • Loading branch information
hironichu committed Sep 24, 2023
1 parent d858f56 commit 54c220c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
1 change: 0 additions & 1 deletion examples/deno/wt_server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 7 additions & 5 deletions mod/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,19 @@ export class WebTransportServer extends EventEmitter<WebTransportServerEvents> {
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;
Expand Down
5 changes: 5 additions & 0 deletions mod/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
37 changes: 31 additions & 6 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {}
Expand Down

0 comments on commit 54c220c

Please sign in to comment.