private async void ReadBufferAsync() { try { try { await lockWaitToRead.WaitAsync(); if (IsWaitToRead || IsClosed) { return; } IsWaitToRead = true; } finally { lockWaitToRead.Release(); } byte[] buffer = new byte[BufferToRead]; System.Diagnostics.Debug.WriteLine($"try to ReadAsync {buffer.Length}"); int readCount = await Stream.ReadAsync(buffer, 0, buffer.Length); System.Diagnostics.Debug.WriteLine($"done ReadAsync {buffer.Length}"); if (readCount <= 0) { IsClosed = true; IsWaitToRead = false; await BlockBuffers.AddAsync(new BufferSegment() { Buffer = null, Position = 0 }); await BlockBuffers.CancelAsync(); return; //throw new Exception("read zero buffer! client disconnected: " + readCount); } else if (readCount != buffer.Length) { Array.Resize(ref buffer, readCount); } IsWaitToRead = false; await BlockBuffers.AddAsync(new BufferSegment() { Buffer = buffer, Position = 0 }); } catch (Exception ex) { //Console.WriteLine(ex); IsClosed = true; IsWaitToRead = false; await BlockBuffers.AddAsync(new BufferSegment() { Buffer = null, Position = 0 }); await BlockBuffers.CancelAsync(); } }
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()); }
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); } }
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); } }