forked from BornToBeRoot/NETworkManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPing.cs
More file actions
216 lines (175 loc) · 6.91 KB
/
Copy pathPing.cs
File metadata and controls
216 lines (175 loc) · 6.91 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using NETworkManager.Utilities;
namespace NETworkManager.Models.Network;
/// <summary>
/// Provides functionality to ping a network host.
/// </summary>
public sealed class Ping
{
#region Variables
/// <summary>
/// The time in milliseconds to wait between ping requests. Default is 1000ms.
/// </summary>
public int WaitTime = 1000;
/// <summary>
/// The time in milliseconds to wait for a reply. Default is 4000ms.
/// </summary>
public int Timeout = 4000;
/// <summary>
/// The buffer to send with the ping request. Default is 32 bytes.
/// </summary>
public byte[] Buffer = new byte[32];
/// <summary>
/// The Time to Live (TTL) value for the ping request. Default is 64.
/// </summary>
public int TTL = 64;
/// <summary>
/// Indicates whether to prevent fragmentation of the data packets. Default is true.
/// </summary>
public bool DontFragment = true;
private const int ExceptionCancelCount = 3;
#endregion
#region Events
/// <summary>
/// Occurs when a ping reply is received.
/// </summary>
public event EventHandler<PingReceivedArgs> PingReceived;
private void OnPingReceived(PingReceivedArgs e)
{
PingReceived?.Invoke(this, e);
}
/// <summary>
/// Occurs when the ping operation is completed.
/// </summary>
public event EventHandler PingCompleted;
private void OnPingCompleted()
{
PingCompleted?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Occurs when a ping exception is thrown.
/// </summary>
public event EventHandler<PingExceptionArgs> PingException;
private void OnPingException(PingExceptionArgs e)
{
PingException?.Invoke(this, e);
}
/// <summary>
/// Occurs when the hostname is resolved.
/// </summary>
public event EventHandler<HostnameArgs> HostnameResolved;
private void OnHostnameResolved(HostnameArgs e)
{
HostnameResolved?.Invoke(this, e);
}
/// <summary>
/// Occurs when the user has canceled the operation.
/// </summary>
public event EventHandler UserHasCanceled;
private void OnUserHasCanceled()
{
UserHasCanceled?.Invoke(this, EventArgs.Empty);
}
#endregion
#region Methods
/// <summary>
/// Sends ping requests to the specified IP address asynchronously.
/// </summary>
/// <param name="ipAddress">The IP address to ping.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
public void SendAsync(IPAddress ipAddress, CancellationToken cancellationToken)
{
Task.Run(async () =>
{
var hostname = string.Empty;
// Try to resolve PTR
var dnsResult = await DNSClient.GetInstance().ResolvePtrAsync(ipAddress);
if (!dnsResult.HasError)
{
hostname = dnsResult.Value;
OnHostnameResolved(new HostnameArgs(hostname));
}
var errorCount = 0;
var options = new PingOptions
{
Ttl = TTL,
DontFragment = DontFragment
};
using (var ping = new System.Net.NetworkInformation.Ping())
{
do
{
try
{
// Get timestamp
var timestamp = DateTime.Now;
// Send ping
var pingReply = ping.Send(ipAddress, Timeout, Buffer, options);
// Reset the error count (if no exception was thrown)
errorCount = 0;
if (pingReply is not { Status: IPStatus.Success })
{
if (pingReply != null)
OnPingReceived(new PingReceivedArgs(
new PingInfo(timestamp, pingReply.Address, hostname, pingReply.Status)));
}
else
{
if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
OnPingReceived(new PingReceivedArgs(
new PingInfo(timestamp, pingReply.Address, hostname,
pingReply.Buffer.Length, pingReply.RoundtripTime, pingReply.Options!.Ttl,
pingReply.Status)));
else
OnPingReceived(new PingReceivedArgs(
new PingInfo(timestamp, pingReply.Address, hostname,
pingReply.Buffer.Length, pingReply.RoundtripTime, pingReply.Status)));
}
}
catch (PingException ex)
{
errorCount++;
if (errorCount == ExceptionCancelCount)
{
OnPingException(new PingExceptionArgs(ex.Message, ex.InnerException));
break;
}
}
// If ping is canceled... dont wait for example 5 seconds
for (var i = 0; i < WaitTime; i += 100)
{
Thread.Sleep(100);
if (cancellationToken.IsCancellationRequested)
break;
}
} while (!cancellationToken.IsCancellationRequested);
}
if (cancellationToken.IsCancellationRequested)
OnUserHasCanceled();
// Currently not used (ping will run until the user cancels it)
else
OnPingCompleted();
}, cancellationToken);
}
// Param: disableSpecialChar --> ExportManager --> "<" this char cannot be displayed in xml
/// <summary>
/// Converts the ping time to a string representation.
/// </summary>
/// <param name="status">The IP status of the ping reply.</param>
/// <param name="time">The round-trip time in milliseconds.</param>
/// <param name="disableSpecialChar">If true, disables special characters like '<' in the output (e.g., for XML export).</param>
/// <returns>The formatted time string.</returns>
public static string TimeToString(IPStatus status, long time, bool disableSpecialChar = false)
{
if (status != IPStatus.Success && status != IPStatus.TtlExpired)
return "-/-";
_ = long.TryParse(time.ToString(), out var t);
return disableSpecialChar ? $"{t} ms" : t == 0 ? "<1 ms" : $"{t} ms";
}
#endregion
}