XE8-indy10中关于TIdTCPClientCustom.Connect的源码和解读

TIdTCPClientCustom.Connect源码如下,中文注释是自己写的。

procedure TIdTCPClientCustom.Connect;
begin
  if Connected then begin		
    EIdAlreadyConnected.Toss(RSAlreadyConnected);	//如果已经连接则抛出AlreadyConnected的异常
  end;

  if Host = '' then begin
    EIdHostRequired.Toss('');		//如果Host为空,则抛出EIdHostRequired的异常
  end;
  if Port = 0 then begin
    EIdPortRequired.Toss('');		//如果Host为空,则抛出EIdPortRequired的异常
  end;

  if IOHandler = nil then begin
    IOHandler := MakeImplicitClientHandler;


    IOHandler.OnStatus := OnStatus;	//改变OnStatus的状态

    ManagedIOHandler := True;
  end;

  try
    // Bypass GetDestination
    if FDestination <> '' then begin
      IOHandler.Destination := FDestination;
    end;

{BGO: not any more, TIdTCPClientCustom has precedence now (for port protocols, and things like that)
    // We retain the settings that are in here (filled in by the user)
    // we only do this when the iohandler has no settings,
    // because the iohandler has precedence
    if (IOHandler.Port = 0) and (IOHandler.Host = '') then begin
      IOHandler.Port := FPort;
      IOHandler.Host := FHost;
    end;
}

    IOHandler.Port := FPort; //BGO: just to make sure
    IOHandler.Host := FHost;
    IOHandler.ConnectTimeout := FConnectTimeout;
    IOHandler.ReadTimeout := FReadTimeout;

    if Socket <> nil then begin
      Socket.BoundIP := FBoundIP;
      Socket.BoundPort := FBoundPort;
      Socket.BoundPortMin := FBoundPortMin;
      Socket.BoundPortMax := FBoundPortMax;
      Socket.IPVersion := FIPVersion;
      Socket.ReuseSocket := FReuseSocket;
      Socket.UseNagle := FUseNagle;
      Socket.OnBeforeBind := FOnBeforeBind;
      Socket.OnAfterBind := FOnAfterBind;
      Socket.OnSocketAllocated := FOnSocketAllocated;
    end;

    IOHandler.Open;		//打开IO
    if IOHandler.Intercept <> nil then begin
      IOHandler.Intercept.Connect(Self);
    end;

    DoStatus(hsConnected, [Host]);
    DoOnConnected;		//调用OnConnected函数
  except		//捕捉异常
    if IOHandler <> nil then begin
      IOHandler.Close;
      if ManagedIOHandler then begin
        IOHandler := nil; // RLebeau - SetIOHandler() will free the IOHandler
      end;
    end;
    raise;
  end;
end;


你可能感兴趣的:(Delphi,indy10)