mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
net: rework errors to be cross-platform
This commit is contained in:
@@ -24,13 +24,67 @@ import "core:c"
|
||||
import win "core:sys/windows"
|
||||
import "core:time"
|
||||
|
||||
Socket_Option :: enum c.int {
|
||||
// bool: Whether the address that this socket is bound to can be reused by other sockets.
|
||||
// This allows you to bypass the cooldown period if a program dies while the socket is bound.
|
||||
Reuse_Address = win.SO_REUSEADDR,
|
||||
|
||||
// bool: Whether other programs will be inhibited from binding the same endpoint as this socket.
|
||||
Exclusive_Addr_Use = win.SO_EXCLUSIVEADDRUSE,
|
||||
|
||||
// bool: When true, keepalive packets will be automatically be sent for this connection. TODO: verify this understanding
|
||||
Keep_Alive = win.SO_KEEPALIVE,
|
||||
|
||||
// bool: When true, client connections will immediately be sent a TCP/IP RST response, rather than being accepted.
|
||||
Conditional_Accept = win.SO_CONDITIONAL_ACCEPT,
|
||||
|
||||
// bool: If true, when the socket is closed, but data is still waiting to be sent, discard that data.
|
||||
Dont_Linger = win.SO_DONTLINGER,
|
||||
|
||||
// bool: When true, 'out-of-band' data sent over the socket will be read by a normal net.recv() call, the same as normal 'in-band' data.
|
||||
Out_Of_Bounds_Data_Inline = win.SO_OOBINLINE,
|
||||
|
||||
// bool: When true, disables send-coalescing, therefore reducing latency.
|
||||
TCP_Nodelay = win.TCP_NODELAY,
|
||||
|
||||
// win.LINGER: Customizes how long (if at all) the socket will remain open when there
|
||||
// is some remaining data waiting to be sent, and net.close() is called.
|
||||
Linger = win.SO_LINGER,
|
||||
|
||||
// win.DWORD: The size, in bytes, of the OS-managed receive-buffer for this socket.
|
||||
Receive_Buffer_Size = win.SO_RCVBUF,
|
||||
|
||||
// win.DWORD: The size, in bytes, of the OS-managed send-buffer for this socket.
|
||||
Send_Buffer_Size = win.SO_SNDBUF,
|
||||
|
||||
// win.DWORD: For blocking sockets, the time in milliseconds to wait for incoming data to be received, before giving up and returning .Timeout.
|
||||
// For non-blocking sockets, ignored.
|
||||
// Use a value of zero to potentially wait forever.
|
||||
Receive_Timeout = win.SO_RCVTIMEO,
|
||||
|
||||
// win.DWORD: For blocking sockets, the time in milliseconds to wait for outgoing data to be sent, before giving up and returning .Timeout.
|
||||
// For non-blocking sockets, ignored.
|
||||
// Use a value of zero to potentially wait forever.
|
||||
Send_Timeout = win.SO_SNDTIMEO,
|
||||
|
||||
// bool: Allow sending to, receiving from, and binding to, a broadcast address.
|
||||
Broadcast = win.SO_BROADCAST,
|
||||
}
|
||||
|
||||
|
||||
Shutdown_Manner :: enum c.int {
|
||||
Receive = win.SD_RECEIVE,
|
||||
Send = win.SD_SEND,
|
||||
Both = win.SD_BOTH,
|
||||
}
|
||||
|
||||
@(init, private)
|
||||
ensure_winsock_initialized :: proc() {
|
||||
win.ensure_winsock_initialized()
|
||||
}
|
||||
|
||||
@(private)
|
||||
_create_socket :: proc(family: Address_Family, protocol: Socket_Protocol) -> (socket: Any_Socket, err: Network_Error) {
|
||||
_create_socket :: proc(family: Address_Family, protocol: Socket_Protocol) -> (socket: Any_Socket, err: Create_Socket_Error) {
|
||||
c_type, c_protocol, c_family: c.int
|
||||
|
||||
switch family {
|
||||
@@ -49,7 +103,7 @@ _create_socket :: proc(family: Address_Family, protocol: Socket_Protocol) -> (so
|
||||
|
||||
sock := win.socket(c_family, c_type, c_protocol)
|
||||
if sock == win.INVALID_SOCKET {
|
||||
err = Create_Socket_Error(win.WSAGetLastError())
|
||||
err = _create_socket_error()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -80,7 +134,7 @@ _dial_tcp_from_endpoint :: proc(endpoint: Endpoint, options := default_tcp_optio
|
||||
sockaddr := _endpoint_to_sockaddr(endpoint)
|
||||
res := win.connect(win.SOCKET(socket), &sockaddr, size_of(sockaddr))
|
||||
if res < 0 {
|
||||
err = Dial_Error(win.WSAGetLastError())
|
||||
err = _dial_error()
|
||||
close(socket)
|
||||
return {}, err
|
||||
}
|
||||
@@ -93,12 +147,12 @@ _dial_tcp_from_endpoint :: proc(endpoint: Endpoint, options := default_tcp_optio
|
||||
}
|
||||
|
||||
@(private)
|
||||
_bind :: proc(socket: Any_Socket, ep: Endpoint) -> (err: Network_Error) {
|
||||
_bind :: proc(socket: Any_Socket, ep: Endpoint) -> (err: Bind_Error) {
|
||||
sockaddr := _endpoint_to_sockaddr(ep)
|
||||
sock := any_socket_to_socket(socket)
|
||||
res := win.bind(win.SOCKET(sock), &sockaddr, size_of(sockaddr))
|
||||
if res < 0 {
|
||||
err = Bind_Error(win.WSAGetLastError())
|
||||
err = _bind_error()
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -117,17 +171,17 @@ _listen_tcp :: proc(interface_endpoint: Endpoint, backlog := 1000) -> (socket: T
|
||||
bind(sock, interface_endpoint) or_return
|
||||
|
||||
if res := win.listen(win.SOCKET(socket), i32(backlog)); res == win.SOCKET_ERROR {
|
||||
err = Listen_Error(win.WSAGetLastError())
|
||||
err = _listen_error()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@(private)
|
||||
_bound_endpoint :: proc(sock: Any_Socket) -> (ep: Endpoint, err: Network_Error) {
|
||||
_bound_endpoint :: proc(sock: Any_Socket) -> (ep: Endpoint, err: Listen_Error) {
|
||||
sockaddr: win.SOCKADDR_STORAGE_LH
|
||||
sockaddrlen := c.int(size_of(sockaddr))
|
||||
if win.getsockname(win.SOCKET(any_socket_to_socket(sock)), &sockaddr, &sockaddrlen) == win.SOCKET_ERROR {
|
||||
err = Listen_Error(win.WSAGetLastError())
|
||||
err = _listen_error()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -136,7 +190,7 @@ _bound_endpoint :: proc(sock: Any_Socket) -> (ep: Endpoint, err: Network_Error)
|
||||
}
|
||||
|
||||
@(private)
|
||||
_accept_tcp :: proc(sock: TCP_Socket, options := default_tcp_options) -> (client: TCP_Socket, source: Endpoint, err: Network_Error) {
|
||||
_accept_tcp :: proc(sock: TCP_Socket, options := default_tcp_options) -> (client: TCP_Socket, source: Endpoint, err: Accept_Error) {
|
||||
for {
|
||||
sockaddr: win.SOCKADDR_STORAGE_LH
|
||||
sockaddrlen := c.int(size_of(sockaddr))
|
||||
@@ -150,7 +204,7 @@ _accept_tcp :: proc(sock: TCP_Socket, options := default_tcp_options) -> (client
|
||||
// can do this to match the behaviour.
|
||||
continue
|
||||
}
|
||||
err = Accept_Error(e)
|
||||
err = _accept_error()
|
||||
return
|
||||
}
|
||||
client = TCP_Socket(client_sock)
|
||||
@@ -170,20 +224,20 @@ _close :: proc(socket: Any_Socket) {
|
||||
}
|
||||
|
||||
@(private)
|
||||
_recv_tcp :: proc(socket: TCP_Socket, buf: []byte) -> (bytes_read: int, err: Network_Error) {
|
||||
_recv_tcp :: proc(socket: TCP_Socket, buf: []byte) -> (bytes_read: int, err: TCP_Recv_Error) {
|
||||
if len(buf) <= 0 {
|
||||
return
|
||||
}
|
||||
res := win.recv(win.SOCKET(socket), raw_data(buf), c.int(len(buf)), 0)
|
||||
if res < 0 {
|
||||
err = TCP_Recv_Error(win.WSAGetLastError())
|
||||
err = _tcp_recv_error()
|
||||
return
|
||||
}
|
||||
return int(res), nil
|
||||
}
|
||||
|
||||
@(private)
|
||||
_recv_udp :: proc(socket: UDP_Socket, buf: []byte) -> (bytes_read: int, remote_endpoint: Endpoint, err: Network_Error) {
|
||||
_recv_udp :: proc(socket: UDP_Socket, buf: []byte) -> (bytes_read: int, remote_endpoint: Endpoint, err: UDP_Recv_Error) {
|
||||
if len(buf) <= 0 {
|
||||
return
|
||||
}
|
||||
@@ -192,7 +246,7 @@ _recv_udp :: proc(socket: UDP_Socket, buf: []byte) -> (bytes_read: int, remote_e
|
||||
fromsize := c.int(size_of(from))
|
||||
res := win.recvfrom(win.SOCKET(socket), raw_data(buf), c.int(len(buf)), 0, &from, &fromsize)
|
||||
if res < 0 {
|
||||
err = UDP_Recv_Error(win.WSAGetLastError())
|
||||
err = _udp_recv_error()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -202,13 +256,13 @@ _recv_udp :: proc(socket: UDP_Socket, buf: []byte) -> (bytes_read: int, remote_e
|
||||
}
|
||||
|
||||
@(private)
|
||||
_send_tcp :: proc(socket: TCP_Socket, buf: []byte) -> (bytes_written: int, err: Network_Error) {
|
||||
_send_tcp :: proc(socket: TCP_Socket, buf: []byte) -> (bytes_written: int, err: TCP_Send_Error) {
|
||||
for bytes_written < len(buf) {
|
||||
limit := min(int(max(i32)), len(buf) - bytes_written)
|
||||
remaining := buf[bytes_written:]
|
||||
res := win.send(win.SOCKET(socket), raw_data(remaining), c.int(limit), 0)
|
||||
if res < 0 {
|
||||
err = TCP_Send_Error(win.WSAGetLastError())
|
||||
err = _tcp_send_error()
|
||||
return
|
||||
}
|
||||
bytes_written += int(res)
|
||||
@@ -217,34 +271,34 @@ _send_tcp :: proc(socket: TCP_Socket, buf: []byte) -> (bytes_written: int, err:
|
||||
}
|
||||
|
||||
@(private)
|
||||
_send_udp :: proc(socket: UDP_Socket, buf: []byte, to: Endpoint) -> (bytes_written: int, err: Network_Error) {
|
||||
if len(buf) > int(max(c.int)) {
|
||||
// NOTE(tetra): If we don't guard this, we'll return (0, nil) instead, which is misleading.
|
||||
err = .Message_Too_Long
|
||||
return
|
||||
}
|
||||
_send_udp :: proc(socket: UDP_Socket, buf: []byte, to: Endpoint) -> (bytes_written: int, err: UDP_Send_Error) {
|
||||
toaddr := _endpoint_to_sockaddr(to)
|
||||
res := win.sendto(win.SOCKET(socket), raw_data(buf), c.int(len(buf)), 0, &toaddr, size_of(toaddr))
|
||||
if res < 0 {
|
||||
err = UDP_Send_Error(win.WSAGetLastError())
|
||||
return
|
||||
for bytes_written < len(buf) {
|
||||
limit := min(int(max(i32)), len(buf) - bytes_written)
|
||||
remaining := buf[bytes_written:]
|
||||
res := win.sendto(win.SOCKET(socket), raw_data(remaining), c.int(limit), 0, &toaddr, size_of(toaddr))
|
||||
if res < 0 {
|
||||
err = _udp_send_error()
|
||||
return
|
||||
}
|
||||
|
||||
bytes_written += int(res)
|
||||
}
|
||||
bytes_written = int(res)
|
||||
return
|
||||
}
|
||||
|
||||
@(private)
|
||||
_shutdown :: proc(socket: Any_Socket, manner: Shutdown_Manner) -> (err: Network_Error) {
|
||||
_shutdown :: proc(socket: Any_Socket, manner: Shutdown_Manner) -> (err: Shutdown_Error) {
|
||||
s := any_socket_to_socket(socket)
|
||||
res := win.shutdown(win.SOCKET(s), c.int(manner))
|
||||
if res < 0 {
|
||||
return Shutdown_Error(win.WSAGetLastError())
|
||||
return _shutdown_error()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@(private)
|
||||
_set_option :: proc(s: Any_Socket, option: Socket_Option, value: any, loc := #caller_location) -> Network_Error {
|
||||
_set_option :: proc(s: Any_Socket, option: Socket_Option, value: any, loc := #caller_location) -> Socket_Option_Error {
|
||||
level := win.SOL_SOCKET if option != .TCP_Nodelay else win.IPPROTO_TCP
|
||||
|
||||
bool_value: b32
|
||||
@@ -283,11 +337,8 @@ _set_option :: proc(s: Any_Socket, option: Socket_Option, value: any, loc := #ca
|
||||
t := value.(time.Duration) or_else panic("set_option() value must be a time.Duration here", loc)
|
||||
|
||||
num_secs := i64(time.duration_seconds(t))
|
||||
if time.Duration(num_secs * 1e9) != t {
|
||||
return .Linger_Only_Supports_Whole_Seconds
|
||||
}
|
||||
if num_secs > i64(max(u16)) {
|
||||
return .Value_Out_Of_Range
|
||||
return .Invalid_Value
|
||||
}
|
||||
linger_value.l_onoff = 1
|
||||
linger_value.l_linger = c.ushort(num_secs)
|
||||
@@ -323,19 +374,19 @@ _set_option :: proc(s: Any_Socket, option: Socket_Option, value: any, loc := #ca
|
||||
socket := any_socket_to_socket(s)
|
||||
res := win.setsockopt(win.SOCKET(socket), c.int(level), c.int(option), ptr, len)
|
||||
if res < 0 {
|
||||
return Socket_Option_Error(win.WSAGetLastError())
|
||||
return _socket_option_error()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@(private)
|
||||
_set_blocking :: proc(socket: Any_Socket, should_block: bool) -> (err: Network_Error) {
|
||||
_set_blocking :: proc(socket: Any_Socket, should_block: bool) -> (err: Set_Blocking_Error) {
|
||||
socket := any_socket_to_socket(socket)
|
||||
arg: win.DWORD = 0 if should_block else 1
|
||||
res := win.ioctlsocket(win.SOCKET(socket), transmute(win.c_long)win.FIONBIO, &arg)
|
||||
if res == win.SOCKET_ERROR {
|
||||
return Set_Blocking_Error(win.WSAGetLastError())
|
||||
return _set_blocking_error()
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user