1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using System;
- using System.Collections.Generic;
- using System.Net;
- using System.Net.NetworkInformation;
- using System.Threading.Tasks;
- using SiteStatus.Dbo;
-
- namespace SiteStatus.Business
- {
- public class HostBusiness
- {
- public async Task<HostStatusDbo> GetHostStatusAsync(string host, int count, int timeout)
- {
- var hostStatus = new HostStatusDbo
- {
- Ip = host,
- Times = new List<long?>()
- };
-
- var tasks = new List<Task<PingReply>>();
-
- for (var i = 0; i < count; ++i)
- {
- try
- {
- var ping = new Ping();
- var pingTask = ping.SendPingAsync(host, timeout);
- tasks.Add(pingTask);
- }
- catch (Exception e)
- {
- hostStatus.Times.Add(null);
- }
- }
-
- foreach (var pingResult in await Task.WhenAll(tasks))
- {
- if (pingResult != null && pingResult.Status == IPStatus.Success)
- {
- hostStatus.Times.Add(pingResult.RoundtripTime);
- }
- else
- {
- hostStatus.Times.Add(null);
- }
- }
-
- try
- {
- var dnsResult = await Dns.GetHostEntryAsync(host);
- if (dnsResult != null)
- {
- hostStatus.Hostname = dnsResult.HostName;
- }
- else
- {
- hostStatus.Hostname = null;
- }
- }
- catch (Exception e)
- {
- hostStatus.Hostname = null;
- }
-
- return hostStatus;
- }
- }
- }
|