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); } } }
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); } }
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); } }