Module Net.Listener

Network connection listeners.

Listeners

type t

The type for connection listeners.

val open' : ?nonblock:bool -> ?socket_type:Unix.socket_type -> ?backlog:int -> endpoint:Endpoint.t -> unit -> (t, string) Stdlib.result

open' ~endpoint () opens a connection listener on endpoint with:

  • nonblock if true the listening socket and accepted connection are in non-blocking mode (default).
  • socket_type is the type of connection. Defaults to Unix.SOCK_STREAM or the fd's type if on is `Fd fd – if socket_type is specified in this case this errors if fd is not of the specified type.
  • backlog is the argument for Unix.listen (defaults to 128). Only relevant if socket_type is Unix.SOCK_STREAM.
  • If a new file descriptor is created it is with cloexec set to true.
  • If peer is `Fd fd it is assumed to be already bound with Unix.bind. So the call just tries to honour the nonblock argument, gets local address from the fd and calls Unix.listen if the socket type is Unix.SOCK_STREAM. Also note that in this case close_noerr does not close fd it is your duty to do so.

If a listener is returned it must always eventually be closed with close_noerr.

Note. If you used an endpoint with a port 0, endpoint or endpoint_addr have the port that has been attributed by the operating system.

val close_noerr : t -> unit

close_noerr l closes l without raising errors and can be safely called repeatedly. Note that if endpoint c is `Fd fd this does not close fd.

Accepting connections

val accept : t -> (Connection.t, string) Stdlib.result

accept l offers a connection on l. Blocks until a client connects. If a connection is returned, it must always eventually be closed with Connection.close_noerr.

Actions

val wait_connection : t -> 'tag -> 'tag Affect.Action.t

wait_connection l t is the action used by accept. The action invocation is enabled and synchronizes with tag whenever accept can be called without blocking. This is just Affect_unix.Unix.wait_readable on the listener fd.

Properties

val fd : t -> Unix.file_descr

fd l is the listening file descriptor of l.

val endpoint : t -> Endpoint.t

endpoint l is the endpoint on which of l is listening. If you open'ed with a port of 0 this gives you the endpoint with the port that was attributed by the operating system.

val endpoint_addr : t -> Unix.sockaddr

endpoint_addr l is the socket address on which l is listening.

val socket_type : t -> Unix.socket_type

socket_type l is the socket type of the listener.

Formatting

val error : t -> string -> string

error l msg formats an error message for listener l.

val pp : Stdlib.Format.formatter -> t -> unit

pp formats connection listeners for inspection.

val pp_debug : Stdlib.Format.formatter -> t -> unit

pp_debug formats connection listeners for more inspection.