UnixStream
buffered
(Fn [UnixStream] BufReader)
wraps this UnixStream in a BufReader for buffered I/O. Takes ownership of the stream — the BufReader will close it when deleted.
(match (UnixStream.connect "/tmp/my.sock")
(Result.Success s)
(let [br (UnixStream.buffered s)]
(do
(BufReader.write &br "hello\n")
(ignore (BufReader.flush &br))
(match (BufReader.read-line &br)
(Result.Success line) (println* &line)
_ ())
(BufReader.delete br)))
_ ())
close!
(Fn [(Ref UnixStream a)] ())
closes the stream by reference, for use when the stream lives in a collection. Sets the fd to -1 to prevent double-close.
connect
(Fn [(Ref String a)] (Result UnixStream String))
(connect path)
connects to a Unix domain socket at the given path. Returns (Result UnixStream String).
read
(Fn [(Ref UnixStream a)] (Result String b))
(read stream)
reads up to 4096 bytes from the stream. Returns the data as a string, or an error. Returns an empty string on connection close.
read-append
(Fn [(Ref UnixStream a), (Ref (Array Byte) b)] (Result Int String))
(read-append stream buf)
reads from the stream and appends to an existing byte buffer. Returns bytes read (0 = connection closed), or an error.
read-append-nb
(Fn [(Ref UnixStream a), (Ref (Array Byte) b)] (Result Int String))
(read-append-nb stream buf)
non-blocking append-read. Reads whatever the kernel
has buffered into buf, growing it as needed.
Returns (Result Int String). The Int is one of:
> 0bytes appended tobuf,0peer closed cleanly (EOF),read-blocked(-2) socket has no data right now, retry on the next readable event.
read-blocked
Int
sentinel returned by read-append-nb when no data is
currently available on a non-blocking socket.
read-bytes
(Fn [(Ref UnixStream a)] (Result (Array Byte) b))
(read-bytes stream)
reads up to 4096 bytes as a byte array.
send
(Fn [(Ref UnixStream a), (Ref String b)] (Result Int String))
(send stream msg)
sends a string over the stream. Returns bytes sent or an error.
send-bytes
(Fn [(Ref UnixStream a), (Ref (Array Byte) b)] (Result Int String))
(send-bytes stream data)
sends binary data over the stream. Returns bytes sent or an error.
send-len
(Fn [(Ref UnixStream a), (Ref String b), Int] (Result Int String))
(send-len stream msg len)
sends a string with known length (avoids strlen). len must be
between 0 and the string’s length; returns an error otherwise.
Returns bytes sent or an error.
send-nb
(Fn [(Ref UnixStream a), (Ref (Array Byte) b), Int] (Result Int String))
(send-nb stream data offset)
non-blocking send. Sends as many bytes as the kernel will
accept right now from data, starting at offset.
Returns (Result Int String). The Int is the number of bytes actually
written, which may be 0 if the socket is not currently writable. Re-arm
write interest on the next event-loop iteration in that case.
set-nonblocking
(Fn [(Ref UnixStream a)] ())
puts the socket into non-blocking mode. After this
call, send-nb and read-append-nb are the appropriate I/O entry points;
the blocking variants will return EAGAIN instead of waiting.
shutdown
(Fn [(Ref UnixStream a), Int] ())
(shutdown stream how)
shuts down the stream. 0 = reads, 1 = writes, 2 = both.
with-stream
Macro
(with-stream name path :rest forms)
connects, executes forms, then closes the stream.