XE8-indy10에서 TIDTCPClient 컨트롤 Disconnect 소스 분석

4647 단어 Delphi컨트롤indy10
indy10의 TIDTCPClient 컨트롤 Disconnect 소스:
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
                                                                            
      // 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 begin
        DisconnectNotifyPeer;
      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;

주석:
    {
     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) 연결이 없으나 Closed Gracefully 때문에 (CheckForDisconnect 호출이 표시되지 않는 한 Lconnected에서 오류가 발생합니다.
소스 코드의 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 connection
        CheckForDisconnect(ARaiseExceptionIfDisconnected);

//Check here as other side may have closed connection
여기에서 다른 쪽 연결이 닫혔는지 확인
그래서 CheckForDisconnect는 상대방의 연결이 끊겼는지 확인하는 함수라고 생각합니다.

좋은 웹페이지 즐겨찾기