當我們將自定義屬性分配給TIdTCPServer上的連接的上下文時,如何以線程安全的方式訪問此屬性(讀/寫)?例如:TIdTCPServer訪問自定義AContext屬性
自定義屬性:
type
Local_Socket = class(TIdContext)
public
Tunnel_Requested: bool;
Remote_Tunnel: TIdContext;
end;
type
Remote_Socket = class(TIdContext)
public
Local_Tunnel: TIdContext;
end;
分配他們:
procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
begin
if AContext.Binding.PeerIP = '127.0.0.1' then
begin
Local_Socket(AContext).Tunnel_Requested:= false;
Local_Socket(AContext).Remote_Tunnel:= Nil;
end
else
begin
AssignRemoteTunnel(AContext);
end;
end;
procedure TForm1.AssignRemoteTunnel(AContext: TIdContext);
var
iContext: integer;
List: TIdContextList;
Assigned: bool;
begin
Assigned:= false;
List:= IdTCPServer1.Contexts.LockList;
try
for iContext:= 0 to List.Count - 1 do
begin
if (TIdContext(List[iContext]).Binding.PeerIP = '127.0.0.1') and
(Local_Socket(List[iContext]).Remote_Tunnel = Nil) then
begin
Local_Socket(List[iContext]).Remote_Tunnel:= AContext;
Remote_Socket(AContext).Local_Tunnel:= TIdContext(List[iContext]);
Assigned:= true;
end;
end;
if Assigned = false then
AContext.Connection.Disconnect;
finally
IdTCPServer1.Contexts.UnlockList;
end;
end;
我嘗試實現這個代碼,如果連接是本地(127.0.0.1),我需要什麼將其重定向到遠程連接,這將在下面的代碼中請求。一旦遠程連接到達服務器上,我AssignRemoteTunnel,與遠程連接local_socket.remote_tunnel財產,並與當地連接remote_socket.local_tunnel,這樣我可以在隧道之間的透明通信相關:
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
Buffer: TIdBytes;
begin
if AContext.Binding.PeerIP = '127.0.0.1' then
begin
if Local_Socket(AContext).Tunnel_Requested = false then
begin
TunnelSocket.Connection.IOHandler.Write(REQ_TUNNEL);
Local_Socket(AContext).Tunnel_Requested:= true;
end;
if (Local_Socket(AContext).Remote_Tunnel <> Nil) and
(Local_Socket(AContext).Remote_Tunnel.Connection.Connected) then
begin
AContext.Connection.IOHandler.CheckForDataOnSource(500);
if not AContext.Connection.IOHandler.InputBufferIsEmpty then
begin
AContext.Connection.IOHandler.InputBuffer.ExtractToBytes(Buffer);
Local_Socket(AContext).Remote_Tunnel.Connection.IOHandler.Write(Buffer);
end;
end;
這裏我正在觀察是否分配了一個remote_tunnel屬性來通過這個remote_tunnel發送緩衝區......但是當我讀取這個屬性時,也許我正在將它寫在AssignRemoteTunnel過程中。這個可以嗎?
線程安全可以通過TMonitor.Lock實現 - https://mikejustin.wordpress.com/2010/11/21/thread-synchronization-with-guarded-在德爾福塊/但真正的問題似乎是類型安全。 'Local_Socket(AContext).Tunnel_Requested' - 是否正確?我認爲你應該檢查'IF AContext IS Local_Socket THEN BEGIN ...'或者更好在這裏做'WITH AContext AS Local_Socket DO BEGIN Tunnel_Requested:= false; Remote_Tunnel:=無; END;' –
感謝您的信息。關於TMonitor.Lock,我在考慮使用TIdThreadSafe類,這樣我就可以保留Indy類的所有內容。 – user2864778
我不熟悉Indy ......但你爲什麼認爲你的上下文不是標準的Indy類,而是你自己的擴展類? –