From: jxnshi Date: Fri, 31 Jan 2025 15:55:49 +0000 (+0100) Subject: Update X-Git-Url: https://jxnshi.xyz/repos?a=commitdiff_plain;h=bbdf40b4f05b3a5bcd7eb5c3a930af551be1cff8;p=mesange.git Update --- diff --git a/client-cli/client-cli b/client-cli/client-cli index 4a7a161..04e6537 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 9382e8d..9a6482b 100644 --- a/client-cli/command.odin +++ b/client-cli/command.odin @@ -108,13 +108,15 @@ handle_command :: proc(app: ^App, command: string) -> Maybe(Handle_Command_Error room_id = room_get_id(&room), } - err := request_host(app.host.?, request, &app.profile.private_key) + request_id, err := request_host(app, request, &app.profile.private_key) if err != nil { app_set_info_bar(app, "Could not subscribe to room.") return .Command_Failed } + response := wait_host_response(app, request_id) + app_add_room(app, room) app_set_info_bar(app, "Room added.") @@ -142,16 +144,14 @@ handle_command :: proc(app: ^App, command: string) -> Maybe(Handle_Command_Error room_id = room_get_id(&room), } - err := request_host(app.host.?, request, &app.profile.private_key) + request_id, err := request_host(app, request, &app.profile.private_key) if err != nil { app_set_info_bar(app, "Could not subscribe to room.") return .Command_Failed } - buffer: [1_000]u8 - bytes_read, _ := net.recv_tcp(app.host.?, buffer[:]) - response := transmute(string)buffer[:bytes_read] + response := wait_host_response(app, request_id) log.infof("Response %v", response) @@ -196,16 +196,14 @@ handle_command :: proc(app: ^App, command: string) -> Maybe(Handle_Command_Error room_id = room_get_id(room), } - err := request_host(app.host.?, request, &app.profile.private_key) + request_id, err := request_host(app, request, &app.profile.private_key) if err != nil { app_set_info_bar(app, "Could not subscribe to room.") return .Command_Failed } - buffer: [1_000]u8 - bytes_read, _ := net.recv_tcp(app.host.?, buffer[:]) - response := transmute(string)buffer[:bytes_read] + response := wait_host_response(app, request_id) log.infof("Response %v", response) diff --git a/client-cli/host.odin b/client-cli/host.odin index 5a745fd..0d3c4d4 100644 --- a/client-cli/host.odin +++ b/client-cli/host.odin @@ -4,18 +4,24 @@ import "core:bytes" import "core:crypto/ed25519" import "core:encoding/endian" import "core:io" +import "core:math/rand" import "core:net" import "core:slice" +import "core:sync" import "core:time" import "../common" -request_host :: proc(host: net.TCP_Socket, inner: common.Client_Request_Inner, private_key: ^ed25519.Private_Key) -> (id: u32, err: Error) { - client_request: Client_Request - ed25519.public_key_set_priv(&client_request.public_key, private_key) - client_request.inner = inner - client_request.timestamp = time.time_to_unix_nano(time.now()) - client_request.signature = client_request_signature(client_request, private_key) +request_host :: proc( + app: ^App, + inner: common.Client_Request_Inner, + private_key: ^ed25519.Private_Key +) -> (id: u32, err: common.Error) { + request: common.Client_Request + ed25519.public_key_set_priv(&request.public_key, private_key) + request.inner = inner + request.timestamp = time.time_to_unix_nano(time.now()) + request.signature = common.client_request_signature(request, private_key) bytes_buf: [10_000]u8 bytes_buffer: bytes.Buffer @@ -31,7 +37,7 @@ request_host :: proc(host: net.TCP_Socket, inner: common.Client_Request_Inner, p _, _ = io.write(request_stream, id_bytes[:]) // Request. - _ = client_request_to_bytes(request_stream, request) + _ = common.client_request_to_bytes(request_stream, request) // Length. buffer_length := bytes.buffer_length(&bytes_buffer) @@ -43,11 +49,50 @@ request_host :: proc(host: net.TCP_Socket, inner: common.Client_Request_Inner, p request_bytes := bytes.buffer_to_bytes(&bytes_buffer) - _ = net.send_tcp(server, request_bytes) or_return + _ = net.send_tcp(app.host.?, request_bytes) or_return return id, nil } -receive_host :: proc(host: net.TCP_Socket) -> Message { - +receive_host :: proc(app: ^App) -> (message: common.Server_Message, err: common.Error) { + buffer: [1_000]u8 + packet := common.receive_packet(app.host.?) or_return + packet_buffer: bytes.Buffer + packet_buffer.buf = slice.into_dynamic(bytes_buf[:]) + packet_stream := bytes.buffer_to_stream(&bytes_buffer) + + // Message type. + message_type, _ := io.read_byte(packet_stream) + + // Message ID. + id_bytes: [size_of(u32)]u8 + _, _ = io.read(packet_stream, id_bytes[:]) + + id, _ := endian.get_u32(.Little, id_bytes[:]) + + switch message_type { + // Request. + case 0: + + // Response. + case 1: + + case: + return nil, common.Receive_Message_Error.Invalid_Type + } +} + +wait_host_response :: proc(app: ^App, id: u32) -> common.Server_Response { + for { + time.sleep(1_000_000) + + sync.mutex_lock(&app.mutex) + defer sync.mutex_unlock(&app.mutex) + + for response in app.responses { + if response.id == id { + return response + } + } + } } diff --git a/client-cli/main.odin b/client-cli/main.odin index 839e2fd..9a7cb14 100644 --- a/client-cli/main.odin +++ b/client-cli/main.odin @@ -62,7 +62,7 @@ App :: struct { seed_phrase_checksum: u8, host: Maybe(net.TCP_Socket), - waiting_respones: [dynamic]common.Server_Response, + waiting_responses: [dynamic]common.Server_Response, selected_room: int, } @@ -735,5 +735,17 @@ main :: proc() { time.sleep(1_000_000) free_all(context.temp_allocator) } + + server_message := receive_host(&app) + + #partial switch message in server_message { + case common.Server_Response: + response := message + + sync.mutex_lock(&app.mutex) + defer sync.mutex_unlock(&app.mutex) + + append(&app.waiting_responses, response) + } } } diff --git a/client-cli/state.odin b/client-cli/state.odin index a351d12..733182d 100644 --- a/client-cli/state.odin +++ b/client-cli/state.odin @@ -552,7 +552,7 @@ state_room :: proc(app: ^App) { encrypted_message = encrypted_message, } - err := common.request_server(app.host.?, request, &app.profile.private_key) + _, err := request_host(app, request, &app.profile.private_key) if err != nil { app_set_info_bar(app, "Failed to send message.") diff --git a/common/request.odin b/common/request.odin index 844fef1..9bfcfb7 100644 --- a/common/request.odin +++ b/common/request.odin @@ -16,6 +16,7 @@ ROOM_KEY_SIZE :: 32 MESSAGE_SIZE :: 1_000 * 4 Receive_Packet_Error :: enum { + Packet_Empty, Packet_Too_Large, } @@ -23,6 +24,10 @@ Receive_Request_Error :: enum { Invalid_Request, } +Receive_Message_Error :: enum { + Invalid_Type, +} + Client_Request_From_Bytes_Error :: enum { Invalid_Inner_Kind, Invalid_Public_Key, @@ -264,4 +269,42 @@ client_request_verify :: proc(request: Client_Request, expiration: Maybe(i64)) - return nil } -Server_Request :: union {} +Server_Request :: struct {} + +Server_Response_Inner :: union { + string, +} + +Server_Response :: struct { + id: u32, + inner: Server_Response_Inner, +} + +Server_Message :: union { + Server_Request, + Server_Response, +} + +receive_packet :: proc(socket: net.Socket, buffer: []u8) -> (packet: []u8, err: Error) { + length_bytes: [size_of(u32)]u8 + _ = net.recv_tcp(client, length_bytes[:]) or_return + + length, _ := endian.get_u32(length_bytes[:], .Little) + + if length == 0 { + return {}, common.Receive_Packet_Error.Packet_Empty + } + + if int(length) > len(buffer) { + // Empty the socket buffer. + for _ in 0.. (id: u32, request: common.Client_Request, err: common.Error) { - length_bytes: [size_of(u32)]u8 - _ = net.recv_tcp(client, length_bytes[:]) or_return - - length, _ := endian.get_u32(length_bytes[:], .Little) - - if length == 0 { - return 0, {}, nil - } - buffer: [10_000]u8 + packet := common.receive_packet(client, buffer[:]) or_return - if int(length) > len(buffer) { - // Empty the socket buffer. - for _ in 0.. commo response_stream := bytes.buffer_to_stream(&bytes_buffer) // Message type. - _ = io.write_byte(response_stream, 0) + _ = io.write_byte(response_stream, 1) // Reponse ID. id_bytes: [size_of(u32)]u8 diff --git a/server/server b/server/server index 92462e0..4970315 100755 Binary files a/server/server and b/server/server differ