TcpStream

buffered

external

(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

external

(Fn [(Ref (Array Byte) a)] ())

resets a byte buffer's length to zero without freeing memory.

close

external

(Fn [TcpStream] ())

closes the stream, consuming it.

close!

external

(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

defn

(Fn [(Ref String a), Int] (Result TcpStream String))

                        (connect addr port)
                    

connects to a remote TCP address. Returns (Result TcpStream String).

connect-timeout

defn

(Fn [(Ref String a), Int, Int] (Result TcpStream String))

                        (connect-timeout addr port timeout)
                    

connects with a timeout in seconds. Returns (Result TcpStream String).

copy

external

(Fn [(Ref TcpStream a)] TcpStream)

delete

defn

(Fn [TcpStream] ())

                        (delete s)
                    

peer-address

external

(Fn [(Ref TcpStream a)] String)

returns the IP address of the remote peer.

peer-port

external

(Fn [(Ref TcpStream a)] Int)

returns the port of the remote peer.

poll-fd

defn

(Fn [(Ref TcpStream a)] Int)

                        (poll-fd s)
                    

prn

defn

(Fn [(Ref TcpStream a)] String)

                        (prn s)
                    

read

defn

(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

defn

(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-append-nb

defn

(Fn [(Ref TcpStream 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:

  • > 0 bytes appended to buf,
  • 0 peer closed cleanly (EOF),
  • read-blocked (-2) socket has no data right now, retry on the next readable event.

read-blocked

def

Int

sentinel returned by read-append-nb when no data is currently available on a non-blocking socket.

read-bytes

defn

(Fn [(Ref TcpStream a)] (Result (Array Byte) b))

                        (read-bytes stream)
                    

reads up to 4096 bytes as a byte array.

send

defn

(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

defn

(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

defn

(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.

send-nb

defn

(Fn [(Ref TcpStream 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.

sendfile-chunk

defn

(Fn [(Ref TcpStream a), Int, (Ref Long b), Long] (Result Int String))

                        (sendfile-chunk stream file-fd offset count)
                    

transfers up to count bytes from open file descriptor file-fd starting at offset directly to the socket, without user-space buffering. Updates offset with the new position.

Returns (Result Int String) where Int is bytes transferred (0 means would-block on a non-blocking socket).

set-keepalive

external

(Fn [(Ref TcpStream a), Int] ())

enables or disables TCP keep-alive.

set-linger

external

(Fn [(Ref TcpStream a), Int] ())

sets the linger timeout in seconds. Pass -1 to disable linger.

set-nodelay

external

(Fn [(Ref TcpStream a)] ())

disables Nagle's algorithm for lower latency.

set-nonblocking

external

(Fn [(Ref TcpStream 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.

set-read-timeout

external

(Fn [(Ref TcpStream a), Int] ())

sets the read timeout in seconds.

set-recv-buffer

external

(Fn [(Ref TcpStream a), Int] ())

sets the receive buffer size in bytes.

set-send-buffer

external

(Fn [(Ref TcpStream a), Int] ())

sets the send buffer size in bytes.

set-timeout

external

(Fn [(Ref TcpStream a), Int] ())

sets read and write timeouts in seconds.

shutdown

defn

(Fn [(Ref TcpStream a), Int] ())

                        (shutdown stream how)
                    

shuts down the stream. 0 = reads, 1 = writes, 2 = both.

shutdown-

external

(Fn [(Ref TcpStream a), Int] ())

shutdown-read

external

(Fn [(Ref TcpStream a)] ())

shuts down the read side of the stream.

shutdown-write

external

(Fn [(Ref TcpStream a)] ())

shuts down the write side of the stream.

with-stream

macro

Macro

                        (with-stream name addr port :rest forms)
                    

connects, executes forms, then closes the stream.