-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAmIOnline.cs
60 lines (50 loc) · 2.13 KB
/
AmIOnline.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
RMLib: Nonvisual support classes used by multiple R&M Software programs
Copyright (C) Rick Parrish, R&M Software
This file is part of RMLib.
RMLib is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
RMLib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with RMLib. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
namespace RandM.RMLib
{
public static class AmIOnline
{
private static object _Lock = new object();
private static string[] _OnlineCheckHosts = { "akamai.com", "baidu.com", "blogspot.com", "cachefly.com", "cdnetworks.com", "cloudflare.com", "facebook.com", "gmail.com", "google.com", "hotmail.com", "linkedin.com", "qq.com", "twitter.com", "wikipedia.org", "wordpress.com", "yahoo.com", "youtube.com" };
private static int _OnlineCheckIndex = new Random().Next(0, _OnlineCheckHosts.Length);
public static bool PingNextHosts(int tries)
{
for (int i = 0; i < tries; i++)
{
int NextHostIndex = 0;
lock (_Lock)
{
_OnlineCheckIndex = (++_OnlineCheckIndex % _OnlineCheckHosts.Length);
NextHostIndex = _OnlineCheckIndex;
}
if (WebUtils.Ping(_OnlineCheckHosts[NextHostIndex], 5000)) return true;
}
return false;
}
public static void CheckHosts()
{
foreach (string Host in _OnlineCheckHosts)
{
if (!WebUtils.Ping(Host, 5000))
{
throw new Exception("Failed to ping " + Host);
}
}
throw new Exception("All hosts pingable");
}
}
}