Esempio n. 1
0
        void ProcessBufferedIO(bool force = false)
        {
            BufferedIO io;

            while (WriteQueue.Count > 0)
            {
                io = WriteQueue.Peek();
                // This means we wanted to wait until all the writes had been flushed
                // before we attempt to generate the hash of a given piece.
                if (io.manager == null && io.buffer == null)
                {
                    io = WriteQueue.Dequeue();
                    io.tcs.SetResult(true);
                    continue;
                }

                if (!force && !WriteLimiter.TryProcess(io.count))
                {
                    break;
                }

                io = WriteQueue.Dequeue();

                try {
                    Interlocked.Add(ref pendingWrites, -io.count);
                    Write(io.manager, io.offset, io.buffer, io.count);
                    io.tcs.SetResult(true);
                } catch (Exception ex) {
                    io.tcs.SetException(ex);
                }
            }

            while (ReadQueue.Count > 0)
            {
                if (!force && !ReadLimiter.TryProcess(ReadQueue.Peek().count))
                {
                    break;
                }

                io = ReadQueue.Dequeue();

                try {
                    Interlocked.Add(ref pendingReads, -io.count);
                    var result = Read(io.manager, io.offset, io.buffer, io.count);
                    io.tcs.SetResult(result);
                } catch (Exception ex) {
                    io.tcs.SetException(ex);
                }
            }
        }
Esempio n. 2
0
 public T Peek()
 {
     return(ReadQueue.Peek());
 }