TURN 服务器 | 分配

所有 TURN 操作都围绕分配展开,所有 TURN 消息都与一个分配相关联。一个分配由以下内容组成:

 

 

当 TURN 客户端发送 Allocate 请求时,TURN 服务器处理该消息并尝试创建新的中继传输地址。默认情况下,如果有可用的 UDP 端口,将创建新的中继地址,但您可以使用 OnTURNBeforeAllocate 事件来拒绝新的分配请求。

 


procedure OnTURNBeforeAllocate(Sender: TObject; const aSocket: TsgcSocketConnection; 
  const aIP: string; aPort: Word; var Reject: Boolean);
begin
  if not (your own rules) then
	Reject := false;
end;

如果流程继续,服务器将创建一个新的 Allocation,并调用事件 OnTURNCreateAllocation。此事件通过 TsgcTURNAllocationItem 类提供有关 Allocation 的信息。

 


procedure OnTURNCreateAllocation(Sender: TObject; const aSocket: TsgcSocketConnection; 
  const Allocation: TsgcTURNAllocationItem);
begin
  DoLog('New Allocation: ' + Allocation.RelayIP + ':' + IntToStr(Allocation.RelayPort));
end;

 

当分配过期或被删除(客户端发送生存期为零的刷新请求)时,触发 OnTURNDeleteAllocation 事件。

 


procedure OnTURNDeleteAllocation(Sender: TObject; const aSocket: TsgcSocketConnection; 
  const Allocation: TsgcTURNAllocationItem);
begin
  DoLog('Allocation Deleted: ' + Allocation.RelayIP + ':' + IntToStr(Allocation.RelayPort));
end;