Xirsys.Sockets.Client (xturn_sockets v1.2.0)

View Source

TCP protocol socket client for TURN server usage.

Handles TCP connections with buffering and provides 5-tuple information to callbacks for proper STUN/TURN protocol processing.

## Callback Interface

Your callback module must implement:

def process_buffer(data, client_ip, client_port, server_ip, server_port)

Where:

  • data - Binary data from TCP stream (may be partial packets)
  • client_ip - Client IP address tuple
  • client_port - Client port number
  • server_ip - Server IP address tuple
  • server_port - Server port number

The function should return {parsed_packet, remaining_buffer} where:

  • parsed_packet - Complete packet ready for processing (or nil if incomplete)
  • remaining_buffer - Any remaining unparsed data

The 5-tuple information is essential for STUN/TURN over TCP (RFC 5389, RFC 5766).

Example

defmodule MyTCPHandler do
  def process_buffer(data, client_ip, client_port, server_ip, server_port) do
    case parse_stun_packet(data, client_ip, client_port, server_ip, server_port) do
      {:ok, packet, rest} -> {packet, rest}
      {:partial, _} -> {nil, data}  # Wait for more data
    end
  end

  def dispatch(conn) do
    # Process complete STUN/TURN packet
    handle_packet(conn.message, conn)
  end
end

Summary

Functions

Returns a specification to start this module under a supervisor.

Asynchronous socket response handler

Message handler to update cache

Callback implementation for GenServer.init/1.

Standard OTP module startup

Types

callback()

@type callback() :: (pid(), any(), binary(), binary() -> :ok)

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

create(socket, callback, ssl)

handle_cast(msg, state)

Asynchronous socket response handler

handle_info(msg, state)

Message handler to update cache

init(list)

Callback implementation for GenServer.init/1.

start_link(socket, callback, ssl)

@spec start_link(pid(), callback(), boolean()) :: :ok

Standard OTP module startup