Esempio n. 1
0
        private void setvalues(FrameReader reader)
        {
            this.frameCount = reader.FrameCount;
            this.header = reader.TagHeader;
            this.title = reader.Read<string>("TIT2", ReadString, null);
            this.artist = reader.Read<string>("TPE1", ReadString, null);
            this.album = reader.Read<string>("TALB", ReadString, null);
            this.year = reader.Read<string>("TYER", ReadString, null);
            this.trackstring = reader.Read<string>("TRCK", ReadString, null);

            if (trackstring == null)
            {
                this.tracknumber = -1;
            }
            else if (trackstring.Contains("/"))
            {
                string s = trackstring.Substring(0, trackstring.IndexOf('/'));

                this.tracknumber = -1;
                int.TryParse(s, out this.tracknumber);
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < trackstring.Length; i++)
                {
                    if (char.IsDigit(trackstring[i]))
                        sb.Append(trackstring[i]);
                }
                this.tracknumber = -1;
                int.TryParse(sb.ToString(), out this.tracknumber);
            }
        }
Esempio n. 2
0
        internal Response(Frame frame)
        {
            if (frame == null)
            {
                throw new ArgumentNullException("frame");
            }
            if (frame.Body == null)
            {
                throw new InvalidOperationException("Response body of the received frame was null");
            }
            if (!frame.Header.Flags.HasFlag(HeaderFlag.Compression) && frame.Header.BodyLength > frame.Body.Length - frame.Body.Position)
            {
                throw new DriverInternalError(string.Format(
                                                  "Response body length should be contained in stream: Expected {0} but was {1} (position {2})",
                                                  frame.Header.BodyLength, frame.Body.Length - frame.Body.Position, frame.Body.Position));
            }

            Reader = new FrameReader(frame.Body, frame.Serializer);

            if (frame.Header.Flags.HasFlag(HeaderFlag.Tracing))
            {
                //If a response frame has the tracing flag set, the first item in its body is the trace id
                var buffer = new byte[16];
                Reader.Read(buffer, 0, 16);
                TraceId = new Guid(TypeSerializer.GuidShuffle(buffer));
            }
        }
Esempio n. 3
0
 internal OutputPrepared(byte protocolVersion, FrameReader reader)
 {
     var length = reader.ReadInt16();
     QueryId = new byte[length];
     reader.Read(QueryId, 0, length);
     Metadata = new RowSetMetadata(reader, protocolVersion >= 4);
 }
Esempio n. 4
0
        protected override void Load(FrameReader cb)
        {
            short len = cb.ReadInt16();

            _info.UnknownId = new byte[len];
            cb.Read(_info.UnknownId, 0, len);
        }
Esempio n. 5
0
        internal OutputPrepared(ProtocolVersion protocolVersion, FrameReader reader)
        {
            var length = reader.ReadInt16();

            QueryId = new byte[length];
            reader.Read(QueryId, 0, length);
            Metadata = new RowSetMetadata(reader, protocolVersion.SupportsPreparedPartitionKey());
        }
        internal OutputPrepared(byte protocolVersion, FrameReader reader)
        {
            var length = reader.ReadInt16();

            QueryId = new byte[length];
            reader.Read(QueryId, 0, length);
            Metadata = new RowSetMetadata(reader, protocolVersion >= 4);
        }
        private void WriteAndRead(FrameWriter writer, FrameReader reader, Input.DirectX.Identifier input, Output.Joystick.Identifier output, Axis axis, double value)
        {
            writer.Write(new Dictionary <Output.IIdentifier, State>
            {
                { output, new State()
                  {
                      Axis = new Dictionary <Axis, double>
                      {
                          { axis, value }
                      }
                  } }
            });
            Thread.Sleep(1);
            var actual = reader.Read()[input].Axis[axis];

            Assert.IsTrue(value <= actual + 0.001 && actual - 0.001 <= value);
        }
Esempio n. 8
0
        public async Task WriteReadDataFrame()
        {
            var input  = new byte[] { 1, 12, 0, 4, 0, 15, 6, 49, 5, 4, 34, 0, 3, 239 };
            var stream = new MockDuplexStream();

            stream.Input.Write(input, 0, input.Length);
            stream.Input.Position = 0;
            var reader   = new FrameReader(stream);
            var response = (DataFrame)(await reader.Read(CancellationToken.None));

            var request = new DataFrame(DataFrameType.REQ, response.Payload);
            var writer  = new FrameWriter(stream);
            await writer.Write(request, CancellationToken.None);

            var output = stream.Output.ToArray();

            Assert.IsTrue(input.SequenceEqual(output));
        }
Esempio n. 9
0
        public async Task WriteReadAckFrame()
        {
            var stream = new MockDuplexStream();

            var writer  = new FrameWriter(stream);
            var request = Frame.ACK;
            await writer.Write(request, CancellationToken.None);

            var data = stream.Output.ToArray();
            await stream.Input.WriteAsync(data, 0, data.Length);

            stream.Input.Position = 0;

            var reader   = new FrameReader(stream);
            var response = await reader.Read(CancellationToken.None);

            Assert.AreEqual(request, response);
        }
Esempio n. 10
0
        internal Response(Frame frame)
        {
            if (frame == null) throw new ArgumentNullException("frame");
            if (frame.Body == null) throw new InvalidOperationException("Response body of the received frame was null");
            if (!frame.Header.Flags.HasFlag(HeaderFlag.Compression) && frame.Header.BodyLength > frame.Body.Length - frame.Body.Position)
            {
                throw new DriverInternalError(string.Format(
                    "Response body length should be contained in stream: Expected {0} but was {1} (position {2})",
                    frame.Header.BodyLength, frame.Body.Length - frame.Body.Position, frame.Body.Position));
            }

            Reader = new FrameReader(frame.Body, frame.Serializer);

            if (frame.Header.Flags.HasFlag(HeaderFlag.Tracing))
            {
                //If a response frame has the tracing flag set, the first item in its body is the trace id
                var buffer = new byte[16];
                Reader.Read(buffer, 0, 16);
                TraceId = new Guid(TypeSerializer.GuidShuffle(buffer));
            }
        }
Esempio n. 11
0
        private Task Execute(IObserver <Frame> observer, CancellationToken cancellationToken = default)
        {
            if (observer == null)
            {
                throw new ArgumentNullException(nameof(observer));
            }

            return(_task = Task.Run(async() =>
            {
                _logger.LogDebug("Started");

                // execute until externally cancelled
                while (!cancellationToken.IsCancellationRequested)
                {
                    var frame = default(Frame);
                    try
                    {
                        // read next frame
                        frame = await _reader.Read(cancellationToken);
                    }
                    catch (StreamClosedException ex)
                    {
                        // most likely removal of ZStick from host
                        _logger.LogDebug(ex.Message);

                        observer.OnError(ex);

                        return;
                    }
                    catch (OperationCanceledException) when(cancellationToken.IsCancellationRequested)
                    {
                        observer.OnCompleted();

                        // the read was cancelled by the passed cancellationtoken so end gracefully
                        break;
                    }
                    catch (ChecksumException ex)
                    {
                        // checksum failure on received frame, might happen during startup when we
                        // are not synchronized and receive a partially frame
                        _logger.LogWarning(ex.Message);

                        // send NACK and hopefully the controller will send this frame again
                        _logger.LogDebug($"Writing: {Frame.NAK}");
                        await _writer.Write(Frame.NAK, cancellationToken);

                        // wait for next frame
                        continue;
                    }

                    //
                    if (frame == Frame.ACK || frame == Frame.NAK || frame == Frame.CAN)
                    {
                        if (frame == Frame.ACK)
                        {
                            _logger.LogDebug($"Received: {frame}");
                        }
                        else
                        {
                            _logger.LogWarning($"Received: {frame}");
                        }

                        // publish the frame
                        observer.OnNext(frame);

                        // wait for next frame
                        continue;
                    }

                    if (frame is DataFrame dataFrame)
                    {
                        _logger.LogDebug($"Received: {frame}");

                        // dataframes must be aknowledged
                        _logger.LogDebug($"Writing: {Frame.ACK}");
                        await _writer.Write(Frame.ACK, cancellationToken);

                        // publish the frame
                        observer.OnNext(dataFrame);

                        // wait for next frame
                        continue;
                    }
                }

                _logger.LogDebug("Completed");
            }, cancellationToken));
        }
Esempio n. 12
0
 protected override void Load(FrameReader cb)
 {
     short len = cb.ReadInt16();
     _info.UnknownId = new byte[len];
     cb.Read(_info.UnknownId, 0, len);
 }
Esempio n. 13
0
        public IEnumerable <BasicMessage> Read(Stream stream)
        {
            var frames = frameReader.Read(stream);

            return(frameCollector.ToMessage(frames));
        }