summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2017-09-12 21:06:38 -0700
committerOri Bernstein <ori@eigenstate.org>2017-09-12 21:07:13 -0700
commit52a9615661dfd2c9d4114b1a6f1d48830c3e3427 (patch)
tree45b03bc265ee9d04d88ae9c8638dc0cc6162f079 /lib
parent615d1d878c603e55120b5d4250d3173fba19f135 (diff)
downloadmc-52a9615661dfd2c9d4114b1a6f1d48830c3e3427.tar.gz
Make announce API more cross platform.
Encapsulate announcement into a struct.
Diffstat (limited to 'lib')
-rw-r--r--lib/http/server.myr10
-rw-r--r--lib/http/types.myr2
-rw-r--r--lib/std/listen+plan9.myr76
-rw-r--r--lib/std/listen+posixy.myr27
4 files changed, 66 insertions, 49 deletions
diff --git a/lib/http/server.myr b/lib/http/server.myr
index 90fba7e..d256b18 100644
--- a/lib/http/server.myr
+++ b/lib/http/server.myr
@@ -16,10 +16,10 @@ pkg http =
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 statusstr = {st
}
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
;;
diff --git a/lib/http/types.myr b/lib/http/types.myr
index 357c3fe..68e2c28 100644
--- a/lib/http/types.myr
+++ b/lib/http/types.myr
@@ -15,7 +15,7 @@ pkg http =
type server = struct
- lfd : std.fd
+ ann : std.announce#
refs : uint32
quit : bool
;;
diff --git a/lib/std/listen+plan9.myr b/lib/std/listen+plan9.myr
index cf1c921..be82bcf 100644
--- 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 Maxpath = 256
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 @@ const announce = {ds
match read(ctl, nbuf[:])
| `Err e: ->`Err "unable to read ctl fd"
- | `Ok nn: l = bfmt(lbuf[:], "{}/{}/listen", f[:dir], nbuf[:nn])
- ;;
-
- put("announce {}\n", a)
- n = fput(ctl, "announce {}", a)
- if n <= 0
- close(ctl)
- -> `Err "writing announce"
+ | `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]),
+ ])
;;
- /* 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"
diff --git a/lib/std/listen+posixy.myr b/lib/std/listen+posixy.myr
index 7f4b35b..49d1054 100644
--- a/lib/std/listen+posixy.myr
+++ b/lib/std/listen+posixy.myr
@@ -5,6 +5,7 @@ use "chartype"
use "dialparse"
use "die"
use "endian"
+use "mk"
use "option"
use "resolve"
use "result"
@@ -16,8 +17,14 @@ use "syswrap"
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 announce = {ds
;;
}
+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 @@ const announcesock = {proto, str
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 @@ const announceunix = {path
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"
;;