Recent

Author Topic: lNet Timeout  (Read 11929 times)

Eclipticon

  • Jr. Member
  • **
  • Posts: 51
lNet Timeout
« on: May 05, 2011, 07:27:56 pm »
Hello everyone,

I need to fetch just a short string from a webserver and I am doing so with the lNet TLHTTPClientComponent. Everything works like a charm, just not setting the timeout. In the object inspector it always jumps to 0, in the code it seems not to have any effect. What am I doing wrong?

Thanks!

Code: [Select]
procedure TAboutForm.SomeButtonClick(Sender: TObject);
begin
  HTTPBuffer := '';
  HTTPFinished := false;
  HTTPError := '';
  CheckUpdateLHTTPClient.Host := 'www.something.net';
  CheckUpdateLHTTPClient.URI  := '/test.txt';
  CheckUpdateLHTTPClient.Port := 809; // Meant to produce a timeout
  CheckUpdateLHTTPClient.Timeout:= 5; // seconds or what?
  CheckUpdateLHTTPClient.SendRequest;

  // Wait until OnDoneInput or OnError
  while (not HTTPFinished)
    do Application.ProcessMessages;

  if (Length(HTTPError) = 0) then begin
    MessageDlg(HTTPBuffer, mtInformation, [mbOk], 0);
  end
  else
    MessageDlg(HTTPError, mtError, [mbOk], 0);

end;

procedure TAboutForm.CheckUpdateLHTTPClientDisconnect(aSocket: TLSocket);
begin
  HTTPFinished := true;
end;

procedure TAboutForm.CheckUpdateLHTTPClientDoneInput(ASocket: TLHTTPClientSocket);
begin
  ASocket.Disconnect;
end;

procedure TAboutForm.CheckUpdateLHTTPClientError(const Msg: string; ASocket: TLSocket);
begin
  HTTPError:=Msg;
  HTTPFinished:=true;
  ASocket.Disconnect; // REQUIRED?
end;

function TAboutForm.CheckUpdateLHTTPClientInput(ASocket: TLHTTPClientSocket; ABuffer: pchar; ASize: integer): integer;
var
  OldLength: dword;
begin
  OldLength := Length(HTTPBuffer);
  SetLength(HTTPBuffer, OldLength + ASize);
  Move(ABuffer^, HTTPBuffer[OldLength + 1], ASize);
  Result := ASize; // tell the http buffer we read it all
end;
Lazarus 1.2.4 on Win XP/7 Virtual Machine

Almindor

  • Sr. Member
  • ****
  • Posts: 412
    • http://www.astarot.tk
Re: lNet Timeout
« Reply #1 on: May 05, 2011, 08:29:07 pm »
Timeout is irrelevant for visual components, it is only useful for non-visual (e.g.: console apps) when you need to handle the event listening and timing yourself.

In visual components it is ignored (that's why it jumps back) because lNet "integrates" into the main widgetset loop to see events on sockets.

Eclipticon

  • Jr. Member
  • **
  • Posts: 51
Re: lNet Timeout
« Reply #2 on: May 05, 2011, 09:51:04 pm »
Almindor,

thanks for your reply (and of course the effort and time you invest into lNet)!

1) Is there any way to influence the timeout if using the visual components?

2) Does my approach of coding this look sensible to you? Sorry, completely new to this :-)

Thanks, best regards from nearby Vienna!
Lazarus 1.2.4 on Win XP/7 Virtual Machine

Almindor

  • Sr. Member
  • ****
  • Posts: 412
    • http://www.astarot.tk
Re: lNet Timeout
« Reply #3 on: May 06, 2011, 03:48:53 pm »
Almindor,

thanks for your reply (and of course the effort and time you invest into lNet)!

1) Is there any way to influence the timeout if using the visual components?

2) Does my approach of coding this look sensible to you? Sorry, completely new to this :-)

Thanks, best regards from nearby Vienna!

I think you shouldn't try to "block and wait" inside the application like you seem to be doing.

Just wait for the download/activity to finish and when so change state (enable components or whatever you need) :)

Shebuka

  • Sr. Member
  • ****
  • Posts: 427
Re: lNet Timeout
« Reply #4 on: June 09, 2011, 11:53:20 am »
I'm using lnetbase with gui on mac os x and all works fine until http server exists, but as it doesn't with timeout set to -1 (as in example console code) all gui get frozen...
so what timeout valor is reasonable? and is it seconds or milliseconds?

p.s. thinking btw to put it on thread.

Almindor

  • Sr. Member
  • ****
  • Posts: 412
    • http://www.astarot.tk
Re: lNet Timeout
« Reply #5 on: June 09, 2011, 11:57:20 am »
If you're using the console lNet objects (lnetbase) then timeout = -1 means "block until something happens".

So unless the other side sends you a message it will block :)

Shebuka

  • Sr. Member
  • ****
  • Posts: 427
Re: lNet Timeout
« Reply #6 on: June 09, 2011, 12:20:44 pm »
If you're using the console lNet objects (lnetbase) then timeout = -1 means "block until something happens".

So unless the other side sends you a message it will block :)

ok, i'v tried different values and it seems like any value 0 < x < 1000 is like immediate responce
as i set it to 1000 all gui froze, if i 'pause' the execution it points to n := KEvent(FQueue, @FChanges[0], FFreeSlot, @FEvents[0], Length(FEvents), @FTimeout) in TLKQueueEventer.CallAction and after 'play' it resumes.

maybe it's only mac os x issue...

edit: done a class that handles http client in thread way, now working like a charm but timeout is still a misterious thing for me, if i set it to more that 1000 it took allways 200 seconds (3:20 min) to get response:
Code: [Select]
17.48.39.761 INFO     TFrm_Get.GetTerminalContacts => HttpClient Started
17.52.00.061 ERROR    ERROR => TFrm_Get.DownloadReceive => HTTPClient error: Shutdown error [57]: Socket is not connected
17.52.00.061 ERROR    ERROR => TFrm_Get.DownloadReceive => HTTPClient error: Get error [54]: Connection reset by peer
« Last Edit: June 09, 2011, 06:00:26 pm by Shebuka »

Almindor

  • Sr. Member
  • ****
  • Posts: 412
    • http://www.astarot.tk
Re: lNet Timeout
« Reply #7 on: June 10, 2011, 09:35:44 am »
Ok let me explain fully :)

Timeout < 0 => block until something happens (however long it takes)
Timeout = 0 => shouldn't block at all
Timeout > 0 => blocks for Timeout milliseconds or until something happens (if it's sooner than timeout elapsed)

Shebuka

  • Sr. Member
  • ****
  • Posts: 427
Re: lNet Timeout
« Reply #8 on: June 10, 2011, 09:51:42 am »
OK, i'm not saying that i don't understand what it must do, but that it's not actually doing what it must do when there is wrong host name (not http server)
i'v tried timeout of 500 and it blocks for 500 milliseconds
i'v tried timeout of 1000 (1 second) and it blocks for 200.000 milliseconds (3:20 minutes)
i'v tried timeout of 2000 and it blocks for 200.000 milliseconds
i'v tried timeout of 5000 and it blocks for 200.000 milliseconds

with values of timeout under 1000 it works correctly, with timeouts over 1000 it's always blocks for 200000 instead.

maybe it's only a Mac issue with some timers or event handlers...
« Last Edit: June 10, 2011, 10:06:15 am by Shebuka »

Almindor

  • Sr. Member
  • ****
  • Posts: 412
    • http://www.astarot.tk
Re: lNet Timeout
« Reply #9 on: June 11, 2011, 10:38:49 am »
Are you sure you're not getting a receive event on the socket? I cannot test this since I don't own a mac...

 

TinyPortal © 2005-2018