mirror of
https://github.com/janet-lang/janet
synced 2025-01-25 22:56:52 +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:
parent
79bb9e54d5
commit
f4a46ba6ea
@ -1,7 +1,6 @@
|
|||||||
(def conn (net/connect "127.0.0.1" "8000"))
|
(with [conn (net/connect "127.0.0.1" "8000")]
|
||||||
(printf "Connected to %q!" conn)
|
(printf "Connected to %q!" conn)
|
||||||
(net/write conn "Echo...")
|
(:write conn "Echo...")
|
||||||
(print "Wrote to connection...")
|
(print "Wrote to connection...")
|
||||||
(def res (net/read conn 1024))
|
(def res (:read conn 1024))
|
||||||
(pp res)
|
(pp res))
|
||||||
(net/close conn)
|
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
(defn handler
|
(defn handler
|
||||||
"Simple handler for connections."
|
"Simple handler for connections."
|
||||||
[stream]
|
[stream]
|
||||||
(def id (gensym))
|
(defer (:close stream)
|
||||||
(def b @"")
|
(def id (gensym))
|
||||||
(print "Connection " id "!")
|
(def b @"")
|
||||||
(while (net/read stream 1024 b)
|
(print "Connection " id "!")
|
||||||
(net/write stream b)
|
(while (:read stream 1024 b)
|
||||||
(buffer/clear b))
|
(:write stream b)
|
||||||
(printf "Done %v!" id)
|
(buffer/clear b))
|
||||||
(net/close stream))
|
(printf "Done %v!" id)))
|
||||||
|
|
||||||
(net/server "127.0.0.1" "8000" handler)
|
(net/server "127.0.0.1" "8000" handler)
|
||||||
|
@ -50,12 +50,14 @@ typedef struct {
|
|||||||
|
|
||||||
static int janet_stream_close(void *p, size_t s);
|
static int janet_stream_close(void *p, size_t s);
|
||||||
|
|
||||||
static int janet_stream_getter(void *p, size_t, Janet key);
|
static int janet_stream_getter(void *p, Janet key, Janet *out);
|
||||||
|
|
||||||
static const JanetAbstractType StreamAT = {
|
static const JanetAbstractType StreamAT = {
|
||||||
"core/stream",
|
"core/stream",
|
||||||
janet_stream_close,
|
janet_stream_close,
|
||||||
JANET_ATEND_GC
|
NULL,
|
||||||
|
janet_stream_getter,
|
||||||
|
JANET_ATEND_GET
|
||||||
};
|
};
|
||||||
|
|
||||||
static int janet_stream_close(void *p, size_t s) {
|
static int janet_stream_close(void *p, size_t s) {
|
||||||
@ -523,6 +525,20 @@ static Janet cfun_stream_write(int32_t argc, Janet *argv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const JanetMethod stream_methods[] = {
|
||||||
|
{"chunk", cfun_stream_chunk},
|
||||||
|
{"close", cfun_stream_close},
|
||||||
|
{"read", cfun_stream_read},
|
||||||
|
{"write", cfun_stream_write},
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
static int janet_stream_getter(void *p, Janet key, Janet *out) {
|
||||||
|
(void) p;
|
||||||
|
if (!janet_checktype(key, JANET_KEYWORD)) return 0;
|
||||||
|
return janet_getmethod(janet_unwrap_keyword(key), stream_methods, out);
|
||||||
|
}
|
||||||
|
|
||||||
static const JanetReg net_cfuns[] = {
|
static const JanetReg net_cfuns[] = {
|
||||||
{
|
{
|
||||||
"net/server", cfun_net_server,
|
"net/server", cfun_net_server,
|
||||||
|
Loading…
Reference in New Issue
Block a user