From: jxnshi Date: Sat, 11 Jan 2025 22:58:05 +0000 (+0100) Subject: Set connect to host as non blocking X-Git-Url: https://jxnshi.xyz/repos?a=commitdiff_plain;h=f3ed846dd52a9825b75ab277d1146d22d5eb1be3;p=mesange.git Set connect to host as non blocking --- diff --git a/client-cli/main.odin b/client-cli/main.odin index 4549745..98739b4 100644 --- a/client-cli/main.odin +++ b/client-cli/main.odin @@ -57,7 +57,8 @@ App :: struct { seed_phrase_entropy: u128, seed_phrase_checksum: u8, - host: net.TCP_Socket, + host: Maybe(net.TCP_Socket), + connection_thread: Maybe(^thread.Thread), } app_init :: proc(storage_path: string) -> App { @@ -432,5 +433,18 @@ main :: proc() { time.sleep(1_000_000) free_all(context.temp_allocator) } + + #partial switch app.state { + case .Main: + { + sync.mutex_lock(&app.mutex) + defer sync.mutex_unlock(&app.mutex) + + if app.host != nil && app.connection_thread != nil { + thread.destroy(app.connection_thread) + app.connection_thread = nil + } + } + } } } diff --git a/client-cli/state.odin b/client-cli/state.odin index d50fecd..7fdd862 100644 --- a/client-cli/state.odin +++ b/client-cli/state.odin @@ -381,13 +381,8 @@ state_ask_profile_confirm_password :: proc(app: ^App) { app_set_state(app, .Connect_To_Host) } -state_connect_to_host :: proc(app: ^App) { - app_set_box_message( - app, - { - "Connecting to host.", - } - ) +handle_connection_to_host :: proc(app: ^App) { + app_set_info_bar(app, "Connecting to host...") host_endpoint: net.Endpoint host_endpoint.port = DEFAULT_PORT @@ -424,6 +419,22 @@ state_connect_to_host :: proc(app: ^App) { app.host = host } +} + +state_connect_to_host :: proc(app: ^App) { + app_set_box_message( + app, + { + "Connecting to host.", + } + ) + + { + sync.mutex_lock(&app.mutex) + defer sunc.mutex_unlock(&app.mutex) + + app.connection_thread = thread.create_and_start_with_poly_data(app, handle_connection_to_host, context) + } app_set_state(app, .Main) } diff --git a/common/request.odin b/common/request.odin index 8e49086..3a16e20 100644 --- a/common/request.odin +++ b/common/request.odin @@ -4,29 +4,12 @@ import "core:crypto/ed25519" Public_Key_Bytes :: [32]u8 -Create_Room :: struct { - room_id: string, - public: bool, -} - -Kick_User :: struct { - user_public_key: ed25519.Public_Key, -} - -Ban_User :: struct { - user_public_key: ed25519.Public_Key, -} - Message :: struct { room_id: string, content: string, } Client_Request_Content :: union { - Create_Room, - Kick_User, - Ban_User, - Message, } diff --git a/server/main.odin b/server/main.odin index 06197b6..b88772a 100644 --- a/server/main.odin +++ b/server/main.odin @@ -26,8 +26,8 @@ handle_client :: proc(data: Handle_Client_Data) { defer { log.infof("Client %v disconnected.", client_endpoint_string) - sync.mutex_lock(clients_sockets_mutex) - defer sync.mutex_unlock(clients_sockets_mutex) + sync.mutex_lock(mutex) + defer sync.mutex_unlock(mutex) delete_key(clients_sockets, client_endpoint) } @@ -111,7 +111,7 @@ main :: proc() { } server_endpoint := net.Endpoint{ - address = net.IP4_Address{ 127, 0, 0, 1 }, + address = net.IP4_Address{ 0, 0, 0, 0 }, port = 3000, } @@ -174,7 +174,7 @@ main :: proc() { client_handle_data := Handle_Client_Data{ clients_sockets = &clients_sockets, - clients_sockets_mutex = &clients_sockets_mutex, + mutex = &mutex, client_socket = client_socket, client_endpoint = client_endpoint, diff --git a/server/server b/server/server index 5281b5a..0f3f285 100755 Binary files a/server/server and b/server/server differ