public PipedStreamWriter(PipedStreamManager pipe, bool throwsFailedWrite, bool autoFlush) { _pipe = pipe; _length = 0; _throwsFailedWrite = throwsFailedWrite; _autoFlush = autoFlush; _currentBuffer = null; }
public PipedStreamReader(PipedStreamManager pipe) { _pipe = pipe; _currentData = null; _currentDataPos = 0; _position = 0; }
internal async Task <bool> SendAsync(IPipeBufferItem item, CancellationToken cancellationToken) { bool sent = false; using (var ct = CreateCancellationTokenSource(_options.WriteTimeout)) using (var linkedCt = CancellationTokenSource.CreateLinkedTokenSource(ct.Token, cancellationToken)) { sent = await _pipe.SendAsync(item, linkedCt.Token).IgnoreContext(); } return(sent); }
internal bool SendSync(IPipeBufferItem item) { var posted = _pipe.Post(item); if (posted) { return(true); } // _pipe.Post doesn't allow for postponement. Using Async version when posting fails. return(Task.Run(() => SendAsync(item, CancellationToken.None)).GetAwaiter().GetResult()); }
private bool FlushInternalSync() { if (_currentBuffer == null) { return(true); //nothing to flush } var sent = _pipe.SendSync(_currentBuffer); _currentBuffer = null; return(sent); }
private async Task <bool> FlushInternalAsync(CancellationToken cancellationToken) { if (_currentBuffer == null) { return(true); //nothing to flush } var sent = await _pipe.SendAsync(_currentBuffer, cancellationToken).IgnoreContext(); _currentBuffer = null; return(sent); }
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { _pipe.RaiseOnWrite(count); bool?flushed = null; var blockSize = _pipe.BlockSize; var remainingCount = count; while (remainingCount > 0 && !_pipe.IsClosed()) { if (_currentBuffer == null) { _currentBuffer = _pipe.CreateNewBufferItem(); } var remaningBufferSize = blockSize - _currentBuffer.WrittenLength; var writeCount = remainingCount > remaningBufferSize ? remaningBufferSize : remainingCount; _currentBuffer.Write(buffer, offset, writeCount); offset += writeCount; remainingCount -= writeCount; if (_currentBuffer.WrittenLength == blockSize) { flushed = await this.FlushInternalAsync(cancellationToken).IgnoreContext(); if (flushed == false) { break; } } } Interlocked.Add(ref _length, count - remainingCount); var sent = remainingCount == 0 && flushed != false; if (sent && _autoFlush) { sent = await this.FlushInternalAsync(cancellationToken).IgnoreContext(); } AssertBufferWasSent(sent); }
public override async Task <int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { _pipe.RaiseOnRead(count); var remainingCount = count; while (remainingCount > 0) { if (_currentData == null) { _currentData = await _pipe.ReceiveAsync(cancellationToken).IgnoreContext(); _currentDataPos = 0; if (_currentData == null) { break; // No more bytes will be available. Finished. } } var remainingBufferSize = _currentData.WrittenLength - _currentDataPos; var readCount = remainingCount > remainingBufferSize ? remainingBufferSize : remainingCount; _currentData.Read(_currentDataPos, buffer, offset, readCount); _currentDataPos += readCount; if (_currentData.WrittenLength == _currentDataPos) { _currentData = null; _currentDataPos = 0; } offset += readCount; remainingCount -= readCount; } var bytesRead = count - remainingCount; _position += bytesRead; return(bytesRead); }
internal async Task <IPipeBufferItem> ReceiveAsync(CancellationToken cancellationToken) { IPipeBufferItem item = null; using (var ct = CreateCancellationTokenSource(_options.ReadTimeout)) using (var linkedCt = CancellationTokenSource.CreateLinkedTokenSource(ct.Token, cancellationToken)) { if (await _pipe.OutputAvailableAsync(linkedCt.Token).IgnoreContext()) { item = await _pipe.ReceiveAsync(linkedCt.Token).IgnoreContext(); } } if (item == null && _pipe.Completion.Status == TaskStatus.Faulted) { await _pipe.Completion.IgnoreContext(); } return(item); }
public override int Read(byte[] buffer, int offset, int count) { _pipe.RaiseOnRead(count); var remainingCount = count; while (remainingCount > 0) { if (_currentData == null) { _currentData = _pipe.ReceiveSync(); _currentDataPos = 0; if (_currentData == null) { break; // No more bytes will be available. Finished. } } var remainingBufferSize = _currentData.WrittenLength - _currentDataPos; var readCount = remainingCount > remainingBufferSize ? remainingBufferSize : remainingCount; _currentData.Read(_currentDataPos, buffer, offset, readCount); _currentDataPos += readCount; if (_currentData.WrittenLength == _currentDataPos) { _currentData = null; _currentDataPos = 0; } offset += readCount; remainingCount -= readCount; } var bytesRead = count - remainingCount; _position += bytesRead; return(bytesRead); }