Exemplo n.º 1
0
        public override void Flush()
        {
            int bytesWrittenToBuffer = _currentBuffer.BytesWritten;

            _currentBuffer = WriteBufferWrapper.EmptyContainer;
            _dataAvail.TrySetResult(bytesWrittenToBuffer);
        }
Exemplo n.º 2
0
        public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (count < 0 || count > buffer.Length - offset)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            cancellationToken.ThrowIfCancellationRequested();

            if (_disposed)
            {
                throw new ObjectDisposedException("ProducerConsumerStream");
            }

            using (cancellationToken.Register(CancelAndDispose, this))
            {
                while (count > 0)
                {
                    if (_currentBuffer == WriteBufferWrapper.EmptyContainer)
                    {
                        _currentBuffer = await _buffer.Task;
                        _buffer        = new TaskCompletionSource <WriteBufferWrapper>();
                    }

                    int bytesWritten = _currentBuffer.Write(buffer, offset, count);
                    count  -= bytesWritten;
                    offset += bytesWritten;
                    if (_currentBuffer.Count == 0)
                    {
                        _currentBuffer = WriteBufferWrapper.EmptyContainer;
                    }

                    _dataAvail.TrySetResult(bytesWritten);
                }
            }
        }
Exemplo n.º 3
0
 public ProducerConsumerStream()
 {
     _buffer        = new TaskCompletionSource <WriteBufferWrapper>();
     _dataAvail     = new TaskCompletionSource <int>();
     _currentBuffer = WriteBufferWrapper.EmptyContainer;
 }