From: jxnshi Date: Sun, 23 Feb 2025 19:27:27 +0000 (+0100) Subject: Fix response X-Git-Url: https://jxnshi.xyz/repos?a=commitdiff_plain;h=537ec93d7d1144147768e81c538af35ac3b1dcd8;p=mesange.git Fix response --- diff --git a/client-cli/client-cli b/client-cli/client-cli index 3945078..b281996 100755 Binary files a/client-cli/client-cli and b/client-cli/client-cli differ diff --git a/client-cli/command.odin b/client-cli/command.odin index 48cd8ad..3f7b51c 100644 --- a/client-cli/command.odin +++ b/client-cli/command.odin @@ -1,8 +1,8 @@ package main import "core:encoding/base64" -import "core:log" import "core:math/rand" +import "core:mem" import "core:net" import "core:os" import "core:slice" @@ -104,8 +104,14 @@ handle_command :: proc(app: ^App, command: string) -> Maybe(Handle_Command_Error } } + room_name := room_get_name(&room) + + room_name_bytes: [common.ROOM_NAME_SIZE]u8 + mem.copy_non_overlapping(raw_data(room_name_bytes[:]), raw_data(room_name), len(room_name)) + request := common.Subscribe_Request{ room_id = room_get_id(&room), + room_name = room_name_bytes, } err := request_host(app, request, &app.profile.private_key, nil) @@ -128,7 +134,7 @@ handle_command :: proc(app: ^App, command: string) -> Maybe(Handle_Command_Error room_name := args[0] - key: [ROOM_KEY_SIZE]u8 + key: [common.ROOM_KEY_SIZE]u8 _ = rand.read(key[:]) room, ok := room_init(room_name, key) @@ -138,8 +144,12 @@ handle_command :: proc(app: ^App, command: string) -> Maybe(Handle_Command_Error return .Invalid_Arguments } + room_name_bytes: [common.ROOM_NAME_SIZE]u8 + mem.copy_non_overlapping(raw_data(room_name_bytes[:]), raw_data(room_name), len(room_name)) + request := common.Subscribe_Request{ room_id = room_get_id(&room), + room_name = room_name_bytes, } callback :: proc(app: ^App, response: common.Server_Response) { diff --git a/client-cli/host.odin b/client-cli/host.odin index 1adc015..9326617 100644 --- a/client-cli/host.odin +++ b/client-cli/host.odin @@ -69,7 +69,7 @@ request_host :: proc( receive_host :: proc(app: ^App) -> (id: u32, message: common.Server_Message, err: common.Error) { buffer: [1_000]u8 packet := common.receive_packet(app.host.?, buffer[:]) or_return - packet_buffer := common.buffer_from_slice(buffer[:]) + packet_buffer := common.buffer_from_slice(packet[:]) packet_stream := bytes.buffer_to_stream(&packet_buffer) return common.server_message_from_bytes(app.host.?, packet_stream) diff --git a/client-cli/main.odin b/client-cli/main.odin index 56bc79f..f33fd6b 100644 --- a/client-cli/main.odin +++ b/client-cli/main.odin @@ -181,7 +181,7 @@ app_update_room_list_window :: proc(app: ^App) { nc.wclear(room_list_window) - c_room_name_buffer: [ROOM_NAME_MAX_SIZE + 1]u8 + c_room_name_buffer: [common.ROOM_NAME_SIZE + 1]u8 for &room, i in app.profile.rooms { room_color := nc.COLOR_PAIR(3) @@ -715,7 +715,7 @@ app_reset_seed_phrase :: proc(app: ^App) { app.seed_phrase_checksum = entropy_hash[0] & 0b1111 } -app_room_key_exists :: proc(app: ^App, key: [ROOM_KEY_SIZE]u8) -> bool { +app_room_key_exists :: proc(app: ^App, key: [common.ROOM_KEY_SIZE]u8) -> bool { key := key for &room in app.profile.rooms { diff --git a/client-cli/profile.odin b/client-cli/profile.odin index 7ddaa03..2171c80 100644 --- a/client-cli/profile.odin +++ b/client-cli/profile.odin @@ -17,22 +17,19 @@ import "core:unicode/utf8" import chacha "core:crypto/chacha20poly1305" import fpath "core:path/filepath" -ROOM_NAME_LEN :: 30 -ROOM_NAME_SIZE :: ROOM_NAME_LEN * 4 +import "../common" -ROOM_KEY_SIZE :: 32 - -Room_Name :: small_array.Small_Array(ROOM_NAME_SIZE, u8) +Room_Name :: small_array.Small_Array(common.ROOM_NAME_SIZE, u8) Room :: struct { name: Room_Name, - key: [ROOM_KEY_SIZE]u8, + key: [common.ROOM_KEY_SIZE]u8, } -room_init :: proc(name: string, key: [ROOM_KEY_SIZE]u8) -> (Room, bool) { +room_init :: proc(name: string, key: [common.ROOM_KEY_SIZE]u8) -> (Room, bool) { _, name_len, _ := utf8.grapheme_count(name) - if name_len > ROOM_NAME_MAX_LEN { + if name_len > common.ROOM_NAME_LEN { return {}, false } @@ -53,7 +50,7 @@ room_init_with_key_string :: proc(name: string, key_string: string) -> (Room, bo return {}, false } - key_bytes: [ROOM_KEY_SIZE]u8 + key_bytes: [common.ROOM_KEY_SIZE]u8 key_bytes_buffer := bytes.Buffer{ buf = mem.buffer_from_slice(key_bytes[:]) } key_bytes_stream := bytes.buffer_to_stream(&key_bytes_buffer) diff --git a/client-cli/state.odin b/client-cli/state.odin index 5234672..a4d4a7f 100644 --- a/client-cli/state.odin +++ b/client-cli/state.odin @@ -546,7 +546,7 @@ state_room :: proc(app: ^App) { request := common.Push_Request { room_id = room_get_id(selected_room), - encrypted_message = encrypted_message, + message = encrypted_message, } err := request_host(app, request, &app.profile.private_key, nil) diff --git a/common/request.odin b/common/request.odin index 91af095..c74af9e 100644 --- a/common/request.odin +++ b/common/request.odin @@ -81,6 +81,9 @@ push_request_to_bytes :: proc(stream: io.Stream, request: Push_Request) -> Error return nil } +ROOM_NAME_LEN :: 30 +ROOM_NAME_SIZE :: ROOM_NAME_LEN * 4 + Subscribe_Request :: struct { room_id: Room_ID, room_name: [ROOM_NAME_SIZE]u8, @@ -292,9 +295,9 @@ server_response_deinit :: proc(response: Server_Response) { server_response_from_bytes :: proc(stream: io.Stream) -> (response: Server_Response, err: Error) { buffer: [1_000]u8 - _, _ = io.read(stream, buffer[:]) + bytes_read, _ := io.read(stream, buffer[:]) - response, _ = strings.clone_from_bytes(buffer[:]) + response, _ = strings.clone_from_bytes(buffer[:bytes_read]) return response, nil } @@ -306,11 +309,11 @@ Server_Message :: union { server_message_from_bytes :: proc(server: net.TCP_Socket, stream: io.Stream) -> (id: u32, message: Server_Message, err: Error) { // Message type. - message_type, _ := io.read_byte(stream) + message_type := io.read_byte(stream) or_return // Message ID. id_bytes: [size_of(u32)]u8 - _, _ = io.read(stream, id_bytes[:]) + _ = io.read(stream, id_bytes[:]) or_return id, _ = endian.get_u32(id_bytes[:], .Little) @@ -320,13 +323,13 @@ server_message_from_bytes :: proc(server: net.TCP_Socket, stream: io.Stream) -> // Response. case 1: - message, err = server_response_from_bytes(stream) + message = server_response_from_bytes(stream) or_return case: return 0, nil, Receive_Message_Error.Invalid_Type } - return id, message, err + return id, message, nil } receive_packet :: proc(socket: net.TCP_Socket, buffer: []u8) -> (packet: []u8, err: Error) { diff --git a/server/client.odin b/server/client.odin index db9820c..ea2bc7a 100644 --- a/server/client.odin +++ b/server/client.odin @@ -3,7 +3,6 @@ package main import "core:bytes" import "core:encoding/endian" import "core:io" -import "core:log" import "core:mem" import "core:net" import "core:slice" diff --git a/server/main.odin b/server/main.odin index 4dc9104..122c123 100644 --- a/server/main.odin +++ b/server/main.odin @@ -1,5 +1,4 @@ package main - import "base:runtime" import "core:crypto/ed25519" @@ -15,22 +14,44 @@ import "core:thread" import "../common" -Subscribtion :: struct { +Subscription :: struct { public_key: ed25519.Public_Key, - room_id: Room_ID, + room_id: common.Room_ID, room_name: [common.ROOM_NAME_SIZE]u8, } -Handle_Client_Data :: struct { - data_mutex: ^sync.Mutex, - db_mutex: ^sync.Mutex, +App :: struct { + data_mutex: sync.Mutex, + db_mutex: sync.Mutex, + + clients_sockets: map[net.Endpoint]net.TCP_Socket, + + subscriptions: [dynamic]Subscription, +} + +app_init :: proc() -> App { + return { + clients_sockets = make(map[net.Endpoint]net.TCP_Socket), - clients_sockets: ^map[net.Endpoint]net.TCP_Socket, + subscriptions = make([dynamic]Subscription), + } +} + +app_deinit :: proc(app: ^App) { + delete(app.subscriptions) + + for _, socket in app.clients_sockets { + net.close(socket) + } + + delete(app.clients_sockets) +} + +Handle_Client_Data :: struct { + app: ^App, client_socket: net.TCP_Socket, client_endpoint: net.Endpoint, - - subscribtions: [dynamic]Subscription, } handle_client :: proc(data: Handle_Client_Data) { @@ -41,10 +62,10 @@ handle_client :: proc(data: Handle_Client_Data) { defer { log.infof("Client %v disconnected.", client_endpoint_string) - sync.mutex_lock(data_mutex) - defer sync.mutex_unlock(data_mutex) + sync.mutex_lock(&app.data_mutex) + defer sync.mutex_unlock(&app.data_mutex) - delete_key(clients_sockets, client_endpoint) + delete_key(&app.clients_sockets, client_endpoint) } recv_buffer: [10_000]u8 @@ -88,7 +109,7 @@ handle_client :: proc(data: Handle_Client_Data) { log.infof("Received request: %v", request) - #partial switch inner in request.inner { + #partial switch &inner in request.inner { case common.Subscribe_Request: subscription := Subscription{ public_key = request.public_key, @@ -97,21 +118,21 @@ handle_client :: proc(data: Handle_Client_Data) { } { - sync.mutex_lock(&data_mutex) - defer sync.mutex_unlock(&data_mutex) + sync.mutex_lock(&app.data_mutex) + defer sync.mutex_unlock(&app.data_mutex) - append(&subscriptions, subscription) + append(&app.subscriptions, subscription) } case common.Unsubscribe_Request: - sync.mutex_lock(&data_mutex) - defer sync.mutex_unlock(&data_mutex) + sync.mutex_lock(&app.data_mutex) + defer sync.mutex_unlock(&app.data_mutex) - for subscription, i in subscriptions { + for &subscription, i in app.subscriptions { if ed25519.public_key_equal(&subscription.public_key, &request.public_key) && - subscription.room_id == inner.room_id + slice.equal(subscription.room_id[:], inner.room_id[:]) { - unordered_remove(&subscriptions, i) + unordered_remove(&app.subscriptions, i) break } } @@ -170,18 +191,8 @@ main :: proc() { log.infof("Server open on port %v.", server_endpoint.port) - clients_sockets := make(map[net.Endpoint]net.TCP_Socket) - - defer { - for _, socket in clients_sockets { - net.close(socket) - } - - delete(clients_sockets) - } - - data_mutex: sync.Mutex - db_mutex: sync.Mutex + app := app_init() + defer app_deinit(&app) handle_client_thread_context := runtime.default_context() handle_client_thread_context.logger = context.logger @@ -200,17 +211,14 @@ main :: proc() { log.infof("Accepted client from %v.", client_endpoint_string) { - sync.mutex_lock(&data_mutex) - defer sync.mutex_unlock(&data_mutex) + sync.mutex_lock(&app.data_mutex) + defer sync.mutex_unlock(&app.data_mutex) - clients_sockets[client_endpoint] = client_socket + app.clients_sockets[client_endpoint] = client_socket } client_handle_data := Handle_Client_Data{ - data_mutex = &data_mutex, - db_mutex = &db_mutex, - - clients_sockets = &clients_sockets, + app = &app, client_socket = client_socket, client_endpoint = client_endpoint, diff --git a/server/server b/server/server index 01c8b1b..06fc0c6 100755 Binary files a/server/server and b/server/server differ