コード例 #1
0
        private ReadOnlySequence <byte> ConcactSequence(ref ReadOnlySequence <byte> first, ref ReadOnlySequence <byte> second)
        {
            SequenceSegment head = first.Start.GetObject() as SequenceSegment;
            SequenceSegment tail = first.End.GetObject() as SequenceSegment;

            if (head == null)
            {
                foreach (var segment in first)
                {
                    if (head == null)
                    {
                        tail = head = SequenceSegment.CopyFrom(segment);
                    }
                    else
                    {
                        tail = tail.SetNext(SequenceSegment.CopyFrom(segment));
                    }
                }
            }

            if (!second.IsEmpty)
            {
                foreach (var segment in second)
                {
                    tail = tail.SetNext(SequenceSegment.CopyFrom(segment));
                }
            }

            return(new ReadOnlySequence <byte>(head, 0, tail, tail.Memory.Length));
        }
コード例 #2
0
        private ReadOnlySequence <byte> ConcactSequence(ReadOnlySequence <byte> first, ReadOnlySequence <byte> second)
        {
            SequenceSegment head = first.Start.GetObject() as SequenceSegment;
            SequenceSegment tail = first.End.GetObject() as SequenceSegment;

            if (head == null)
            {
                foreach (var segment in first)
                {
                    if (head == null)
                    {
                        tail = head = new SequenceSegment(segment);
                    }
                    else
                    {
                        tail = tail.SetNext(segment);
                    }
                }
            }

            foreach (var segment in second)
            {
                tail = tail.SetNext(segment);
            }

            return(new ReadOnlySequence <byte>(head, 0, tail, tail.Memory.Length));
        }
コード例 #3
0
ファイル: StringDecode.cs プロジェクト: mskenan/SuperSocket
        public void GlobalSetup()
        {
            var sb = new StringBuilder();

            for (var i = 0; i < S; i++)
            {
                while (true)
                {
                    sb.Append(Guid.NewGuid().ToString().Replace("-", string.Empty));

                    if (sb.Length >= N)
                    {
                        var data = _encoding.GetBytes(sb.ToString(0, N));

                        var segment = new SequenceSegment(data, data.Length);

                        if (_head == null)
                        {
                            _tail = _head = segment;
                        }
                        else
                        {
                            _tail = _tail.SetNext(segment);
                        }

                        sb.Clear();
                        break;
                    }
                }
            }
        }
コード例 #4
0
    private void MoveNext()
    {
        var firstSegment = _firstSegment;

        _firstSegmentStartIndex += (int)_jsonReader.BytesConsumed;
        // release previous segments if possible
        if (!_keepBuffers)
        {
            while (firstSegment?.Memory.Length <= _firstSegmentStartIndex)
            {
                _firstSegmentStartIndex -= firstSegment.Memory.Length;
                firstSegment.Dispose();
                firstSegment = (SequenceSegment?)firstSegment.Next;
            }
        }
        // create new segment
        var newSegment = new SequenceSegment(_bufferSize, _lastSegment);

        if (firstSegment != null)
        {
            _firstSegment       = firstSegment;
            newSegment.Previous = _lastSegment;
            _lastSegment?.SetNext(newSegment);
            _lastSegment = newSegment;
        }
        else
        {
            _firstSegment           = _lastSegment = newSegment;
            _firstSegmentStartIndex = 0;
        }
        // read data from stream
        _lastSegmentEndIndex = _stream.Read(newSegment.Buffer.Memory.Span);
        _isFinalBlock        = _lastSegmentEndIndex < newSegment.Buffer.Memory.Length;
        _jsonReader          = new Utf8JsonReader(new ReadOnlySequence <byte>(_firstSegment, _firstSegmentStartIndex, _lastSegment, _lastSegmentEndIndex), _isFinalBlock, _jsonReader.CurrentState);
    }
コード例 #5
0
    private void MoveNext()
    {
        _firstSegmentStartIndex += (int)_jsonReader.BytesConsumed;

        // release previous segments if possible
        while (_firstSegmentStartIndex > 0 && _firstSegment?.Memory.Length <= _firstSegmentStartIndex)
        {
            var currFirstSegment = _firstSegment;
            _firstSegmentStartIndex -= _firstSegment.Memory.Length;
            _firstSegment            = (SequenceSegment?)_firstSegment.Next;
            if (!_keepBuffers)
            {
                currFirstSegment.Dispose();
            }
        }

        // create new segment
        var newSegment = new SequenceSegment(_bufferSize, _lastSegment);

        _lastSegment?.SetNext(newSegment);
        _lastSegment = newSegment;

        if (_firstSegment == null)
        {
            _firstSegment           = newSegment;
            _firstSegmentStartIndex = 0;
        }

        // read data from stream
        _lastSegmentEndIndex = 0;
        int bytesRead;

        do
        {
#if NET
            bytesRead = _stream.Read(newSegment.Buffer.Memory.Span.Slice(_lastSegmentEndIndex));
#else
            var bytes = newSegment.Buffer.Memory.Span.Slice(_lastSegmentEndIndex).ToArray();
            bytesRead = _stream.Read(bytes, 0, bytes.Length);
#endif
            _lastSegmentEndIndex += bytesRead;
        } while (bytesRead > 0 && _lastSegmentEndIndex < newSegment.Buffer.Memory.Length);
        _isFinalBlock = _lastSegmentEndIndex < newSegment.Buffer.Memory.Length;
        var data = new ReadOnlySequence <byte>(_firstSegment, _firstSegmentStartIndex, _lastSegment,
                                               _lastSegmentEndIndex);
        _jsonReader =
            new Utf8JsonReader(data, _isFinalBlock, _jsonReader.CurrentState);
    }
コード例 #6
0
        public void Decode(WebSocketPackage package)
        {
            if (!package.RSV1)
            {
                return;
            }

            var data = package.Data;

            data = data.ConcatSequence(new SequenceSegment(LAST_FOUR_OCTETS_REVERSE, LAST_FOUR_OCTETS_REVERSE.Length, false));

            SequenceSegment head = null;
            SequenceSegment tail = null;

            using (var stream = new DeflateStream(new ReadOnlySequenceStream(data), CompressionMode.Decompress))
            {
                while (true)
                {
                    var buffer = _arrayPool.Rent(_deflateBufferSize);
                    var read   = stream.Read(buffer, 0, buffer.Length);

                    if (read == 0)
                    {
                        break;
                    }

                    var segment = new SequenceSegment(buffer, read);

                    if (head == null)
                    {
                        tail = head = segment;
                    }
                    else
                    {
                        tail.SetNext(segment);
                    }
                }
            }

            package.Data = new ReadOnlySequence <byte>(head, 0, tail, tail.Memory.Length);
        }
コード例 #7
0
        private ReadOnlySequence <byte> CopySequence(ref ReadOnlySequence <byte> seq)
        {
            SequenceSegment head = null;
            SequenceSegment tail = null;

            foreach (var segment in seq)
            {
                var newSegment = SequenceSegment.CopyFrom(segment);

                if (head == null)
                {
                    tail = head = newSegment;
                }
                else
                {
                    tail = tail.SetNext(newSegment);
                }
            }

            return(new ReadOnlySequence <byte>(head, 0, tail, tail.Memory.Length));
        }
コード例 #8
0
        public static ReadOnlySequence <byte> ConcactSequence(this ReadOnlySequence <byte> first,
                                                              ReadOnlySequence <byte> second, bool cloneSecond = true)
        {
            if (cloneSecond)
            {
                second = new ReadOnlySequence <byte>(second.ToArray());
            }
            SequenceSegment head = first.Start.GetObject() as SequenceSegment;
            SequenceSegment tail = first.End.GetObject() as SequenceSegment;

            if (head == null)
            {
                foreach (var segment in first)
                {
                    if (head == null)
                    {
                        tail = head = new SequenceSegment(segment);
                    }
                    else
                    {
                        tail = tail.SetNext(segment);
                    }
                }
            }

            foreach (var segment in second)
            {
                if (tail == null)
                {
                    head = tail = new SequenceSegment(segment);
                }
                else
                {
                    tail = tail.SetNext(segment);
                }
            }

            return(new ReadOnlySequence <byte>(head, 0, tail, tail.Memory.Length));
        }