示例#1
0
        public async Task <PingReply> PingAsync(DateTime pingDate, PingEntity pingEntity, ILogger logger)
        {
            try
            {
                using (var httpClient = new HttpClient())
                {
                    var reply = new HttpRequestMessage
                    {
                        RequestUri = new UriBuilder(pingEntity.ConfigEntity.Host).Uri
                    };

                    var response = await httpClient.SendAsync(reply);

                    if (response.StatusCode == (HttpStatusCode)pingEntity.ConfigEntity.ValidStatusCode)
                    {
                        return(new PingReply(pingDate, pingEntity, IPStatus.Success));
                    }

                    return(new PingReply(pingDate, pingEntity, IPStatus.BadOption));
                }
            }
            catch (Exception e)
            {
                logger.Log(new LogParams(MessageType.Warning, e.Message));
                return(null);
            }
        }
示例#2
0
        private void BuildPing(ConfigEntity configEntity)
        {
            var pingEntity = new PingEntity {
                ConfigEntity = configEntity, ProtocolProvider = _protocolProviderManager.GetProvider(configEntity)
            };

            TimerCallback tm     = Ping;
            var           period = (int)TimeSpan.FromSeconds(pingEntity.ConfigEntity.Period).TotalMilliseconds;

            _timers.Add(new Timer(tm, pingEntity, 0, period));
        }
示例#3
0
        public async Task <PingReply> PingAsync(DateTime pingDate, PingEntity pingEntity, ILogger logger)
        {
            try
            {
                using (var tcpClient = new TcpClient())
                {
                    var host = new UriBuilder(pingEntity.ConfigEntity.Host).Host;
                    var port = pingEntity.ConfigEntity.Port;
                    await Task.WhenAny(tcpClient.ConnectAsync(host, port), Task.Delay(2000));

                    return(!tcpClient.Connected ? new PingReply(pingDate, pingEntity, IPStatus.BadOption) : new PingReply(pingDate, pingEntity, IPStatus.Success));
                }
            }
            catch (Exception e)
            {
                logger.Log(new LogParams(MessageType.Warning, e.Message));
                return(null);
            }
        }
示例#4
0
        public async Task <PingReply> PingAsync(DateTime pingDate, PingEntity pingEntity, ILogger logger)
        {
            try
            {
                using (var ping = new Ping())
                {
                    var host    = new UriBuilder(pingEntity.ConfigEntity.Host).Host;
                    var timeout = (int)TimeSpan.FromSeconds(2).TotalMilliseconds;
                    var reply   = await ping.SendPingAsync(host, timeout);

                    return(new PingReply(pingDate, pingEntity, reply.Status));
                }
            }
            catch (Exception e)
            {
                logger.Log(new LogParams(MessageType.Warning, e.Message));
                return(null);
            }
        }
示例#5
0
 public PingReply(DateTime pingDate, PingEntity pingEntity, IPStatus status)
 {
     PingDate   = pingDate;
     PingEntity = pingEntity;
     Status     = status;
 }