Recent

Author Topic: TTimer stops after wake from sleep state  (Read 7688 times)

karloromic

  • New Member
  • *
  • Posts: 32
Re: TTimer stops after wake from sleep state
« Reply #15 on: September 17, 2019, 02:25:07 am »
@winni

The way i do it is inside Thread.Execute i check internet in two phases:

Ping timeout := 2000ms
1. Ping 8.8.8.8. / if no response in 2000ms then   ----Google DNS
    Ping 8.8.4.4. / if no response -||-     ----Google DNS
    Ping 1.1.1.1.     ---Cloudfare

Only if phase 1 was unsuccesfull do phase 2
ConnectionTimeout := 2000
IOTimeout := 3000
2. Send HTTP header request and check responsecode and headers
    https://google.com. / If not statuscode 200 OK then
    https://bing.com / -||-
    https://yahoo.com / -||-
    .....

If those 2 phases all return false then Internet connection state is offline
I do the second phase because:
"Some software firewalls, as well as NAT routers may block ping requests in an effort to "hide" your external IP address and prevent any response to potential intruders and prevent possible denial-of-service attacks in the form of a ping flood, in which an attacker overwhelms the victim with ICMP echo request packets." - source https://www.speedguide.net/faq/how-to-become-pingable-behind-a-routerfirewall-376
« Last Edit: September 17, 2019, 02:33:50 am by karloromic »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: TTimer stops after wake from sleep state
« Reply #16 on: September 17, 2019, 06:22:06 pm »
@karloromic

You gave enough arguments not to use ping.

Additional is to say that some routers have an option in the interface with the caption "transmit ping" an with one click you can disable it.

And the next point is, that ping by default runs with low priority. And if there is a hell of traffic on a router, it is allowed to drop the ping.

So you never know if you can rely on your information if the net is up or down.

If you want a more reliable answer, ask your nameserver for a well known address like I showed in my example.

Winni


karloromic

  • New Member
  • *
  • Posts: 32
Re: TTimer stops after wake from sleep state
« Reply #17 on: September 17, 2019, 06:27:46 pm »
@karloromic

You gave enough arguments not to use ping.

Additional is to say that some routers have an option in the interface with the caption "transmit ping" an with one click you can disable it.

And the next point is, that ping by default runs with low priority. And if there is a hell of traffic on a router, it is allowed to drop the ping.

So you never know if you can rely on your information if the net is up or down.

If you want a more reliable answer, ask your nameserver for a well known address like I showed in my example.

Winni
What is possible hang time and doest timeout exists when you ask nameserver for wellknown address?
Also what about DNS caching, would that create a problem?

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: TTimer stops after wake from sleep state
« Reply #18 on: September 17, 2019, 06:58:52 pm »
Repeating this
- OnTimer disables itself and starts new Thread(CreateSuspended)
- Thread.OnCreate calls inherited Create(CreateSuspended) and sets
  FreeOnTerminate := True;
- OnTimer Starts the thread
- Thread execute internet check code and on the end of Execute updates data to form with Synchronize(@UpdateStatus);
- UpdateStatus method updates data and when finished updating data, enables TTimer again
End Repeat
Perhaps the UpdateStatus or TThread.Execute code is not reentrant?
For example.
- UpdateStatus method updates data and when finished updating data, enables TTimer again
Computer falls asleep
Computer wakes up (timer interval has expired)
- OnTimer Starts the thread
Two threads exists.

@rvk has already written about a better solution.

BTW: TThread.OnTerminate is called after the Execute method and before releasing the thread, and in the context of the main thread. There is no need for an additional UpdateStatus method.

karloromic

  • New Member
  • *
  • Posts: 32
Re: TTimer stops after wake from sleep state
« Reply #19 on: September 17, 2019, 07:16:06 pm »
]
Perhaps the UpdateStatus or TThread.Execute code is not reentrant?
For example.
- UpdateStatus method updates data and when finished updating data, enables TTimer again
Computer falls asleep
Computer wakes up (timer interval has expired)
- OnTimer Starts the thread
Two threads exists.

@rvk has already written about a better solution.

BTW: TThread.OnTerminate is called after the Execute method and before releasing the thread, and in the context of the main thread. There is no need for an additional UpdateStatus method.
Yes, its possible.
Yes, i'm going to use while not Terminated loop as suggested by @rvk as its much more simple and cleaner approach, no need to create thread every 5 seconds.
Then i dont need UpdateStatus method at all, as OnTerminate does the same thing. Thank you  :D
« Last Edit: September 17, 2019, 07:21:27 pm by karloromic »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: TTimer stops after wake from sleep state
« Reply #20 on: September 17, 2019, 08:35:19 pm »
@karloromic

Good that you simplified your code.

From memory I say, that the reply time for an unknown internet address was a maximum of 3 seconds but in average 2 seconds. And the timeout for no connection to the DNS server is less than two seconds.

What kind of DNS Cache do you mean? If you have a DNS Cache in your LAN you must pass it by. If you mean the DNS Cache of your provider, you don't have to care, because yout got a WLAN connection anyway.

Winni 

karloromic

  • New Member
  • *
  • Posts: 32
Re: TTimer stops after wake from sleep state
« Reply #21 on: September 17, 2019, 09:25:33 pm »
@karloromic

Good that you simplified your code.

From memory I say, that the reply time for an unknown internet address was a maximum of 3 seconds but in average 2 seconds. And the timeout for no connection to the DNS server is less than two seconds.

What kind of DNS Cache do you mean? If you have a DNS Cache in your LAN you must pass it by. If you mean the DNS Cache of your provider, you don't have to care, because yout got a WLAN connection anyway.

Winni
What packages and unit are required for ResolveHostByName and where can i get them?

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: TTimer stops after wake from sleep state
« Reply #22 on: September 17, 2019, 09:45:49 pm »
Hi!

Code: Pascal  [Select][+][-]
  1. uses .....,netdb, Sockets;

Both units are in the  fpc-sources, fpcsrc/3.0.4/packages/fcl-net
so you have nothing to install.

You don't have to install any package.

Winni

karloromic

  • New Member
  • *
  • Posts: 32
Re: TTimer stops after wake from sleep state
« Reply #23 on: September 17, 2019, 09:50:33 pm »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, netdb, Sockets;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.   private
  13.  
  14.   public
  15.     function HostByName (const who : string; var IP : String): Boolean;  inline;
  16.   end;
  17.  
  18. var
  19.   Form1: TForm1;
  20.  
  21. implementation
  22.  
  23. {$R *.lfm}
  24.  
  25. function HostByName (const who : string; var IP : String): Boolean;  inline;
  26. Var H : THostEntry;
  27.   begin
  28.     result := ResolveHostByName(who,H);
  29.     if result then IP := HostAddrToStr(H.Addr) else IP := '';
  30.   end;
  31.  
  32. end.
  33.  
I get:
unit1.pas(8,58) Fatal: Cannot find netdb used by Unit1 of the Project Inspector.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: TTimer stops after wake from sleep state
« Reply #24 on: September 17, 2019, 10:08:14 pm »
Do you have the fpc sources installed?
That is necessary!

karloromic

  • New Member
  • *
  • Posts: 32
Re: TTimer stops after wake from sleep state
« Reply #25 on: September 17, 2019, 10:20:19 pm »
Do you have the fpc sources installed?
That is necessary!
Do i? (Attachment)
« Last Edit: September 17, 2019, 10:26:44 pm by karloromic »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: TTimer stops after wake from sleep state
« Reply #26 on: September 17, 2019, 10:41:34 pm »
Strange, strange, strange .....

We work around:

Place your cursor on "uses ......, netdb,.....;

Press Alt CursorUp
Now the unit netdb should appear in an editor tab.Place your cursor in this unit.

Press Shift F11
He asks if he should add the unit to the project. YES.

Save everything:  Ctrl  Shift S

That should help.

karloromic

  • New Member
  • *
  • Posts: 32
Re: TTimer stops after wake from sleep state
« Reply #27 on: September 17, 2019, 10:50:21 pm »
@winni

It solved the problem, however another one surfaced..

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: TTimer stops after wake from sleep state
« Reply #28 on: September 18, 2019, 12:09:06 am »
Oops  - you are using Windows, as I saw from the screenshots.
Sorry - netdb .is Linux only.

Then use another way with the  fphttpclient , which can be also used in Windows.

There is a good page in the wiki:
https://wiki.lazarus.freepascal.org/fphttpclient

At the end of the page is a

Code: Pascal  [Select][+][-]
  1. function GetExternalIPAddress: string;

Change it to your needs.
This can be used for checking if an internet connections is possible.

Winni



winni

  • Hero Member
  • *****
  • Posts: 3197
Re: TTimer stops after wake from sleep state
« Reply #29 on: September 18, 2019, 01:32:31 am »
@karloromic

Made a little demo program with the routine from the wiki page.
Runs well with Linux and should also do with Windows.

The timer interval is set to 1 second because the reactions are very quick.
Test it!

Zip file  attached.

Winni

 

TinyPortal © 2005-2018