teensyp.stream

A namespace of utility functions for integrating streams into Teensyp.

input-stream

(input-stream readf)(input-stream readf closef)

Create an InputStream from a read and optional close function. The read function maps to the .read method on the InputStream class and takes 3 arguments: a byte array to receive the data, an offset and a length. The read function should return the number of bytes read, or -1 if the stream is closed. The close function maps to the .close method and takes zero arguments.

input-stream-handler

(input-stream-handler f)(input-stream-handler f {:keys [executor on-close read-buffer-size], :or {executor (new-default-executor), on-close (fn [_sock]), read-buffer-size 8192}})

Create a TeensyP server handler from a function f that takes an InputStream and a TeensyP Socket as arguments. The function will be executed in a separate thread when the 1-argument accept arity of the handler is called.

Accepts an options map with the following keys:

:executor - an executor for running the supplied function, defaults to a fixed thread pool of 32 threads :on-close - a function called when the InputStream is closed, takes a single socket argument :read-buffer-size - the size in bytes of the read buffer, defaults to 8K

Triggering the 2-argument close arity of the handler will close the associated InputStream.

output-stream

(output-stream writef)(output-stream writef closef)(output-stream writef closef flushf)

Create an OutputStream from a write function and optional close and flush functions. The write function maps to the .write method on the OutputStream class and takes 3 arguments: a byte array with the data to send, an offset and a length. The close function maps to the .close method and takes zero arguments. Similarly the flush function maps to the .flush method and also takes zero arguments.

socket->output-stream

(socket->output-stream socket)(socket->output-stream socket {:keys [on-close]})

Create a blocking OutputStream from a TeensyP Socket. Writing to the stream will queue a write to the socket, and block until that write had been sent.

Closing the stream will close the socket by default, but this behavior can be overridden by supplying a custom close function that takes the socket as its argument, via the :on-close option.

stream-handler

(stream-handler f)(stream-handler f options)

Create a Teensyp server handler from a function that takes an InputStream and OutputStream as arguments. This combines the socket->output-stream and input-stream-handler functions.

Accepts an options map with the following keys:

:executor - an executor for running the handler function, defaults to a fixed thread pool of 32 threads :read-buffer-size - the size in bytes of the read buffer, defaults to 8K

The socket will be closed when both the InputStream and OutputStream are closed, or when the ‘close’ 2-arity of the returned function is called. Closing the OutputStream will prevent further writes, and closing the InputStream will prevent further reads. Data received after the InputStream has been closed will be silently dropped.