1
0
mirror of https://github.com/janet-lang/janet synced 2025-10-06 11:32:29 +00:00

Add methods to streams.

This makes streams polymorphic with files in many cases.
printf family functions still need porting.
This commit is contained in:
Calvin Rose
2020-02-12 09:32:41 -06:00
parent 79bb9e54d5
commit f4a46ba6ea
3 changed files with 32 additions and 17 deletions

View File

@@ -1,7 +1,6 @@
(def conn (net/connect "127.0.0.1" "8000"))
(printf "Connected to %q!" conn)
(net/write conn "Echo...")
(print "Wrote to connection...")
(def res (net/read conn 1024))
(pp res)
(net/close conn)
(with [conn (net/connect "127.0.0.1" "8000")]
(printf "Connected to %q!" conn)
(:write conn "Echo...")
(print "Wrote to connection...")
(def res (:read conn 1024))
(pp res))

View File

@@ -1,13 +1,13 @@
(defn handler
"Simple handler for connections."
[stream]
(def id (gensym))
(def b @"")
(print "Connection " id "!")
(while (net/read stream 1024 b)
(net/write stream b)
(buffer/clear b))
(printf "Done %v!" id)
(net/close stream))
(defer (:close stream)
(def id (gensym))
(def b @"")
(print "Connection " id "!")
(while (:read stream 1024 b)
(:write stream b)
(buffer/clear b))
(printf "Done %v!" id)))
(net/server "127.0.0.1" "8000" handler)