Beispiel #1
0
        private async Task WaitInQueue()
        {
            int id      = -1;
            int current = -1;

            using (IEnumerator <Ticker> enumerator = this.AuthResult.Tickers.Where <Ticker>((Func <Ticker, bool>)(ticker => ticker.Node == this.AuthResult.Node)).GetEnumerator())
            {
                if (enumerator.MoveNext())
                {
                    Ticker current1 = enumerator.Current;
                    id      = current1.Id;
                    current = current1.Current;
                }
            }
            int    cycleNum = 1;
            double averageMillisPerCycle = 0.0;

            while (id - current > 0)
            {
                DateTime            cycleStartTime      = DateTime.Now;
                HttpResponseMessage httpResponseMessage = await this._httpClient.GetAsync(this._tickerUrl + this.AuthResult.Champ).ConfigureAwait(false);

                if (httpResponseMessage.StatusCode == HttpStatusCode.OK)
                {
                    try
                    {
                        current = int.Parse((string)JsonConvert.DeserializeObject <Dictionary <string, object> >(await httpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false))[this.AuthResult.Node.ToString()], NumberStyles.HexNumber);
                        int num = Math.Max(0, id - current);
                        if (num == 0)
                        {
                            break;
                        }
                        // ISSUE: reference to a compiler-generated field
                        LoginQueue.OnLoginQueueUpdateHandler loginQueueUpdate = this.OnLoginQueueUpdate;
                        if (loginQueueUpdate != null)
                        {
                            int positionInLine = num;
                            loginQueueUpdate(positionInLine);
                        }
                        // ISSUE: reference to a compiler-generated field
                        LoginQueue.StatusMessageUpdateHandler updateStatusMessage = this.OnUpdateStatusMessage;
                        if (updateStatusMessage != null)
                        {
                            string e = string.Format("In login queue at position {0}", (object)num);
                            updateStatusMessage((object)this, e);
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    await Task.Delay(this.AuthResult.Delay).ConfigureAwait(false);
                }
                double totalMilliseconds = (DateTime.Now - cycleStartTime).TotalMilliseconds;
                averageMillisPerCycle = averageMillisPerCycle > 0.0 ? (averageMillisPerCycle + totalMilliseconds) / 2.0 : totalMilliseconds;
                if (cycleNum == 2)
                {
                    double num = averageMillisPerCycle / (double)(1 / this.AuthResult.Rate * 60);
                    if ((int)(averageMillisPerCycle * (num / (1.0 - num)) * ((1.0 + Math.Pow(1.333, 2.0)) / 2.0) / 1000.0) > this.TimeoutSeconds)
                    {
                        throw new TimeoutException();
                    }
                }
                ++cycleNum;
            }
        }
Beispiel #2
0
        public async Task <bool> GetAuthToken()
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            int retriesRemaining = this.AuthRetries;

            try
            {
                while (sw.ElapsedMilliseconds / 1000L <= (long)this.TimeoutSeconds)
                {
                    int num = 0;
                    try
                    {
                        this.AuthResult = await this.GetAuthResult().ConfigureAwait(false);

                        this.User  = this.AuthResult.User;
                        this.Token = AuthResult.Lqt.PartnerToken;
                        //await GetToken();
                        //this.UserId = this.AuthResult.Lqt.AccountId;
                    }
                    catch (IpBannedException ex)
                    {
                        // ISSUE: reference to a compiler-generated field
                        LoginQueue.OnAuthFailedHandler onAuthFailed = this.OnAuthFailed;
                        if (onAuthFailed != null)
                        {
                            string message = "Your IP has been banned due to too many requests.";
                            onAuthFailed(message);
                        }
                        return(false);
                    }
                    catch (JsonReaderException ex)
                    {
                        num = 1;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                    }
                    if (num == 1)
                    {
                        await Task.Delay(1000).ConfigureAwait(false);
                    }
                    else if (this.AuthResult.Reason == null)
                    {
                        if (retriesRemaining > 0)
                        {
                            --retriesRemaining;
                        }
                        else
                        {
                            OnAuthFailed?.Invoke("Unable to get Auth Token.");
                            return(false);
                        }
                    }
                    else
                    {
                        string reason = this.AuthResult.Reason;
                        if (!(reason == "attempt_rate_too_fast"))
                        {
                            if (!(reason == "invalid_credentials"))
                            {
                                if (!(reason == "account_banned"))
                                {
                                    if (!(reason == "account_transferred"))
                                    {
                                        if (!(reason == "account_inactive"))
                                        {
                                            if (reason == "server_full")
                                            {
                                                OnAuthFailed?.Invoke("Server is full. Try again later.");
                                                return(false);
                                            }
                                            if (this.AuthResult.Status == "QUEUE" && this.AuthResult.Tickers != null)
                                            {
                                                await this.WaitInQueue().ConfigureAwait(false);

                                                this.AuthResult.Lqt.Resources = (string)null;
                                                this.AuthResult.Lqt.Other     = (string)null;
                                                HttpResponseMessage httpResponseMessage = await this._httpClient.PostAsync(this._tokenUrl, this.AuthResult.Lqt.ToString(), true).ConfigureAwait(false);

                                                if (httpResponseMessage.StatusCode == HttpStatusCode.OK)
                                                {
                                                    this.AuthToken = JsonConvert.DeserializeObject <AuthResult>(await httpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false)).Lqt.ToString();
                                                    return(true);
                                                }
                                            }
                                            if (this.AuthResult.Status == "LOGIN")
                                            {
                                                this.AuthToken = this.AuthResult.Lqt.ToString();
                                                return(true);
                                            }
                                            await Task.Delay(1000).ConfigureAwait(false);
                                        }
                                        else
                                        {
                                            // ISSUE: reference to a compiler-generated field
                                            LoginQueue.OnAuthFailedHandler onAuthFailed = this.OnAuthFailed;
                                            if (onAuthFailed != null)
                                            {
                                                string message = "Account currently inactive.";
                                                onAuthFailed(message);
                                            }
                                            return(false);
                                        }
                                    }
                                    else
                                    {
                                        // ISSUE: reference to a compiler-generated field
                                        LoginQueue.OnAuthFailedHandler onAuthFailed = this.OnAuthFailed;
                                        if (onAuthFailed != null)
                                        {
                                            string message = string.Format("Account transferred {0}", (object)this.AuthResult.Destination).Trim();
                                            onAuthFailed(message);
                                        }
                                        return(false);
                                    }
                                }
                                else
                                {
                                    DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddMilliseconds(this.AuthResult.Banned);
                                    // ISSUE: reference to a compiler-generated field
                                    LoginQueue.OnAuthFailedHandler onAuthFailed = this.OnAuthFailed;
                                    if (onAuthFailed != null)
                                    {
                                        string message = string.Format("Account banned {0}", (object)dateTime.ToString("d", (IFormatProvider)CultureInfo.CurrentCulture));
                                        onAuthFailed(message);
                                    }
                                    return(false);
                                }
                            }
                            else
                            {
                                // ISSUE: reference to a compiler-generated field
                                LoginQueue.OnAuthFailedHandler onAuthFailed = this.OnAuthFailed;
                                if (onAuthFailed != null)
                                {
                                    string message = "Incorrect username or password.";
                                    onAuthFailed(message);
                                }
                                return(false);
                            }
                        }
                        else
                        {
                            // ISSUE: reference to a compiler-generated field
                            LoginQueue.StatusMessageUpdateHandler updateStatusMessage = this.OnUpdateStatusMessage;
                            if (updateStatusMessage != null)
                            {
                                string e = string.Format("Login rate too fast. Waiting {0} s.", (object)this.AuthResult.RetryWait);
                                updateStatusMessage((object)this, e);
                            }
                            await Task.Delay(TimeSpan.FromSeconds((double)this.AuthResult.RetryWait)).ConfigureAwait(false);

                            sw.Restart();
                        }
                    }
                }
                sw.Stop();
                throw new TimeoutException();
            }
            catch (TimeoutException ex)
            {
                // ISSUE: reference to a compiler-generated field
                LoginQueue.OnAuthFailedHandler onAuthFailed = this.OnAuthFailed;
                if (onAuthFailed != null)
                {
                    string message = "Timeout: queue time too long.";
                    onAuthFailed(message);
                }
            }
            finally
            {
                sw.Stop();
            }
            return(false);
        }