diff --git a/src/Renci.SshNet.Tests/Classes/Common/PipeStreamTest.cs b/src/Renci.SshNet.Tests/Classes/Common/PipeStreamTest.cs
index 4c11b432a..86b067c48 100644
--- a/src/Renci.SshNet.Tests/Classes/Common/PipeStreamTest.cs
+++ b/src/Renci.SshNet.Tests/Classes/Common/PipeStreamTest.cs
@@ -175,18 +175,6 @@ public void LengthTest()
Assert.AreEqual(0L, target.Length);
}
- ///
- ///A test for MaxBufferLength
- ///
- [TestMethod]
- public void MaxBufferLengthTest()
- {
- var target = new PipeStream();
- Assert.AreEqual(200 * 1024 * 1024, target.MaxBufferLength);
- target.MaxBufferLength = 0L;
- Assert.AreEqual(0L, target.MaxBufferLength);
- }
-
[TestMethod]
public void Position_GetterAlwaysReturnsZero()
{
diff --git a/src/Renci.SshNet.Tests/Classes/Common/PipeStream_Close_BlockingWrite.cs b/src/Renci.SshNet.Tests/Classes/Common/PipeStream_Close_BlockingWrite.cs
index d270679a1..f794b49e5 100644
--- a/src/Renci.SshNet.Tests/Classes/Common/PipeStream_Close_BlockingWrite.cs
+++ b/src/Renci.SshNet.Tests/Classes/Common/PipeStream_Close_BlockingWrite.cs
@@ -14,7 +14,7 @@ public class PipeStream_Close_BlockingWrite
[TestInitialize]
public void Init()
{
- _pipeStream = new PipeStream {MaxBufferLength = 3};
+ _pipeStream = new PipeStream(3);
Action writeAction = () =>
{
diff --git a/src/Renci.SshNet.Tests/Classes/PipeStreamTest_Dispose.cs b/src/Renci.SshNet.Tests/Classes/PipeStreamTest_Dispose.cs
index 6ce5e73ff..69d08cd32 100644
--- a/src/Renci.SshNet.Tests/Classes/PipeStreamTest_Dispose.cs
+++ b/src/Renci.SshNet.Tests/Classes/PipeStreamTest_Dispose.cs
@@ -49,17 +49,9 @@ public void Flush_ShouldThrowObjectDisposedException()
}
[TestMethod]
- public void MaxBufferLength_Getter_ShouldReturnTwoHundredMegabyte()
+ public void BufferLength_Getter_ShouldReturnOneMegabyte()
{
- Assert.AreEqual(200 * 1024 * 1024, _pipeStream.MaxBufferLength);
- }
-
- [TestMethod]
- public void MaxBufferLength_Setter_ShouldModifyMaxBufferLength()
- {
- var newValue = new Random().Next(1, int.MaxValue);
- _pipeStream.MaxBufferLength = newValue;
- Assert.AreEqual(newValue, _pipeStream.MaxBufferLength);
+ Assert.AreEqual(1024 * 1024, _pipeStream.BufferLength);
}
[TestMethod]
diff --git a/src/Renci.SshNet/Common/PipeStream.cs b/src/Renci.SshNet/Common/PipeStream.cs
index fac54fb62..c779d2e84 100644
--- a/src/Renci.SshNet/Common/PipeStream.cs
+++ b/src/Renci.SshNet/Common/PipeStream.cs
@@ -1,369 +1,375 @@
-namespace Renci.SshNet.Common
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading;
+
+namespace Renci.SshNet.Common
{
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Threading;
- using System.Globalization;
-
///
- /// PipeStream is a thread-safe read/write data stream for use between two threads in a
- /// single-producer/single-consumer type problem.
+ /// Provides a producer/consumer ring-buffered memory stream. The methods Read() and Write() are
+ /// thread-safe for use by multiple readers and writers.
///
- /// 2006/10/13 1.0
- /// Update on 2008/10/9 1.1 - uses Monitor instead of Manual Reset events for more elegant synchronicity.
- ///
- /// Copyright (c) 2006 James Kolpack (james dot kolpack at google mail)
- ///
- /// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
- /// associated documentation files (the "Software"), to deal in the Software without restriction,
- /// including without limitation the rights to use, copy, modify, merge, publish, distribute,
- /// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
- /// furnished to do so, subject to the following conditions:
- ///
- /// The above copyright notice and this permission notice shall be included in all copies or
- /// substantial portions of the Software.
- ///
- /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
- /// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- /// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- /// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
- /// OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- /// OTHER DEALINGS IN THE SOFTWARE.
- ///
+ ///
+ /// The read lock and the write lock can be removed for a small performance gain when used in a
+ /// single-producer/single-consumer scenario.
+ ///
public class PipeStream : Stream
{
- #region Private members
+ private const int DefaultRingBufferSize = 1024 * 1024;
+
+ private readonly AutoResetEvent _bufferIsNotEmpty = new AutoResetEvent(false);
+ private readonly AutoResetEvent _bufferIsNotFull = new AutoResetEvent(true);
+ private readonly object _commonLock = new object();
+ private readonly object _readLock = new object();
+ private readonly byte[] _ringBuffer;
+ private readonly int _ringBufferSize;
+ private readonly object _writeLock = new object();
+ private bool _isDisposed;
+ private bool _isFlushed;
+ private long _readCount;
+ private int _readOffset;
+ private long _writeCount;
+ private int _writeOffset;
///
- /// Queue of bytes provides the datastructure for transmitting from an
- /// input stream to an output stream.
+ /// Initializes a new instance of the class.
///
- /// Possible more effecient ways to accomplish this.
- private readonly Queue _buffer = new Queue();
+ public PipeStream()
+ : this(DefaultRingBufferSize)
+ {
+ }
///
- /// Indicates that the input stream has been flushed and that
- /// all remaining data should be written to the output stream.
+ /// Initializes a new instance of the class.
///
- private bool _isFlushed;
+ /// The size in bytes of the ring buffer.
+ public PipeStream(int ringBufferSize)
+ {
+ _ringBufferSize = ringBufferSize;
+ _ringBuffer = new byte[ringBufferSize];
+ }
///
- /// Maximum number of bytes to store in the buffer.
+ /// Gets the length in bytes of the ring buffer.
///
- private long _maxBufferLength = 200 * 1024 * 1024;
+ public int BufferLength
+ {
+ get { return _ringBufferSize; }
+ }
///
- /// Setting this to true will cause Read() to block if it appears
- /// that it will run out of data.
+ /// Gets a value indicating whether the current stream supports reading.
///
- private bool _canBlockLastRead;
+ public override bool CanRead
+ {
+ get { return !_isDisposed; }
+ }
///
- /// Indicates whether the current is disposed.
+ /// Gets a value indicating whether the current stream supports seeking. Always returns false.
///
- private bool _isDisposed;
-
- #endregion
-
- #region Public properties
+ public override bool CanSeek
+ {
+ get { return false; }
+ }
///
- /// Gets or sets the maximum number of bytes to store in the buffer.
+ /// Gets a value indicating whether the current stream supports writing.
///
- /// The length of the max buffer.
- public long MaxBufferLength
+ public override bool CanWrite
{
- get { return _maxBufferLength; }
- set { _maxBufferLength = value; }
+ get { return !_isDisposed; }
}
///
- /// Gets or sets a value indicating whether to block last read method before the buffer is empty.
- /// When true, Read() will block until it can fill the passed in buffer and count.
- /// When false, Read() will not block, returning all the available buffer data.
+ /// Gets the length in bytes of the stream.
///
- ///
- /// Setting to true will remove the possibility of ending a stream reader prematurely.
- ///
- ///
- /// true if block last read method before the buffer is empty; otherwise, false.
- ///
- /// Methods were called after the stream was closed.
- public bool BlockLastReadBuffer
+ public override long Length
{
get
{
if (_isDisposed)
+ {
throw CreateObjectDisposedException();
+ }
- return _canBlockLastRead;
- }
- set
- {
- if (_isDisposed)
- throw CreateObjectDisposedException();
-
- _canBlockLastRead = value;
-
- // when turning off the block last read, signal Read() that it may now read the rest of the buffer.
- if (!_canBlockLastRead)
- lock (_buffer)
- Monitor.Pulse(_buffer);
+ lock (_commonLock)
+ {
+ return _writeCount - _readCount;
+ }
}
}
- #endregion
-
- #region Stream overide methods
+ ///
+ /// Gets or sets the position within the current stream.
+ ///
+ public override long Position
+ {
+ get { return 0; }
+ set { throw new NotSupportedException(); }
+ }
///
- /// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.
+ /// Once flushed, any subsequent read operations no longer block until requested bytes are
+ /// available. Any write operation reactivates blocking reads.
///
- /// An I/O error occurs.
- /// Methods were called after the stream was closed.
- ///
- /// Once flushed, any subsequent read operations no longer block until requested bytes are available. Any write operation reactivates blocking
- /// reads.
- ///
public override void Flush()
{
if (_isDisposed)
+ {
throw CreateObjectDisposedException();
+ }
- _isFlushed = true;
- lock (_buffer)
+ lock (_commonLock)
{
- // unblock read hereby allowing buffer to be partially filled
- Monitor.Pulse(_buffer);
+ _isFlushed = true;
}
+
+ _bufferIsNotEmpty.Set();
}
///
- /// When overridden in a derived class, sets the position within the current stream.
+ /// Reads a sequence of bytes from the current stream and advances the position within the
+ /// stream by the number of bytes read.
///
+ ///
+ /// An array of bytes. When this method returns, the buffer contains the specified byte array
+ /// with the values between offset and (offset + count - 1) replaced by the bytes read from
+ /// the current source.
+ ///
+ ///
+ /// The zero-based byte offset in buffer at which to begin storing the data read from the
+ /// current stream.
+ ///
+ /// The maximum number of bytes to be read from the current stream.
///
- /// The new position within the current stream.
+ /// The total number of bytes read into the buffer. This can be less than the number of bytes
+ /// requested if that many bytes are not currently available, or zero (0) if the end of the
+ /// stream has been reached.
///
- /// A byte offset relative to the origin parameter.
- /// A value of type indicating the reference point used to obtain the new position.
- /// The stream does not support seeking, such as if the stream is constructed from a pipe or console output.
- public override long Seek(long offset, SeekOrigin origin)
- {
- throw new NotSupportedException();
- }
-
- ///
- /// When overridden in a derived class, sets the length of the current stream.
- ///
- /// The desired length of the current stream in bytes.
- /// The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output.
- public override void SetLength(long value)
- {
- throw new NotSupportedException();
- }
-
- ///
- ///When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
- ///
- ///
- ///The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero if the stream is closed or end of the stream has been reached.
- ///
- ///The zero-based byte offset in buffer at which to begin storing the data read from the current stream.
- ///The maximum number of bytes to be read from the current stream.
- ///An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.
- ///The sum of offset and count is larger than the buffer length.
- ///Methods were called after the stream was closed.
- ///The stream does not support reading.
- /// is null.
- ///An I/O error occurs.
- ///offset or count is negative.
public override int Read(byte[] buffer, int offset, int count)
{
- if (offset != 0)
- throw new NotSupportedException("Offsets with value of non-zero are not supported");
if (buffer == null)
+ {
throw new ArgumentNullException("buffer");
- if (offset + count > buffer.Length)
+ }
+
+ if (offset < 0)
+ {
+ throw new ArgumentOutOfRangeException("offset", "Offset must be non-negative.");
+ }
+
+ if (count < 0)
+ {
+ throw new ArgumentOutOfRangeException("count", "Count must be non-negative.");
+ }
+
+ if (buffer.Length - offset < count)
+ {
throw new ArgumentException("The sum of offset and count is greater than the buffer length.");
- if (offset < 0 || count < 0)
- throw new ArgumentOutOfRangeException("offset", "offset or count is negative.");
- if (BlockLastReadBuffer && count >= _maxBufferLength)
- throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "count({0}) > mMaxBufferLength({1})", count, _maxBufferLength));
+ }
+
if (_isDisposed)
+ {
throw CreateObjectDisposedException();
+ }
+
if (count == 0)
+ {
return 0;
+ }
- var readLength = 0;
-
- lock (_buffer)
+ lock (_readLock)
{
- while (!_isDisposed && !ReadAvailable(count))
+ int bytesAvailable;
+ while (true)
{
- Monitor.Wait(_buffer);
+ lock (_commonLock)
+ {
+ if (_isDisposed)
+ {
+ return 0;
+ }
+
+ bytesAvailable = (int)(_writeCount - _readCount);
+ if (bytesAvailable >= count)
+ {
+ break;
+ }
+
+ if (_isFlushed)
+ {
+ if (bytesAvailable == 0)
+ {
+ return 0;
+ }
+
+ break;
+ }
+ }
+
+ _bufferIsNotEmpty.WaitOne();
}
- // return zero when the read is interrupted by a close/dispose of the stream
- if (_isDisposed)
+ int bytesToRead = Math.Min(count, bytesAvailable);
+ int contiguousBytesAvailable = _ringBufferSize - _readOffset;
+ if (contiguousBytesAvailable < bytesToRead)
+ {
+ Array.Copy(_ringBuffer, _readOffset, buffer, offset, contiguousBytesAvailable);
+ Array.Copy(_ringBuffer, 0, buffer, offset + contiguousBytesAvailable, bytesToRead - contiguousBytesAvailable);
+ _readOffset = (_readOffset + bytesToRead) % _ringBufferSize;
+ }
+ else
{
- return 0;
+ Array.Copy(_ringBuffer, _readOffset, buffer, offset, bytesToRead);
+ _readOffset += bytesToRead;
}
- // fill the read buffer
- for (; readLength < count && _buffer.Count > 0; readLength++)
+ lock (_commonLock)
{
- buffer[readLength] = _buffer.Dequeue();
+ _readCount += bytesToRead;
}
- Monitor.Pulse(_buffer);
+ _bufferIsNotFull.Set();
+ return bytesToRead;
}
+ }
- return readLength;
+ ///
+ /// Sets the position within the current stream. Always throws .
+ ///
+ /// A byte offset relative to the origin parameter.
+ ///
+ /// A value of type System.IO.SeekOrigin indicating the reference point used to obtain the
+ /// new position.
+ ///
+ /// The new position within the current stream.
+ public override long Seek(long offset, SeekOrigin origin)
+ {
+ throw new NotSupportedException();
}
///
- /// Returns true if there are
+ /// Sets the length of the current stream. Always throws .
///
- /// The count.
- /// True if data available; otherwisefalse.
- private bool ReadAvailable(int count)
+ /// The desired length of the current stream in bytes.
+ public override void SetLength(long value)
{
- var length = Length;
- return (_isFlushed || length >= count) && (length >= (count + 1) || !BlockLastReadBuffer);
+ throw new NotSupportedException();
}
- ///
- ///When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
- ///
- ///The zero-based byte offset in buffer at which to begin copying bytes to the current stream.
- ///The number of bytes to be written to the current stream.
- ///An array of bytes. This method copies count bytes from buffer to the current stream.
- ///An I/O error occurs.
- ///The stream does not support writing.
- ///Methods were called after the stream was closed.
- /// is null.
- ///The sum of offset and count is greater than the buffer length.
- ///offset or count is negative.
+ ///
+ /// Writes a sequence of bytes to the current stream and advances the current position within
+ /// this stream by the number of bytes written.
+ ///
+ ///
+ /// An array of bytes. This method copies count bytes from buffer to the current stream.
+ ///
+ ///
+ /// The zero-based byte offset in buffer at which to begin copying bytes to the current stream.
+ ///
+ /// The number of bytes to be written to the current stream.
public override void Write(byte[] buffer, int offset, int count)
{
if (buffer == null)
+ {
throw new ArgumentNullException("buffer");
- if (offset + count > buffer.Length)
+ }
+
+ if (offset < 0)
+ {
+ throw new ArgumentOutOfRangeException("offset", "Offset must be non-negative.");
+ }
+
+ if (count < 0 || count > _ringBufferSize)
+ {
+ throw new ArgumentOutOfRangeException("count", "Count must be non-negative and less than or equal to the ring buffer size.");
+ }
+
+ if (buffer.Length - offset < count)
+ {
throw new ArgumentException("The sum of offset and count is greater than the buffer length.");
- if (offset < 0 || count < 0)
- throw new ArgumentOutOfRangeException("offset", "offset or count is negative.");
+ }
+
if (_isDisposed)
+ {
throw CreateObjectDisposedException();
+ }
+
if (count == 0)
+ {
return;
+ }
- lock (_buffer)
+ lock (_writeLock)
{
- // wait until the buffer isn't full
- while (Length >= _maxBufferLength)
- Monitor.Wait(_buffer);
+ int spaceAvailable;
+ while (true)
+ {
+ lock (_commonLock)
+ {
+ if (_isDisposed)
+ {
+ throw CreateObjectDisposedException();
+ }
+
+ spaceAvailable = _ringBufferSize - (int)(_writeCount - _readCount);
+ if (spaceAvailable >= count)
+ {
+ _isFlushed = false;
+ break;
+ }
+ }
+
+ _bufferIsNotFull.WaitOne();
+ }
- _isFlushed = false; // if it were flushed before, it soon will not be.
+ int contiguousSpaceAvailable = _ringBufferSize - _writeOffset;
+ if (contiguousSpaceAvailable < count)
+ {
+ Array.Copy(buffer, offset, _ringBuffer, _writeOffset, contiguousSpaceAvailable);
+ Array.Copy(buffer, offset + contiguousSpaceAvailable, _ringBuffer, 0, count - contiguousSpaceAvailable);
+ _writeOffset = (_writeOffset + count) % _ringBufferSize;
+ }
+ else
+ {
+ Array.Copy(buffer, offset, _ringBuffer, _writeOffset, count);
+ _writeOffset += count;
+ }
- // queue up the buffer data
- for (var i = offset; i < offset + count; i++)
+ lock (_commonLock)
{
- _buffer.Enqueue(buffer[i]);
+ _writeCount += count;
}
- Monitor.Pulse(_buffer); // signal that write has occurred
+ _bufferIsNotEmpty.Set();
}
}
///
- /// Releases the unmanaged resources used by the Stream and optionally releases the managed resources.
+ /// Releases the unmanaged resources used by the and
+ /// optionally releases the managed resources.
///
- /// true to release both managed and unmanaged resources; false to release only unmanaged resources.
- ///
- /// Disposing a will interrupt blocking read and write operations.
- ///
+ ///
+ /// true to release both managed and unmanaged resources; false to release only
+ /// unmanaged resources.
+ ///
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
-
if (!_isDisposed)
{
- lock (_buffer)
+ lock (_commonLock)
{
_isDisposed = true;
- Monitor.Pulse(_buffer);
+ _bufferIsNotEmpty.Set();
+ _bufferIsNotFull.Set();
}
}
}
- ///
- ///When overridden in a derived class, gets a value indicating whether the current stream supports reading.
- ///
- ///
- ///true if the stream supports reading; otherwise, false.
- ///
- public override bool CanRead
- {
- get { return !_isDisposed; }
- }
-
- ///
- /// When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
- ///
- ///
- /// true if the stream supports seeking; otherwise, false.
- ///
- public override bool CanSeek
- {
- get { return false; }
- }
-
- ///
- /// When overridden in a derived class, gets a value indicating whether the current stream supports writing.
- ///
- ///
- /// true if the stream supports writing; otherwise, false.
- ///
- public override bool CanWrite
- {
- get { return !_isDisposed; }
- }
-
- ///
- /// When overridden in a derived class, gets the length in bytes of the stream.
- ///
- ///
- /// A long value representing the length of the stream in bytes.
- ///
- /// A class derived from Stream does not support seeking.
- /// Methods were called after the stream was closed.
- public override long Length
- {
- get
- {
- if (_isDisposed)
- throw CreateObjectDisposedException();
-
- return _buffer.Count;
- }
- }
-
- ///
- /// When overridden in a derived class, gets or sets the position within the current stream.
- ///
- ///
- /// The current position within the stream.
- ///
- /// The stream does not support seeking.
- public override long Position
- {
- get { return 0; }
- set { throw new NotSupportedException(); }
- }
-
- #endregion
-
private ObjectDisposedException CreateObjectDisposedException()
{
return new ObjectDisposedException(GetType().FullName);