Example #1
0
        private void DoData()
        {
            var dataRedirector = this.ResolveDataRedirector(_lastMetadata);

            dataRedirector?.Redirect();

            _state = StreamRedirectorState.Idle;
        }
Example #2
0
        private void Routine()
        {
            _state = StreamRedirectorState.Idle;

            try
            {
                var goOn = true;
                while (goOn)
                {
                    switch (_state)
                    {
                    case StreamRedirectorState.Idle:
                        this.DoIdle();
                        break;

                    case StreamRedirectorState.Metadata:
                        this.DoMetadata();
                        break;

                    case StreamRedirectorState.Data:
                        this.DoData();
                        break;

                    case StreamRedirectorState.Stop:
                        goOn = false;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }

                Logger.Info("Stopped due to request");
            }
            catch (Exception ex)
            {
                // todo1[ak] log or something
                Logger.InfoFormat("{0} Stopped due to an exception {1}", this.GetType().FullName, ex.GetType().FullName);
                this.Relay.StopSignal.Set();
            }

            ThreadPool.QueueUserWorkItem(x => this.Relay.Dispose());
        }
Example #3
0
        private void DoMetadata()
        {
            if (this.SourceStream.AccumulatedBytesCount == 0)
            {
                // let's accumulate some data
                var bytesReadCount = this.SourceStream.ReadInnerStream(PORTION_SIZE);

                if (bytesReadCount == 0)
                {
                    _state = StreamRedirectorState.Idle;
                    return;
                }
            }

            var metadataLength = this.GetMetadataLength();

            if (metadataLength >= 0)
            {
                this.SourceBuffer.Allocate(metadataLength);
                this.SourceStream.Read(this.SourceBuffer.Raw, 0, metadataLength);

                var metadata = this.ParseMetadata(this.SourceBuffer.Raw, 0);
                this.CheckMetadata(metadata);
                var transformedMetadata      = this.TransformMetadata(metadata);
                var transformedMetadataBytes = transformedMetadata.Serialize();

                //Logger.Info(transformedMetadataBytes.ToAsciiString());

                this.DestinationStream.Write(transformedMetadataBytes, 0, transformedMetadataBytes.Length);

                _lastMetadata = transformedMetadata;
                _state        = StreamRedirectorState.Data;
            }
            else
            {
                // seems like not enough bytes to extract metadata, read more
                this.SourceStream.ReadInnerStream(PORTION_SIZE);
            }
        }
Example #4
0
        private void DoIdle()
        {
            if (this.SourceStream.AccumulatedBytesCount > 0)
            {
                _state = StreamRedirectorState.Metadata;
                return;
            }

            var bytesReadCount = this.SourceStream.ReadInnerStream(PORTION_SIZE);

            if (bytesReadCount == 0)
            {
                var gotSignal = this.Relay.StopSignal.WaitOne(_idleTimeout);
                if (gotSignal)
                {
                    _state = StreamRedirectorState.Stop;
                }
            }
            else
            {
                _state = StreamRedirectorState.Metadata;
            }
        }