Module Tezos_base_unix.Socket

type addr =
  1. | Unix of string
  2. | Tcp of string * string * Unix.getaddrinfo_option list
val connect : ?timeout:Ptime.Span.t -> addr -> Lwt_unix.file_descr Tezos_base.TzPervasives.Error_monad.tzresult Lwt.t

connect ?timeout addr tries connecting to addr and returns the resulting socket file descriptor on success. When using TCP, Unix.getaddrinfo is used to resolve the hostname and service (port). The different socket addresses returned by Unix.getaddrinfo are tried sequentially, and the ?timeout argument (default: 5s) governs how long it waits to get a connection. If a connection is not obtained in less than ?timeout, the connection is canceled and and the next socket address (if it exists) is tried.

val with_connection : ?timeout:Ptime.Span.t -> addr -> (Lwt_unix.file_descr -> 'a Tezos_base.TzPervasives.Error_monad.tzresult Lwt.t) -> 'a Tezos_base.TzPervasives.Error_monad.tzresult Lwt.t
val bind : ?backlog:int -> addr -> Lwt_unix.file_descr list Tezos_base.TzPervasives.Error_monad.tzresult Lwt.t
val send : Lwt_unix.file_descr -> 'a Tezos_base.TzPervasives.Data_encoding.t -> 'a -> unit Tezos_base.TzPervasives.Error_monad.tzresult Lwt.t
val recv : ?timeout:Ptime.Span.t -> Lwt_unix.file_descr -> 'a Tezos_base.TzPervasives.Data_encoding.t -> 'a Tezos_base.TzPervasives.Error_monad.tzresult Lwt.t
val handshake : Lwt_unix.file_descr -> bytes -> unit Tezos_base.TzPervasives.Error_monad.tzresult Lwt.t

handshake socket magic_bytes is a function to synchronize two separate processes and start a communication.

The scenario of the handshake is the following:

  • both processes simultaneously send some magic_bytes,
  • both processes wait and checks the received bytes validity,
  • handshake is finished.
val get_temporary_socket_dir : unit -> string

get_temporary_socket_dir () returns a temporary path for the socket to be spawned. $XDG_RUNTIME_DIR is returned if the environment variable is defined. Otherwise, the default temporary directory is used.

val set_tcp_user_timeout : Unix.file_descr -> ms:int -> (unit, [ `Unix_error of exn | `Unsupported ]) Stdlib.result

set_tcp_user_timeout fd ~ms sets the TCP user timeout on socket fd. If a message sent on this socket is not acknowledged within ms milliseconds, the connection is considered dead. This function uses the TCP_USER_TIMEOUT socket option.

  • parameter fd

    the file descriptor of the socket.

  • parameter ms

    the timeout value in milliseconds.

  • returns

    Ok () if the option was successfully set, or Error (`Unix_error exn) if a Unix error occurred, or Error `Unsupported if the TCP_USER_TIMEOUT option is not supported on this platform.

val set_tcp_keepalive : Unix.file_descr -> ms:int -> intv:int -> (unit, [ `Unix_error of exn | `Unsupported ]) Stdlib.result

set_tcp_keep_alive fd ~ms ~intv enables and sets the TCP keep alive timeout on socket fd. If a message sent on this socket is not acknowledged within ms milliseconds, empty TCP packets will be sent each intv ms.

This function uses:

  • SO_KEEPALIVE, TCP_KEEPIDLE, TCP_KEEPINTVL socket options on linux
  • SO_KEEPALIVE, TCP_KEEPALIVE on macos.
  • parameter fd

    the file descriptor of the socket.

  • parameter ms

    the timeout value in milliseconds.

  • parameter intv

    the delay to wait before retrying another probe

  • returns

    Ok () if the option was successfully set, or Error (`Unix_error exn) if a Unix error occurred, or Error `Unsupported if the KEEP_ALIVE option is not supported on this platform.