Xirsys.Sockets.Handler behaviour (xturn_sockets v2.2.1)
View SourceBusiness logic for one pipeline tier.
handle_packet/4 is required. handle_connect/1 and handle_disconnect/2
are optional.
Return values from handle_packet/4:
{:ok, state}- packet consumed, no reply{:reply, iodata(), state}- sendiodataon the same connection{:descend, tier, payload, state}- pushpayloadinto another named tier{:close, state}- stop the connection after this packet
What problem this solves
After an accumulator yields a whole packet, something must interpret it. XTurn and host applications implement this behaviour per tier so STUN binding, TURN allocation, and relay logic stay out of the generic drain engine.
RFCs
Handlers typically implement protocol logic from:
This behaviour only defines the callback contract; it does not parse messages.
Summary
Types
Packet metadata from the accumulator (:from, :received_at, ...).
Opaque handler state, returned from handle_connect/1 or handle_packet/4.
Callbacks
Called once when a stream connection is accepted (not used on stateless UDP).
Called when the connection process exits.
Handles one whole packet from this tier's accumulator.
Types
Callbacks
@callback handle_connect(Xirsys.Sockets.Conn.t()) :: {:ok, state()}
Called once when a stream connection is accepted (not used on stateless UDP).
Parameters
conn- transport context (Xirsys.Sockets.Conn)
Called when the connection process exits.
Parameters
reason- exit reason (:normal,:tcp_closed, ...)state- last handler state
@callback handle_packet(binary(), meta(), Xirsys.Sockets.Conn.t(), state()) :: {:ok, state()} | {:reply, iodata(), state()} | {:descend, atom(), binary(), state()} | {:close, state()}
Handles one whole packet from this tier's accumulator.
Parameters
packet- framed payload (header stripped for length-prefixed tiers)meta- accumulator metadata for this packetconn- connection context (addresses, socket, assigns)state- previous handler state