#include <icy/turn/client/client.h>Inherits:
Stateful< ClientState >,IAllocationSubclassed by:TCPClient,UDPClient
TURN client that manages relay allocations, permissions, and data relay via RFC 5766.
| Return | Name | Description |
|---|---|---|
Client | #### Parameters | |
void | start virtual | Connects the socket to the TURN server and starts the allocation sequence. Permissions must be added via addPermission() before calling this. |
void | stop virtual | Stops the timer, cancels pending transactions, and closes the socket. |
void | sendAllocate virtual | Sends an Allocate request to the server with the configured transport and lifetime. On first call the server will typically respond with a 401 challenge; the client re-sends with credentials automatically. |
void | addPermission virtual | Adds multiple peer IP addresses to the permission list. |
void | addPermission virtual | Adds a single peer IP to the permission list, or refreshes it if already present. Permissions should be added before start(); they may also be added later, in which case a new CreatePermission request is required. |
void | sendCreatePermission virtual | Sends a CreatePermission request for all IPs currently in the permission list. Called automatically after allocation succeeds and periodically by the timer to refresh expiring permissions. |
void | sendChannelBind virtual | Channel bindings (RFC 5766 Section 11) are intentionally not implemented. They are a bandwidth optimization that replaces STUN-framed Send/Data indications with a compact 4-byte ChannelData header. This only benefits high-throughput media relay scenarios; in practice, media flows directly via ICE/DTLS rather than through this TURN client's data path, so the optimization is not worth the complexity (channel number allocation, 10-minute binding refresh timers, ChannelData wire framing). Data relay uses sendData() with Send Indications instead. |
void | sendRefresh virtual | Sends a Refresh request to extend the allocation lifetime. Called automatically by the timer when roughly one-third of the lifetime remains. |
void | sendData virtual | Sends a Send Indication to relay data to peerAddress through the TURN server. If permissions are still being negotiated the indication is queued and flushed once CreatePermission succeeds. |
bool | handleResponse virtual | Dispatches an incoming STUN/TURN response to the appropriate handler. |
void | handleAllocateResponse virtual | Processes a successful Allocate response; extracts mapped/relayed addresses and advances the state to Authorizing, then sends CreatePermission. |
void | handleAllocateErrorResponse virtual | Handles an Allocate error response; manages the 401 challenge/re-send flow and sets the client to Failed for unrecoverable errors. |
void | handleCreatePermissionResponse virtual | Handles a successful CreatePermission response; flushes queued Send Indications and advances state to Success. |
void | handleCreatePermissionErrorResponse virtual | Handles a failed CreatePermission response; clears all permissions and sets the client to Failed. |
void | handleRefreshResponse virtual | Handles a Refresh response; updates the stored lifetime. |
void | handleDataIndication virtual | Handles an incoming Data Indication; extracts peer address and data and forwards to ClientObserver::onRelayDataReceived(). |
int | transportProtocol virtual | #### Returns |
stun::Transaction::Ptr | createTransaction virtual | Creates a new STUN transaction, registers the progress callback, and adds it to the active transaction list. |
void | authenticateRequest virtual | Adds STUN long-term credential attributes (Username, Realm, Nonce, MessageIntegrity) to request if the realm has been received from the server. |
bool | sendAuthenticatedTransaction virtual | Calls authenticateRequest() then sends the transaction. |
stun::Transaction::Ptr | removeTransaction virtual | Removes a transaction from the active list. The IntrusivePtr keeps the object alive until the caller's copy is released. |
net::Address | mappedAddress const | #### Returns |
net::Address | relayedAddress virtual const | #### Returns |
bool | closed const | #### Returns |
ClientObserver & | observer | #### Returns |
const Options & | options const | #### Returns |
void | addPermission virtual | Adds a permission for ip, or refreshes the existing one. |
void | addPermission virtual | Adds a permission for address, or refreshes the existing one. The port is ignored; TURN permissions are IP-only. |
bool | hasPermission virtual | Checks whether a permission exists for peerIP. Local addresses (192.168.x.x and 127.x.x.x) are always permitted. |
bool | hasPermission virtual | Checks whether a permission exists for peerAddress. The port is ignored; TURN permissions are IP-only. |
void | removePermission virtual | Removes the permission for ip if present. |
void | removePermission virtual | Removes the permission for address if present. The port is ignored; TURN permissions are IP-only. |
Client(ClientObserver & observer, const Options & options, const net::Socket::Ptr & socket)observer Observer to receive lifecycle and data callbacks.
options Configuration for this client instance.
socket Underlying transport socket (TCP or UDP).
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.
virtual
virtual void stop()Stops the timer, cancels pending transactions, and closes the socket.
virtual
virtual void sendAllocate()Sends an Allocate request to the server with the configured transport and lifetime. On first call the server will typically respond with a 401 challenge; the client re-sends with credentials automatically.
virtual
virtual void addPermission(const IPList & peerIPs)Adds multiple peer IP addresses to the permission list.
peerIPs List of IPv4 address strings to permit.virtual
virtual void addPermission(const std::string & ip)Adds a single peer IP to the permission list, or refreshes it if already present. Permissions should be added before start(); they may also be added later, in which case a new CreatePermission request is required.
ip IPv4 address string of the permitted peer.virtual
virtual void sendCreatePermission()Sends a CreatePermission request for all IPs currently in the permission list. Called automatically after allocation succeeds and periodically by the timer to refresh expiring permissions.
virtual
virtual void sendChannelBind(const std::string & peerIP)Channel bindings (RFC 5766 Section 11) are intentionally not implemented. They are a bandwidth optimization that replaces STUN-framed Send/Data indications with a compact 4-byte ChannelData header. This only benefits high-throughput media relay scenarios; in practice, media flows directly via ICE/DTLS rather than through this TURN client's data path, so the optimization is not worth the complexity (channel number allocation, 10-minute binding refresh timers, ChannelData wire framing). Data relay uses sendData() with Send Indications instead.
peerIP Unused; always throws std::logic_error.virtual
virtual void sendRefresh()Sends a Refresh request to extend the allocation lifetime. Called automatically by the timer when roughly one-third of the lifetime remains.
virtual
virtual void sendData(const char * data, size_t size, const net::Address & peerAddress)Sends a Send Indication to relay data to peerAddress through the TURN server. If permissions are still being negotiated the indication is queued and flushed once CreatePermission succeeds.
data Pointer to the payload to relay.
size Number of bytes to relay.
peerAddress Destination peer address (must have an active permission).
virtual
virtual bool handleResponse(const stun::Message & response)Dispatches an incoming STUN/TURN response to the appropriate handler.
response Received STUN message.true if the message was handled, false if it was unrecognised.
virtual
virtual void handleAllocateResponse(const stun::Message & response)Processes a successful Allocate response; extracts mapped/relayed addresses and advances the state to Authorizing, then sends CreatePermission.
response Allocate success response from the server.virtual
virtual void handleAllocateErrorResponse(const stun::Message & response)Handles an Allocate error response; manages the 401 challenge/re-send flow and sets the client to Failed for unrecoverable errors.
response Allocate error response from the server.virtual
virtual void handleCreatePermissionResponse(const stun::Message & response)Handles a successful CreatePermission response; flushes queued Send Indications and advances state to Success.
response CreatePermission success response from the server.virtual
virtual void handleCreatePermissionErrorResponse(const stun::Message & response)Handles a failed CreatePermission response; clears all permissions and sets the client to Failed.
response CreatePermission error response from the server.virtual
virtual void handleRefreshResponse(const stun::Message & response)Handles a Refresh response; updates the stored lifetime.
response Refresh response from the server.virtual
virtual void handleDataIndication(const stun::Message & response)Handles an incoming Data Indication; extracts peer address and data and forwards to ClientObserver::onRelayDataReceived().
response Data Indication message from the server.virtual
virtual int transportProtocol()The IANA protocol number for the underlying transport (kProtocolUDP or kProtocolTCP).
virtual
virtual stun::Transaction::Ptr createTransaction(const net::Socket::Ptr & socket)Creates a new STUN transaction, registers the progress callback, and adds it to the active transaction list.
socket Optional override socket; uses the control socket if nullptr.IntrusivePtr to the new transaction.
virtual
virtual void authenticateRequest(stun::Message & request)Adds STUN long-term credential attributes (Username, Realm, Nonce, MessageIntegrity) to request if the realm has been received from the server.
request Message to decorate with authentication attributes.virtual
virtual bool sendAuthenticatedTransaction(stun::Transaction * transaction)Calls authenticateRequest() then sends the transaction.
transaction Transaction whose request to authenticate and send.true if the send succeeded.
virtual
virtual stun::Transaction::Ptr removeTransaction(stun::Transaction * transaction)Removes a transaction from the active list. The IntrusivePtr keeps the object alive until the caller's copy is released.
transaction Transaction to remove.The removed Ptr, or nullptr if not found.
const
net::Address mappedAddress() constThe server-reflexive (mapped) address assigned by the TURN server.
virtual const
virtual net::Address relayedAddress() constThe relayed transport address assigned by the TURN server.
const
bool closed() consttrue when the client is in the None or Failed state.
ClientObserver & observer()Reference to the observer provided at construction.
const
const Options & options() constReference to the immutable options struct.
virtual
virtual void addPermission(const std::string & ip)Adds a permission for ip, or refreshes the existing one.
ip IPv4 address string to permit.virtual
virtual void addPermission(const net::Address & address)Adds a permission for address, or refreshes the existing one. The port is ignored; TURN permissions are IP-only.
virtual
virtual bool hasPermission(const std::string & peerIP)Checks whether a permission exists for peerIP. Local addresses (192.168.x.x and 127.x.x.x) are always permitted.
peerIP IPv4 address string to check.true if a valid (non-expired) permission exists.
virtual
virtual bool hasPermission(const net::Address & peerAddress)Checks whether a permission exists for peerAddress. The port is ignored; TURN permissions are IP-only.
virtual
virtual void removePermission(const std::string & ip)Removes the permission for ip if present.
ip IPv4 address string to remove.virtual
virtual void removePermission(const net::Address & address)Removes the permission for address if present. The port is ignored; TURN permissions are IP-only.
| Return | Name | Description |
|---|---|---|
ClientObserver & | _observer | |
Options | _options | |
net::SocketEmitter | _socket | |
Timer | _timer | |
icy::Error | _error | |
net::Address | _mappedAddress | |
net::Address | _relayedAddress | |
std::string | _realm | |
std::string | _nonce | |
std::deque< stun::Message > | _pendingIndications | A list of queued Send indication packets awaiting server permissions. |
std::vector< stun::Transaction::Ptr > | _transactions | A list containing currently active transactions. |
ClientObserver & _observerOptions _optionsnet::SocketEmitter _socketTimer _timericy::Error _errornet::Address _mappedAddressnet::Address _relayedAddressstd::string _realmstd::string _noncestd::deque< stun::Message > _pendingIndicationsA list of queued Send indication packets awaiting server permissions.
std::vector< stun::Transaction::Ptr > _transactionsA list containing currently active transactions.
| Return | Name | Description |
|---|---|---|
void | setError virtual | Sets the error field and transitions the client to the Failed state. |
bool | onSocketConnect virtual | Socket connect callback; starts the timer and sends the first Allocate request. |
bool | onSocketRecv virtual | Socket receive callback; parses STUN messages from the buffer and dispatches them. |
bool | onSocketClose virtual | Socket close callback; shuts down the client and records the socket error. |
void | onTransactionProgress virtual | STUN transaction state-change callback; handles Success and Failed outcomes. |
void | onStateChange | Forwards state-change events to the observer. |
void | onTimer virtual | Periodic timer callback; re-allocates on expiry or refreshes when lifetime is below one-third remaining. Also calls ClientObserver::onTimer(). |
virtual
virtual void setError(const icy::Error & error)Sets the error field and transitions the client to the Failed state.
error Error descriptor.virtual
virtual bool onSocketConnect(net::Socket & socket)Socket connect callback; starts the timer and sends the first Allocate request.
socket The connected socket.virtual
virtual bool onSocketRecv(net::Socket & socket, const MutableBuffer & buffer, const net::Address & peerAddress)Socket receive callback; parses STUN messages from the buffer and dispatches them.
socket The receiving socket.
buffer Received data.
peerAddress Source address of the received data.
virtual
virtual bool onSocketClose(net::Socket & socket)Socket close callback; shuts down the client and records the socket error.
socket The closed socket.virtual
virtual void onTransactionProgress(void * sender, TransactionState & state, const TransactionState &)STUN transaction state-change callback; handles Success and Failed outcomes.
sender Pointer to the stun::Transaction that changed state.
state The new transaction state.
void onStateChange(ClientState & state, const ClientState & oldState)Forwards state-change events to the observer.
virtual
virtual void onTimer()Periodic timer callback; re-allocates on expiry or refreshes when lifetime is below one-third remaining. Also calls ClientObserver::onTimer().