예제 #1
0
        ReusableTask Tick(int delta, bool waitForBufferedIO)
        {
            UpdateTimer.Restart();

            ReadMonitor.Tick(delta);
            WriteMonitor.Tick(delta);

            WriteLimiter.UpdateChunks(Settings.MaximumDiskWriteRate, WriteRate);
            ReadLimiter.UpdateChunks(Settings.MaximumDiskReadRate, ReadRate);

            var processTask = ProcessBufferedIOAsync();

            return(waitForBufferedIO ? processTask : ReusableTask.CompletedTask);
        }
예제 #2
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);
                }
            }
        }
예제 #3
0
        internal async ReusableTask <bool> ReadAsync(ITorrentData manager, long offset, byte [] buffer, int count)
        {
            Interlocked.Add(ref pendingReads, count);
            await IOLoop;

            if (ReadLimiter.TryProcess(count))
            {
                Interlocked.Add(ref pendingReads, -count);
                return(Read(manager, offset, buffer, count));
            }
            else
            {
                var tcs = new ReusableTaskCompletionSource <bool>();
                ReadQueue.Enqueue(new BufferedIO(manager, offset, buffer, count, tcs));
                return(await tcs.Task);
            }
        }
예제 #4
0
        internal DiskManager(ClientEngine engine, PieceWriter writer)
        {
            this.bufferedReads  = new Queue <BufferedIO>();
            this.bufferedWrites = new Queue <BufferedIO>();
            this.engine         = engine;
            this.ReadLimiter    = new RateLimiter();
            this.readMonitor    = new SpeedMonitor();
            this.writeMonitor   = new SpeedMonitor();
            this.WriteLimiter   = new RateLimiter();
            this.Writer         = writer;

            IOLoop.QueueTimeout(TimeSpan.FromSeconds(1), () => {
                readMonitor.Tick();
                writeMonitor.Tick();

                WriteLimiter.UpdateChunks(engine.Settings.MaximumDiskWriteRate, WriteRate);
                ReadLimiter.UpdateChunks(engine.Settings.MaximumDiskReadRate, ReadRate);

                ProcessBufferedIO();

                return(!Disposed);
            });
        }
예제 #5
0
        internal async Task <bool> ReadAsync(TorrentManager manager, long offset, byte [] buffer, int count)
        {
            await IOLoop;

            try
            {
                if (ReadLimiter.TryProcess(count))
                {
                    return(Read(manager, offset, buffer, count));
                }
                else
                {
                    var tcs = new TaskCompletionSource <bool>();
                    bufferedReads.Enqueue(new BufferedIO(manager, offset, buffer, count, tcs));
                    return(await tcs.Task);
                }
            }
            catch (Exception ex)
            {
                await SetError(manager, Reason.ReadFailure, ex);

                return(false);
            }
        }