From: jxnshi Date: Fri, 21 Feb 2025 17:11:41 +0000 (+0100) Subject: Update X-Git-Url: https://jxnshi.xyz/repos?a=commitdiff_plain;h=edc27faf631d27656352d71867ae07453b4a37ea;p=mesange.git Update --- diff --git a/client-cli/.gitignore b/client-cli/.gitignore index 3f3f686..63c3678 100644 --- a/client-cli/.gitignore +++ b/client-cli/.gitignore @@ -1 +1 @@ -mesange-cli +client-cli diff --git a/client-cli/client-cli b/client-cli/client-cli index 2ef8885..3945078 100755 Binary files a/client-cli/client-cli and b/client-cli/client-cli differ diff --git a/client-cli/main.odin b/client-cli/main.odin index 61c8a9b..56bc79f 100644 --- a/client-cli/main.odin +++ b/client-cli/main.odin @@ -336,10 +336,12 @@ app_set_info_bar :: proc(app: ^App, format: string, args: ..any) { fmt.sbprintf(&info_bar_content_builder, format, ..args) - sync.mutex_lock(&app.data_mutex) - defer sync.mutex_unlock(&app.data_mutex) + { + sync.mutex_lock(&app.data_mutex) + defer sync.mutex_unlock(&app.data_mutex) - app.info_bar_content = strings.to_string(info_bar_content_builder) + app.info_bar_content = strings.to_string(info_bar_content_builder) + } } app_set_box_message :: proc(app: ^App, lines: []string) { @@ -414,6 +416,9 @@ app_update_info_bar :: proc(app: ^App) { nc.wattroff(app.info_bar, color) nc.wrefresh(app.info_bar) + + nc.wmove(app.input_window, 0, 0) + nc.wrefresh(app.input_window) } app_clear_box_message :: proc(app: ^App) { diff --git a/client-cli/profile.odin b/client-cli/profile.odin index 63eb2f2..7ddaa03 100644 --- a/client-cli/profile.odin +++ b/client-cli/profile.odin @@ -17,12 +17,12 @@ import "core:unicode/utf8" import chacha "core:crypto/chacha20poly1305" import fpath "core:path/filepath" -ROOM_NAME_MAX_LEN :: 30 -ROOM_NAME_MAX_SIZE :: ROOM_NAME_MAX_LEN * 4 +ROOM_NAME_LEN :: 30 +ROOM_NAME_SIZE :: ROOM_NAME_LEN * 4 ROOM_KEY_SIZE :: 32 -Room_Name :: small_array.Small_Array(ROOM_NAME_MAX_SIZE, u8) +Room_Name :: small_array.Small_Array(ROOM_NAME_SIZE, u8) Room :: struct { name: Room_Name, diff --git a/common/request.odin b/common/request.odin index 3264d07..91af095 100644 --- a/common/request.odin +++ b/common/request.odin @@ -51,13 +51,15 @@ Error :: union { net.Network_Error, } +Room_ID :: [sha3.DIGEST_SIZE_256]u8 + Push_Request :: struct { - room_id: [sha3.DIGEST_SIZE_256]u8, - encrypted_message: [MESSAGE_SIZE]u8, + room_id: Room_ID, + message: [MESSAGE_SIZE]u8, } push_request_from_bytes :: proc(stream: io.Stream) -> (request: Push_Request, err: Error) { - room_id: [sha3.DIGEST_SIZE_256]u8 + room_id: Room_ID _ = io.read(stream, room_id[:]) or_return encrypted_message: [MESSAGE_SIZE]u8 @@ -65,7 +67,7 @@ push_request_from_bytes :: proc(stream: io.Stream) -> (request: Push_Request, er return { room_id = room_id, - encrypted_message = encrypted_message, + message = encrypted_message, }, nil } @@ -74,38 +76,45 @@ push_request_to_bytes :: proc(stream: io.Stream, request: Push_Request) -> Error request := request _ = io.write(stream, request.room_id[:]) or_return - _ = io.write(stream, request.encrypted_message[:]) or_return + _ = io.write(stream, request.message[:]) or_return return nil } Subscribe_Request :: struct { - room_id: [sha3.DIGEST_SIZE_256]u8, + room_id: Room_ID, + room_name: [ROOM_NAME_SIZE]u8, } subscribe_request_from_bytes :: proc(stream: io.Stream) -> (request: Subscribe_Request, err: Error) { - room_id: [sha3.DIGEST_SIZE_256]u8 + room_id: Room_ID _ = io.read(stream, room_id[:]) or_return + room_name: [ROOM_NAME_SIZE]u8 + _ = io.read(stream, room_name[:]) or_return + return { room_id = room_id, + room_name = room_name, }, nil } subscribe_request_to_bytes :: proc(stream: io.Stream, request: Subscribe_Request) -> Error { request := request + _ = io.write(stream, request.room_id[:]) or_return + _ = io.write(stream, request.room_name[:]) or_return return nil } Unsubscribe_Request :: struct { - room_id: [sha3.DIGEST_SIZE_256]u8, + room_id: Room_ID, } unsubscribe_request_from_bytes :: proc(stream: io.Stream) -> (request: Unsubscribe_Request, err: Error) { - room_id: [sha3.DIGEST_SIZE_256]u8 + room_id: Room_ID _ = io.write(stream, request.room_id[:]) or_return return { diff --git a/server/main.odin b/server/main.odin index 9ab2064..4dc9104 100644 --- a/server/main.odin +++ b/server/main.odin @@ -2,6 +2,7 @@ package main import "base:runtime" +import "core:crypto/ed25519" import "core:encoding/endian" import "core:fmt" import "core:io" @@ -14,6 +15,12 @@ import "core:thread" import "../common" +Subscribtion :: struct { + public_key: ed25519.Public_Key, + room_id: Room_ID, + room_name: [common.ROOM_NAME_SIZE]u8, +} + Handle_Client_Data :: struct { data_mutex: ^sync.Mutex, db_mutex: ^sync.Mutex, @@ -22,6 +29,8 @@ Handle_Client_Data :: struct { client_socket: net.TCP_Socket, client_endpoint: net.Endpoint, + + subscribtions: [dynamic]Subscription, } handle_client :: proc(data: Handle_Client_Data) { @@ -80,6 +89,33 @@ handle_client :: proc(data: Handle_Client_Data) { log.infof("Received request: %v", request) #partial switch inner in request.inner { + case common.Subscribe_Request: + subscription := Subscription{ + public_key = request.public_key, + room_id = inner.room_id, + room_name = inner.room_name, + } + + { + sync.mutex_lock(&data_mutex) + defer sync.mutex_unlock(&data_mutex) + + append(&subscriptions, subscription) + } + + case common.Unsubscribe_Request: + sync.mutex_lock(&data_mutex) + defer sync.mutex_unlock(&data_mutex) + + for subscription, i in subscriptions { + if ed25519.public_key_equal(&subscription.public_key, &request.public_key) && + subscription.room_id == inner.room_id + { + unordered_remove(&subscriptions, i) + break + } + } + case: response = "Unhandled request type" continue @@ -144,7 +180,8 @@ main :: proc() { delete(clients_sockets) } - mutex: sync.Mutex + data_mutex: sync.Mutex + db_mutex: sync.Mutex handle_client_thread_context := runtime.default_context() handle_client_thread_context.logger = context.logger @@ -163,15 +200,17 @@ main :: proc() { log.infof("Accepted client from %v.", client_endpoint_string) { - sync.mutex_lock(&mutex) - defer sync.mutex_unlock(&mutex) + sync.mutex_lock(&data_mutex) + defer sync.mutex_unlock(&data_mutex) clients_sockets[client_endpoint] = client_socket } client_handle_data := Handle_Client_Data{ + data_mutex = &data_mutex, + db_mutex = &db_mutex, + clients_sockets = &clients_sockets, - mutex = &mutex, client_socket = client_socket, client_endpoint = client_endpoint, diff --git a/server/server b/server/server index b8df374..01c8b1b 100755 Binary files a/server/server and b/server/server differ