mirror of
https://github.com/odin-lang/Odin.git
synced 2026-01-02 03:02:37 +00:00
More coalescing.
This commit is contained in:
@@ -165,6 +165,12 @@ UDP_Send_Error :: enum c.int {
|
||||
No_Memory_Available = c.int(os.ENOMEM), // No memory was available to properly manage the send queue.
|
||||
}
|
||||
|
||||
Shutdown_Manner :: enum c.int {
|
||||
Receive = c.int(os.SHUT_RD),
|
||||
Send = c.int(os.SHUT_WR),
|
||||
Both = c.int(os.SHUT_RDWR),
|
||||
}
|
||||
|
||||
Shutdown_Error :: enum c.int {
|
||||
Aborted = c.int(os.ECONNABORTED),
|
||||
Reset = c.int(os.ECONNRESET),
|
||||
|
||||
@@ -158,6 +158,12 @@ UDP_Send_Error :: enum c.int {
|
||||
No_Memory_Available = c.int(os.ENOMEM), // No memory was available to properly manage the send queue.
|
||||
}
|
||||
|
||||
Shutdown_Manner :: enum c.int {
|
||||
Receive = c.int(os.SHUT_RD),
|
||||
Send = c.int(os.SHUT_WR),
|
||||
Both = c.int(os.SHUT_RDWR),
|
||||
}
|
||||
|
||||
Shutdown_Error :: enum c.int {
|
||||
Aborted = c.int(os.ECONNABORTED),
|
||||
Reset = c.int(os.ECONNRESET),
|
||||
@@ -173,4 +179,4 @@ Socket_Option_Error :: enum c.int {
|
||||
Invalid_Option_For_Socket = c.int(os.ENOPROTOOPT),
|
||||
Reset_When_Keepalive_Set = c.int(os.ENOTCONN),
|
||||
Not_Socket = c.int(os.ENOTSOCK),
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,18 @@ import "core:time"
|
||||
|
||||
Platform_Socket :: os.Socket
|
||||
|
||||
Socket_Option :: enum c.int {
|
||||
Reuse_Address = c.int(os.SO_REUSEADDR),
|
||||
Keep_Alive = c.int(os.SO_KEEPALIVE),
|
||||
Out_Of_Bounds_Data_Inline = c.int(os.SO_OOBINLINE),
|
||||
TCP_Nodelay = c.int(os.TCP_NODELAY),
|
||||
Linger = c.int(os.SO_LINGER),
|
||||
Receive_Buffer_Size = c.int(os.SO_RCVBUF),
|
||||
Send_Buffer_Size = c.int(os.SO_SNDBUF),
|
||||
Receive_Timeout = c.int(os.SO_RCVTIMEO),
|
||||
Send_Timeout = c.int(os.SO_SNDTIMEO),
|
||||
}
|
||||
|
||||
create_socket :: proc(family: Address_Family, protocol: Socket_Protocol) -> (socket: Any_Socket, err: Network_Error) {
|
||||
c_type, c_protocol, c_family: int
|
||||
|
||||
@@ -163,8 +175,6 @@ recv_udp :: proc(skt: UDP_Socket, buf: []byte) -> (bytes_read: int, remote_endpo
|
||||
return
|
||||
}
|
||||
|
||||
recv :: proc{recv_tcp, recv_udp}
|
||||
|
||||
// Repeatedly sends data until the entire buffer is sent.
|
||||
// If a send fails before all data is sent, returns the amount
|
||||
// sent up to that point.
|
||||
@@ -197,14 +207,6 @@ send_udp :: proc(skt: UDP_Socket, buf: []byte, to: Endpoint) -> (bytes_written:
|
||||
return
|
||||
}
|
||||
|
||||
send :: proc{send_tcp, send_udp}
|
||||
|
||||
Shutdown_Manner :: enum c.int {
|
||||
Receive = c.int(os.SHUT_RD),
|
||||
Send = c.int(os.SHUT_WR),
|
||||
Both = c.int(os.SHUT_RDWR),
|
||||
}
|
||||
|
||||
shutdown :: proc(skt: Any_Socket, manner: Shutdown_Manner) -> (err: Network_Error) {
|
||||
s := any_socket_to_socket(skt)
|
||||
res := os.shutdown(Platform_Socket(s), int(manner))
|
||||
@@ -214,18 +216,6 @@ shutdown :: proc(skt: Any_Socket, manner: Shutdown_Manner) -> (err: Network_Erro
|
||||
return
|
||||
}
|
||||
|
||||
Socket_Option :: enum c.int {
|
||||
Reuse_Address = c.int(os.SO_REUSEADDR),
|
||||
Keep_Alive = c.int(os.SO_KEEPALIVE),
|
||||
Out_Of_Bounds_Data_Inline = c.int(os.SO_OOBINLINE),
|
||||
TCP_Nodelay = c.int(os.TCP_NODELAY),
|
||||
Linger = c.int(os.SO_LINGER),
|
||||
Receive_Buffer_Size = c.int(os.SO_RCVBUF),
|
||||
Send_Buffer_Size = c.int(os.SO_SNDBUF),
|
||||
Receive_Timeout = c.int(os.SO_RCVTIMEO),
|
||||
Send_Timeout = c.int(os.SO_SNDTIMEO),
|
||||
}
|
||||
|
||||
set_option :: proc(s: Any_Socket, option: Socket_Option, value: any, loc := #caller_location) -> Network_Error {
|
||||
level := os.SOL_SOCKET if option != .TCP_Nodelay else os.IPPROTO_TCP
|
||||
|
||||
|
||||
@@ -22,9 +22,20 @@ import "core:c"
|
||||
import "core:os"
|
||||
import "core:time"
|
||||
|
||||
Platform_Socket :: os.Socket
|
||||
Socket_Option :: enum c.int {
|
||||
Reuse_Address = c.int(os.SO_REUSEADDR),
|
||||
Keep_Alive = c.int(os.SO_KEEPALIVE),
|
||||
Out_Of_Bounds_Data_Inline = c.int(os.SO_OOBINLINE),
|
||||
TCP_Nodelay = c.int(os.TCP_NODELAY),
|
||||
Linger = c.int(os.SO_LINGER),
|
||||
Receive_Buffer_Size = c.int(os.SO_RCVBUF),
|
||||
Send_Buffer_Size = c.int(os.SO_SNDBUF),
|
||||
Receive_Timeout = c.int(os.SO_RCVTIMEO_NEW),
|
||||
Send_Timeout = c.int(os.SO_SNDTIMEO_NEW),
|
||||
}
|
||||
|
||||
create_socket :: proc(family: Address_Family, protocol: Socket_Protocol) -> (socket: Any_Socket, err: Network_Error) {
|
||||
@(private)
|
||||
_create_socket :: proc(family: Address_Family, protocol: Socket_Protocol) -> (socket: Any_Socket, err: Network_Error) {
|
||||
c_type, c_protocol, c_family: int
|
||||
|
||||
switch family {
|
||||
@@ -55,7 +66,8 @@ create_socket :: proc(family: Address_Family, protocol: Socket_Protocol) -> (soc
|
||||
}
|
||||
}
|
||||
|
||||
dial_tcp_from_endpoint :: proc(endpoint: Endpoint, options := default_tcp_options) -> (skt: TCP_Socket, err: Network_Error) {
|
||||
@(private)
|
||||
_dial_tcp_from_endpoint :: proc(endpoint: Endpoint, options := default_tcp_options) -> (skt: TCP_Socket, err: Network_Error) {
|
||||
if endpoint.port == 0 {
|
||||
return 0, .Port_Required
|
||||
}
|
||||
@@ -70,31 +82,32 @@ dial_tcp_from_endpoint :: proc(endpoint: Endpoint, options := default_tcp_option
|
||||
_ = set_option(skt, .Reuse_Address, true)
|
||||
|
||||
sockaddr := _endpoint_to_sockaddr(endpoint)
|
||||
res := os.connect(Platform_Socket(skt), (^os.SOCKADDR)(&sockaddr), size_of(sockaddr))
|
||||
res := os.connect(os.Socket(skt), (^os.SOCKADDR)(&sockaddr), size_of(sockaddr))
|
||||
if res != os.ERROR_NONE {
|
||||
err = Dial_Error(res)
|
||||
return
|
||||
}
|
||||
|
||||
if options.no_delay {
|
||||
_ = set_option(sock, .TCP_Nodelay, true) // NOTE(tetra): Not vital to succeed; error ignored
|
||||
_ = _set_option(sock, .TCP_Nodelay, true) // NOTE(tetra): Not vital to succeed; error ignored
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
bind :: proc(skt: Any_Socket, ep: Endpoint) -> (err: Network_Error) {
|
||||
@(private)
|
||||
_bind :: proc(skt: Any_Socket, ep: Endpoint) -> (err: Network_Error) {
|
||||
sockaddr := _endpoint_to_sockaddr(ep)
|
||||
s := any_socket_to_socket(skt)
|
||||
res := os.bind(Platform_Socket(s), (^os.SOCKADDR)(&sockaddr), size_of(sockaddr))
|
||||
res := os.bind(os.Socket(s), (^os.SOCKADDR)(&sockaddr), size_of(sockaddr))
|
||||
if res != os.ERROR_NONE {
|
||||
err = Bind_Error(res)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
listen_tcp :: proc(interface_endpoint: Endpoint, backlog := 1000) -> (skt: TCP_Socket, err: Network_Error) {
|
||||
@(private)
|
||||
_listen_tcp :: proc(interface_endpoint: Endpoint, backlog := 1000) -> (skt: TCP_Socket, err: Network_Error) {
|
||||
assert(backlog > 0 && i32(backlog) < max(i32))
|
||||
|
||||
family := family_from_endpoint(interface_endpoint)
|
||||
@@ -110,7 +123,7 @@ listen_tcp :: proc(interface_endpoint: Endpoint, backlog := 1000) -> (skt: TCP_S
|
||||
|
||||
bind(sock, interface_endpoint) or_return
|
||||
|
||||
res := os.listen(Platform_Socket(skt), backlog)
|
||||
res := os.listen(os.Socket(skt), backlog)
|
||||
if res != os.ERROR_NONE {
|
||||
err = Listen_Error(res)
|
||||
return
|
||||
@@ -119,11 +132,12 @@ listen_tcp :: proc(interface_endpoint: Endpoint, backlog := 1000) -> (skt: TCP_S
|
||||
return
|
||||
}
|
||||
|
||||
accept_tcp :: proc(sock: TCP_Socket, options := default_tcp_options) -> (client: TCP_Socket, source: Endpoint, err: Network_Error) {
|
||||
@(private)
|
||||
_accept_tcp :: proc(sock: TCP_Socket, options := default_tcp_options) -> (client: TCP_Socket, source: Endpoint, err: Network_Error) {
|
||||
sockaddr: os.SOCKADDR_STORAGE_LH
|
||||
sockaddrlen := c.int(size_of(sockaddr))
|
||||
|
||||
client_sock, ok := os.accept(Platform_Socket(sock), cast(^os.SOCKADDR) &sockaddr, &sockaddrlen)
|
||||
client_sock, ok := os.accept(os.Socket(sock), cast(^os.SOCKADDR) &sockaddr, &sockaddrlen)
|
||||
if ok != os.ERROR_NONE {
|
||||
err = Accept_Error(ok)
|
||||
return
|
||||
@@ -131,23 +145,23 @@ accept_tcp :: proc(sock: TCP_Socket, options := default_tcp_options) -> (client:
|
||||
client = TCP_Socket(client_sock)
|
||||
source = _sockaddr_storage_to_endpoint(&sockaddr)
|
||||
if options.no_delay {
|
||||
_ = set_option(client, .TCP_Nodelay, true) // NOTE(tetra): Not vital to succeed; error ignored
|
||||
_ = _set_option(client, .TCP_Nodelay, true) // NOTE(tetra): Not vital to succeed; error ignored
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
|
||||
close :: proc(skt: Any_Socket) {
|
||||
@(private)
|
||||
_close :: proc(skt: Any_Socket) {
|
||||
s := any_socket_to_socket(skt)
|
||||
os.close(os.Handle(Platform_Socket(s)))
|
||||
os.close(os.Handle(os.Socket(s)))
|
||||
}
|
||||
|
||||
recv_tcp :: proc(skt: TCP_Socket, buf: []byte) -> (bytes_read: int, err: Network_Error) {
|
||||
@(private)
|
||||
_recv_tcp :: proc(skt: TCP_Socket, buf: []byte) -> (bytes_read: int, err: Network_Error) {
|
||||
if len(buf) <= 0 {
|
||||
return
|
||||
}
|
||||
res, ok := os.recv(Platform_Socket(skt), buf, 0)
|
||||
res, ok := os.recv(os.Socket(skt), buf, 0)
|
||||
if ok != os.ERROR_NONE {
|
||||
err = TCP_Recv_Error(ok)
|
||||
return
|
||||
@@ -155,7 +169,8 @@ recv_tcp :: proc(skt: TCP_Socket, buf: []byte) -> (bytes_read: int, err: Network
|
||||
return int(res), nil
|
||||
}
|
||||
|
||||
recv_udp :: proc(skt: UDP_Socket, buf: []byte) -> (bytes_read: int, remote_endpoint: Endpoint, err: Network_Error) {
|
||||
@(private)
|
||||
_recv_udp :: proc(skt: UDP_Socket, buf: []byte) -> (bytes_read: int, remote_endpoint: Endpoint, err: Network_Error) {
|
||||
if len(buf) <= 0 {
|
||||
return
|
||||
}
|
||||
@@ -167,7 +182,7 @@ recv_udp :: proc(skt: UDP_Socket, buf: []byte) -> (bytes_read: int, remote_endpo
|
||||
// and no error is returned.
|
||||
// However, if you pass MSG_TRUNC here, 'res' will be the size of the incoming message, rather than how much was read.
|
||||
// We can use this fact to detect this condition and return .Buffer_Too_Small.
|
||||
res, ok := os.recvfrom(Platform_Socket(skt), buf, os.MSG_TRUNC, cast(^os.SOCKADDR) &from, &fromsize)
|
||||
res, ok := os.recvfrom(os.Socket(skt), buf, os.MSG_TRUNC, cast(^os.SOCKADDR) &from, &fromsize)
|
||||
if ok != os.ERROR_NONE {
|
||||
err = UDP_Recv_Error(ok)
|
||||
return
|
||||
@@ -185,17 +200,12 @@ recv_udp :: proc(skt: UDP_Socket, buf: []byte) -> (bytes_read: int, remote_endpo
|
||||
return
|
||||
}
|
||||
|
||||
recv :: proc{recv_tcp, recv_udp}
|
||||
|
||||
|
||||
// Repeatedly sends data until the entire buffer is sent.
|
||||
// If a send fails before all data is sent, returns the amount
|
||||
// sent up to that point.
|
||||
send_tcp :: proc(skt: TCP_Socket, buf: []byte) -> (bytes_written: int, err: Network_Error) {
|
||||
@(private)
|
||||
_send_tcp :: proc(skt: TCP_Socket, buf: []byte) -> (bytes_written: int, err: Network_Error) {
|
||||
for bytes_written < len(buf) {
|
||||
limit := min(int(max(i32)), len(buf) - bytes_written)
|
||||
remaining := buf[bytes_written:][:limit]
|
||||
res, ok := os.send(Platform_Socket(skt), remaining, 0)
|
||||
res, ok := os.send(os.Socket(skt), remaining, 0)
|
||||
if ok != os.ERROR_NONE {
|
||||
err = TCP_Send_Error(ok)
|
||||
return
|
||||
@@ -205,13 +215,10 @@ send_tcp :: proc(skt: TCP_Socket, buf: []byte) -> (bytes_written: int, err: Netw
|
||||
return
|
||||
}
|
||||
|
||||
// Sends a single UDP datagram packet.
|
||||
//
|
||||
// Datagrams are limited in size; attempting to send more than this limit at once will result in a Message_Too_Long error.
|
||||
// UDP packets are not guarenteed to be received in order.
|
||||
send_udp :: proc(skt: UDP_Socket, buf: []byte, to: Endpoint) -> (bytes_written: int, err: Network_Error) {
|
||||
@(private)
|
||||
_send_udp :: proc(skt: UDP_Socket, buf: []byte, to: Endpoint) -> (bytes_written: int, err: Network_Error) {
|
||||
toaddr := _endpoint_to_sockaddr(to)
|
||||
res, os_err := os.sendto(Platform_Socket(skt), buf, 0, cast(^os.SOCKADDR) &toaddr, size_of(toaddr))
|
||||
res, os_err := os.sendto(os.Socket(skt), buf, 0, cast(^os.SOCKADDR) &toaddr, size_of(toaddr))
|
||||
if os_err != os.ERROR_NONE {
|
||||
err = UDP_Send_Error(os_err)
|
||||
return
|
||||
@@ -220,36 +227,18 @@ send_udp :: proc(skt: UDP_Socket, buf: []byte, to: Endpoint) -> (bytes_written:
|
||||
return
|
||||
}
|
||||
|
||||
send :: proc{send_tcp, send_udp}
|
||||
|
||||
Shutdown_Manner :: enum c.int {
|
||||
Receive = c.int(os.SHUT_RD),
|
||||
Send = c.int(os.SHUT_WR),
|
||||
Both = c.int(os.SHUT_RDWR),
|
||||
}
|
||||
|
||||
shutdown :: proc(skt: Any_Socket, manner: Shutdown_Manner) -> (err: Network_Error) {
|
||||
@(private)
|
||||
_shutdown :: proc(skt: Any_Socket, manner: Shutdown_Manner) -> (err: Network_Error) {
|
||||
s := any_socket_to_socket(skt)
|
||||
res := os.shutdown(Platform_Socket(s), int(manner))
|
||||
res := os.shutdown(os.Socket(s), int(manner))
|
||||
if res != os.ERROR_NONE {
|
||||
return Shutdown_Error(res)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Socket_Option :: enum c.int {
|
||||
Reuse_Address = c.int(os.SO_REUSEADDR),
|
||||
Keep_Alive = c.int(os.SO_KEEPALIVE),
|
||||
Out_Of_Bounds_Data_Inline = c.int(os.SO_OOBINLINE),
|
||||
TCP_Nodelay = c.int(os.TCP_NODELAY),
|
||||
Linger = c.int(os.SO_LINGER),
|
||||
Receive_Buffer_Size = c.int(os.SO_RCVBUF),
|
||||
Send_Buffer_Size = c.int(os.SO_SNDBUF),
|
||||
Receive_Timeout = c.int(os.SO_RCVTIMEO_NEW),
|
||||
Send_Timeout = c.int(os.SO_SNDTIMEO_NEW),
|
||||
}
|
||||
|
||||
set_option :: proc(s: Any_Socket, option: Socket_Option, value: any, loc := #caller_location) -> Network_Error {
|
||||
@(private)
|
||||
_set_option :: proc(s: Any_Socket, option: Socket_Option, value: any, loc := #caller_location) -> Network_Error {
|
||||
level := os.SOL_SOCKET if option != .TCP_Nodelay else os.IPPROTO_TCP
|
||||
|
||||
// NOTE(tetra, 2022-02-15): On Linux, you cannot merely give a single byte for a bool;
|
||||
@@ -319,7 +308,7 @@ set_option :: proc(s: Any_Socket, option: Socket_Option, value: any, loc := #cal
|
||||
}
|
||||
|
||||
skt := any_socket_to_socket(s)
|
||||
res := os.setsockopt(Platform_Socket(skt), int(level), int(option), ptr, len)
|
||||
res := os.setsockopt(os.Socket(skt), int(level), int(option), ptr, len)
|
||||
if res != os.ERROR_NONE {
|
||||
return Socket_Option_Error(res)
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ _create_socket :: proc(family: Address_Family, protocol: Socket_Protocol) -> (so
|
||||
}
|
||||
|
||||
@(private)
|
||||
_dial_tcp_from_endpoint :: proc(endpoint: Endpoint, options := default_tcp_options) -> (skt: TCP_Socket, err: Network_Error) {
|
||||
_dial_tcp_from_endpoint :: proc(endpoint: Endpoint, options := default_tcp_options) -> (socket: TCP_Socket, err: Network_Error) {
|
||||
if endpoint.port == 0 {
|
||||
err = .Port_Required
|
||||
return
|
||||
@@ -70,15 +70,15 @@ _dial_tcp_from_endpoint :: proc(endpoint: Endpoint, options := default_tcp_optio
|
||||
|
||||
family := family_from_endpoint(endpoint)
|
||||
sock := create_socket(family, .TCP) or_return
|
||||
skt = sock.(TCP_Socket)
|
||||
socket = sock.(TCP_Socket)
|
||||
|
||||
// NOTE(tetra): This is so that if we crash while the socket is open, we can
|
||||
// bypass the cooldown period, and allow the next run of the program to
|
||||
// use the same address immediately.
|
||||
_ = set_option(skt, .Reuse_Address, true)
|
||||
_ = set_option(socket, .Reuse_Address, true)
|
||||
|
||||
sockaddr := _endpoint_to_sockaddr(endpoint)
|
||||
res := win.connect(Platform_Socket(skt), &sockaddr, size_of(sockaddr))
|
||||
res := win.connect(Platform_Socket(socket), &sockaddr, size_of(sockaddr))
|
||||
if res < 0 {
|
||||
err = Dial_Error(win.WSAGetLastError())
|
||||
return
|
||||
@@ -92,9 +92,9 @@ _dial_tcp_from_endpoint :: proc(endpoint: Endpoint, options := default_tcp_optio
|
||||
}
|
||||
|
||||
@(private)
|
||||
_bind :: proc(skt: Any_Socket, ep: Endpoint) -> (err: Network_Error) {
|
||||
_bind :: proc(socket: Any_Socket, ep: Endpoint) -> (err: Network_Error) {
|
||||
sockaddr := _endpoint_to_sockaddr(ep)
|
||||
s := any_socket_to_socket(skt)
|
||||
s := any_socket_to_socket(socket)
|
||||
res := win.bind(Platform_Socket(s), &sockaddr, size_of(sockaddr))
|
||||
if res < 0 {
|
||||
err = Bind_Error(win.WSAGetLastError())
|
||||
@@ -103,18 +103,18 @@ _bind :: proc(skt: Any_Socket, ep: Endpoint) -> (err: Network_Error) {
|
||||
}
|
||||
|
||||
@(private)
|
||||
_listen_tcp :: proc(interface_endpoint: Endpoint, backlog := 1000) -> (skt: TCP_Socket, err: Network_Error) {
|
||||
_listen_tcp :: proc(interface_endpoint: Endpoint, backlog := 1000) -> (socket: TCP_Socket, err: Network_Error) {
|
||||
family := family_from_endpoint(interface_endpoint)
|
||||
sock := create_socket(family, .TCP) or_return
|
||||
skt = sock.(TCP_Socket)
|
||||
socket = sock.(TCP_Socket)
|
||||
|
||||
// NOTE(tetra): While I'm not 100% clear on it, my understanding is that this will
|
||||
// prevent hijacking of the server's endpoint by other applications.
|
||||
set_option(skt, .Exclusive_Addr_Use, true) or_return
|
||||
set_option(socket, .Exclusive_Addr_Use, true) or_return
|
||||
|
||||
bind(sock, interface_endpoint) or_return
|
||||
|
||||
if res := win.listen(Platform_Socket(skt), i32(backlog)); res == win.SOCKET_ERROR {
|
||||
if res := win.listen(Platform_Socket(socket), i32(backlog)); res == win.SOCKET_ERROR {
|
||||
err = Listen_Error(win.WSAGetLastError())
|
||||
}
|
||||
return
|
||||
@@ -148,18 +148,18 @@ _accept_tcp :: proc(sock: TCP_Socket, options := default_tcp_options) -> (client
|
||||
}
|
||||
|
||||
@(private)
|
||||
_close :: proc(skt: Any_Socket) {
|
||||
if s := any_socket_to_socket(skt); s != {} {
|
||||
_close :: proc(socket: Any_Socket) {
|
||||
if s := any_socket_to_socket(socket); s != {} {
|
||||
win.closesocket(Platform_Socket(s))
|
||||
}
|
||||
}
|
||||
|
||||
@(private)
|
||||
_recv_tcp :: proc(skt: TCP_Socket, buf: []byte) -> (bytes_read: int, err: Network_Error) {
|
||||
_recv_tcp :: proc(socket: TCP_Socket, buf: []byte) -> (bytes_read: int, err: Network_Error) {
|
||||
if len(buf) <= 0 {
|
||||
return
|
||||
}
|
||||
res := win.recv(Platform_Socket(skt), raw_data(buf), c.int(len(buf)), 0)
|
||||
res := win.recv(Platform_Socket(socket), raw_data(buf), c.int(len(buf)), 0)
|
||||
if res < 0 {
|
||||
err = TCP_Recv_Error(win.WSAGetLastError())
|
||||
return
|
||||
@@ -168,14 +168,14 @@ _recv_tcp :: proc(skt: TCP_Socket, buf: []byte) -> (bytes_read: int, err: Networ
|
||||
}
|
||||
|
||||
@(private)
|
||||
_recv_udp :: proc(skt: 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: Network_Error) {
|
||||
if len(buf) <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
from: win.SOCKADDR_STORAGE_LH
|
||||
fromsize := c.int(size_of(from))
|
||||
res := win.recvfrom(Platform_Socket(skt), raw_data(buf), c.int(len(buf)), 0, &from, &fromsize)
|
||||
res := win.recvfrom(Platform_Socket(socket), raw_data(buf), c.int(len(buf)), 0, &from, &fromsize)
|
||||
if res < 0 {
|
||||
err = UDP_Recv_Error(win.WSAGetLastError())
|
||||
return
|
||||
@@ -187,11 +187,11 @@ _recv_udp :: proc(skt: UDP_Socket, buf: []byte) -> (bytes_read: int, remote_endp
|
||||
}
|
||||
|
||||
@(private)
|
||||
_send_tcp :: proc(skt: TCP_Socket, buf: []byte) -> (bytes_written: int, err: Network_Error) {
|
||||
_send_tcp :: proc(socket: TCP_Socket, buf: []byte) -> (bytes_written: int, err: Network_Error) {
|
||||
for bytes_written < len(buf) {
|
||||
limit := min(int(max(i32)), len(buf) - bytes_written)
|
||||
remaining := buf[bytes_written:]
|
||||
res := win.send(Platform_Socket(skt), raw_data(remaining), c.int(limit), 0)
|
||||
res := win.send(Platform_Socket(socket), raw_data(remaining), c.int(limit), 0)
|
||||
if res < 0 {
|
||||
err = TCP_Send_Error(win.WSAGetLastError())
|
||||
return
|
||||
@@ -202,14 +202,14 @@ _send_tcp :: proc(skt: TCP_Socket, buf: []byte) -> (bytes_written: int, err: Net
|
||||
}
|
||||
|
||||
@(private)
|
||||
_send_udp :: proc(skt: UDP_Socket, buf: []byte, to: Endpoint) -> (bytes_written: int, err: Network_Error) {
|
||||
_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
|
||||
}
|
||||
toaddr := _endpoint_to_sockaddr(to)
|
||||
res := win.sendto(Platform_Socket(skt), raw_data(buf), c.int(len(buf)), 0, &toaddr, size_of(toaddr))
|
||||
res := win.sendto(Platform_Socket(socket), raw_data(buf), c.int(len(buf)), 0, &toaddr, size_of(toaddr))
|
||||
if res < 0 {
|
||||
err = UDP_Send_Error(win.WSAGetLastError())
|
||||
return
|
||||
@@ -219,8 +219,8 @@ _send_udp :: proc(skt: UDP_Socket, buf: []byte, to: Endpoint) -> (bytes_written:
|
||||
}
|
||||
|
||||
@(private)
|
||||
_shutdown :: proc(skt: Any_Socket, manner: Shutdown_Manner) -> (err: Network_Error) {
|
||||
s := any_socket_to_socket(skt)
|
||||
_shutdown :: proc(socket: Any_Socket, manner: Shutdown_Manner) -> (err: Network_Error) {
|
||||
s := any_socket_to_socket(socket)
|
||||
res := win.shutdown(Platform_Socket(s), c.int(manner))
|
||||
if res < 0 {
|
||||
return Shutdown_Error(win.WSAGetLastError())
|
||||
@@ -303,8 +303,8 @@ _set_option :: proc(s: Any_Socket, option: Socket_Option, value: any, loc := #ca
|
||||
len = size_of(int_value)
|
||||
}
|
||||
|
||||
skt := any_socket_to_socket(s)
|
||||
res := win.setsockopt(Platform_Socket(skt), c.int(level), c.int(option), ptr, len)
|
||||
socket := any_socket_to_socket(s)
|
||||
res := win.setsockopt(Platform_Socket(socket), c.int(level), c.int(option), ptr, len)
|
||||
if res < 0 {
|
||||
return Socket_Option_Error(win.WSAGetLastError())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user