forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostCheck.cs
More file actions
117 lines (103 loc) · 3.64 KB
/
Copy pathHostCheck.cs
File metadata and controls
117 lines (103 loc) · 3.64 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.ServiceProcess;
using System.Threading;
using L2dotNET.Logging.Abstraction;
using L2dotNET.Shared;
namespace L2dotNET.Repositories.Utils
{
public static class HostCheck
{
private static readonly ILog Log = LogProvider.GetCurrentClassLogger();
public static bool IsPingSuccessful(string host, int timeoutMs)
{
try
{
PingReply pingReply = new Ping().Send(host, timeoutMs, new byte[1]);
return (pingReply != null) && (pingReply.Status == IPStatus.Success);
}
catch (Exception ex)
{
Log.Error($"HostCheck: IsPingSuccessful: {ex.Message}");
}
return false;
}
public static bool IsLocalIpAddress(string host)
{
try
{
// get host IP addresses
IPAddress[] hostIPs = Dns.GetHostAddresses(host);
// get local IP addresses
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
// test if any host IP equals to any local IP or to localhost
foreach (IPAddress hostIp in hostIPs)
{
// is localhost
if (IPAddress.IsLoopback(hostIp))
return true;
// is local address
if (localIPs.Contains(hostIp))
return true;
}
}
catch (Exception ex)
{
Log.Error($"HostCheck: IsLocalIPAddress: {ex.Message}");
}
return false;
}
public static bool ServiceExists(string serviceName)
{
try
{
return ServiceController.GetServices().Any(service => service.ServiceName.StartsWithIgnoreCase(serviceName));
}
catch (Exception ex)
{
Log.Error($"HostCheck: ServiceExists: {ex.Message}");
}
return false;
}
public static bool IsServiceRunning(string serviceName)
{
try
{
return ServiceController.GetServices().Any(service => service.ServiceName.StartsWithIgnoreCase(serviceName) && (service.Status == ServiceControllerStatus.Running));
}
catch (Exception ex)
{
Log.Error($"HostCheck: IsServiceRunning: {ex.Message}");
}
return false;
}
public static void StartService(string serviceName, int timeoutMs)
{
ServiceController service = ServiceController.GetServices().FirstOrDefault(filter => filter.ServiceName.StartsWithIgnoreCase(serviceName));
if (service == null)
return;
switch (service.Status)
{
case ServiceControllerStatus.Running:
break;
case ServiceControllerStatus.Stopped:
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMs);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch (Exception ex)
{
Log.Error($"HostCheck: StartService: {ex.Message}");
}
break;
default:
Thread.Sleep(timeoutMs);
break;
}
}
}
}