TlsStream

accept

defn

(Fn [(Ref TlsServerCtx a), Int] (Result TlsStream String))

                        (accept server-ctx fd)
                    

wraps an existing TCP file descriptor with a server-side TLS session. The fd should come from a TCP accept call. Takes ownership of the fd: on success the returned stream closes it (via close); on error accept closes it. Either way, do not close the fd yourself. Returns (Result TlsStream String).

close

external

(Fn [TlsStream] ())

closes the TLS stream, consuming it. Performs SSL shutdown.

close!

external

(Fn [(Ref TlsStream a)] ())

closes the TLS stream by reference. Safe against double-close.

connect

defn

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

                        (connect host port)
                    

connects to a remote host over TLS. Returns (Result TlsStream String).

copy

external

(Fn [(Ref TlsStream a)] TlsStream)

shallow copy: the result shares the underlying SSL session and socket fd with the original. It is a non-owning alias, so close exactly one copy; closing more than one double-frees the SSL session. Mirrors TcpStream from the socket library.

read

defn

(Fn [(Ref TlsStream a)] (Result String String))

                        (read stream)
                    

reads up to 4096 bytes from the TLS stream. Returns the data as a string. Returns an empty string on a clean connection close, or an error if the read failed (timeout, truncation, or a fatal protocol error).

read-append

defn

(Fn [(Ref TlsStream a), (Ref (Array Byte) b)] (Result Int String))

                        (read-append stream buf)
                    

reads from the TLS stream and appends to an existing byte buffer. Returns bytes read (0 = connection closed), or an error.

read-append-nb

defn

(Fn [(Ref TlsStream a), (Ref (Array Byte) b)] (Result Int String))

                        (read-append-nb stream buf)
                    

non-blocking append-read. Reads whatever data TLS has available 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 TlsStream a)] (Result (Array Byte) String))

                        (read-bytes stream)
                    

reads up to 4096 bytes as a byte array. Returns an empty array on a clean connection close, or an error if the read failed.

send

defn

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

                        (send stream msg)
                    

sends a string over the TLS stream. Returns bytes sent or an error.

send-bytes

defn

(Fn [(Ref TlsStream a), (Ref (Array Byte) b)] (Result Int String))

                        (send-bytes stream data)
                    

sends binary data over the TLS stream. Returns bytes sent or an error.

send-nb

defn

(Fn [(Ref TlsStream a), (Ref (Array Byte) b), Int] (Result Int String))

                        (send-nb stream data offset)
                    

non-blocking send. Sends as many bytes as TLS 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 (including TLS renegotiation). Re-arm write interest on the next event-loop iteration in that case.

set-nonblocking

external

(Fn [(Ref TlsStream a)] ())

puts the underlying socket into non-blocking mode. After this call, send-nb and read-append-nb are the appropriate I/O entry points; the blocking variants do not handle would-block correctly.

set-timeout

external

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

sets read and write timeouts in seconds.

with-stream

macro

Macro

                        (with-stream name host port :rest forms)
                    

connects over TLS, executes forms, then closes the stream.