Module Async_unix.Tcp
Tcp
supports connection to inet
sockets and unix
sockets.
These are two different types. We use 'a Where_to_connect.t
to specify a socket to connect to, where the 'a
identifies the type of socket.
module Where_to_connect : sig ... end
A
Where_to_connect
describes the socket that a tcp client should connect to.
type 'a with_connect_options
= ?buffer_age_limit:[ `At_most of Core.Time.Span.t | `Unlimited ] -> ?interrupt:unit Async_unix__.Import.Deferred.t -> ?reader_buffer_size:int -> ?writer_buffer_size:int -> ?timeout:Core.Time.Span.t -> 'a
val with_connection : ('addr Where_to_connect.t -> (([ `Active ], 'addr) Async_unix__.Unix_syscalls.Socket.t -> Reader.t -> Writer.t -> 'a Async_unix__.Import.Deferred.t) -> 'a Async_unix__.Import.Deferred.t) with_connect_options
with_connection where_to_connect f
looks upwhere_to_connect
(using DNS as needed), connects, then callsf
, passing the connected socket and a reader and writer for it. When the deferred returned byf
is determined, or any exception is thrown, the socket, reader and writer are closed. The returnedDeferred.t
is fulfilled afterf
has finished processing and the file descriptor for the socket is closed. Ifinterrupt
is supplied, the connection attempt will be aborted ifinterrupt
is fulfilled before the connection has been established. Similarly, all connection attempts have a timeout (default 10s), which can be overridden withtimeout
.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 callSocket
functions.
val connect_sock : ?socket:([ `Unconnected ], 'addr) Async_unix__.Unix_syscalls.Socket.t -> ?interrupt:unit Async_unix__.Import.Deferred.t -> ?timeout:Core.Time.Span.t -> 'addr Where_to_connect.t -> ([ `Active ], 'addr) Async_unix__.Unix_syscalls.Socket.t Async_unix__.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 whenconnect_sock
was called.
val connect : ?socket:([ `Unconnected ], 'addr) Async_unix__.Unix_syscalls.Socket.t -> ('addr Where_to_connect.t -> (([ `Active ], 'addr) Async_unix__.Unix_syscalls.Socket.t * Reader.t * Writer.t) Async_unix__.Import.Deferred.t) with_connect_options
connect where_to_connect
is a convenience wrapper aroundconnect_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 underlyingfd
. 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 closedfd
. You should close theWriter
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 Bind_to_address : sig ... end
module Bind_to_port : sig ... end
module Where_to_listen : sig ... end
A
Where_to_listen
describes the socket that a tcp server should listen on.
module Server : sig ... end
A
Server.t
represents a TCP server listening on a socket.