Example #1
0
        public async Task <byte[]> ReadAsync(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)
                    {
                        ReadBufferAsync();
                    }
                    result = await BlockBuffers.TakeAsync();

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

                if (result.IsFinished)
                {
                    QueueBuffers.TryDequeue(out result);
                    return(await ReadAsync(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());
        }
Example #2
0
        public async Task <int> ReadAsync(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)
                {
                    ReadBufferAsync();
                }


                result = await BlockBuffers.TakeAsync();

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

            if (result.IsFinished)
            {
                QueueBuffers.TryDequeue(out result);
                return(await ReadAsync(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);
            }
        }
Example #3
0
        public async Task <byte> ReadOneByteAsync()
        {
            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)
                {
                    ReadBufferAsync();
                }

                result = await BlockBuffers.TakeAsync();

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

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