- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 Indy 10.6.2.5298。
TIdTCPConnection.Disconnect 和 TIdIOHandler.Close 有什么区别?他们都断开线路,但有时前者会出现访问冲突错误。
很抱歉,我无法通过帮助文档及其源代码理解它。
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
事件):
procedure TIdTCPConnection.Disconnect(ANotifyPeer: Boolean);
var
// under ARC, convert a weak reference to a strong reference before working with it
LIOHandler: TIdIOHandler;
begin
try
// Separately to avoid calling .Connected unless needed
if ANotifyPeer then begin
// TODO: do not call Connected() here if DisconnectNotifyPeer() is not
// overriden. Ideally, Connected() should be called by overridden
// DisconnectNotifyPeer() implementations if they really need it. But
// to avoid any breakages in third-party overrides, we could check here
// if DisconnectNotifyPeer() has been overridden and then call Connected()
// to maintain existing behavior...
//
try
if Connected then begin
DisconnectNotifyPeer;
end;
except
// TODO: maybe allow only EIdConnClosedGracefully and EIdSocketError?
end;
end;
finally
{
there are a few possible situations here:
1) we are still connected, then everything works as before,
status disconnecting, then disconnect, status disconnected
2) we are not connected, and this is just some "rogue" call to
disconnect(), then nothing happens
3) we are not connected, because ClosedGracefully, then
LConnected will be false, but the implicit call to
CheckForDisconnect (inside Connected) will call the events
}
// We dont check connected here - we realy dont care about actual socket state
// Here we just want to close the actual IOHandler. It is very possible for a
// socket to be disconnected but the IOHandler still open. In this case we only
// care of the IOHandler is still open.
//
// This is especially important if the socket has been disconnected with error, at this
// point we just want to ignore it and checking .Connected would trigger this. We
// just want to close. For some reason NS 7.1 (And only 7.1, not 7.0 or Mozilla) cause
// CONNABORTED. So its extra important we just disconnect without checking socket state.
LIOHandler := IOHandler;
if Assigned(LIOHandler) then begin
if LIOHandler.Opened then begin
DoStatus(hsDisconnecting);
LIOHandler.Close;
DoOnDisconnected;
DoStatus(hsDisconnected);
//LIOHandler.InputBuffer.Clear;
end;
end;
end;
end;
TIdIOHandler.Close()
如果已分配套接字,则只需关闭套接字:
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;
出现访问冲突错误的原因可能是因为您的测试代码一开始就不是线程安全的。 TIdTCPServer
是一个多线程组件。它的OnConnect
, OnDisconnect
, OnExecute
,和OnException
事件在管理TIdContext
的工作线程的上下文中触发。目的。您的OnClick
处理程序正在访问 TIdContext
该线程之外的对象。一旦套接字关闭,TIdTCPServer
将检测到并停止线程,破坏 TIdContext
及其 TIdTCPConnection
和TIdIOHandler
对象。由于线程计时和上下文切换,您的 OnClick
处理程序很可能在这些对象被销毁后继续访问它们。你在 OnExecute
中不存在这个问题。处理程序,因为对象在线程运行时仍然有效。
让您的OnClick
代码与 TIdTCPServer
配合得很好,您需要锁定TIdTCPServer.Contexts
列出 TIdContext
OnClick
期间对象无法被销毁仍在尝试使用它,例如:
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;
关于delphi - TIdTCPConnection.Disconnect 和 TIdIOHandler.Close 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34182171/
我使用 Indy 10.6.2.5298。 TIdTCPConnection.Disconnect 和 TIdIOHandler.Close 有什么区别?他们都断开线路,但有时前者会出现访问冲突错误。
当 TIdContext.Connection 触发 OnWorkEnd 事件时,可以从 TIdTCPConnection 检索一些信息(用于记录目的)吗? 我想要以下信息: - 用户ip地址(在So
我是一名优秀的程序员,十分优秀!