TIdTCPServer上の接続のコンテキストにカスタムプロパティを割り当てると、スレッドセーフな方法でこのプロパティにアクセスするにはどうすればよいですか?例:カスタムAContextプロパティにアクセスするTIdTCPServer
カスタムプロパティ:
type
Local_Socket = class(TIdContext)
public
Tunnel_Requested: bool;
Remote_Tunnel: TIdContext;
end;
type
Remote_Socket = class(TIdContext)
public
Local_Tunnel: TIdContext;
end;
それらの割り当て:接続がローカル(127.0.0.1)であれば、私はこのコードで実現しようと何
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;
は、ですが、私は必要これをリモート接続にリダイレクトするには、以下のコードで要求されます。ここで
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;
:リモート接続がサーバーに到着したら、私はリモート接続とlocal_socket.remote_tunnel性、ローカル接続とremote_socket.local_tunnel、私は透過的にトンネルの間で通信することができ、このように相関する、AssignRemoteTunnel私は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 ...'をチェックするか、ここで 'AContext AS Local_Socket DO BEGIN Tunnel_Requested:= false; Remote_Tunnel:= Nil; END; ' –
情報ありがとうございます。 TMonitor.Lockについて、私はTIdThreadSafeクラスの使用を考えていました。この方法でIndyクラスのすべてを保持します。 – user2864778
私はインディーに精通していません...しかし、なぜあなたはあなたの文脈が標準的なインディーズクラスではなくあなた自身の拡張されたものだろうと思いますか? –