Module Net.Connection

Network connections.

Connections

type t

The type for connections.

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

open ~peer () opens a connection to a peer listening on peer with:

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

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

val try_open : ?nonblock:bool -> ?socket_type:Unix.socket_type -> peer:Endpoint.t -> unit -> (t option, string) Stdlib.result

try_open ~endpoint is like open' except None is returned if Unix.connect errors with ENOENT, ECONNREFUSED or EHOSTUNREACH.

val close_noerr : t -> unit

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

Properties

val fd : t -> Unix.file_descr

fd c is the file descriptor of c.

val peer : t -> Endpoint.t

peer c is the peer of c.

val peer_addr : t -> Unix.sockaddr

peer_addr c is the peer address for c.

val our_addr : t -> Unix.sockaddr

our_addr c is our address for c.

val socket_type : t -> Unix.socket_type

socket_type c is the socket type of c.

Formatting

val error : t -> string -> string

error c msg formats an error message for connection c.

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

pp formats connections for inspection.

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

pp_debug formats connection for more inspection.