From: Brendan Hansen Date: Wed, 30 Mar 2022 21:09:29 +0000 (-0500) Subject: various addition for networking UDP X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=7299e6b77f04aee8883e1df50978d2fb4d3b5858;p=onyx.git various addition for networking UDP --- diff --git a/core/container/array.onyx b/core/container/array.onyx index af39135e..dfaac276 100644 --- a/core/container/array.onyx +++ b/core/container/array.onyx @@ -150,6 +150,18 @@ concat :: (arr: ^[..] $T, other: [] T) { for ^o: other do push(arr, *o); } +filter :: macro (arr: ^[..] $T, body: Code) { + move := 0; + + while i := 0; i < arr.count - move { + defer i += 1; + + if !(#unquote body) do move += 1; + if move != 0 do arr.data[i] = arr.data[i + move]; + } + + arr.count -= move; +} fold_idx_elem :: (arr: [] $T, cmp: (T, T) -> bool) -> (i32, T) { idx := 0; diff --git a/core/net/net.onyx b/core/net/net.onyx index bec97988..5f2b89b0 100644 --- a/core/net/net.onyx +++ b/core/net/net.onyx @@ -7,6 +7,7 @@ Socket :: struct { use stream : io.Stream; handle: Handle; + type: SocketType; close :: socket_close setting :: socket_setting @@ -43,6 +44,7 @@ SocketType :: enum { SocketSetting :: enum { NonBlocking :: 0x01; + Broadcast :: 0x02; } // This structure is twenty bytes in size to accommodate IPV6 addresses. @@ -65,6 +67,7 @@ Socket_Address :: struct #size 20 { socket_create :: (domain: SocketDomain, type: SocketType) -> (Socket, SocketError) { s: Socket; + s.type = type; err := __net_create_socket(^s.handle, domain, type); if err == .None { @@ -137,7 +140,7 @@ socket_send :: (s: ^Socket, data: [] u8) -> i32 { socket_sendto :: (s: ^Socket, data: [] u8, addr: ^Socket_Address) -> i32 { sent := __net_sendto(s.handle, data, addr); - if sent < 0 { s.vtable = null; } + // if sent < 0 s.{ s.vtable = null; } return sent; } @@ -186,6 +189,14 @@ socket_recvfrom :: (s: ^Socket, buffer: [] u8) -> (Socket_Address, i32) { return sa, received; } +host_to_network :: #match {} +#match host_to_network (x: u16) => __net_host_to_net_s(x); +#match host_to_network (x: u32) => __net_host_to_net_l(x); + +network_to_host :: #match {} +#match network_to_host (x: u16) => __net_net_to_host_s(x); +#match network_to_host (x: u32) => __net_net_to_host_l(x); + #local __net_socket_vtable := io.Stream_Vtable.{ read = (use s: ^Socket, buffer: [] u8) -> (io.Error, u32) { if handle == 0 do return .BadFile, 0; @@ -226,6 +237,11 @@ socket_recvfrom :: (s: ^Socket, buffer: [] u8) -> (Socket_Address, i32) { #package __net_recv :: (handle: Socket.Handle, data: [] u8, async_would_block: ^bool) -> i32 --- #package __net_recvfrom :: (handle: Socket.Handle, data: [] u8, out_recv_addr: ^Socket_Address, async_would_block: ^bool) -> i32 --- #package __net_poll_recv :: (handle: [] Socket.Handle, timeout: i32, out_recv_indicies: ^i32) -> i32 --- + + #package __net_host_to_net_s :: (s: u16) -> u16 --- + #package __net_host_to_net_l :: (s: u32) -> u32 --- + #package __net_net_to_host_s :: (s: u16) -> u16 --- + #package __net_net_to_host_l :: (s: u32) -> u32 --- } #operator >= macro (a, b: Socket.Handle) => cast(u32) a >= cast(u32) b; diff --git a/include/astnodes.h b/include/astnodes.h index b43869ed..4f052b13 100644 --- a/include/astnodes.h +++ b/include/astnodes.h @@ -905,6 +905,7 @@ struct AstStructType { b32 pending_type_is_valid : 1; b32 is_union : 1; + b32 is_packed : 1; }; struct AstStructMember { AstTyped_base; diff --git a/src/onyx_runtime.c b/src/onyx_runtime.c index d78f8b44..7e871e2b 100644 --- a/src/onyx_runtime.c +++ b/src/onyx_runtime.c @@ -862,6 +862,12 @@ ONYX_DEF(__net_setting, (WASM_I32, WASM_I32, WASM_I32), ()) { fcntl(s, F_SETFL, flags); break; } + + case 2: { // :EnumDependent Broadcast + int s = params->data[0].of.i32; + setsockopt(s, SOL_SOCKET, SO_BROADCAST, (void *) ¶ms->data[2].of.i32, sizeof(int)); + break; + } } #endif @@ -1053,35 +1059,25 @@ ONYX_DEF(__net_poll_recv, (WASM_I32, WASM_I32, WASM_I32, WASM_I32), (WASM_I32)) return NULL; } -ONYX_DEF(__net_address_get_address, (WASM_I64, WASM_I32, WASM_I32), (WASM_I32)) { - #ifdef _BH_LINUX - int maximum_length = params->data[2].of.i32; - int address_len = 0; - - struct sockaddr_in *addr = (struct sockaddr_in *) params->data[0].of.i64; - if (addr == NULL) goto done; - - char *address = inet_ntoa(addr->sin_addr); - if (address) address_len = strnlen(address, maximum_length); - - memcpy(ONYX_PTR(params->data[1].of.i32), address, address_len); - -done: - results->data[0] = WASM_I32_VAL(address_len); - #endif +ONYX_DEF(__net_host_to_net_s, (WASM_I32), (WASM_I32)) { + results->data[0] = WASM_I32_VAL(htons(params->data[0].of.i32)); return NULL; } -ONYX_DEF(__net_address_get_port, (WASM_I64), (WASM_I32)) { - #ifdef _BH_LINUX - struct sockaddr_in *addr = (struct sockaddr_in *) params->data[0].of.i64; - if (addr == NULL) results->data[0] = WASM_I32_VAL(-1); - else results->data[0] = WASM_I32_VAL(addr->sin_port); - #endif +ONYX_DEF(__net_host_to_net_l, (WASM_I32), (WASM_I32)) { + results->data[0] = WASM_I32_VAL(htonl(params->data[0].of.i32)); return NULL; } +ONYX_DEF(__net_net_to_host_s, (WASM_I32), (WASM_I32)) { + results->data[0] = WASM_I32_VAL(ntohs(params->data[0].of.i32)); + return NULL; +} +ONYX_DEF(__net_net_to_host_l, (WASM_I32), (WASM_I32)) { + results->data[0] = WASM_I32_VAL(ntohl(params->data[0].of.i32)); + return NULL; +} // @@ -1166,6 +1162,10 @@ ONYX_LIBRARY { ONYX_FUNC(__net_recv) ONYX_FUNC(__net_recvfrom) ONYX_FUNC(__net_poll_recv) + ONYX_FUNC(__net_host_to_net_s) + ONYX_FUNC(__net_host_to_net_l) + ONYX_FUNC(__net_net_to_host_s) + ONYX_FUNC(__net_net_to_host_l) ONYX_FUNC(__cptr_make) ONYX_FUNC(__cptr_read) diff --git a/src/parser.c b/src/parser.c index ca0f90cc..83b722cd 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1928,6 +1928,8 @@ static AstStructType* parse_struct(OnyxParser* parser) { if (parse_possible_directive(parser, "union")) s_node->is_union = 1; + else if (parse_possible_directive(parser, "pack")) s_node->is_packed = 1; + else if (parse_possible_directive(parser, "align")) { AstNumLit* numlit = parse_int_literal(parser); if (numlit == NULL) return NULL; diff --git a/src/types.c b/src/types.c index daf88fc8..981d5eb7 100644 --- a/src/types.c +++ b/src/types.c @@ -390,7 +390,10 @@ Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) { } if (mem_alignment > alignment) alignment = mem_alignment; - bh_align(offset, mem_alignment); + + if (!s_node->is_packed) { + bh_align(offset, mem_alignment); + } token_toggle_end((*member)->token); if (shgeti(s_type->Struct.members, (*member)->token->text) != -1) { @@ -427,7 +430,10 @@ Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) { } alignment = bh_max(s_node->min_alignment, alignment); - bh_align(size, alignment); + if (!s_node->is_packed) { + bh_align(size, alignment); + } + size = bh_max(s_node->min_size, size); s_type->Struct.alignment = alignment; diff --git a/tests/struct_use_pointer_member b/tests/struct_use_pointer_member index 6d70f1cf..2960ea4f 100644 --- a/tests/struct_use_pointer_member +++ b/tests/struct_use_pointer_member @@ -1,2 +1,2 @@ Hello, I am Billy! -Go away!! func[17] +Go away!! func[16]