aboutsummaryrefslogtreecommitdiff
path: root/core/net/socket.odin
diff options
context:
space:
mode:
authorflysand7 <yyakut.ac@gmail.com>2023-11-24 23:27:12 +1100
committerflysand7 <yyakut.ac@gmail.com>2023-11-24 23:27:12 +1100
commit8063569cdd6277044a3eb6d5ae7aee63f519cb85 (patch)
tree7485216a788ee1489b988ffcab2ef2e6d4fb8a70 /core/net/socket.odin
parent59675949da61078c67faaaf127c54e89ff37fb52 (diff)
[net]: Add send_any, recv_any variants to proc groups for Any_Socket
Diffstat (limited to 'core/net/socket.odin')
-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
+}