By Admin on Tuesday, 17 August 2021
Category: All

Delphi TURN Client Component (2/4)

​From sgcWebSockets 4.5.2, TURN protocol is supported on Client Components. 

TsgcTURNClient is the client that implements the TURN Protocol and allows to send allocation requests to TURN servers. The client inherits from STUN Client, so all methods supported by the STUN Client are already supported by TURN Client.

Basic Usage 

Usually TURN servers runs on UDP port 3478 and don't require authentication, so in order to send a TURN request, fill the server properties to allow the client know where connect and Handle the events where the component will receive the response from server.

Configure the server

Call the method Allocate, to send a request to allocate an IP Address and a Port to the TURN server.

Handle the events

TURN Relay Data 

There are basically 2 ways to send data between peers:

1. Send Indications, which encapsulates the data in a STUN packet. Use the method SendIndication to send an indication to other peer.

2. Use Channel Data, it's a more efficient way to send data between peers because the packet size is smaller than indications. Use SendChannelData method to send a channel data to other peer.

When a TURN server receives a packet in a Relayed IP Address from an IP Address with an active permission, if there is channel data bound to the peer IP Address, the TURN client will receive the data in the event OnTURNChannelData. But if there is no channel, the TURN client will receive the data in the event OnTURNData.

Methods 

Allocate

This method sends a request to the server to allocate an IP Address and a Port which will be used to relay date between the peers.

If the server can allocate successfully an IP Address and a Port, the event OnTURNAllocate event will be called. If not, the OnSTUNRequestError event will be called.

The client saves in the Allocation property of the client, the data returned by server about the allocated IP Address.

Refresh

If there is an active allocation, the client can refresh it sending a Refresh request.

This method has a parameter called Lifetime, if the value is zero, the allocation will expire immediately. If the value is greater of zero, it means the number of seconds to expiry.

If the result is successful, the event OnTURNRefresh will be called.

CreatePermission

This method creates a new permission fo the IP Address set as an argument of the CreatePermission method. If the permission already exists for this IP, it will be refreshed by the server.

If the result is successful, the event OnCreatePermission will be called.

SendIndication

This method sends a data to the peer identified as PeerIP and PeerPort. This method requires there is an active permission for this IP in the TURN server.

ChannelBind

This method sends a request to the server to create a new channel to communicate with the peer identified as PeerIP and PeerPort.

if the result is successful, the event OnChannelBind will be called. You can access to the channel-id assigned, reading the parameter aChannelBind of the event.

SendChannelData

This method sends data to a peer using a ChannelId. This method requires the channel exists and is active.

Related Posts