Exemplo n.º 1
0
 public IAsyncResult BeginAccept(AsyncCallback callback, object state)
 {
     return(availableStreams.BeginDequeue(TimeSpan.MaxValue, callback, state));
 }
Exemplo n.º 2
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0 || offset >= buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (count < 0 || offset + count > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            if (count == 0)
            {
                return(0);
            }

            bool waitForChunk = true;
            int  bytesRead    = 0;

            while (true)
            {
                if (currentChunk == null)
                {
                    if (isStreamAtEnd)
                    {
                        return(0);
                    }
                    if (waitForChunk)
                    {
                        IAsyncResult dequeueAsyncResult = dataChunks.BeginDequeue(TimeSpan.MaxValue, null, null);
                        if (!dequeueAsyncResult.CompletedSynchronously &&
                            WaitHandle.WaitAny(new WaitHandle[] { dequeueAsyncResult.AsyncWaitHandle, done }) == 1)
                        {
                            return(0);
                        }
                        currentChunk = dataChunks.EndDequeue(dequeueAsyncResult);
                        waitForChunk = false;
                    }
                    else
                    {
                        if (!dataChunks.Dequeue(naglingDelay, out currentChunk))
                        {
                            return(bytesRead);
                        }
                    }
                    currentChunkPosition = 0;
                }
                else
                {
                    waitForChunk = false;
                }

                int bytesAvailable = currentChunk.Length - currentChunkPosition;
                int bytesToCopy;
                if (bytesAvailable > count)
                {
                    bytesToCopy = count;
                    Buffer.BlockCopy(currentChunk, currentChunkPosition,
                                     buffer, offset, count);
                    currentChunkPosition += count;
                    return(bytesRead + bytesToCopy);
                }
                else
                {
                    bytesToCopy = bytesAvailable;
                    Buffer.BlockCopy(currentChunk, currentChunkPosition,
                                     buffer, offset, bytesToCopy);
                    currentChunk         = null;
                    currentChunkPosition = 0;
                    bytesRead           += bytesToCopy;
                    offset += bytesToCopy;
                    count  -= bytesToCopy;
                }
            }
        }