-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
842 lines (668 loc) · 34.8 KB
/
Program.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
#region Copyright (c) 2012 - 2013, Roland Pihlakas
/////////////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2012 - 2013, Roland Pihlakas.
//
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
//
/////////////////////////////////////////////////////////////////////////////////////////
#endregion Copyright (c) 2012 - 2013, Roland Pihlakas
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Runtime;
using System.Reflection;
using System.IO;
using System.Text.RegularExpressions;
using System.Security;
namespace HttpPingTool
{
// Summary:
// Encapsulates a method that takes no parameters and does not return a value.
public delegate void Action(); //.net 3.0 does not contain Action delegate declaration
[SuppressUnmanagedCodeSecurity] //SuppressUnmanagedCodeSecurity - For methods in this particular class, execution time is often critical. Security can be traded for additional speed by applying the SuppressUnmanagedCodeSecurity attribute to the method declaration. This will prevent the runtime from doing a security stack walk at runtime. - MSDN: Generally, whenever managed code calls into unmanaged code (by PInvoke or COM interop into native code), there is a demand for the UnmanagedCode permission to ensure all callers have the necessary permission to allow this. By applying this explicit attribute, developers can suppress the demand at run time. The developer must take responsibility for assuring that the transition into unmanaged code is sufficiently protected by other means. The demand for the UnmanagedCode permission will still occur at link time. For example, if function A calls function B and function B is marked with SuppressUnmanagedCodeSecurityAttribute, function A will be checked for unmanaged code permission during just-in-time compilation, but not subsequently during run time.
partial class Program
{
[DllImport("kernel32.dll")]
internal static extern bool SetProcessWorkingSetSize(IntPtr hProcess, IntPtr dwMinimumWorkingSetSize, IntPtr dwMaximumWorkingSetSize);
[/*DllImport("kernel32.dll")*/DllImport("psapi.dll")]
internal static extern bool EmptyWorkingSet(IntPtr processHandle);
static MultiAdvancedPinger multiPinger = null;
// ############################################################################
[STAThread] //prevent message loop creation
static void Main(string[] args)
{
Console.WriteLine();
Console.WriteLine(Application.ProductName + " / Version: " + Application.ProductVersion);
Console.WriteLine();
GetConsoleArgumentsValues(args);
if (ValueShowHelp)
{
OutputConsoleArgumentsHelp();
return;
}
ExitHandler.InitUnhandledExceptionHandler();
ExitHandler.HookSessionEnding();
ExitHandler.ExitEventOnce += ExitEventHandler;
Process CurrentProcess = Process.GetCurrentProcess();
IntPtr handle = CurrentProcess.Handle;
CurrentProcess.PriorityClass = ProcessPriorityClass.High;
NativeMethods.SetPagePriority(handle, 1); //NB! lowest Page priority
NativeMethods.SetIOPriority(handle, NativeMethods.PROCESSIOPRIORITY.PROCESSIOPRIORITY_NORMAL); //ensure that we do not inherit low IO priority from the parent process or something like that
GC.Collect(2, GCCollectionMode.Forced); //collect now all unused startup info because later we will be relatively steady state and will not need any more much memory management
GCSettings.LatencyMode = GCLatencyMode.Batch; //most intrusive mode - most efficient //This option affects only garbage collections in generation 2; generations 0 and 1 are always non-concurrent because they finish very fast. - http://msdn.microsoft.com/en-us/library/ee787088(v=VS.110).aspx#workstation_and_server_garbage_collection
try
{
//GC.WaitForFullGCComplete(); //cob roland: the exception cannot be caught when the method name is written inline, see also http://stackoverflow.com/questions/3546580/why-is-it-not-possible-to-catch-missingmethodexception
typeof(GC).InvokeMember("WaitForFullGCComplete",
BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod,
null, null, null); //see ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_mscorlib/html/9926d3b0-b0ef-e965-bc72-9ee34bf84df5.htm
}
catch (MissingMethodException) //GC.WaitForFullGCComplete() is only available on .NET SP1 versions
{
Thread.Sleep(100);
}
AutoResetEvent GC_WaitForPendingFinalizers_done = new AutoResetEvent(false);
Thread thread = new Thread(() =>
{
// Wait for all finalizers to complete before continuing.
// Without this call to GC.WaitForPendingFinalizers,
// the worker loop below might execute at the same time
// as the finalizers.
// With this call, the worker loop executes only after
// all finalizers have been called.
GC.WaitForPendingFinalizers();
GC_WaitForPendingFinalizers_done.Set();
});
thread.Name = "GC.WaitForPendingFinalizers thread";
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true; //Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating.
thread.Start();
GC_WaitForPendingFinalizers_done.WaitOne(10000); //NB! prevent hangs
NativeMethods.SetProcessWorkingSetSize(handle, new IntPtr(-1), new IntPtr(-1)); //empty the working set
NativeMethods.EmptyWorkingSet(handle); //empty the working set
try
{
multiPinger = new MultiAdvancedPinger(ValueHosts, /*ValueSourceHosts*/null, ValueSuccessReplyBodyRegExes);
{
multiPinger.PingUntilOutageOrCancel
(
ValueOutageTimeBeforeGiveUpSeconds,
ValueOutageConditionNumPings,
ValuePassedPingIntervalMs,
ValueFailedPingIntervalMs,
ValuePingTimeoutMs,
/*pingSuccessCallback = */null
);
}
}
finally
{
if (multiPinger != null)
multiPinger.Dispose();
multiPinger = null;
}
//exit the program...
}
// ############################################################################
static void ExitEventHandler(bool hasShutDownStarted)
{
if (multiPinger != null)
multiPinger.SetCancelFlag();
Console.WriteLine("Exiting...");
ExitHandler.DoExit();
}
} //partial class Program
// ############################################################################
public class MultiAdvancedPinger : IDisposable
{
internal static IntPtr CurrentProcessHandle;
List<AdvancedPinger> advancedPingers = new List<AdvancedPinger>();
List<Thread> advancedPingerThreads = new List<Thread>();
volatile bool cancelFlag = false;
volatile int currentOutageHostCount = 0;
// ############################################################################
public MultiAdvancedPinger(IEnumerable<string> hosts)
{
foreach (string host in hosts)
advancedPingers.Add(new AdvancedPinger(host));
}
public MultiAdvancedPinger(IEnumerable<string> hosts, IEnumerable<string> sourceHosts)
{
using (IEnumerator<string> sourceHostEnumerator = sourceHosts.GetEnumerator())
{
foreach (string host in hosts)
{
string sourceHost;
if (sourceHostEnumerator.MoveNext())
sourceHost = sourceHostEnumerator.Current;
else
sourceHost = null;
advancedPingers.Add(new AdvancedPinger(host, sourceHost));
}
}
}
public MultiAdvancedPinger(IEnumerable<string> hosts, IEnumerable<string> sourceHosts,
IEnumerable<string> successReplyBodyRegExes)
{
using (IEnumerator<string> sourceHostEnumerator = sourceHosts != null ? sourceHosts.GetEnumerator() : null)
using (IEnumerator<string> successReplyBodyRegExEnumerator = successReplyBodyRegExes.GetEnumerator())
{
foreach (string host in hosts)
{
string sourceHost;
if (sourceHostEnumerator != null && sourceHostEnumerator.MoveNext())
sourceHost = sourceHostEnumerator.Current;
else
sourceHost = null;
string successReplyBodyRegEx;
if (successReplyBodyRegExEnumerator.MoveNext())
successReplyBodyRegEx = successReplyBodyRegExEnumerator.Current;
else
successReplyBodyRegEx = null;
advancedPingers.Add(new AdvancedPinger(host, sourceHost, successReplyBodyRegEx));
}
}
}
// ############################################################################
public void SetCancelFlag()
{
this.cancelFlag = true;
foreach (var advancedPinger in advancedPingers) //propagate cancel flag to all pingers
advancedPinger.SetCancelFlag();
}
// ############################################################################
public void Dispose()
{
if (advancedPingers != null)
{
foreach (var advancedPinger in advancedPingers)
advancedPinger.Dispose();
advancedPingers = null;
}
}
// ############################################################################
/// <summary>
///
/// </summary>
/// <param name="outageTimeBeforeGiveUpSeconds"></param>
/// <param name="outageConditionNumPings"></param>
/// <param name="passedPingIntervalMs"></param>
/// <param name="failedPingIntervalMs"></param>
/// <param name="timeoutMs"></param>
/// <param name="pingSuccessCallback">NB! pingSuccessCallback should be <b>threadsafe</b> since it can be called from multiple threads simultaneously</param>
/// <returns></returns>
public bool PingUntilOutageOrCancel(int outageTimeBeforeGiveUpSeconds, int outageConditionNumPings, int passedPingIntervalMs, int failedPingIntervalMs, int timeoutMs, Action pingSuccessCallback)
{
currentOutageHostCount = 0;
//start all parallel pinger threads
foreach (var advancedPinger1 in advancedPingers)
{
AdvancedPinger advancedPinger = advancedPinger1; //NB! need to copy so that each thread has separate pinger variable instance
Thread thread = new Thread(t =>
{
Thread.CurrentThread.Priority = ThreadPriority.Highest;
bool outageBegun = false;
do
{
bool success = advancedPinger.PingUntilOutageOrCancel
(
outageBegun,
outageTimeBeforeGiveUpSeconds,
outageConditionNumPings,
passedPingIntervalMs,
failedPingIntervalMs,
timeoutMs,
() =>
{
if (pingSuccessCallback != null)
pingSuccessCallback();
Debug.Assert(currentOutageHostCount > 0);
#pragma warning disable 0420 //warning CS0420: 'PingTool.MultiAdvancedPinger.currentOutageHostCount': a reference to a volatile field will not be treated as volatile
Interlocked.Decrement(ref currentOutageHostCount);
#pragma warning restore 0420
Debug.Assert(currentOutageHostCount >= 0);
outageBegun = false; //NB!
}
);
if (this.cancelFlag)
return;
Debug.Assert(!success);
if (!outageBegun) //NB! count each pinger's outage only once per outage begin
{
#pragma warning disable 0420 //warning CS0420: 'PingTool.MultiAdvancedPinger.currentOutageHostCount': a reference to a volatile field will not be treated as volatile
Interlocked.Increment(ref currentOutageHostCount);
#pragma warning restore 0420
outageBegun = true;
}
//check whether there is a global outage occurring
if (currentOutageHostCount == advancedPingers.Count)
{
foreach (var otherPinger in advancedPingers) //propagate cancel flag to all pingers but do not set cancel flag in current MultiPinger object. Actually this should not be necessary since all pingers should be exiting anyway
otherPinger.SetCancelFlag();
return; //now here we quit from the loop
} //if (currentOutageHostCount == advancedPingers.Count)
}
while (true); //NB! repeat the pinger even when it encountered an outage
});
thread.Start();
advancedPingerThreads.Add(thread);
} //foreach (var advancedPinger in advancedPingers)
Thread.CurrentThread.Priority = ThreadPriority.Lowest;
GC.Collect(2, GCCollectionMode.Forced); //collect now all unused startup info because later we will be relatively steady state and will not need any more much memory management
GCSettings.LatencyMode = GCLatencyMode.Batch; //most intrusive mode - most efficient //This option affects only garbage collections in generation 2; generations 0 and 1 are always non-concurrent because they finish very fast. - http://msdn.microsoft.com/en-us/library/ee787088(v=VS.110).aspx#workstation_and_server_garbage_collection
GC.WaitForFullGCComplete();
CurrentProcessHandle = Process.GetCurrentProcess().Handle;
TrimWorkingSet(CurrentProcessHandle);
//sit here until all pinger threads have exited
foreach (var thread in advancedPingerThreads)
{
thread.Join();
}
return this.cancelFlag;
}
internal static void TrimWorkingSet(IntPtr handle)
{
try
{
Program.SetProcessWorkingSetSize(handle, new IntPtr(-1), new IntPtr(-1)); //empty the working set
}
catch
{
}
try
{
Program.EmptyWorkingSet(handle); //empty the working set
}
catch
{
}
}
} //class MultiAdvancedPinger
// ############################################################################
public class AdvancedPinger : BasicPinger
{
volatile bool cancelFlag = false;
// ############################################################################
public AdvancedPinger(string host)
: base(host)
{
}
public AdvancedPinger(string host, string sourceHost)
: base(host, sourceHost)
{
}
public AdvancedPinger(string host, string sourceHost, string successReplyBodyRegEx)
: base(host, sourceHost, successReplyBodyRegEx)
{
}
// ############################################################################
public void SetCancelFlag()
{
this.cancelFlag = true;
}
// ############################################################################
/// <summary>
///
/// </summary>
/// <param name="outageTimeBeforeGiveUpSeconds"></param>
/// <param name="outageConditionNumPings"></param>
/// <param name="passedPingIntervalMs"></param>
/// <param name="failedPingIntervalMs"></param>
/// <param name="timeoutMs"></param>
/// <param name="pingSuccessCallback">NB! The callback is called only <b>once</b> after each outage end</param>
/// <returns></returns>
public bool PingUntilOutageOrCancel(bool outer_outageState, int outageTimeBeforeGiveUpSeconds, int outageConditionNumPings, int passedPingIntervalMs, int failedPingIntervalMs, int timeoutMs, Action pingSuccessCallback)
{
DateTime? outageBegin = null;
do
{
bool success = PingUntilOutageOrCancel
(
outageBegin != null,
outageConditionNumPings,
passedPingIntervalMs,
failedPingIntervalMs,
timeoutMs,
() =>
{
if (outer_outageState) //NB! propagate the success message only when the outage was started
{
if (pingSuccessCallback != null)
pingSuccessCallback();
outer_outageState = false;
}
outageBegin = null; //reset outage status
}
);
if (this.cancelFlag)
break;
Debug.Assert(!success);
DateTime now = DateTime.UtcNow;
if (outageBegin == null)
{
outageBegin = now;
}
else
{
TimeSpan outageDuration = now - outageBegin.Value;
if (outageDuration.TotalSeconds >= outageTimeBeforeGiveUpSeconds) //should we give up?
break;
}
}
while (true);
return this.cancelFlag;
}
// ############################################################################
/// <summary>
///
/// </summary>
/// <param name="outageConditionNumPings"></param>
/// <param name="passedPingIntervalMs"></param>
/// <param name="failedPingIntervalMs"></param>
/// <param name="timeoutMs"></param>
/// <param name="pingSuccessCallback">NB! The callback is called only <b>once</b> after each outage end</param>
/// <returns></returns>
public bool PingUntilOutageOrCancel(bool outer_outageState, int outageConditionNumPings, int passedPingIntervalMs, int failedPingIntervalMs, int timeoutMs, Action pingSuccessCallback)
{
this.cancelFlag = false;
int outageCount = 0;
bool success;
do
{
success = base.PingHost(timeoutMs);
if (success)
{
if (outer_outageState) //NB! propagate the success message only when the outage was started
{
if (pingSuccessCallback != null)
pingSuccessCallback();
outer_outageState = false;
}
outageCount = Math.Max(0, outageCount - 1);
if (!this.cancelFlag)
{
if (outageCount == 0) //roland 4.06.2013
{
//Thread.Sleep(passedPingIntervalMs);
int sleepStep = 1000;
for (int i = 0; i < passedPingIntervalMs; i += sleepStep)
{
if (!this.cancelFlag)
Thread.Sleep(Math.Min(sleepStep, passedPingIntervalMs - i));
}
}
else //if (outageCount == 0)
{
//Thread.Sleep(failedPingIntervalMs);
int sleepStep = 1000;
for (int i = 0; i < failedPingIntervalMs; i += sleepStep)
{
if (!this.cancelFlag)
Thread.Sleep(Math.Min(sleepStep, failedPingIntervalMs - i));
}
}
} //if (!this.cancelFlag)
}
else
{
outageCount++;
if (!this.cancelFlag
//&& outageCount < outageConditionNumPings) //sleep only when outage count not exceeded //cob roland: sleep also when outage count is exceeded since we are likely going to repeat the loop
)
{
//Thread.Sleep(failedPingIntervalMs);
int sleepStep = 1000;
for (int i = 0; i < failedPingIntervalMs; i += sleepStep)
{
if (!this.cancelFlag)
Thread.Sleep(Math.Min(sleepStep, failedPingIntervalMs - i));
}
}
}
MultiAdvancedPinger.TrimWorkingSet(MultiAdvancedPinger.CurrentProcessHandle);
}
while (outageCount < outageConditionNumPings && !this.cancelFlag);
return this.cancelFlag;
}
} //class AdvancedPinger
// ############################################################################
public class BasicPinger : IDisposable
{
Regex successResponseRegex;
bool useGetMethod = true; //TODO
string address;
IPAddress sourceAddress; //TODO
int timeoutMs;
const int defaultTimeoutMs = 5000; //.NET default ping timeout
// ############################################################################
public BasicPinger(string host)
: this(host, null, null, defaultTimeoutMs)
{
}
public BasicPinger(string host, string sourceHost)
: this(host, sourceHost, null, defaultTimeoutMs)
{
}
public BasicPinger(string host, string sourceHost, string successReplyBodyRegEx)
: this(host, sourceHost, successReplyBodyRegEx, defaultTimeoutMs)
{
}
public BasicPinger(string host, int timeoutMs_in)
: this(host, null, null, timeoutMs_in)
{
}
public BasicPinger(string host, string sourceHost, int timeoutMs_in)
: this(host, sourceHost, null, timeoutMs_in)
{
}
public BasicPinger(string host, string sourceHost, string successReplyBodyRegEx, int timeoutMs_in)
{
//IPAddress instance for holding the returned host
this.address = host; //GetIpFromHost(host);
this.sourceAddress = GetIpFromHost(sourceHost);
//this.rand = new Random(unchecked((int)DateTime.UtcNow.Ticks));
this.timeoutMs = timeoutMs_in;
if (successReplyBodyRegEx != null)
{
this.successResponseRegex = new Regex
(
successReplyBodyRegEx, //auto-lock the regex endpoints
RegexOptions.Compiled
//| RegexOptions.CultureInvariant
| RegexOptions.ExplicitCapture
//| RegexOptions.Multiline
| RegexOptions.Singleline
);
} //if (successReplyBodyRegEx != null)
string destStr;
if (address.ToString() != host)
destStr = string.Format("{0} [{1}]", host, address);
else
destStr = host;
string sourceStr;
if (sourceHost != null && sourceAddress.ToString() != sourceHost)
sourceStr = string.Format("{0} [{1}]", sourceHost, sourceAddress);
else
sourceStr = sourceHost;
if (sourceStr != null)
Console.WriteLine("Requesting {0} from {1}:", destStr, sourceStr);
else
Console.WriteLine("Requesting {0}:", destStr);
} //public BasicPinger(string host, string sourceHost, int timeoutMs_in)
// ############################################################################
public void Dispose()
{
}
// ############################################################################
public bool PingHost()
{
string returnMessage;
bool success = PingHost(out returnMessage);
Console.WriteLine("request {0}: {1}", this.address, returnMessage);
return success;
}
public bool PingHost(int timeoutMs_in)
{
string returnMessage;
bool success = PingHost(out returnMessage, timeoutMs_in);
Console.WriteLine("request {0}: {1}", this.address, returnMessage);
return success;
}
public bool PingHost(out string returnMessage)
{
return PingHost(out returnMessage, this.timeoutMs);
}
public bool PingHost(out string returnMessage, int timeoutMs_in)
{
//first make sure we actually have an internet connection
if (HasConnection())
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(/*m_strWebAddress*/address);
//request.Proxy = null; //no proxy //HttpWebRequest will actually use the IE proxy settings by default. - http://stackoverflow.com/questions/1155363/how-to-autodetect-use-ie-proxy-settings-in-net-httpwebrequest
//by default it's set to WebRequest.DefaultWebProxy
IWebProxy proxy = request.Proxy;
if (proxy != null)
{
string proxyuri = proxy.GetProxy(request.RequestUri).ToString();
request.UseDefaultCredentials = true;
request.Proxy = new WebProxy(proxyuri, false);
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
}
request.UserAgent = "HttpPingTool";
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.Credentials = CredentialCache.DefaultCredentials; //TODO
request.Method = useGetMethod ? "GET" : "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = 0; //
request.KeepAlive = false;
request.Timeout = timeoutMs_in;
request.ReadWriteTimeout = timeoutMs_in;
using (Stream dataStream = !useGetMethod ? request.GetRequestStream() : null)
{
if (!useGetMethod)
{
//dataStream.Write(compressedBytes, 0, numCompressedBytes); //compressedBytes.Length);
dataStream.Flush(); //necessary to prevent occasional exceptions
dataStream.Close(); //necessary to prevent occasional exceptions
}
using (HttpWebResponse webresponse = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = webresponse.GetResponseStream())
{
Encoding encoding2 = webresponse.CharacterSet != null ? Encoding.GetEncoding(webresponse.CharacterSet) : Encoding.ASCII;
using (StreamReader loResponseStream = new StreamReader(responseStream, encoding2))
{
string Response = loResponseStream.ReadToEnd();
loResponseStream.Close();
if (successResponseRegex != null)
{
bool isMatch = successResponseRegex.IsMatch(Response);
if (!isMatch)
{
returnMessage = string.Format("FAILURE: Reply: {0}", Response);
return false;
}
}
returnMessage = string.Format("SUCCESS: Reply: {0}", Response);
return true;
} //using (StreamReader loResponseStream = new StreamReader(responseStream, encoding2))
} //using (Stream responseStream = webresponse.GetResponseStream())
} //using (HttpWebResponse webresponse = (HttpWebResponse)request.GetResponse())
} //using (Stream dataStream = request.GetRequestStream())
}
catch (Exception ex)
{
returnMessage = string.Format("FAILURE: Error: {0}", ex.Message);
}
}
else //if (HasConnection())
{
returnMessage = "FAILURE: transmit failed. General failure. No Internet connection found...";
}
return false;
} //public bool PingHost(out string returnMessage)
// ############################################################################
/// <summary>
/// method for retrieving the IP address from the host provided
/// </summary>
/// <param name="host">the host we need the address for</param>
/// <returns></returns>
[SuppressUnmanagedCodeSecurity] //SuppressUnmanagedCodeSecurity - For methods in this particular class, execution time is often critical. Security can be traded for additional speed by applying the SuppressUnmanagedCodeSecurity attribute to the method declaration. This will prevent the runtime from doing a security stack walk at runtime. - MSDN: Generally, whenever managed code calls into unmanaged code (by PInvoke or COM interop into native code), there is a demand for the UnmanagedCode permission to ensure all callers have the necessary permission to allow this. By applying this explicit attribute, developers can suppress the demand at run time. The developer must take responsibility for assuring that the transition into unmanaged code is sufficiently protected by other means. The demand for the UnmanagedCode permission will still occur at link time. For example, if function A calls function B and function B is marked with SuppressUnmanagedCodeSecurityAttribute, function A will be checked for unmanaged code permission during just-in-time compilation, but not subsequently during run time.
private static IPAddress GetIpFromHost(string host)
{
if (host == null)
return null;
//variable to hold our error message (if something fails)
//string errMessage = string.Empty;
//IPAddress instance for holding the returned host
IPAddress address = null;
//wrap the attempt in a try..catch to capture
//any exceptions that may occur
try
{
//get the host IP from the name provided
if (!IPAddress.TryParse(host, out address)) //first try to parse as IP string if this fails only then try DNS name resolving
{
address = Dns.GetHostEntry(host).AddressList[0];
}
}
catch (SocketException ex)
{
//some DNS error happened, return the message
string errMessage = string.Format("host: {0} DNS Error: {1}", host, ex.Message);
Console.WriteLine(errMessage);
ExitHandler.DoExit(); //NB!
//address = null;
}
return address;
}
// ############################################################################
/// <summary>
/// enum to hold the possible connection states
/// </summary>
[Flags]
enum ConnectionStatusEnum : int
{
INTERNET_CONNECTION_MODEM = 0x1,
INTERNET_CONNECTION_LAN = 0x2,
INTERNET_CONNECTION_PROXY = 0x4,
INTERNET_RAS_INSTALLED = 0x10,
INTERNET_CONNECTION_OFFLINE = 0x20,
INTERNET_CONNECTION_CONFIGURED = 0x40
}
[DllImport("wininet", CharSet = CharSet.Auto)]
static extern bool InternetGetConnectedState(ref ConnectionStatusEnum flags, int dw);
/// <summary>
/// method to check the status of the pinging machines internet connection
/// </summary>
/// <returns></returns>
private static bool HasConnection()
{
//instance of our ConnectionStatusEnum
ConnectionStatusEnum state = 0;
//call the API
InternetGetConnectedState(ref state, 0);
//check the status, if not offline and the returned state
//isnt 0 then we have a connection
if (((int)ConnectionStatusEnum.INTERNET_CONNECTION_OFFLINE & (int)state) != 0)
{
//return true, we have a connection
return false;
}
//return false, no connection available
return true;
}
// ############################################################################
} //class BasicPinger
}