mirror of
https://github.com/janet-lang/janet
synced 2025-12-02 22:58:09 +00:00
Working TCP echo server and client.
Required a few changes to APIs, namely janet_root_fiber() to get topmost fiber that is active in the current scheduler. This is distinct from janet_current_fiber(), which gets the bottom most fiber in the fiber stack - it might have a parent, and so cannot be reliably resumed. This is the kind of situation that makes symmetric coroutines more attractive.
This commit is contained in:
13
examples/tcpserver.janet
Normal file
13
examples/tcpserver.janet
Normal file
@@ -0,0 +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))
|
||||
|
||||
(net/server "127.0.0.1" "8000" handler)
|
||||
Reference in New Issue
Block a user