Пример #1
0
        public byte[] Read(byte[] exitBytes)
        {
            if (IsClosed && QueueBuffers.IsEmpty && BlockBuffers.Count == 0)
            {
                throw new Exception("read zero buffer! client disconnected");
            }
            List <byte> bytes = new List <byte>();

            while (true)
            {
                BufferSegment result = null;
                if (QueueBuffers.IsEmpty)
                {
                    if (BlockBuffers.Count == 0)
                    {
                        ReadBuffer();
                    }

                    result = BlockBuffers.Take();

                    if (IsClosed && BlockBuffers.Count == 0)
                    {
                        throw new Exception("read zero buffer! client disconnected");
                    }
                    QueueBuffers.Enqueue(result);
                }
                else
                {
                    if (!QueueBuffers.TryPeek(out result))
                    {
                        return(Read(exitBytes));
                    }
                }

                if (result.IsFinished)
                {
                    QueueBuffers.TryDequeue(out result);
                    return(Read(exitBytes));
                }
                else
                {
                    if (bytes.Count > 0 && bytes.Last() == exitBytes.First() && result.WhatIsFirstByte() == exitBytes.Last())
                    {
                        exitBytes = new byte[] { exitBytes.Last() }
                    }
                    ;
                    bytes.AddRange(result.Read(exitBytes, out bool isFound));
                    if (result.IsFinished)
                    {
                        QueueBuffers.TryDequeue(out result);
                    }
                    if (isFound)
                    {
                        break;
                    }
                }
            }
            return(bytes.ToArray());
        }
Пример #2
0
        public int Read(byte[] bytes, int count)
        {
            if (IsClosed && QueueBuffers.IsEmpty && BlockBuffers.Count == 0)
            {
                throw new Exception("read zero buffer! client disconnected");
            }
            if (bytes != null && bytes.Length < count)
            {
                throw new Exception("count size is greater than bytes.length");
            }
            BufferSegment result = null;

            if (QueueBuffers.IsEmpty)
            {
                if (BlockBuffers.Count == 0)
                {
                    ReadBuffer();
                }

                result = BlockBuffers.Take();
                if (IsClosed && BlockBuffers.Count == 0)
                {
                    throw new Exception("read zero buffer! client disconnected");
                }
                QueueBuffers.Enqueue(result);
            }
            else
            {
                if (!QueueBuffers.TryPeek(out result))
                {
                    return(Read(bytes, count));
                }
            }

            if (result.IsFinished)
            {
                QueueBuffers.TryDequeue(out result);
                return(Read(bytes, count));
            }
            else
            {
                byte[] readBytes = result.ReadBufferSegment(count, out int readCount);
                if (result.IsFinished)
                {
                    QueueBuffers.TryDequeue(out result);
                }
                Array.Copy(readBytes, bytes, readCount);
                return(readCount);
            }
        }
Пример #3
0
        public byte ReadOneByte()
        {
            if (IsClosed && QueueBuffers.IsEmpty && BlockBuffers.Count == 0)
            {
                throw new Exception("read zero buffer! client disconnected");
            }
            BufferSegment result = null;

            if (QueueBuffers.IsEmpty)
            {
                if (BlockBuffers.Count == 0)
                {
                    ReadBuffer();
                }

                result = BlockBuffers.Take();

                if (IsClosed && BlockBuffers.Count == 0)
                {
                    throw new Exception("read zero buffer! client disconnected");
                }
                QueueBuffers.Enqueue(result);
            }
            else
            {
                if (!QueueBuffers.TryPeek(out result))
                {
                    return(ReadOneByte());
                }
            }

            if (result.IsFinished)
            {
                QueueBuffers.TryDequeue(out result);
                return(ReadOneByte());
            }
            else
            {
                byte b = result.ReadFirstByte();
                if (result.IsFinished)
                {
                    QueueBuffers.TryDequeue(out result);
                }
                return(b);
            }
        }