Esempio n. 1
0
        public static ValueTask DisposeAsync(this ILocker locker)
        {
            if (locker is IAsyncDisposable asyncDisposable)
            {
                return(asyncDisposable.DisposeAsync());
            }

            locker.Dispose();

            return(default);
Esempio n. 2
0
        /// <summary>
        /// 通过网络中心执行操作,避免网络中断导致异常
        /// </summary>
        /// <param name="func"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T Execute <T>(Func <T> func)
        {
            if (!SupportAdsl)
            {
                return(func());
            }

            ILocker locker       = null;
            ILocker redialLocker = null;

            try
            {
                redialLocker = _lockerFactory.GetLocker(RedialLocker);
                locker       = _lockerFactory.GetLocker();
                redialLocker.Dispose();
                return(func());
            }
            finally
            {
                redialLocker?.Dispose();
                locker?.Dispose();
            }
        }
Esempio n. 3
0
 /// <summary>
 /// 通过网络中心执行操作,避免网络中断导致异常
 /// </summary>
 /// <param name="action"></param>
 public void Execute(Action action)
 {
     if (!SupportAdsl)
     {
         action();
     }
     else
     {
         ILocker locker       = null;
         ILocker redialLocker = null;
         try
         {
             redialLocker = _lockerFactory.GetLocker(RedialLocker);
             locker       = _lockerFactory.GetLocker();
             redialLocker.Dispose();
             action();
         }
         finally
         {
             redialLocker?.Dispose();
             locker?.Dispose();
         }
     }
 }
Esempio n. 4
0
        /// <summary>
        /// 进行拨号
        /// </summary>
        /// <exception cref="SpiderException"></exception>
        public void Redial()
        {
            if (!_options.SupportAdsl)
            {
                throw new SpiderException("下载代理器不支持 ADSL 拨号");
            }

            ILocker locker = null;

            try
            {
                locker = _lockerFactory.GetLocker(RedialLocker);

                var interval = double.MaxValue;
                if (DateTime.TryParse(locker.Information, out DateTime latestRedialTime))
                {
                    interval = (DateTime.Now - latestRedialTime).TotalSeconds;
                }

                if (interval < _options.RedialIntervalLimit * 60)
                {
                    _logger.LogInformation($"在间隔时间内 {_options.RedialIntervalLimit} 已经拨号");
                }
                else
                {
                    WaitForAllSessionsExit();

                    bool success = false;
                    for (int i = 0; i < 10; ++i)
                    {
                        try
                        {
                            if (!_options.IgnoreRedialForTest)
                            {
                                _logger.LogInformation($"尝试拨号 [{i}]");
                                _redialer.Redial();
                            }

                            if (_options.IgnoreRedialForTest || _internetDetector.Detect())
                            {
                                _logger.LogInformation("拨号成功");
                                success = true;
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.LogInformation($"拨号失败 [{i}]: {ex}");
                        }
                    }

                    if (!success)
                    {
                        throw new SpiderException("拨号失败");
                    }
                }
            }
            finally
            {
                locker?.Dispose();
            }
        }