Exemplo n.º 1
0
        private void DataThread()
        {
            const int N_States        = 10;
            var       blockTotal      = _reader.SampleCount / _buffer.Length;
            var       stateSaveFactor = blockTotal / N_States;
            var       totalRead       = 0L;

            // find last state from bag
            var nearestState = GetNearestState(_fileTargetPosition);

            if (nearestState != null)
            {
                var nearestStateDistance = _fileTargetPosition - nearestState.Position;
                var currentDistance      = _fileTargetPosition - _reader.Position;

                if (currentDistance < 0 || currentDistance > nearestStateDistance)
                {
                    Parent.LoadState(nearestState.States);

                    System.Diagnostics.Debug.WriteLine("Load state from position " + nearestState.Position);

                    if (_seekWindow != null)
                    {
                        _seekWindow.ValueBackground = _reader.Position;
                    }
                }
            }

            var newStateRefPos = _reader.Position;

            if (_stateBag.Count > 0 && _stateBag.Last().Position < newStateRefPos)
            {
                newStateRefPos = _stateBag.Last().Position;
            }

            // current block in file
            totalRead = _reader.Position;

            while (_running)
            {
                if (_fileTargetPosition > _reader.Position)
                {
                    // save state
                    //if (totalRead % (stateSaveFactor * _buffer.Length) == 0) {
                    if ((_reader.Position - newStateRefPos) > (stateSaveFactor * _buffer.Length))
                    {
                        if (!_stateBag.Exists(t => t.Item1 == _reader.Position))
                        {
                            _seekWindow?.AddStatePointMarker(_reader.Position);
                            var states = Parent.SaveState();
                            _stateBag.Add(new StateEntry(_reader.Position, states));
                            newStateRefPos = _reader.Position;
                            System.Diagnostics.Debug.WriteLine("Added to statebag: " + _reader.Position);
                        }
                    }

                    // push data into graph
                    var continueReading = true;
                    try {
                        var shouldRead = Math.Min(_buffer.Length, _fileTargetPosition - _reader.Position);
                        totalRead += FillBuffer(shouldRead);
                    } catch (IOException) {
                        continueReading = false;
                    }

                    DistributeBuffer();

                    if (!continueReading)
                    {
                        NodeSystemSettings.Instance.SystemHost.StopProcessing();
                        return;
                    }
                }
                else
                {
                    break;
                }
            }
        }