Module Async_extra.Tcp

Tcp supports connection to inet sockets and unix sockets. These are two different types. We use 'a where_to_connect to specify a socket to connect to, where the 'a identifies the type of socket.

type 'a where_to_connect constraint 'a = [< Async_extra.Import.Socket.Address.t ]
val to_host_and_port : ?via_local_interface:Import.Unix.Inet_addr.t ‑> ?via_local_port:int ‑> string ‑> int ‑> Import.Socket.Address.Inet.t where_to_connect
val to_inet_address : ?via_local_interface:Import.Unix.Inet_addr.t ‑> ?via_local_port:int ‑> Import.Socket.Address.Inet.t ‑> Import.Socket.Address.Inet.t where_to_connect
val to_file : string ‑> Import.Socket.Address.Unix.t where_to_connect
val to_unix_address : Import.Socket.Address.Unix.t ‑> Import.Socket.Address.Unix.t where_to_connect
type 'a with_connect_options = ?buffer_age_limit:[ `At_most of Core.Time.Span.t | `Unlimited ] ‑> ?interrupt:unit Import.Deferred.t ‑> ?reader_buffer_size:int ‑> ?timeout:Core.Time.Span.t ‑> 'a
val with_connection : ('addr where_to_connect ‑> (([ `Active ], 'addrImport.Socket.t ‑> Import.Reader.t ‑> Import.Writer.t ‑> 'a Import.Deferred.t) ‑> 'a Import.Deferred.t) with_connect_options

with_connection ~host ~port f looks up host from a string (using DNS as needed), connects, then calls f, passing the connected socket and a reader and writer for it. When the deferred returned by f is determined, or any exception is thrown, the socket, reader and writer are closed. The return deferred is fulfilled after f has finished processing and the file descriptor for the socket is closed. If interrupt is supplied then the connection attempt will be aborted if interrupt is fulfilled before the connection has been established. Similarly, all connection attempts have a timeout (default 10s), that can be overridden with timeout.

It is fine for f to ignore the supplied socket and just use the reader and writer. The socket is there to make it convenient to call Socket functions.

val connect_sock : ?socket:([ `Unconnected ], 'addrImport.Socket.t ‑> ?interrupt:unit Import.Deferred.t ‑> ?timeout:Core.Time.Span.t ‑> 'addr where_to_connect ‑> ([ `Active ], 'addrImport.Socket.t Import.Deferred.t

connect_sock where_to_connect creates a socket and opens a TCP connection. To use an existing socket, supply ~socket. Any errors in the connection will be reported to the monitor that was current when connect_sock was called.

val connect : ?socket:([ `Unconnected ], 'addrImport.Socket.t ‑> ('addr where_to_connect ‑> (([ `Active ], 'addrImport.Socket.t * Import.Reader.t * Import.Writer.t) Import.Deferred.t) with_connect_options

connect ~host ~port is a convenience wrapper around connect_sock that returns the socket, and a reader and writer for the socket. The reader and writer share a file descriptor, and so closing one will affect the other by closing its underlying fd. In particular, closing the reader before closing the writer will cause the writer to subsequently raise an exception when it attempts to flush internally-buffered bytes to the OS, due to a closed fd. You should close the Writer first to avoid this problem.

If possible, use with_connection, which automatically handles closing.

It is fine to ignore the returned socket and just use the reader and writer. The socket is there to make it convenient to call Socket functions.

module Where_to_listen : sig ... end

A Where_to_listen describes the socket that a tcp server should listen on.

module Bind_to_address : sig ... end
module Bind_to_port : sig ... end
val bind_to : Bind_to_address.t ‑> Bind_to_port.t ‑> Where_to_listen.inet

Listen on the specified port on the specified addresses

val on_port : int ‑> Where_to_listen.inet

on_port port is bind_to All_addresses (On_port port)

val on_port_chosen_by_os : Where_to_listen.inet

on_port_chosen_by_os port is bind_to All_addresses On_port_chosen_by_os

val on_file : string ‑> Where_to_listen.unix

Listen on a unix domain socket using the specified path

module Server : sig ... end

A Server.t represents a TCP server listening on a socket.