Delphi TIdTCPConnection.Disconnect和TIdIOHandler.Close的区别
我使用Indy 10.6.2.5298 TIdTCPConnection.Disconnect和TIdIOHandler.Close的区别是什么?它们都会断开线路,但有时前者会出现访问冲突错误 很抱歉,我无法通过帮助文档及其源代码理解它Delphi TIdTCPConnection.Disconnect和TIdIOHandler.Close的区别,delphi,indy,Delphi,Indy,我使用Indy 10.6.2.5298 TIdTCPConnection.Disconnect和TIdIOHandler.Close的区别是什么?它们都会断开线路,但有时前者会出现访问冲突错误 很抱歉,我无法通过帮助文档及其源代码理解它 type TForm1 = class(TForm) IdTCPServer1: TIdTCPServer; procedure FormClick(Sender: TObject); procedure IdTCPServer1Ex
type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
procedure FormClick(Sender: TObject);
procedure IdTCPServer1Execute(AContext: TIdContext);
private
TestContext: TIdContext;
end;
procedure TForm1.FormClick(Sender: TObject);
begin
TestContext.Connection.Disconnect; // access violation
TestContext.Connection.IOHandler.Close; // always works well
end;
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
begin
TestContext := AContext;
AContext.Connection.Disconnect; // works well
end;
TIdTCPConnection.Disconnect()
在内部调用IOHandler.Close()
,如果IOHandler
已分配并已打开(它还调用TIdTCPConnection.DisconnectNotifyPeer()
并触发OnDisconnected
和OnStatus
事件):
TIdIOHandler.Close()
只需关闭套接字(如果已分配):
procedure TIdIOHandlerSocket.Close;
begin
if FBinding <> nil then begin
FBinding.CloseSocket;
end;
inherited Close;
end;
procedure TIdIOHandlerSocket.Close;
begin
if FBinding <> nil then begin
FBinding.CloseSocket;
end;
inherited Close;
end;
procedure TIdIOHandler.Close;
//do not do FInputBuffer.Clear; here.
//it breaks reading when remote connection does a disconnect
var
// under ARC, convert a weak reference to a strong reference before working with it
LIntercept: TIdConnectionIntercept;
begin
try
LIntercept := Intercept;
if LIntercept <> nil then begin
LIntercept.Disconnect;
end;
finally
FOpened := False;
WriteBufferClear;
end;
end;
procedure TForm1.FormClick(Sender: TObject);
var
List: TIdContextList;
begin
List := IdTCPServer1.Contexts.LockList;
try
//has the context already been removed?
if List.IndexOf(TestContext) <> -1 then
TestContext.Connection.Disconnect;
finally
IdTCPServer1.Contexts.UnlockList;
end;
end;