TcpStream
buffered
(Fn [TcpStream] BufReader)
wraps this TcpStream in a BufReader for buffered I/O. Takes ownership of the stream — the BufReader will close it when deleted.
(match (TcpStream.connect "example.com" 80)
(Result.Success s)
(let [br (TcpStream.buffered s)]
(do
(BufReader.write &br "GET / HTTP/1.0\r\n\r\n")
(ignore (BufReader.flush &br))
(match (BufReader.read-line &br)
(Result.Success line) (println* &line)
_ ())
(BufReader.delete br)))
_ ())
clear-buf
(Fn [(Ref (Array Byte) a)] ())
resets a byte buffer's length to zero without freeing memory.
close!
(Fn [(Ref TcpStream 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), Int] (Result TcpStream String))
(connect addr port)
connects to a remote TCP address. Returns (Result TcpStream String).
connect-timeout
(Fn [(Ref String a), Int, Int] (Result TcpStream String))
(connect-timeout addr port timeout)
connects with a timeout in seconds. Returns (Result TcpStream String).
read
(Fn [(Ref TcpStream 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 TcpStream 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-bytes
(Fn [(Ref TcpStream a)] (Result (Array Byte) b))
(read-bytes stream)
reads up to 4096 bytes as a byte array.
send
(Fn [(Ref TcpStream 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 TcpStream 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 TcpStream a), (Ref String b), Int] (Result Int String))
(send-len stream msg len)
sends a string with known length (avoids strlen). Returns bytes sent or an error.
set-linger
(Fn [(Ref TcpStream a), Int] ())
sets the linger timeout in seconds. Pass -1 to disable linger.
shutdown
(Fn [(Ref TcpStream a), Int] ())
(shutdown stream how)
shuts down the stream. 0 = reads, 1 = writes, 2 = both.
with-stream
Macro
(with-stream name addr port :rest forms)
connects, executes forms, then closes the stream.