commit 89827272cb71296bf172774be74daeef1de42f7b
parent 702a133287a19ec671b60f636a3c14848ea0095b
Author: Drew DeVault <sir@cmpwn.com>
Date: Sun, 20 Jun 2021 10:43:35 -0400
net: add tcp::accept, unix::accept, etc
This clarifies the purpose of net::listener and adds a convenience
function to ignore it for programs which do not need to abstract their
listeners.
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
5 files changed, 40 insertions(+), 1 deletion(-)
diff --git a/net/listener.ha b/net/listener.ha
@@ -2,7 +2,12 @@ use errors;
use io;
// A listener binds a socket and listens for incoming traffic for some
-// unspecified protocol.
+// unspecified protocol. This is generally most useful for providing an
+// abstraction between a TCP socket and Unix socket (or any other stream
+// oriented protocol), where the implementation which accepts and processes
+// connections is not aware of the underlying transport. Most users will not
+// need to use this interface directly, preferring functions such as
+// [[tcp::accept]].
export type listener = struct {
accept: nullable *fn(l: *listener) (*io::stream | io::error),
shutdown: nullable *fn(l: *listener) void,
diff --git a/net/tcp/listener.ha b/net/tcp/listener.ha
@@ -0,0 +1,14 @@
+use io;
+use net;
+
+// Accepts the next connection from a listener. Blocks until a new connection is
+// available. This is a convenience wrapper around [[net::accept]].
+export fn accept(l: *net::listener) (*io::stream | io::error) = {
+ return net::accept(l);
+};
+
+// Shuts down a [[net::listener]] and frees resources associated with it. This
+// is a convenience wrapper around [[net::shutdown]].
+export fn shutdown(l: *net::listener) void = {
+ return net::shutdown(l);
+};
diff --git a/net/unix/listener.ha b/net/unix/listener.ha
@@ -0,0 +1,14 @@
+use io;
+use net;
+
+// Accepts the next connection from a listener. Blocks until a new connection is
+// available. This is a convenience wrapper around [[net::accept]].
+export fn accept(l: *net::listener) (*io::stream | io::error) = {
+ return net::accept(l);
+};
+
+// Shuts down a [[net::listener]] and frees resources associated with it. This
+// is a convenience wrapper around [[net::shutdown]].
+export fn shutdown(l: *net::listener) void = {
+ return net::shutdown(l);
+};
diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib
@@ -579,6 +579,7 @@ net_tcp() {
printf '# net::tcp\n'
gen_srcs net::tcp \
'$(PLATFORM).ha' \
+ listener.ha \
options.ha
gen_ssa net::tcp io net net::ip os rt
}
@@ -596,6 +597,7 @@ net_unix() {
gen_srcs net::unix \
'$(PLATFORM).ha' \
addr.ha \
+ listener.ha \
options.ha
gen_ssa net::unix net errors os io strings types fmt
}
diff --git a/stdlib.mk b/stdlib.mk
@@ -846,6 +846,7 @@ $(HARECACHE)/net/ip/net_ip.ssa: $(stdlib_net_ip_srcs) $(stdlib_rt) $(stdlib_byte
# net::tcp
stdlib_net_tcp_srcs= \
$(STDLIB)/net/tcp/$(PLATFORM).ha \
+ $(STDLIB)/net/tcp/listener.ha \
$(STDLIB)/net/tcp/options.ha
$(HARECACHE)/net/tcp/net_tcp.ssa: $(stdlib_net_tcp_srcs) $(stdlib_rt) $(stdlib_io) $(stdlib_net) $(stdlib_net_ip) $(stdlib_os) $(stdlib_rt)
@@ -871,6 +872,7 @@ $(HARECACHE)/net/udp/net_udp.ssa: $(stdlib_net_udp_srcs) $(stdlib_rt) $(stdlib_n
stdlib_net_unix_srcs= \
$(STDLIB)/net/unix/$(PLATFORM).ha \
$(STDLIB)/net/unix/addr.ha \
+ $(STDLIB)/net/unix/listener.ha \
$(STDLIB)/net/unix/options.ha
$(HARECACHE)/net/unix/net_unix.ssa: $(stdlib_net_unix_srcs) $(stdlib_rt) $(stdlib_net) $(stdlib_errors) $(stdlib_os) $(stdlib_io) $(stdlib_strings) $(stdlib_types) $(stdlib_fmt)
@@ -1965,6 +1967,7 @@ $(TESTCACHE)/net/ip/net_ip.ssa: $(testlib_net_ip_srcs) $(testlib_rt) $(testlib_b
# net::tcp
testlib_net_tcp_srcs= \
$(STDLIB)/net/tcp/$(PLATFORM).ha \
+ $(STDLIB)/net/tcp/listener.ha \
$(STDLIB)/net/tcp/options.ha
$(TESTCACHE)/net/tcp/net_tcp.ssa: $(testlib_net_tcp_srcs) $(testlib_rt) $(testlib_io) $(testlib_net) $(testlib_net_ip) $(testlib_os) $(testlib_rt)
@@ -1990,6 +1993,7 @@ $(TESTCACHE)/net/udp/net_udp.ssa: $(testlib_net_udp_srcs) $(testlib_rt) $(testli
testlib_net_unix_srcs= \
$(STDLIB)/net/unix/$(PLATFORM).ha \
$(STDLIB)/net/unix/addr.ha \
+ $(STDLIB)/net/unix/listener.ha \
$(STDLIB)/net/unix/options.ha
$(TESTCACHE)/net/unix/net_unix.ssa: $(testlib_net_unix_srcs) $(testlib_rt) $(testlib_net) $(testlib_errors) $(testlib_os) $(testlib_io) $(testlib_strings) $(testlib_types) $(testlib_fmt)