示例#1
0
        private bool WaitForConnection()
        {
            lock (connectionLock)
            {
                if (connection == null)
                {
                    connection = ConnectionFactory.Create(socketAddress);
                    if (apiProxy != null)
                    {
                        connection.SetProxy(apiProxy.Host, apiProxy.Port);
                    }

                    proxy = connection.CreateHubProxy(HubName);

                    connection.Closed         += SocketClosed;
                    connection.Error          += SocketError;
                    connection.ConnectionSlow += SocketSlow;
                    connection.StateChanged   += SocketStateChange;
                    connection.UserAgent       = UserAgent;

                    proxy.Subscribe(MarketDeltaEvent).Received   += SocketMessageMarketDeltas;
                    proxy.Subscribe(ExchangeStateEvent).Received += SocketMessageExchangeState;
                }

                // If failed, try to get CloudFlare bypass
                log.Write(LogVerbosity.Warning, "Getting CloudFlare cookies");
                var sw = Stopwatch.StartNew();
                var cookieContainer = CloudFlareAuthenticator.GetCloudFlareCookies(cloudFlareAuthenticationAddress, UserAgent, cloudFlareRetries).ConfigureAwait(false).GetAwaiter().GetResult();
                sw.Stop();

                log.Write(LogVerbosity.Debug, $"CloudFlare cookie retrieving done in {sw.ElapsedMilliseconds}ms");
                if (cookieContainer == null)
                {
                    log.Write(LogVerbosity.Error, "CloudFlareAuthenticator didn't give us the cookies, trying to start without");
                }
                else
                {
                    connection.Cookies = cookieContainer;
                }

                // Try connecting
                return(TryStart().ConfigureAwait(false).GetAwaiter().GetResult());
            }
        }
        private bool WaitForConnection()
        {
            lock (connectionLock)
            {
                if (connection == null)
                {
                    connection = ConnectionFactory.Create(log, baseAddress);
                    if (apiProxy != null)
                    {
                        connection.SetProxy(apiProxy.Host, apiProxy.Port);
                    }

                    proxy = connection.CreateHubProxy(HubName);

                    proxy.On(MarketEvent, (data) => SocketMessageExchangeState(data));
                    proxy.On(SummaryEvent, (data) => SocketMessageMarketSummaries(data));
                    proxy.On(SummaryLiteEvent, (data) => SocketMessageMarketSummariesLite(data));
                    proxy.On(BalanceEvent, (data) => SocketMessageBalance(data));
                    proxy.On(OrderEvent, (data) => SocketMessageOrder(data));

                    connection.Closed         += SocketClosed;
                    connection.Error          += SocketError;
                    connection.ConnectionSlow += SocketSlow;
                    connection.StateChanged   += SocketStateChange;
                }

                // Try connecting
                var connectResult = TryStart().ConfigureAwait(false).GetAwaiter().GetResult();

                if (!connectResult)
                {
                    return(connectResult);
                }

                // Resubscribe the subscriptions
                List <BittrexRegistration> registrationsCopy;
                lock (registrationLock)
                    registrationsCopy = registrations.ToList();

                if (registrationsCopy.Count > 0)
                {
                    log.Write(LogVerbosity.Info, $"Resubscribing {registrationsCopy.Count} subscriptions");
                }

                bool failedResubscribe = false;
                foreach (var registration in registrationsCopy)
                {
                    if (registration is BittrexMarketSummariesRegistration)
                    {
                        var resubSuccess = InvokeProxy <bool>(SummaryDeltaSub).ConfigureAwait(false).GetAwaiter().GetResult();
                        if (!resubSuccess.Success)
                        {
                            log.Write(LogVerbosity.Warning, "Failed to resubscribe summary delta: " + resubSuccess.Error);
                            failedResubscribe = true;
                            break;
                        }
                    }
                    else if (registration is BittrexExchangeStateRegistration)
                    {
                        var resubSuccess = InvokeProxy <bool>(ExchangeDeltaSub, ((BittrexExchangeStateRegistration)registration).MarketName).ConfigureAwait(false).GetAwaiter().GetResult();
                        if (!resubSuccess.Success)
                        {
                            log.Write(LogVerbosity.Warning, "Failed to resubscribe exchange delta: " + resubSuccess.Error);
                            failedResubscribe = true;
                            break;
                        }
                    }
                    else if (registration is BittrexMarketSummariesLiteRegistration)
                    {
                        var resubSuccess = InvokeProxy <bool>(SummaryLiteDeltaSub).ConfigureAwait(false).GetAwaiter().GetResult();
                        if (!resubSuccess.Success)
                        {
                            log.Write(LogVerbosity.Warning, "Failed to resubscribe summary lite delta: " + resubSuccess.Error);
                            failedResubscribe = true;
                            break;
                        }
                    }
                    else if (registration is BittrexBalanceUpdateRegistration || registration is BittrexOrderUpdateRegistration)
                    {
                        if (!authenticated)
                        {
                            var authResult = Authenticate().ConfigureAwait(false).GetAwaiter().GetResult();
                            if (!authResult.Success)
                            {
                                log.Write(LogVerbosity.Warning, "Failed to re-authenticate: " + authResult.Error);
                                failedResubscribe = true;
                                break;
                            }
                        }
                    }
                }

                if (failedResubscribe)
                {
                    log.Write(LogVerbosity.Warning, "Failed to resubscribe all running subscriptions -> Reconnect and try again");
                    connection.Stop(TimeSpan.FromSeconds(1));
                    return(false);
                }

                return(connectResult);
            }
        }