private bool WaitForConnection()
        {
            lock (connectionLock)
            {
                if (connection == null)
                {
                    if (this.ProxyHost != "" && this.ProxyPort != 0)
                    {
                        connection = ConnectionFactory.Create(SocketAddress, this.ProxyHost, this.ProxyPort);
                    }
                    else
                    {
                        connection = ConnectionFactory.Create(SocketAddress);
                    }


                    proxy = connection.CreateHubProxy(HubName);

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

                    Subscription sub = proxy.Subscribe(UpdateEvent);
                    sub.Received += SocketMessage;

                    // regular updates
                    Subscription subExchangeState = proxy.Subscribe("updateExchangeState");
                    subExchangeState.Received += SocketMessageExchangeState;
                }

                // Try to start
                if (TryStart())
                {
                    return(true);
                }

                // If failed, try to get CloudFlare bypass
                log.Write(LogVerbosity.Warning, "Couldn't connect to Bittrex server, going to try CloudFlare bypass");
                var cookieContainer = CloudFlareAuthenticator.GetCloudFlareCookies(BaseAddress, GetUserAgentString(), CloudFlareRetries);
                if (cookieContainer == null)
                {
                    log.Write(LogVerbosity.Error, $"CloudFlareAuthenticator didn't gave us the cookies");
                    return(false);
                }

                connection.Cookies   = cookieContainer;
                connection.UserAgent = GetUserAgentString();
                log.Write(LogVerbosity.Debug, "CloudFlare cookies retrieved, retrying connection");

                // Try again with cookies
                return(TryStart());
            }
        }
Example #2
0
        private bool WaitForConnection()
        {
            lock (connectionLock)
            {
                if (connection == null)
                {
                    connection = ConnectionFactory.Create(SocketAddress);
                    var proxy = connection.CreateHubProxy(HubName);

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

                    Subscription sub = proxy.Subscribe(UpdateEvent);
                    sub.Received += jsonData =>
                    {
                        if (jsonData.Count == 0 || jsonData[0] == null)
                        {
                            return;
                        }

                        try
                        {
                            var data = JsonConvert.DeserializeObject <BittrexStreamDeltas>(jsonData[0].ToString());
                            foreach (var delta in data.Deltas)
                            {
                                foreach (var update in registrations.Where(r => r.MarketName == delta.MarketName))
                                {
                                    update.Callback(delta);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            log.Write(LogVerbosity.Warning, $"Received an event but an unknown error occured. Message: {e.Message}, Received data: {jsonData[0]}");
                        }
                    };
                }

                // Try to start
                if (TryStart())
                {
                    return(true);
                }

                // If failed, try to get CloudFlare bypass
                log.Write(LogVerbosity.Warning, "Couldn't connect to Bittrex server, going to try CloudFlare bypass");
                var cookieContainer = CloudFlareAuthenticator.GetCloudFlareCookies(BaseAddress, GetUserAgentString(), CloudFlareRetries);
                if (cookieContainer == null)
                {
                    return(false);
                }

                connection.Cookies = cookieContainer;
                log.Write(LogVerbosity.Debug, "CloudFlare cookies retrieved, retrying connection");

                // Try again with cookies
                return(TryStart());
            }
        }