4.90. kink/socket/TCP_SERVER

4.90.1. type tcp_server

tcp_server type represents a listening socket of 1) TCP/IP, or 2) stream based Unix Domain Socket.

4.90.1.1. Serv.bind_listen(Local ...[$config_fun={}])

bind_listen method binds the local address to the socket, and starts waiting for incoming connections.

If the address is bound, and the socket starts waiting for incoming connections, bind_listen tail-calls the success cont with no arg.

If an IO error occurs, bind_listen tail-calls the error cont with a str as the error message.

Preconditions

• `Local` must be a socket_address val.

• $config_fun must be a fun which takes a config val.

Config val methods

• C.backlog(Backlog): specifies the maximum number of pending connections. `Backlog` must be a positive int num. If `backlog` method is not called, the system default is used.

• C.on_success($success): specifies the success cont. $success must be a thunk. If on_success is not called, a NOP fun like `{}` is used as the default success cont.

• C.on_error($error): specifies the error cont. $error must be a fun which takes a str as the error message. If on_error is not called, a fun which raises an exception is used as the default error cont.

4.90.1.2. Serv.accept(...[$config_fun={}])

`accept` method accepts an incoming connection. The newly created connecting socket is in blocking mode.

In blocking mode

When the socket is is blocking mode, `accept` method waits until a connection becomes available, or an IO error occurs.

If a connection is established, `accept` method tail-calls the present cont with the tcp_connection val of the connecting socket.

If an IO error occurs, `accept` method tail-calls the error cont with a str as the error message.

In non-blocking mode

When the socket is in non-blocking mode, `accept` doesn't wait for an incoming connection.

If a connection is established immediately, `accept` method tail-calls the present cont with the tcp_connection val of the connecting socket.

If no connection is available, `accept` method tail-calls the absent cont with no arg.

If an IO error occurs, `accept` method tail-calls the error cont with a str as the error message.

Precondition

• $config_fun must be a fun which takes a config val.

Config val methods

• C.on_present($present): specifies the present cont. $present must be a fun which takes a tcp_connection val. If on_present method is not called, VAL.identity is used as the default present cont.

• C.on_absent($absent): specifies the absent cont. $absent must be a thunk. If on_absent method is not called, a fun which raises an exception is used as the default absent cont.

• C.on_error($error): specifies the error cont. $error must be a fun which takes a str as the error message. If on_error is not called, a fun which raises an exception is used as the default error cont.

4.90.1.3. Serv.close(...[$config_fun={}])

`close` method closes the socket.

If the socket is closed successfully, `close` tail-calls the success cont.

If an IO error occurs, `close` tail-calls the error cont with a str as the error message.

Precondition

• $config_fun must be a fun which takes a config val.

Config fun methods

• C.on_success($success): specifies the success cont. $success must be a fun which takes no arg. If on_success method is not called, a NOP fun like `{}` is used as the default success cont.

• C.on_error($error): specifies the error cont. $error must be a fun which takes a str as the error message. If on_error method is not called, a fun which raises an exception is used as the default error cont.

4.90.1.4. Serv.local_address(...[$config_fun])

local_address method retrieves the socket_address val of the local address of the listening socket.

If the local address is available, `local_address` tail-calls the present cont with the socket_address val.

If the local address is not available, `local_address` tail-calls the absent cont with no arg.

If an IO error occurs, `local_address` tail-calls the error cont with a str as an error message.

Config val method

• C.on_present($present): specifies the present cont.

• C.on_absent($absent): specifies the absent cont.

• C.on_error($error): specifies the error cont.

4.90.1.5. Serv.blocking?

`blocking?` method returns whether the socket is in blocking mode.

4.90.1.6. Serv.set_blocking

set_blocking method turns on blocking mode.

4.90.1.7. Serv.so_reuseaddr?

so_reuseaddr? method returns a bool val of SO_REUSEADDR option.

Precondition: The protocol family must be ipv4 or ipv6.

4.90.1.8. Serv.set_so_reuseaddr

set_so_reuseaddr method turns on SO_REUSEADDR option.

Precondition: The protocol family must be ipv4 or ipv6.

4.90.1.9. Serv.unset_so_reuseaddr

unset_so_reuseaddr method turns off SO_REUSEADDR option.

Precondition: The protocol family must be ipv4 or ipv6.

4.90.1.10. Serv.so_rcvbuf

so_rcvbuf method returns the int num of SO_RCVBUF option.

4.90.1.11. Serv.set_so_rcvbuf(Buf_size)

set_so_rcvbuf method sets Buf_size as SO_RCVBUF option.

Precondition: Buf_size must be a non-negative int num.

4.90.1.12. Serv.unset_blocking

unset_blocking method turns off blocking mode. It sets the listening socket in non-blocking mode.

4.90.2. TCP_SERVER.open(Protocol_family ...[$config_fun={}])

`open` method makes a new listening socket. The newly created listening socket is in blocking mode.

If a listening socket is created, `open` tail-calls the success cont with a tcp_server val of the listening socket.

If an IO error occurs, `open` tail-calls the error cont with a str as the error message.

If Protocol_family is not supported, `open` tail-calls the unsupported cont with no arg.

Preconditions

• Protocol_family must be a protocol_family val.

• $config_fun must be a fun which takes a config val.

Config val methods

• C.on_success($success): specifies $success as the success cont. $success must be a fun which takes a protocol_family val. If on_success method is not called, VAL.identity is used as the default success cont.

• C.on_error($error): speciifes $error as the error cont. $error must be a fun which takes a str as the error message. If on_error method is not called, a fun which raises an exception is used as the default error cont.

• C.on_unsupported($unsupported): specifies $unsupported as the unsupported cont. $unsupported must be a fun which takes no arg. If on_unsupported method is not called, a fun which tail-calls the error cont is used as the default unsupported cont.

4.90.3. TCP_SERVER.is?(Val)

`is?` method returns whether `Val` is a tcp_server.