commit 4cf3fe287657d4ff9668640611fa84a7af052662
parent b7d0baf1c4869af7864c393a77c5cb8129f737c9
Author: Drew DeVault <sir@cmpwn.com>
Date: Mon, 21 Jun 2021 14:02:06 -0400
net::udp: add stream function
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
3 files changed, 15 insertions(+), 4 deletions(-)
diff --git a/net/udp/+linux.ha b/net/udp/+linux.ha
@@ -1,6 +1,8 @@
use errors;
-use net;
+use io;
use net::ip;
+use net;
+use os;
use rt;
export type socket = int;
@@ -26,6 +28,15 @@ export fn connect(
};
};
+// Creates an [[io::stream]] for an open UDP socket, which allows you to send
+// and receive UDP packets using [[io::read]] and [[io::write]], albiet without
+// being able to receive the sender address. Only meaningful if the socket was
+// created with [[connect]].
+export fn stream(sock: socket) *io::stream = {
+ return os::fdopen(sock, "<UDP stream>",
+ io::mode::READ | io::mode::WRITE);
+};
+
// Creates a UDP socket bound to an interface.
export fn listen(
addr: ip::addr,
diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib
@@ -593,7 +593,7 @@ net_udp() {
gen_srcs net::udp \
'$(PLATFORM).ha' \
options.ha
- gen_ssa net::udp net net::ip errors rt
+ gen_ssa net::udp net net::ip errors rt os io
}
net_unix() {
diff --git a/stdlib.mk b/stdlib.mk
@@ -872,7 +872,7 @@ stdlib_net_udp_srcs= \
$(STDLIB)/net/udp/$(PLATFORM).ha \
$(STDLIB)/net/udp/options.ha
-$(HARECACHE)/net/udp/net_udp.ssa: $(stdlib_net_udp_srcs) $(stdlib_rt) $(stdlib_net) $(stdlib_net_ip) $(stdlib_errors) $(stdlib_rt)
+$(HARECACHE)/net/udp/net_udp.ssa: $(stdlib_net_udp_srcs) $(stdlib_rt) $(stdlib_net) $(stdlib_net_ip) $(stdlib_errors) $(stdlib_rt) $(stdlib_os) $(stdlib_io)
@printf 'HAREC \t$@\n'
@mkdir -p $(HARECACHE)/net/udp
@HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nnet::udp \
@@ -2024,7 +2024,7 @@ testlib_net_udp_srcs= \
$(STDLIB)/net/udp/$(PLATFORM).ha \
$(STDLIB)/net/udp/options.ha
-$(TESTCACHE)/net/udp/net_udp.ssa: $(testlib_net_udp_srcs) $(testlib_rt) $(testlib_net) $(testlib_net_ip) $(testlib_errors) $(testlib_rt)
+$(TESTCACHE)/net/udp/net_udp.ssa: $(testlib_net_udp_srcs) $(testlib_rt) $(testlib_net) $(testlib_net_ip) $(testlib_errors) $(testlib_rt) $(testlib_os) $(testlib_io)
@printf 'HAREC \t$@\n'
@mkdir -p $(TESTCACHE)/net/udp
@HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nnet::udp \