[fpc-pascal] fphttclient, no way to specify a connect timeout

Dimitrios Chr. Ioannidis d.ioannidis at nephelae.eu
Fri Feb 23 22:57:34 CET 2018


Hi

Στις 23/2/2018 11:14 μμ, ο Luca Olivetti έγραψε:

<snip>
> yes, it does (in procedure TFPCustomHTTPClient.ConnectToServer)
>
>   FSocket:=TInetSocket.Create(AHost,APort,G);
>   try
>     if FIOTimeout<>0 then
>       FSocket.IOTimeout:=FIOTimeout; <-----
>     FSocket.Connect;
>   except
>     FreeAndNil(FSocket);
>     Raise;
>   end;

please follow the call stack.

The TFPCustomHTTPClient calls TInetSocket.Create constructor which it 
calls the inherited constructor from TSocketStream.

So just before the FSocket.Connect, the above code assigns the internal 
TSocketStream's FIOTimeout field ( as the TInetSocket is a descendant ) 
the value from the TFPCustomHTTPClient's FIOTimeout field.

Now the folowing addition in the procedure  TInetSocket.Connect uses the 
parents (TSocketStream) private procedure SetIOTimeout and FIOTimeout 
field to setup the socket timeout option.

ssockets.pp line 979

   addr.sin_family := AF_INET;
   addr.sin_port := ShortHostToNet(FPort);
   addr.sin_addr.s_addr := HostToNet(a.s_addr);
   SetIOTimeout(FIOTimeout); <------------------------------------
   {$ifdef unix}
   Res:=ESysEINTR;
     While (Res=ESysEINTR) do
   {$endif}
       Res:=fpConnect(Handle, @addr, sizeof(addr));


It works because the option is set after the socket is created and 
before the socket connect call. Of course this is a hack/workaround 
because if you need different timeouts for connect and receive then you 
need in the onconnect event handler to change the TFPCustomHTTPClient's 
FIOTimeout field again for the receive timeout .

AFAIK I'm sure Michael will solved it in a more elegant way than my 30 
min code review ....

Just try it and tell me if it works.

regards,

-- 
Dimitrios Chr. Ioannidis


More information about the fpc-pascal mailing list