ranch_transport(3)

Name

ranch_transport - Transport modules

Description

The module ranch_transport defines the interface used by Ranch transports.

Callbacks

Ranch transports implement the following interface:

accept

accept(LSocket :: socket(), Timeout :: timeout())
    -> {ok, Socket :: socket()}
     | {error, closed | timeout | atom()}

Use the listening socket returned by listen/1 to accept a new connection. The timeout is specified in milliseconds.

close

close(Socket :: socket()) -> ok

Close the socket.

controlling_process

controlling_process(Socket :: socket(), Pid :: pid())
    -> ok | {error, closed | not_owner | atom()}

Assign a new controlling process to the socket. The controlling process is the process that is linked to and receives messages from the socket.

getopts

getopts(Socket :: socket(), SockOpts :: [atom()])
    -> {ok, any()} | {error, atom()}

Get one or more options for the socket.

getstat

getstat(Socket :: socket())
    -> {ok, SockStatValues :: any()} | {error, atom()}

Get all statistics for the socket.

getstat(Socket :: socket(), SockStats :: [atom()])
    -> {ok, SockStatValues :: any()} | {error, atom()}

Get one or more statistic options for the socket.

handshake

handshake(Socket0  :: socket(),
          Timeout  :: timeout())
    -> {ok, Socket :: socket()}
     | {ok, Socket :: socket(), Info :: any()}
     | {error, any()}

handshake(Socket0  :: socket(),
          SockOpts :: opts(),
          Timeout  :: timeout())
    -> {ok, Socket :: socket()}
     | {ok, Socket :: socket(), Info :: any()}
     | {error, any()}

Perform the transport-level handshake.

This function will be called by connection processes before performing any socket operation. It allows transports that require extra initialization to perform their task and return a socket that is ready to use.

If the handshake is completed by this call, the function will return {ok, Socket}. However, some transports (notably, ranch_ssl if {handshake, hello} is specified in the socket options) may pause the handshake at a certain point and return {ok, Socket, Info} instead, in order to allow for additional decisions to be made before resuming the handshake with handshake_continue/3 or cancelling it with handshake_cancel/1.

This function may also be used to upgrade a connection from a transport to another depending on the capabilities of the transports. For example a ranch_tcp socket may be upgraded to a ranch_ssl one using this function.

handshake_continue

handshake_continue(Socket0  :: socket(),
                   Timeout  :: timeout())
    -> {ok, Socket :: socket()}
     | {error, any()}

handshake_continue(Socket0  :: socket(),
                   SockOpts :: opts(),
                   Timeout  :: timeout())
    -> {ok, Socket :: socket()}
     | {error, any()}

Resume the paused transport-level handshake and return a socket that is ready to use.

This function will be called by connection processes to resume a paused handshake.

handshake_cancel

handshake_cancel(Socket :: socket()) -> ok

Cancel the paused transport-level handshake.

listen

listen(TransportOpts :: ranch:transport_opts(any()))
    -> {ok, LSocket :: socket()} | {error, atom()}

Create a socket that listens on the port given in the socket options.

The port may not be specified or may be set to 0, which means a random available port number will be chosen.

messages

messages()
    -> {OK      :: atom(),
        Closed  :: atom(),
        Error   :: atom(),
        Passive :: atom()}

Return the tuple keys for the messages sent by the socket.

name

name() -> Name :: atom()

Return the name of the transport.

peername

peername(Socket :: socket())
    -> {ok, {inet:ip_address(), inet:port_number()}}
     | {local, binary()} | {error, atom()}.

Return the address and port number for the other end of the connection.

For UNIX Domain sockets the return value will be {local, PeerSocket}, with PeerSocket typically an empty binary.

recv

recv(Socket :: socket(),
     Length :: non_neg_integer(),
     Timeout :: timeout())
    -> {ok, Packet :: any()}
     | {error, closed | timeout | atom()}

Receive a packet from the socket in passive mode.

Attempting to receive data from a socket that is in active mode will return an error.

A length of 0 will return the data available on the socket as soon as possible, regardless of length.

While it is possible to use the timeout value infinity, it is highly discouraged as it could cause your process to get stuck waiting for data that will never come. This may happen when a socket becomes half-open due to a crash of the remote endpoint. Wi-Fi going down is another common culprit.

secure

secure() -> boolean()

Return whether the transport can be used for secure connections.

send

send(Socket :: socket(), Packet :: iodata())
    -> ok | {error, atom()}

Send a packet on the socket.

sendfile

sendfile(Socket, File)
    -> sendfile(Socket, File, 0, 0, [])

sendfile(Socket, File, Offset, Bytes)
    -> sendfile(Socket, File, Offset, Bytes, [])

sendfile(Socket :: socket(),
         File   :: file:name_all() | file:fd(),
         Offset :: non_neg_integer(),
         Bytes  :: non_neg_integer(),
         Opts   :: sendfile_opts())
    -> {ok, SentBytes :: non_neg_integer()} | {error, atom()}

Send a file on the socket.

The file may be sent full or in parts, and may be specified by its filename or by an already open file descriptor.

Transports that manipulate TCP directly may use the file:sendfile/2,4,5 function, which calls the sendfile syscall where applicable (on Linux, for example). Other transports can use the sendfile/6 function exported from this module.

setopts

setopts(Socket :: socket(), SockOpts :: any())
    -> ok | {error, atom()}

Set one or more options for the socket.

shutdown

shutdown(Socket :: socket(),
         How    :: read | write | read_write)
    -> ok | {error, atom()}

Close the socket for reading and/or writing.

sockname

sockname(Socket :: socket())
    -> {ok, {inet:ip_address(), inet:port_number()}}
     | {error, atom()}.

Return the address and port number for the local end of the connection.

For UNIX Domain sockets the return value will be {local, SocketFile}.

Exports

The following function can be used when implementing transport modules:

Types

sendfile_opts()

sendfile_opts() :: [{chunk_size, non_neg_integer()}]

Options accepted by the sendfile function and callbacks:

chunk_size (8191)

The chunk size, in bytes.

socket()

socket() :: any()

The socket.

The exact type will vary depending on the transport module.

Changelog

  • 2.0: The callback listen/1 has changed to accept a map of transport options instead of socket options.
  • 2.0: The callback messages/0 return value was updated to include the passive message for {active, N}.
  • 1.6: The socket() type was added for documentation purposes.
  • 1.6: The type of the sendfile filename was extended.

See also

ranch(7), ranch_tcp(3), ranch_ssl(3)

Ranch 2.0 Function Reference

Navigation

Version select

Like my work? Donate!

Donate to Loïc Hoguin because his work on Cowboy, Ranch, Gun and Erlang.mk is fantastic:

Recurring payment options are also available via GitHub Sponsors. These funds are used to cover the recurring expenses like food, dedicated servers or domain names.