easy-nio.channel

Wrapper around java.nio.channels for non-blocking network I/O.

Channels are stateful. Functions that mutate channel state use the ! suffix convention.

accept!

(accept! ch)

Accepts an incoming connection on ch.

In blocking mode: blocks until a connection arrives and returns the new SocketChannel.

In non-blocking mode: returns the new SocketChannel immediately, or nil if no connection is pending.

bind!

(bind! ch addr)(bind! ch addr backlog)

Binds ch to addr (an InetSocketAddress).

For ServerSocketChannel, accepts an optional backlog — the maximum number of pending connections. Defaults to 0 (system default).

blocking?

(blocking? ch)

Returns true if ch is in blocking mode.

configure-blocking!

(configure-blocking! ch blocking)

Sets the blocking mode of ch. Pass false for non-blocking I/O. Returns ch.

connect!

(connect! ch addr)

Connects ch to addr (an InetSocketAddress).

In blocking mode: blocks until the connection is established and returns true.

In non-blocking mode: initiates the connection and returns false if it cannot be completed immediately. Call finish-connect! later to complete it (typically after a Selector signals OP_CONNECT).

connected?

(connected? ch)

Returns true if ch has an established connection.

connection-pending?

(connection-pending? ch)

Returns true if a non-blocking connect! has been initiated on ch but not yet completed via finish-connect!.

datagram-bind!

(datagram-bind! ch addr)

Binds ch (a DatagramChannel) to addr (an InetSocketAddress). Pass (inet-address port) with no host to bind to the wildcard address.

datagram-channel

(datagram-channel)

Opens and returns a new DatagramChannel in blocking mode.

datagram-connect!

(datagram-connect! ch addr)

Connects ch (a DatagramChannel) to addr. This is optional and restricts the channel to only send/receive datagrams from that address. Not required for general UDP usage.

datagram-connected?

(datagram-connected? ch)

Returns true if ch (a DatagramChannel) is connected to a specific remote address.

disconnect!

(disconnect! ch)

Disconnects ch (a DatagramChannel) from its connected address, if any. Safe to call on an unconnected channel. Returns ch.

finish-connect!

(finish-connect! ch)

Completes a non-blocking connection initiated by connect!. Returns true if the connection is now established, false if it is still in progress.

get-option

(get-option ch option)

Gets the current value of a socket option on ch.

inet-address

(inet-address port)(inet-address host port)

Constructs an InetSocketAddress from host (String) and port (int). With arity-1, constructs a wildcard address bound to port.

local-address

(local-address ch)

Returns the local SocketAddress bound to ch, or nil if unbound.

remote-address

(remote-address ch)

Returns the remote SocketAddress connected to ch, or nil if the channel is not connected.

server-socket-channel

(server-socket-channel)

Opens and returns a new ServerSocketChannel in blocking mode.

set-option!

(set-option! ch option value)

Sets a socket option on ch. option should be a constant from java.net.StandardSocketOptions e.g. StandardSocketOptions/TCP_NODELAY. Returns ch.

shutdown-input!

(shutdown-input! ch)

Shuts down the read half of ch without closing the channel. Subsequent reads will return -1 (EOF). The write half remains open. Returns ch. Throws NotYetConnectedException if the channel is not connected.

shutdown-output!

(shutdown-output! ch)

Shuts down the write half of ch without closing the channel. Subsequent writes will throw ClosedChannelException. The read half remains open, allowing any in-flight data from the remote end to still be received. Returns ch. Throws NotYetConnectedException if the channel is not connected.

socket-channel

(socket-channel)(socket-channel addr)

Opens and returns a new SocketChannel in blocking mode. With arity-1, opens and immediately connects to addr.