请选择 进入手机版 | 继续访问电脑版

硬汉嵌入式论坛

 找回密码
 立即注册
查看: 4863|回复: 0
收起左侧

indy10中TIdTCPClient控件Disconnect源码分析

[复制链接]

747

主题

1048

回帖

3294

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3294
发表于 2015-10-24 23:33:10 | 显示全部楼层 |阅读模式
http://www.codes51.com/article/detail_176641.html


本篇文章主要介绍了"indy10中TIdTCPClient控件Disconnect源码分析",主要涉及到方面的内容,对于Delphi教程感兴趣的同学可以参考一下: indy10中TIdTCPClient控件Disconnect源码:procedure TIdTCPConnection.Disconnect(ANotifyP...


indy10中TIdTCPClient控件Disconnect源码


procedure TIdTCPConnection.Disconnect(ANotifyPeer: Boolean);var// under ARC, convert a weak reference to a strong reference before working with itLIOHandler: TIdIOHandler;begintry// Separately to avoid calling .Connected unless neededif ANotifyPeer then begin// 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...//if Connected then beginDisconnectNotifyPeer;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 disconnected2) we are not connected, and this is just some "rogue" call todisconnect(), then nothing happens3) we are not connected, because ClosedGracefully, thenLConnected will be false, but the implicit call toCheckForDisconnect (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 beginif LIOHandler.Opened then beginDoStatus(hsDisconnecting);LIOHandler.Close;DoOnDisconnected;DoStatus(hsDisconnected);//LIOHandler.InputBuffer.Clear;end;end;end;end;


注释:

    {
     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.
    // CONNABORTED. So its extra important we just disconnect without checking socket state.



中文翻译(可能不是很准确):
一些可能的情况:
1)正常连接,在其他事情发生之前,状态变为断开连接中,然后断开连接,最后状态变为断开连接;
2)没有连接,并且只是有一些流氓调用断开连接函数,那么什么事也不会发生;
3)没有连接,但因为ClosedGracefully(不知道怎么翻译好),那么Lconnected会出错,除非显示调用CheckForDisconnect。


另外附上源码中的CheckForDisconnect原型与注解。

    // CheckForDisconnect allows the implementation to check the status of the// connection at the request of the user or this base class.procedure CheckForDisconnect(ARaiseExceptionIfDisconnected: Boolean = True;AIgnoreBuffer: Boolean = False); virtual; abstract;
根据源码某处的注解

        // Check here as other side may have closed connectionCheckForDisconnect(ARaiseExceptionIfDisconnected);
//Check here as other side may have closed connection
中文翻译:在这里检查另一端是否已经关闭连接
所以,我认为CheckForDisconnect是用来检查对方连接是否断开的函数。



以上就介绍了indy10中TIdTCPClient控件Disconnect源码分析,包括了方面的内容,希望对Delphi教程有兴趣的朋友有所帮助。


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-4-19 06:30 , Processed in 0.277366 second(s), 33 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表