Esempio n. 1
0
        private bool TryReadTick(long length)
        {
            tickIO.SetSymbol(lSymbol);
            byte size = dataIn.ReadByte();

            // Check for old style prior to version 8 where
            // single byte version # was first.
            if (dataVersion < 8 && size < 8)
            {
                tickIO.FromReader((byte)size, dataIn);
            }
            else
            {
                // Subtract the size byte.
                if (dataIn.BaseStream.Position + size - 1 > length)
                {
                    return(false);
                }
                int count = 1;
                memory.SetLength(size);
                memory.GetBuffer()[0] = size;
                while (count < size)
                {
                    count += dataIn.Read(buffer, count, size - count);
                }
                memory.Position = 0;
                tickIO.FromReader(memory);
                if (dataVersion == 0)
                {
                    dataVersion = tickIO.DataVersion;
                }
            }
            var utcTime = new TimeStamp(tickIO.Extract().UtcTime);

            tickIO.SetTime(utcTime);
            return(true);
        }
Esempio n. 2
0
        public virtual Yield Invoke()
        {
            EventItem eventItem;

            if (fileReaderTask.Filter.Receive(out eventItem))
            {
                switch (eventItem.EventType)
                {
                default:
                    throw new ApplicationException("Unexpected event: " + eventItem);
                }
            }
            if (!isStarted)
            {
                return(Yield.NoWork.Repeat);
            }
            lock (taskLocker)
            {
                if (isDisposed)
                {
                    return(Yield.Terminate);
                }
                try
                {
                    var loopCount = 0;
                    while (!CancelPending && loopCount < 1000 && tickBoxPool.AllocatedCount < tickBoxPool.Capacity)
                    {
                        ++loopCount;
                        if (!tickFile.TryReadTick(tickIO))
                        {
                            if (debug)
                            {
                                log.Debug("Finished reading to file length: " + tickFile.Length);
                            }
                            return(SendFinish());
                        }

                        tick       = tickIO.Extract();
                        isDataRead = true;

                        if (Factory.TickCount > nextUpdate)
                        {
                            try {
                                progressCallback("Loading bytes...", tickFile.Position, tickFile.Length);
                            } catch (Exception ex) {
                                log.Debug("Exception on progressCallback: " + ex.Message);
                            }
                            nextUpdate = Factory.TickCount + 2000;
                        }

                        if (MaxCount > 0 && Count > MaxCount)
                        {
                            if (debug)
                            {
                                log.Debug("Ending data read because count reached " + MaxCount + " ticks.");
                            }
                            return(SendFinish());
                        }

                        if (IsAtEnd(tick))
                        {
                            return(SendFinish());
                        }

                        if (IsAtStart(tick))
                        {
                            count = Count + 1;
                            if (debug && Count < 10)
                            {
                                log.Debug("Read a tick " + tickIO);
                            }
                            else if (trace)
                            {
                                log.Trace("Read a tick " + tickIO);
                            }
                            tick.Symbol = tickFile.Symbol.BinaryIdentifier;

                            if (tick.UtcTime <= lastTime)
                            {
                                tick.UtcTime = lastTime + 1;
                            }
                            lastTime = tick.UtcTime;

                            if (isFirstTick)
                            {
                                isFirstTick = false;
                                StartEvent();
                            }
                            else
                            {
                                tickCount++;
                            }

                            box = tickBoxPool.Create(tickPoolCallerId);
                            if (debug)
                            {
                                log.Debug("Allocated box id in reader " + box.Id + ", count " + tickBoxPool.AllocatedCount);
                            }
                            var tickId = box.TickBinary.Id;
                            box.TickBinary    = tick;
                            box.TickBinary.Id = tickId;

                            TickEvent();
                        }
                        tickCount++;
                    }
                }
                catch (ObjectDisposedException)
                {
                    return(SendFinish());
                }
                return(Yield.DidWork.Repeat);
            }
        }