コード例 #1
0
        protected async Task <SessionStatus> CheckStatus(int timeoutMs = ApiServices.DefaultTimeout)
        {
            try
            {
                // Cancel previous request (if it still in progress)
                // PC connections can be changed on a current moment. Should be used new connection for request
                // Info: WebRequest can use not more than 2 simultaneous connections to same host
                __RequestCancellationSource?.Cancel();

                if (!__Credentials.IsUserLoggedIn())
                {
                    return(null); // User not loged-in () no registered sesion - nonthing to check
                }
                // send request to rest server
                try
                {
                    __RequestCancellationSource = new CancellationTokenSource();
                    SessionStatus acc = await ApiServices.Instance.SessionStatusAsync(__RequestCancellationSource.Token, timeoutMs);

                    if (acc != null)
                    {
                        __IsStatusReceived = true;
                    }

                    return(acc);
                }
                finally
                {
                    __LastCheckTime             = DateTime.Now;
                    __RequestCancellationSource = null;
                }
            }
            catch (IVPNRestRequestApiException ex)
            {
                Logging.Info($"{ex}");
                if (ex.ApiStatusCode == ApiStatusCode.SessionNotFound)
                {
                    OnSessionRequestError(ex);
                }
                throw;
            }
            catch (Exception ex)
            {
                Logging.Info($"Error checking account status: {ex}");
                throw;
            }
        }
コード例 #2
0
        private async Task StatusTimerElapsed()
        {
            __TimerAccountCheck.Stop();
            if (!__IsActive)
            {
                return;
            }

            SessionStatus retSessionStatus = null;

            try
            {
                // request not often than once per 'MinCheckIntervalMs'
                if (__IsStatusReceived && (DateTime.Now - __LastCheckTime).TotalMilliseconds < MinCheckIntervalMs)
                {
                    return;
                }

                // request acount status
                try
                {
                    var account = await CheckStatus();

                    if (account != null)
                    {
                        retSessionStatus = account;
                    }
                }
                catch
                {
                    retSessionStatus = null;
                    // IGNORE ALLL !!!
                }

                if (!__IsActive)
                {
                    return;
                }

                if (retSessionStatus != null)
                {
                    OnSessionStatusReceived(retSessionStatus);
                }
            }
            catch (Exception ex)
            {
                Logging.Info($"{ex}");
            }
            finally
            {
                __TimerAccountCheck.Interval = DefaultCheckIntervalMs;

                if (__IsActive)
                {
                    // calculate next account check time based on 'ActiveUtil'
                    if (retSessionStatus != null)
                    {
                        double accountAliveMsLeft = (retSessionStatus.ActiveUtil - DateTime.Now).TotalMilliseconds;
                        if (accountAliveMsLeft > 0)
                        {
                            if (accountAliveMsLeft < DefaultCheckIntervalMs)
                            {
                                __TimerAccountCheck.Interval = accountAliveMsLeft;
                            }
                        }
                    }

                    __TimerAccountCheck.Start();
                }
            }
        }