aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2023-11-24 13:42:48 +0100
committerGitHub <noreply@github.com>2023-11-24 13:42:48 +0100
commitbb6d73953cfcfbf195948d39438f98af8a015f7f (patch)
tree8f7634323dda28fdd0d1d5b4e90ff84eb84ce285
parentc9c14bab8a26f2f0614929b01a7f0001089fec26 (diff)
parent8063569cdd6277044a3eb6d5ae7aee63f519cb85 (diff)
Merge pull request #2972 from flysand7/net-socket-any
[net]: Add send_any, recv_any variants to proc groups for Any_Socket
-rw-r--r--core/net/socket.odin41
1 files changed, 38 insertions, 3 deletions
diff --git a/core/net/socket.odin b/core/net/socket.odin
index 40fa6ab56..1bfa52257 100644
--- a/core/net/socket.odin
+++ b/core/net/socket.odin
@@ -148,7 +148,29 @@ recv_udp :: proc(socket: UDP_Socket, buf: []byte) -> (bytes_read: int, remote_en
return _recv_udp(socket, buf)
}
-recv :: proc{recv_tcp, recv_udp}
+/*
+ Receive data from into a buffer from any socket.
+
+ Note: `remote_endpoint` parameter is non-nil only if the socket type is UDP. On TCP sockets it
+ will always return `nil`.
+*/
+recv_any :: proc(socket: Any_Socket, buf: []byte) -> (
+ bytes_read: int,
+ remote_endpoint: Maybe(Endpoint),
+ err: Network_Error,
+) {
+ switch socktype in socket {
+ case TCP_Socket:
+ bytes_read, err := recv_tcp(socktype, buf)
+ return bytes_read, nil, err
+ case UDP_Socket:
+ bytes_read, endpoint, err := recv_udp(socktype, buf)
+ return bytes_read, endpoint, err
+ case: panic("Not supported")
+ }
+}
+
+recv :: proc{recv_tcp, recv_udp, recv_any}
/*
Repeatedly sends data until the entire buffer is sent.
@@ -168,7 +190,20 @@ send_udp :: proc(socket: UDP_Socket, buf: []byte, to: Endpoint) -> (bytes_writte
return _send_udp(socket, buf, to)
}
-send :: proc{send_tcp, send_udp}
+send_any :: proc(socket: Any_Socket, buf: []byte, to: Maybe(Endpoint) = nil) -> (
+ bytes_written: int,
+ err: Network_Error,
+) {
+ switch socktype in socket {
+ case TCP_Socket:
+ return send_tcp(socktype, buf)
+ case UDP_Socket:
+ return send_udp(socktype, buf, to.(Endpoint))
+ case: panic("Not supported")
+ }
+}
+
+send :: proc{send_tcp, send_udp, send_any}
shutdown :: proc(socket: Any_Socket, manner: Shutdown_Manner) -> (err: Network_Error) {
return _shutdown(socket, manner)
@@ -180,4 +215,4 @@ set_option :: proc(socket: Any_Socket, option: Socket_Option, value: any, loc :=
set_blocking :: proc(socket: Any_Socket, should_block: bool) -> (err: Network_Error) {
return _set_blocking(socket, should_block)
-} \ No newline at end of file
+}