shithub: mc

Download patch

ref: 52a9615661dfd2c9d4114b1a6f1d48830c3e3427
parent: 615d1d878c603e55120b5d4250d3173fba19f135
author: Ori Bernstein <ori@eigenstate.org>
date: Tue Sep 12 17:06:38 EDT 2017

Make announce API more cross platform.

	Encapsulate announcement into a struct.

--- a/lib/http/server.myr
+++ b/lib/http/server.myr
@@ -16,10 +16,10 @@
 const announce = {ds
 	match std.announce(ds)
 	| `std.Err e:	-> `std.Err `Econn
-	| `std.Ok lfd:
+	| `std.Ok a:
 		-> `std.Ok std.mk([
 			.refs=1,
-			.lfd=lfd,
+			.ann=a,
 			.quit=false
 		])
 	;;
@@ -76,14 +76,12 @@
 }
 
 const shutdown = {srv
-	std.close(srv.lfd)
+	std.aclose(srv.ann)
 	srv.quit = true
-
 }
 
-
 const waitconn = {srv
-	match std.accept(srv.lfd)
+	match std.accept(srv.ann)
 	| `std.Ok fd:	-> `std.Ok fd
 	| `std.Err e:	-> `std.Err `Econn
 	;;
--- a/lib/http/types.myr
+++ b/lib/http/types.myr
@@ -15,7 +15,7 @@
 
 
 	type server = struct
-		lfd	: std.fd
+		ann	: std.announce#
 		refs	: uint32
 		quit	: bool
 	;;
--- a/lib/std/listen+plan9.myr
+++ b/lib/std/listen+plan9.myr
@@ -1,19 +1,29 @@
 use sys
 
 use "alloc"
+use "cstrconv"
 use "die"
 use "dirname"
 use "fmt"
+use "mk"
 use "option"
 use "result"
+use "sldup"
 use "strfind"
 use "syswrap"
-use "cstrconv"
 use "utf"
 
+
 pkg std =
-	const announce	: (ds : byte[:] -> result(fd, byte[:]))
-	const accept	: (lfd : fd -> result(fd, byte[:]))
+	type announce = struct
+		afd	: fd
+		lpath	: byte[:]
+		netdir	: byte[:]
+	;;
+
+	const announce	: (ds : byte[:] -> result(announce#, byte[:]))
+	const aclose	: (a : announce# -> void)
+	const accept	: (a : announce# -> result(fd, byte[:]))
 ;;
 
 const Maxpath = 256
@@ -21,9 +31,8 @@
 const announce = {ds
 	var a, abuf : byte[Maxpath]
 	var f, fbuf : byte[Maxpath]
-	var l, lbuf : byte[Maxpath]
 	var nbuf : byte[32]
-	var dir, ctl, lfd, n
+	var dir, ctl, n
 
 	/* announce */
 	match translate(ds, abuf[:], fbuf[:])
@@ -45,50 +54,45 @@
 
 	match read(ctl, nbuf[:])
 	| `Err e:	->`Err "unable to read ctl fd"
-	| `Ok nn:	l = bfmt(lbuf[:], "{}/{}/listen", f[:dir], nbuf[:nn])
+	| `Ok nn:
+		n = fput(ctl, "announce {}", a)
+		if n <= 0
+			close(ctl)
+			-> `Err "writing announce"
+		;;
+	
+		-> `std.Ok mk([
+			.afd=ctl,
+			.lpath = fmt("{}/{}/listen", f[:dir], nbuf[:nn]),
+			.netdir = sldup(f[:dir]),
+		])
 	;;
 
-	put("announce {}\n", a)
-	n = fput(ctl, "announce {}", a)
-	if n <= 0
-		close(ctl)
-		-> `Err "writing announce"
-	;;
+}
 
-	/* listen */
-	match open(l, Ordwr)
-	| `Err e:
-		close(ctl)
-		-> `Err "could not open ctl"
-	| `Ok fd:
-		lfd = fd
-	;;
-	close(ctl)
-
-	-> `Ok lfd
+const aclose = {a
+	slfree(a.netdir)
+	slfree(a.lpath)
+	close(a.afd)
+	free(a)
 }
 
-const accept = {lfd -> result(fd, byte[:])
-	var dir, dirbuf : byte[40]
-	var num, numbuf : byte[40]
+const accept = {a -> result(fd, byte[:])
+	var num, numbuf : byte[16]
 	var dat, datbuf : byte[Maxpath]
+	var lfd
 
+	match open(a.lpath, Ordwr)
+	| `Err e:	-> `Err "could not open ctl"
+	| `Ok fd:	lfd = fd
+	;;
 
-
 	match read(lfd, numbuf[:])
 	| `Ok n:	num = numbuf[:n]
 	| `Err e:	-> `Err "could not accept"
 	;;
-	fput(lfd, "accept {}", num)
 
-	sys.fd2path((lfd : sys.fd), dirbuf[:])
-	dir = dirname(cstrconv(dirbuf[:]))
-	match strrfind(dir, "/")
-	| `None:	dat = bfmt(datbuf[:], "{}/{}/data", dir, num)
-	| `Some i:	dat = bfmt(datbuf[:], "{}/{}/data", dir[:i], num)
-	;;
-
-	put("data fd: {}\n", dat)
+	dat = bfmt(datbuf[:], "{}/{}/data", a.netdir, num)
 	match open(dat, Ordwr)
 	| `Ok fd:	-> `Ok fd
 	| `Err e:	-> `Err "could not open data fd"
--- a/lib/std/listen+posixy.myr
+++ b/lib/std/listen+posixy.myr
@@ -5,6 +5,7 @@
 use "dialparse"
 use "die"
 use "endian"
+use "mk"
 use "option"
 use "resolve"
 use "result"
@@ -16,8 +17,14 @@
 use "utf"
 
 pkg std =
-	const announce	: (ds : byte[:] -> result(fd, byte[:]))
-	const accept	: (lfd : fd -> result(fd, byte[:]))
+	type announce = struct
+		lfd	: fd
+	;;
+
+	const announce	: (ds : byte[:] -> result(announce#, byte[:]))
+	const aclose	: (a : announce# -> void)
+
+	const accept	: (a : announce# -> result(fd, byte[:]))
 ;;
 
 const announce = {ds
@@ -30,6 +37,11 @@
 	;;
 }
 
+const aclose = {a
+	close(a.lfd)
+	free(a)
+}
+
 const announcesock = {proto, str
 	var sa4 : sys.sockaddr_in
 	var sa6 : sys.sockaddr_in6
@@ -82,7 +94,7 @@
 	if sys.listen((sock : sys.fd), 10) < 0
 		-> `Err "unable to listen on socket"
 	;;
-	-> `Ok (sock : fd)
+	-> `Ok mk([.lfd=(sock : fd)])
 }
 
 const announceunix = {path
@@ -108,16 +120,19 @@
 	if sys.bind(sock, (&sa : sys.sockaddr#), sizeof(sys.sockaddr_un)) < 0
 		-> `Err "failed to bind address"
 	;;
-	-> `Ok (sock : fd)
+	if sys.listen((sock : sys.fd), 10) < 0
+		-> `Err "unable to listen on socket"
+	;;
+	-> `Ok mk([.lfd=(sock : fd)])
 
 }
 
-const accept = {lfd
+const accept = {a
 	var sa : sys.sockaddr_storage
 	var len : sys.size
 	var fd
 
-	fd = sys.accept((lfd : sys.fd), (0 : sys.sockaddr#), (0 : sys.size#))
+	fd = sys.accept((a.lfd : sys.fd), (0 : sys.sockaddr#), (0 : sys.size#))
 	if fd < 0
 		-> `Err "unable to accept socket"
 	;;