various addition for networking UDP
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 30 Mar 2022 21:09:29 +0000 (16:09 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 30 Mar 2022 21:09:29 +0000 (16:09 -0500)
core/container/array.onyx
core/net/net.onyx
include/astnodes.h
src/onyx_runtime.c
src/parser.c
src/types.c
tests/struct_use_pointer_member

index af39135e08535fa7f08bcbb944cfb1838b93cccb..dfaac276493bfabf50bfd574717082a62dd2dab7 100644 (file)
@@ -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;
index bec979887b76fc9641f5272842be6ebc77524837..5f2b89b01e24b92641102b99a5ec8a24c4d76303 100644 (file)
@@ -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;
index b43869ed101bbb325b1cc06fea1bb3cfd9d17bd1..4f052b136d1b4ccd86f6efdc8c224ac2983c1731 100644 (file)
@@ -905,6 +905,7 @@ struct AstStructType {
 
     b32 pending_type_is_valid : 1;
     b32 is_union              : 1;
+    b32 is_packed             : 1;
 };
 struct AstStructMember {
     AstTyped_base;
index d78f8b441aa12fa2d2c96e6a818606ed62aef26b..7e871e2beca24cb423a88d4c8adae2258d39bd83 100644 (file)
@@ -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 *) &params->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)
index ca0f90ccb1e97b112687f3f1feff655986434614..83b722cd76de0526d4f5619df4cdd14014c80815 100644 (file)
@@ -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;
index daf88fc8e64ba7a500ba378ec16c670eb79bffe9..981d5eb70d63624a6193cee5594ab58020836bc8 100644 (file)
@@ -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;
index 6d70f1cf45897b592898b850f3a610e9b11f6e77..2960ea4fff31e49111bbcea289a66bd4c681c9c4 100644 (file)
@@ -1,2 +1,2 @@
 Hello, I am Billy!
-Go away!! func[17]
+Go away!! func[16]