예제 #1
0
        private void DoWork(double pollinterval)
        {
            ConcurrentDictionary <string, bool> bag;

            if (!_marketPollInterval.TryGetValue(pollinterval, out bag))
            {
                return;
            }

            var book = _client.ListMarketBook(bag.Keys, _priceProjection).Result;

            if (book.HasError)
            {
                foreach (var observer in _observers.Where(k => bag.Keys.Contains(k.Key)))
                {
                    observer.Value.OnError(book.Error);
                }
                return;
            }

            // we may have fresher data than the response to this pollinterval request
            Poller p;

            if (!_polling.TryGetValue(pollinterval, out p))
            {
                return;
            }

            if (book.RequestStart < p.LatestDataRequestStart && book.LastByte > p.LatestDataRequestFinish)
            {
                return;
            }

            lock (_lockObj)
            {
                p.LatestDataRequestStart  = book.RequestStart;
                p.LatestDataRequestFinish = book.LastByte;
            }

            foreach (var market in book.Response)
            {
                IObserver <MarketBook> o;
                if (!_observers.TryGetValue(market.MarketId, out o))
                {
                    continue;
                }

                // check to see if the market is finished
                if (market.Status == MarketStatus.CLOSED ||
                    market.Status == MarketStatus.INACTIVE)
                {
                    o.OnCompleted();
                }
                else
                {
                    o.OnNext(market);
                }
            }
        }
예제 #2
0
        protected override void DoWork(double pollinterval)
        {
            ConcurrentDictionary <string, bool> bag;

            if (!PollIntervals.TryGetValue(pollinterval, out bag))
            {
                return;
            }

            BetfairServerResponse <List <MarketBook> > book;

            try
            {
                book = _client.ListMarketBook(bag.Keys, _priceProjection, _orderProjection, _matchProjection).Result;
            }
            catch (AggregateException ex)
            {
                foreach (var e in ex.Flatten().InnerExceptions)
                {
                    _logger.Invoke(e, $"pollInterval {pollinterval}");
                }

                return;
            }

            if (book.HasError)
            {
                foreach (var observer in Observers.Where(k => bag.Keys.Contains(k.Key)))
                {
                    observer.Value.OnError(book.Error);
                }
                return;
            }

            // we may have fresher data than the response to this pollinterval request
            Poller p;

            if (!Pollers.TryGetValue(pollinterval, out p))
            {
                return;
            }

            if (book.RequestStart < p.LatestDataRequestStart && book.LastByte > p.LatestDataRequestFinish)
            {
                return;
            }

            lock (LockObj)
            {
                p.LatestDataRequestStart  = book.RequestStart;
                p.LatestDataRequestFinish = book.LastByte;
            }

            UpdateObservers(book.Response);
        }
예제 #3
0
 public BetfairServerResponse <List <MarketBook> > ListMarketBook(
     IEnumerable <string> marketIds,
     PriceProjection priceProjection = null,
     OrderProjection?orderProjection = null,
     MatchProjection?matchProjection = null)
 {
     return(client.ListMarketBook(
                marketIds,
                priceProjection,
                orderProjection,
                matchProjection).Result);
 }
예제 #4
0
        private void DoWork()
        {
            var book = _client.ListMarketBook(_markets.Keys.ToList(), this._priceProjection).Result;

            if (book.HasError)
            {
                foreach (var observer in _observers)
                {
                    observer.Value.OnError(book.Error);
                }
                return;
            }

            // we may have fresher data than the response to this request
            if (book.RequestStart < _latestDataRequestStart && book.LastByte > _latestDataRequestFinish)
            {
                return;
            }

            lock (_lockObj)
            {
                _latestDataRequestStart  = book.RequestStart;
                _latestDataRequestFinish = book.LastByte;
            }

            foreach (var market in book.Response)
            {
                IObserver <MarketBook> o;
                if (!_observers.TryGetValue(market.MarketId, out o))
                {
                    continue;
                }

                // check to see if the market is finished
                if (market.Status == MarketStatus.CLOSED ||
                    market.Status == MarketStatus.INACTIVE)
                {
                    o.OnCompleted();
                }
                else
                {
                    o.OnNext(market);
                }
            }
        }
예제 #5
0
        // TODO:// replace this with the Rx scheduler
        private void PollMarketBooks()
        {
            for (int i = 0; i < connectionCount; i++)
            {
                Task.Run(() =>
                {
                    while (true)
                    {
                        if (markets.Count > 0)
                        {
                            // TODO:// look at spinwait or signalling instead of this
                            while (connectionCount > 1 && DateTime.Now.Subtract(lastRequestStart).TotalMilliseconds < (1000 / connectionCount))
                            {
                                int waitMs = (1000 / connectionCount) - (int)DateTime.Now.Subtract(lastRequestStart).TotalMilliseconds;
                                Thread.Sleep(waitMs > 0 ? waitMs : 0);
                            }

                            lock (lockObj)
                                lastRequestStart = DateTime.Now;

                            var book = client.ListMarketBook(markets.Keys.ToList(), this.priceProjection).Result;

                            if (!book.HasError)
                            {
                                // we may have fresher data than the response to this request
                                if (book.RequestStart < latestDataRequestStart && book.LastByte > latestDataRequestFinish)
                                {
                                    continue;
                                }
                                else
                                {
                                    lock (lockObj)
                                    {
                                        latestDataRequestStart  = book.RequestStart;
                                        latestDataRequestFinish = book.LastByte;
                                    }
                                }

                                foreach (var market in book.Response)
                                {
                                    IObserver <MarketBook> o;
                                    if (observers.TryGetValue(market.MarketId, out o))
                                    {
                                        // check to see if the market is finished
                                        if (market.Status == MarketStatus.CLOSED ||
                                            market.Status == MarketStatus.INACTIVE)
                                        {
                                            o.OnCompleted();
                                        }
                                        else
                                        {
                                            o.OnNext(market);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                foreach (var observer in observers)
                                {
                                    observer.Value.OnError(book.Error);
                                }
                            }
                        }
                        else
                        {
                            // TODO:// will die with rx scheduler
                            Thread.Sleep(500);
                        }
                    }
                });
                Thread.Sleep(1000 / connectionCount);
            }
        }