Exemplo n.º 1
0
        public void Basic(bool singleSegment)
        {
            byte[] buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            ReadOnlySequence <byte> bytes = singleSegment
                ? new ReadOnlySequence <byte>(buffer)
                : SequenceFactory.CreateSplit(buffer, 2, 4);

            SequenceReader <byte> skipReader = new SequenceReader <byte>(bytes);

            Assert.False(skipReader.TryAdvanceTo(10));
            Assert.True(skipReader.TryAdvanceTo(4, advancePastDelimiter: false));
            Assert.True(skipReader.TryRead(out byte value));
            Assert.Equal(4, value);

            Assert.True(skipReader.TryAdvanceToAny(new byte[] { 3, 12, 7 }, advancePastDelimiter: false));
            Assert.True(skipReader.TryRead(out value));
            Assert.Equal(7, value);
            Assert.Equal(1, skipReader.AdvancePast(8));
            Assert.True(skipReader.TryRead(out value));
            Assert.Equal(9, value);

            skipReader = new SequenceReader <byte>(bytes);
            Assert.Equal(0, skipReader.AdvancePast(2));
            Assert.Equal(3, skipReader.AdvancePastAny(new byte[] { 2, 3, 1 }));
            Assert.True(skipReader.TryRead(out value));
            Assert.Equal(4, value);
        }
Exemplo n.º 2
0
        private StringBuilder GetResponse(ReadResult result)
        {
            SequenceReader <byte> reader = new SequenceReader <byte>(result.Buffer);

            reader.AdvancePast(0);

            StringBuilder sb = new StringBuilder();

            while (reader.Remaining > 0)
            {
                ReadOnlySpan <byte> unread = reader.UnreadSpan;
                sb.Append(Encoding.UTF8.GetString(unread).Trim('\0'));
                reader.Advance(unread.Length);
            }

            return(sb);
        }
Exemplo n.º 3
0
        private bool TryExecute(ICommandNode node, ref SequenceReader <char> sequenceReader, ref List <object> parameters)
        {
            var         startParameterCount = parameters.Count;
            IDisposable?scope = null;

            if (node is INamedCommandNode nn)
            {
                scope = _logger.BeginScope($"{nn.Name}");
                _logger.LogDebug("Beginning to execute {Name}", nn.Name);
            }

            using (scope)
            {
                if (!node.Parse(ref sequenceReader, ref parameters))
                {
                    _logger.LogDebug("Failed to Parse");
                    var parameterCountDiff = parameters.Count - startParameterCount;
                    if (parameterCountDiff > 0)
                    {
                        parameters.RemoveRange(parameters.Count - parameterCountDiff, parameterCountDiff);
                    }
                    return(false);
                }

                sequenceReader.AdvancePast(' ');

                var oldRemaining = sequenceReader.Remaining;
                foreach (var n in node.Children)
                {
                    if (TryExecute(n, ref sequenceReader, ref parameters))
                    {
                        return(true);
                    }

                    var consumed = oldRemaining - sequenceReader.Remaining;
                    sequenceReader.Rewind(consumed);
                }

                // downstream didn't run, so this runs.
                if (node.Run is not null)
                {
                    try
                    {
                        node.Run.DynamicInvoke(parameters.ToArray());
                        return(true);
                    }
                    catch (Exception e)
                    {
                        _logger.LogWarning(e, "Node {@Node} threw while running", node);
                    }
                }

                {
                    var parameterCountDiff = parameters.Count - startParameterCount;
                    if (parameterCountDiff > 0)
                    {
                        parameters.RemoveRange(parameters.Count - parameterCountDiff, parameterCountDiff);
                    }
                    return(false);
                }
            }
        }
Exemplo n.º 4
0
 public long AdvancePast(byte value) => Data.AdvancePast(value);