From: Brendan Hansen Date: Tue, 18 Jan 2022 04:11:30 +0000 (-0600) Subject: random changes to networking X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=a3095436fb6857520cefeff8596474fd768fba08;p=onyx.git random changes to networking --- diff --git a/core/container/array.onyx b/core/container/array.onyx index 7b2928f3..33627f0d 100644 --- a/core/container/array.onyx +++ b/core/container/array.onyx @@ -68,6 +68,12 @@ ensure_capacity :: (arr: ^[..] $T, capacity: u32) -> bool { return true; } +alloc_one :: (arr: ^[..] $T) -> ^T { + if !ensure_capacity(arr, arr.count + 1) do return null; + arr.count += 1; + return ^arr.data[arr.count]; +} + push :: (arr: ^[..] $T, x: T) -> bool { if !ensure_capacity(arr, arr.count + 1) do return false; arr.data[arr.count] = x; diff --git a/core/net/net.onyx b/core/net/net.onyx index 9f2e8cdb..2610f927 100644 --- a/core/net/net.onyx +++ b/core/net/net.onyx @@ -9,6 +9,8 @@ Socket :: struct { handle: Handle; close :: socket_close + setting :: socket_setting + is_alive :: socket_is_alive bind :: socket_bind listen :: socket_listen accept :: socket_accept @@ -38,6 +40,7 @@ SocketType :: enum { } SocketSetting :: enum { + NonBlocking :: 0x01; } socket_create :: (domain: SocketDomain, type: SocketType) -> (Socket, SocketError) { @@ -57,6 +60,11 @@ socket_close :: (s: ^Socket) { } socket_setting :: (s: ^Socket, setting: SocketSetting, value: u32) { + __net_setting(s.handle, setting, value); +} + +socket_is_alive :: (s: ^Socket) -> bool { + return s.vtable != null; } socket_connect :: (s: ^Socket, host: str, port: u16) -> SocketError { @@ -81,8 +89,9 @@ socket_accept :: (s: ^Socket) -> Socket { return new_socket; } -socket_send :: (s: ^Socket, data: [] u8) -> u32 { +socket_send :: (s: ^Socket, data: [] u8) -> i32 { sent := __net_send(s.handle, data); + if sent < 0 { s.vtable = null; } return sent; } @@ -91,7 +100,8 @@ socket_sendall :: (s: ^Socket, data: [] u8) { while to_send.count > 0 { sent := __net_send(s.handle, to_send); - to_send = to_send[sent .. to_send.count]; + if sent < 0 { s.vtable = null; return; } + else do to_send = to_send[sent .. to_send.count]; } } @@ -105,7 +115,7 @@ socket_recv :: (s: ^Socket, maxlen := 1024, allocator := context.allocator) -> [ return result; } -socket_recv_into :: (s: ^Socket, buffer: [] u8) -> u32 { +socket_recv_into :: (s: ^Socket, buffer: [] u8) -> i32 { return __net_recv(s.handle, buffer); } @@ -133,12 +143,13 @@ socket_recv_into :: (s: ^Socket, buffer: [] u8) -> u32 { #foreign "onyx_runtime" { #package __net_create_socket :: (out_handle: ^Socket.Handle, domain: SocketDomain, type: SocketType) -> SocketError --- #package __net_close_socket :: (handle: Socket.Handle) -> void --- + #package __net_setting :: (handle: Socket.Handle, setting: SocketSetting, value: i32) -> void --- #package __net_bind :: (handle: Socket.Handle, port: u16) -> bool --- #package __net_listen :: (handle: Socket.Handle, backlog: i32) -> void --- #package __net_accept :: (handle: Socket.Handle) -> Socket.Handle --- // This should also return the address, but in what format? #package __net_connect :: (handle: Socket.Handle, host: str, port: u16) -> SocketError --- - #package __net_send :: (handle: Socket.Handle, data: [] u8) -> u32 --- - #package __net_recv :: (handle: Socket.Handle, data: [] u8) -> u32 --- + #package __net_send :: (handle: Socket.Handle, data: [] u8) -> i32 --- + #package __net_recv :: (handle: Socket.Handle, data: [] u8) -> i32 --- } #operator >= macro (a, b: Socket.Handle) => cast(u32) a >= cast(u32) b; diff --git a/modules/onyx_runtime/onyx_runtime.c b/modules/onyx_runtime/onyx_runtime.c index 5291f8bd..8ca930f0 100644 --- a/modules/onyx_runtime/onyx_runtime.c +++ b/modules/onyx_runtime/onyx_runtime.c @@ -818,6 +818,7 @@ bad_settings: ONYX_DEF(__net_close_socket, (WASM_I32), ()) { #ifdef _BH_LINUX + shutdown(params->data[0].of.i32, SHUT_RDWR); close(params->data[0].of.i32); #endif @@ -827,6 +828,27 @@ ONYX_DEF(__net_close_socket, (WASM_I32), ()) { return NULL; } +ONYX_DEF(__net_setting, (WASM_I32, WASM_I32, WASM_I32), ()) { + #ifdef _BH_LINUX + switch (params->data[1].of.i32) { + case 1: { // :EnumDependent Non-Blocking + int s = params->data[0].of.i32; + int flags = fcntl(s, F_GETFL, 0); + if (params->data[2].of.i32) { + flags |= O_NONBLOCK; + } else { + flags &= ~O_NONBLOCK; + } + + fcntl(s, F_SETFL, flags); + break; + } + } + #endif + + return NULL; +} + ONYX_DEF(__net_bind, (WASM_I32, WASM_I32), (WASM_I32)) { #ifdef _BH_LINUX @@ -906,7 +928,7 @@ ONYX_DEF(__net_connect, (WASM_I32, WASM_I32, WASM_I32, WASM_I32), (WASM_I32)) { ONYX_DEF(__net_send, (WASM_I32, WASM_I32, WASM_I32), (WASM_I32)) { #ifdef _BH_LINUX // TODO: The flags at the end should be controllable. - int sent = send(params->data[0].of.i32, ONYX_PTR(params->data[1].of.i32), params->data[2].of.i32, 0); + int sent = send(params->data[0].of.i32, ONYX_PTR(params->data[1].of.i32), params->data[2].of.i32, MSG_NOSIGNAL); results->data[0] = WASM_I32_VAL(sent); #endif @@ -957,6 +979,7 @@ ONYX_LIBRARY { ONYX_FUNC(__net_create_socket) ONYX_FUNC(__net_close_socket) + ONYX_FUNC(__net_setting) ONYX_FUNC(__net_bind) ONYX_FUNC(__net_listen) ONYX_FUNC(__net_accept)