summaryrefslogtreecommitdiff
path: root/lib/std/listen+posixy.myr
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/listen+posixy.myr')
-rw-r--r--lib/std/listen+posixy.myr34
1 files changed, 22 insertions, 12 deletions
diff --git a/lib/std/listen+posixy.myr b/lib/std/listen+posixy.myr
index be4456d..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,9 +17,14 @@ use "syswrap"
use "utf"
pkg std =
- const announce : (ds : byte[:] -> result(fd, byte[:]))
- const listen : (sock : fd -> 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
@@ -31,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
@@ -80,7 +91,10 @@ const announcesock = {proto, str
if sys.bind(sock, sa, sz) < 0
-> `Err "failed to bind socket"
;;
- -> `Ok (sock : fd)
+ if sys.listen((sock : sys.fd), 10) < 0
+ -> `Err "unable to listen on socket"
+ ;;
+ -> `Ok mk([.lfd=(sock : fd)])
}
const announceunix = {path
@@ -106,23 +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)
-
-}
-
-const listen = {sock : std.fd -> result(fd, byte[:])
if sys.listen((sock : sys.fd), 10) < 0
-> `Err "unable to listen on socket"
;;
- -> `Ok (sys.dup((sock : sys.fd)) : fd)
+ -> `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"
;;