package async_unix

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module Async_unix.SocketSource

Sourcemodule Address : sig ... end
Sourcemodule Family : sig ... end
Sourcetype (+'a, 'b) t constraint 'a = [< `Unconnected | `Bound | `Passive | `Active ] constraint 'b = [< Address.t ]

Sockets have a phantom type parameter that tracks the state of the socket in order to eliminate certain errors in which socket functions are called in the wrong order. Initially, a socket is `Unconnected. As various socket functions are called, they return a socket with a new phantom state. Here is a chart of the allowed state transitions.

  Unconnected ---connect--> Active
  |
  | ---bind--> Bound ---listen--> Passive ---accept---> Active
               |
               | ---connect--> Active
Sourceval sexp_of_t : ('a -> Sexplib0.Sexp.t) -> ('b -> Sexplib0.Sexp.t) -> ('a, 'b) t -> Sexplib0.Sexp.t
Sourcemodule Type : sig ... end
Sourceval create : 'addr Type.t -> ([ `Unconnected ], 'addr) t
Sourceval connect : ([< `Unconnected | `Bound ], 'addr) t -> 'addr -> ([ `Active ], 'addr) t Async_kernel.Deferred.t

Calling Fd.close on the socket's file descriptor before or during connect may cause connect to raise.

Sourceval connect_interruptible : ([< `Unconnected | `Bound ], 'addr) t -> 'addr -> interrupt:unit Async_kernel.Deferred.t -> [ `Ok of ([ `Active ], 'addr) t | `Interrupted ] Async_kernel.Deferred.t
Sourceval bind_keep_opts : ([ `Unconnected ], 'addr) t -> 'addr -> ([ `Bound ], 'addr) t Async_kernel.Deferred.t

Like bind, but preserves the existing socket options and the CLOEXEC flag.

Sourceval bind : ?reuseaddr:bool -> ([ `Unconnected ], 'addr) t -> 'addr -> ([ `Bound ], 'addr) t Async_kernel.Deferred.t

bind socket addr sets close_on_exec for the fd of socket.

Sourceval bind_inet_keep_opts : ([ `Unconnected ], Address.Inet.t) t -> Address.Inet.t -> ([ `Bound ], Address.Inet.t) t

Like bind_inet, but preserves the existing socket options and the CLOEXEC flag.

Sourceval bind_inet : ?reuseaddr:bool -> ([ `Unconnected ], Address.Inet.t) t -> Address.Inet.t -> ([ `Bound ], Address.Inet.t) t

bind_inet socket addr is just like bind but is restricted to Inet.t addresses and is therefore guaranteed not to block.

Sourceval listen : ?backlog:int -> ([ `Bound ], 'addr) t -> ([ `Passive ], 'addr) t
Sourceval accept : ([ `Passive ], 'addr) t -> [ `Ok of ([ `Active ], 'addr) t * 'addr | `Socket_closed ] Async_kernel.Deferred.t
Sourceval accept_interruptible : ([ `Passive ], 'addr) t -> interrupt:unit Async_kernel.Deferred.t -> [ `Ok of ([ `Active ], 'addr) t * 'addr | `Socket_closed | `Interrupted ] Async_kernel.Deferred.t
Sourceval accept_at_most : ([ `Passive ], 'addr) t -> limit:int -> [ `Ok of (([ `Active ], 'addr) t * 'addr) list | `Socket_closed ] Async_kernel.Deferred.t

accept_at_most is like accept, but will return up to limit connections before yielding, where limit >= 1. accept_at_most first waits for one connection and then attempts to retrieve up to limit connections through non-blocking Unix.accept calls. If a call to Unix.accept would block before limit is reached, accept_at_most returns the connections retrieved thus far.

Servers that must service a large number of connections tend to observe a stall in connection accept rates when under heavy load. Increasing limit will ameliorate this effect, and increase accept rates and overall throughput of the server at the cost of increased contention for resources amongst connections.

For details, see:

   Acceptable strategies for improving web server performance
   Brecht, Pariag, and Gammo.  USENIX ATEC '04
Sourceval accept_at_most_interruptible : ([ `Passive ], 'addr) t -> limit:int -> interrupt:unit Async_kernel.Deferred.t -> [ `Ok of (([ `Active ], 'addr) t * 'addr) list | `Socket_closed | `Interrupted ] Async_kernel.Deferred.t
Sourceval shutdown : ('a, 'addr) t -> [ `Receive | `Send | `Both ] -> unit
Sourceval fd : ('a, 'addr) t -> Fd.t
Sourceval of_fd : Fd.t -> 'addr Type.t -> ('a, 'addr) t
Sourceval getsockname : ('a, 'addr) t -> 'addr
Sourceval getpeername : ('a, 'addr) t -> 'addr
Sourcemodule Opt : sig ... end
Sourceval getopt : ('a, 'addr) t -> 'c Opt.t -> 'c
Sourceval setopt : ('a, 'addr) t -> 'c Opt.t -> 'c -> unit
Sourceval mcast_join : ?ifname:string -> ?source:Core_unix__.Import.Unix.inet_addr -> ('a, 'addr) t -> 'addr -> unit
Sourceval mcast_leave : ?ifname:string -> ?source:Core_unix__.Import.Unix.inet_addr -> ('a, 'addr) t -> 'addr -> unit
Sourceval bind_to_interface_exn : (([ `Unconnected ], Address.t) t -> Linux_ext.Bound_to_interface.t -> unit) Core.Or_error.t

bind_to_interface_exn t (`Interface_name "eth0") restricts messages from being received or sent on interfaces other than eth0. See Linux_ext.bind_to_interface.

Typically, one would use this function for very specific non-multicast requirements. For similar functionality when using multicast, see Core_unix.mcast_set_ifname.

OCaml

Innovation. Community. Security.