Xirsys.Sockets.Transport behaviour (xturn_sockets v2.2.0)
View SourceUniform socket I/O behaviour for TCP, UDP, TLS, DTLS, and SCTP.
Implementations live under Xirsys.Sockets.Transport.*. Acceptor and
DatagramServer call these callbacks; they never talk to :gen_tcp /
:gen_udp / :ssl directly.
Optional callbacks: peername/1, controlling_process/2, connect/3.
Summary
Types
Peer address for a datagram, or nil for a connected stream socket.
IPv4 or IPv6 address tuple.
UDP/TCP port in 0..65535.
Opaque socket handle returned by the underlying driver.
Callbacks
Accepts one inbound connection from a listen socket.
Closes socket. Always returns :ok.
Opens an outbound connection.
Transfers socket ownership to pid after accept.
How this transport frames application payloads on the wire.
Normalizes a driver message into a drain-engine event.
Opens a listening socket.
Remote address of a connected socket.
Sends data on socket.
Applies socket options (typically [:binary, active: :once] to re-arm).
Local address of socket.
Types
@type from() :: {ip_address(), port_number()} | nil
Peer address for a datagram, or nil for a connected stream socket.
@type ip_address() :: :inet.ip_address()
IPv4 or IPv6 address tuple.
@type port_number() :: :inet.port_number()
UDP/TCP port in 0..65535.
@type socket() :: term()
Opaque socket handle returned by the underlying driver.
Callbacks
Accepts one inbound connection from a listen socket.
Connectionless transports return {:error, :connectionless}.
Parameters
socket- listen socket fromlisten/3timeout- milliseconds to wait, or:infinity
@callback close(socket()) :: :ok
Closes socket. Always returns :ok.
Parameters
socket- socket to close
@callback connect(ip_address(), port_number(), keyword()) :: {:ok, socket()} | {:error, term()}
Opens an outbound connection.
Optional. Used when this library is the TCP client.
Parameters
ip- destination addressport- destination portopts- extra connect options merged after library defaults
Transfers socket ownership to pid after accept.
Optional. Required for stream acceptors that spawn a Connection process.
Parameters
socket- accepted client socketpid- process that will receive subsequent messages
@callback framing() :: :stream | :datagram
How this transport frames application payloads on the wire.
:stream- bytes are a continuous stream; 4-byte alignment is applied when a caller asks for it:datagram- each send is one message; no padding
@callback handle_message(term(), socket()) :: {:data, binary(), from()} | {:closed, term()} | {:icmp, map()} | :ignore
Normalizes a driver message into a drain-engine event.
Parameters
message- rawhandle_info/2payload ({:tcp, ...},{:udp, ...}, ...)socket- socket the message belongs to
Returns
{:data, binary(), from()}- payload ready to push into the accumulator{:closed, reason}- peer or error close{:icmp, map()}- ICMP error from a datagram socket (relay sockets):ignore- nothing to drain (unknown or control message)
@callback listen(ip_address(), port_number(), keyword()) :: {:ok, socket()} | {:error, term()}
Opens a listening socket.
Parameters
ip- bind address ({0, 0, 0, 0}or{0, 0, 0, 0, 0, 0, 0, 0})port- bind port;0lets the OS assign oneopts- extra listen options merged after library defaults
@callback peername(socket()) :: {:ok, {ip_address(), port_number()}} | {:local, binary()} | {:unspec, <<_::0>>} | {:undefined, any()} | {:error, term()}
Remote address of a connected socket.
Optional. Datagram transports typically return {:error, :connectionless}.
Parameters
socket- connected socket
Sends data on socket.
Parameters
socket- connected or datagram socketdata- payloadfrom-{ip, port}for datagrams; ignored (nil) on streams
Applies socket options (typically [:binary, active: :once] to re-arm).
Parameters
socket- open socketopts- option keyword list understood by the driver
@callback sockname(socket()) :: {:ok, {ip_address(), port_number()}} | {:local, binary()} | {:unspec, <<_::0>>} | {:undefined, any()} | {:error, term()}
Local address of socket.
Parameters
socket- open socket