TURN module (rfc5766)

TCPClient

TCP transport TURN client with relay connection management per RFC 6062.

TCPClient

#include <icy/turn/client/tcpclient.h>

Inherits: Client

TCP transport TURN client with relay connection management per RFC 6062. Uses a single TCP control connection for STUN signalling and opens additional TCP relay connections for each peer via the Connect / ConnectionBind flow.

Public Methods

ReturnNameDescription
TCPClient#### Parameters
voidstart virtualConnects the socket to the TURN server and starts the allocation sequence. Permissions must be added via addPermission() before calling this.
voidstop virtualStops the control connection, cancels all relay connections, and calls the base class stop().
voidsendConnectRequest virtualSends a Connect request to the server asking it to open a TCP connection to peerAddress on the client's behalf (RFC 6062 section 4.3).
voidsendData virtualSends raw data to peerAddress over the established relay connection. The peer must have an active permission and a bound relay connection.
boolhandleResponse virtualExtends the base handler with Connect, ConnectionBind, and ConnectionAttempt responses.
voidhandleConnectResponse virtualProcesses a successful Connect response; extracts the connection ID and calls createAndBindConnection().
voidhandleConnectErrorResponse virtualProcesses a Connect error response; notifies the observer that binding failed.
voidhandleConnectionBindResponse virtualProcesses a successful ConnectionBind response; arms the relay socket for data transfer and notifies the observer.
voidhandleConnectionBindErrorResponse virtualProcesses a ConnectionBind error response; frees the failed connection.
voidhandleConnectionAttemptIndication virtualProcesses a ConnectionAttempt indication from the server; optionally accepts the incoming peer connection by calling createAndBindConnection().
boolcreateAndBindConnection virtualOpens a new TCP relay socket to the TURN server and sends a ConnectionBind request carrying connectionID.
boolonRelayConnectionConnect virtualRelay socket connect callback; sends the ConnectionBind request.
boolonRelayDataReceived virtualRelay socket receive callback; forwards data to the observer.
boolonRelayConnectionError virtualRelay socket error callback; notifies the observer of the error.
boolonRelayConnectionClosed virtualRelay socket close callback; notifies the observer and frees the connection.
voidfreeConnectionRemoves the relay connection for peerAddress, unregisters its callbacks, and deletes the associated RelayConnectionBinding.
inttransportProtocol virtual#### Returns
ConnectionManager &connections#### Returns
const char *className virtual const inline

TCPClient

TCPClient(TCPClientObserver & observer, const Client::Options & options)

Parameters

  • observer Observer for TCP-specific relay events.

  • options Client configuration; defaults to loopback server, 5-min lifetime.


start

virtual

virtual void start()

Connects the socket to the TURN server and starts the allocation sequence. Permissions must be added via addPermission() before calling this.


stop

virtual

virtual void stop()

Stops the control connection, cancels all relay connections, and calls the base class stop().


sendConnectRequest

virtual

virtual void sendConnectRequest(const net::Address & peerAddress)

Sends a Connect request to the server asking it to open a TCP connection to peerAddress on the client's behalf (RFC 6062 section 4.3).

Parameters

  • peerAddress Target peer address.

sendData

virtual

virtual void sendData(const char * data, size_t size, const net::Address & peerAddress)

Sends raw data to peerAddress over the established relay connection. The peer must have an active permission and a bound relay connection.

Parameters

  • data Payload bytes.

  • size Number of bytes to send.

  • peerAddress Destination peer (must have an entry in connections()).


handleResponse

virtual

virtual bool handleResponse(const stun::Message & response)

Extends the base handler with Connect, ConnectionBind, and ConnectionAttempt responses.

Parameters

  • response Incoming STUN response.

Returns

true if the message was handled.


handleConnectResponse

virtual

virtual void handleConnectResponse(const stun::Message & response)

Processes a successful Connect response; extracts the connection ID and calls createAndBindConnection().

Parameters

  • response Connect success response.

handleConnectErrorResponse

virtual

virtual void handleConnectErrorResponse(const stun::Message & response)

Processes a Connect error response; notifies the observer that binding failed.

Parameters

  • response Connect error response.

handleConnectionBindResponse

virtual

virtual void handleConnectionBindResponse(const stun::Message & response)

Processes a successful ConnectionBind response; arms the relay socket for data transfer and notifies the observer.

Parameters

  • response ConnectionBind success response.

handleConnectionBindErrorResponse

virtual

virtual void handleConnectionBindErrorResponse(const stun::Message & response)

Processes a ConnectionBind error response; frees the failed connection.

Parameters

  • response ConnectionBind error response.

handleConnectionAttemptIndication

virtual

virtual void handleConnectionAttemptIndication(const stun::Message & response)

Processes a ConnectionAttempt indication from the server; optionally accepts the incoming peer connection by calling createAndBindConnection().

Parameters

  • response ConnectionAttempt indication message.

createAndBindConnection

virtual

virtual bool createAndBindConnection(uint32_t connectionID, const net::Address & peerAddress)

Opens a new TCP relay socket to the TURN server and sends a ConnectionBind request carrying connectionID.

Parameters

  • connectionID TURN CONNECTION-ID from the server.

  • peerAddress Peer associated with this connection.

Returns

true on success.


onRelayConnectionConnect

virtual

virtual bool onRelayConnectionConnect(net::Socket & socket)

Relay socket connect callback; sends the ConnectionBind request.

Parameters

  • socket The relay socket that just connected.

onRelayDataReceived

virtual

virtual bool onRelayDataReceived(net::Socket & socket, const MutableBuffer & buffer, const net::Address & peerAddress)

Relay socket receive callback; forwards data to the observer.

Parameters

  • socket The receiving relay socket.

  • buffer Received data.

  • peerAddress Source address (populated by the socket layer).


onRelayConnectionError

virtual

virtual bool onRelayConnectionError(net::Socket & socket, const Error & error)

Relay socket error callback; notifies the observer of the error.

Parameters

  • socket The affected relay socket.

  • error Error descriptor.


onRelayConnectionClosed

virtual

virtual bool onRelayConnectionClosed(net::Socket & socket)

Relay socket close callback; notifies the observer and frees the connection.

Parameters

  • socket The closed relay socket.

freeConnection

void freeConnection(const net::Address & peerAddress)

Removes the relay connection for peerAddress, unregisters its callbacks, and deletes the associated RelayConnectionBinding.

Parameters

  • peerAddress Peer whose connection to release.

transportProtocol

virtual

virtual int transportProtocol()

Returns

kProtocolTCP (6).


connections

ConnectionManager & connections()

Returns

Reference to the map of active relay connections keyed by peer address.


className

virtual const inline

virtual inline const char * className() const

Protected Attributes

ReturnNameDescription
TCPClientObserver &_observer
ConnectionManager_connections

_observer

TCPClientObserver & _observer

_connections

ConnectionManager _connections