PipeStream.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. namespace WinSCP
  6. {
  7. /// <summary>
  8. /// PipeStream is a thread-safe read/write data stream for use between two threads in a
  9. /// single-producer/single-consumer type problem.
  10. /// </summary>
  11. /// <version>2006/10/13 1.0</version>
  12. /// <remarks>Update on 2008/10/9 1.1 - uses Monitor instead of Manual Reset events for more elegant synchronicity.</remarks>
  13. /// <license>
  14. /// Copyright (c) 2006 James Kolpack (james dot kolpack at google mail)
  15. ///
  16. /// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  17. /// associated documentation files (the "Software"), to deal in the Software without restriction,
  18. /// including without limitation the rights to use, copy, modify, merge, publish, distribute,
  19. /// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  20. /// furnished to do so, subject to the following conditions:
  21. ///
  22. /// The above copyright notice and this permission notice shall be included in all copies or
  23. /// substantial portions of the Software.
  24. ///
  25. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  26. /// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  27. /// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. /// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
  29. /// OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  30. /// OTHER DEALINGS IN THE SOFTWARE.
  31. /// </license>
  32. internal class PipeStream : Stream
  33. {
  34. #region Private members
  35. /// <summary>
  36. /// Queue of bytes provides the datastructure for transmitting from an
  37. /// input stream to an output stream.
  38. /// </summary>
  39. /// <remarks>Possible more effecient ways to accomplish this.</remarks>
  40. private readonly Queue<byte> _buffer = new Queue<byte>();
  41. /// <summary>
  42. /// Indicates that the input stream has been flushed and that
  43. /// all remaining data should be written to the output stream.
  44. /// </summary>
  45. private bool _isFlushed;
  46. /// <summary>
  47. /// Indicates whether the current <see cref="PipeStream"/> is disposed.
  48. /// </summary>
  49. private bool _isDisposed;
  50. private bool _closedWrite;
  51. private long _position;
  52. #endregion
  53. #region Public properties
  54. /// <summary>
  55. /// Gets or sets the maximum number of bytes to store in the buffer.
  56. /// </summary>
  57. /// <value>The length of the max buffer.</value>
  58. public long MaxBufferLength { get; set; } = 200 * 1024 * 1024;
  59. public Action OnDispose { get; set; }
  60. #endregion
  61. #region Stream overide methods
  62. /// <summary>
  63. /// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.
  64. /// </summary>
  65. /// <exception cref="IOException">An I/O error occurs.</exception>
  66. /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception>
  67. /// <remarks>
  68. /// Once flushed, any subsequent read operations no longer block until requested bytes are available. Any write operation reactivates blocking
  69. /// reads.
  70. /// </remarks>
  71. public override void Flush()
  72. {
  73. CheckDisposed();
  74. _isFlushed = true;
  75. lock (_buffer)
  76. {
  77. // unblock read hereby allowing buffer to be partially filled
  78. Monitor.Pulse(_buffer);
  79. }
  80. }
  81. /// <summary>
  82. /// When overridden in a derived class, sets the position within the current stream.
  83. /// </summary>
  84. /// <returns>
  85. /// The new position within the current stream.
  86. /// </returns>
  87. /// <param name="offset">A byte offset relative to the origin parameter.</param>
  88. /// <param name="origin">A value of type <see cref="SeekOrigin"/> indicating the reference point used to obtain the new position.</param>
  89. /// <exception cref="NotSupportedException">The stream does not support seeking, such as if the stream is constructed from a pipe or console output.</exception>
  90. public override long Seek(long offset, SeekOrigin origin)
  91. {
  92. throw new NotSupportedException();
  93. }
  94. /// <summary>
  95. /// When overridden in a derived class, sets the length of the current stream.
  96. /// </summary>
  97. /// <param name="value">The desired length of the current stream in bytes.</param>
  98. /// <exception cref="NotSupportedException">The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output.</exception>
  99. public override void SetLength(long value)
  100. {
  101. throw new NotSupportedException();
  102. }
  103. ///<summary>
  104. ///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.
  105. ///</summary>
  106. ///<returns>
  107. ///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.
  108. ///</returns>
  109. ///<param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
  110. ///<param name="count">The maximum number of bytes to be read from the current stream.</param>
  111. ///<param name="buffer">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.</param>
  112. ///<exception cref="ArgumentException">The sum of offset and count is larger than the buffer length.</exception>
  113. ///<exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception>
  114. ///<exception cref="NotSupportedException">The stream does not support reading.</exception>
  115. ///<exception cref="ArgumentNullException"><paramref name="buffer"/> is <c>null</c>.</exception>
  116. ///<exception cref="IOException">An I/O error occurs.</exception>
  117. ///<exception cref="ArgumentOutOfRangeException">offset or count is negative.</exception>
  118. public override int Read(byte[] buffer, int offset, int count)
  119. {
  120. if (offset < 0)
  121. throw new NotSupportedException("Offset cnnot be negative");
  122. if (buffer == null)
  123. throw new ArgumentNullException("buffer");
  124. if (offset + count > buffer.Length)
  125. throw new ArgumentException("The sum of offset and count is greater than the buffer length.");
  126. if (offset < 0 || count < 0)
  127. throw new ArgumentOutOfRangeException("offset", "offset or count is negative.");
  128. CheckDisposed();
  129. if (count == 0)
  130. return 0;
  131. var readLength = 0;
  132. lock (_buffer)
  133. {
  134. while (!_isDisposed && !_closedWrite && !ReadAvailable(1))
  135. {
  136. Monitor.Wait(_buffer);
  137. }
  138. // return zero when the read is interrupted by a close/dispose of the stream
  139. if (_isDisposed)
  140. {
  141. return 0;
  142. }
  143. // fill the read buffer
  144. for (; readLength < count && _buffer.Count > 0; readLength++)
  145. {
  146. buffer[offset + readLength] = _buffer.Dequeue();
  147. }
  148. if (_closedWrite)
  149. {
  150. // Throw any pending exception asap, not only once the stream is closed.
  151. // Also releases the lock.
  152. Closed();
  153. }
  154. Monitor.Pulse(_buffer);
  155. _position += readLength;
  156. }
  157. return readLength;
  158. }
  159. /// <summary>
  160. /// Returns true if there are
  161. /// </summary>
  162. /// <param name="count">The count.</param>
  163. /// <returns><c>True</c> if data available; otherwise<c>false</c>.</returns>
  164. public bool ReadAvailable(int count)
  165. {
  166. CheckDisposed();
  167. var length = _buffer.Count;
  168. return (_isFlushed || length >= count);
  169. }
  170. ///<summary>
  171. ///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.
  172. ///</summary>
  173. ///<param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
  174. ///<param name="count">The number of bytes to be written to the current stream.</param>
  175. ///<param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
  176. ///<exception cref="IOException">An I/O error occurs.</exception>
  177. ///<exception cref="NotSupportedException">The stream does not support writing.</exception>
  178. ///<exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception>
  179. ///<exception cref="ArgumentNullException"><paramref name="buffer"/> is <c>null</c>.</exception>
  180. ///<exception cref="ArgumentException">The sum of offset and count is greater than the buffer length.</exception>
  181. ///<exception cref="ArgumentOutOfRangeException">offset or count is negative.</exception>
  182. public override void Write(byte[] buffer, int offset, int count)
  183. {
  184. throw new NotSupportedException();
  185. }
  186. public void WriteInternal(byte[] buffer, int offset, int count)
  187. {
  188. if (buffer == null)
  189. throw new ArgumentNullException("buffer");
  190. if (offset + count > buffer.Length)
  191. throw new ArgumentException("The sum of offset and count is greater than the buffer length.");
  192. if (offset < 0 || count < 0)
  193. throw new ArgumentOutOfRangeException("offset", "offset or count is negative.");
  194. if (count == 0)
  195. return;
  196. if (_closedWrite)
  197. throw new InvalidOperationException("Stream closed for writes");
  198. lock (_buffer)
  199. {
  200. // wait until the buffer isn't full
  201. while (_buffer.Count >= MaxBufferLength)
  202. Monitor.Wait(_buffer);
  203. if (!_isDisposed)
  204. {
  205. _isFlushed = false; // if it were flushed before, it soon will not be.
  206. // queue up the buffer data
  207. for (var i = offset; i < offset + count; i++)
  208. {
  209. _buffer.Enqueue(buffer[i]);
  210. }
  211. Monitor.Pulse(_buffer); // signal that write has occurred
  212. }
  213. }
  214. }
  215. /// <summary>
  216. /// Releases the unmanaged resources used by the Stream and optionally releases the managed resources.
  217. /// </summary>
  218. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  219. /// <remarks>
  220. /// Disposing a <see cref="PipeStream"/> will interrupt blocking read and write operations.
  221. /// </remarks>
  222. protected override void Dispose(bool disposing)
  223. {
  224. base.Dispose(disposing);
  225. if (!_isDisposed)
  226. {
  227. lock (_buffer)
  228. {
  229. _isDisposed = true;
  230. _buffer.Clear();
  231. Monitor.Pulse(_buffer);
  232. }
  233. Closed();
  234. }
  235. }
  236. ///<summary>
  237. ///When overridden in a derived class, gets a value indicating whether the current stream supports reading.
  238. ///</summary>
  239. ///<returns>
  240. ///true if the stream supports reading; otherwise, false.
  241. ///</returns>
  242. public override bool CanRead
  243. {
  244. get { return !_isDisposed; }
  245. }
  246. /// <summary>
  247. /// When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
  248. /// </summary>
  249. /// <returns>
  250. /// <c>true</c> if the stream supports seeking; otherwise, <c>false</c>.
  251. ///</returns>
  252. public override bool CanSeek
  253. {
  254. get { return false; }
  255. }
  256. /// <summary>
  257. /// When overridden in a derived class, gets a value indicating whether the current stream supports writing.
  258. /// </summary>
  259. /// <returns>
  260. /// <c>true</c> if the stream supports writing; otherwise, <c>false</c>.
  261. /// </returns>
  262. public override bool CanWrite
  263. {
  264. get { return false; }
  265. }
  266. /// <summary>
  267. /// When overridden in a derived class, gets the length in bytes of the stream.
  268. /// </summary>
  269. /// <returns>
  270. /// A long value representing the length of the stream in bytes.
  271. /// </returns>
  272. /// <exception cref="NotSupportedException">A class derived from Stream does not support seeking.</exception>
  273. /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception>
  274. public override long Length
  275. {
  276. get
  277. {
  278. throw new NotSupportedException();
  279. }
  280. }
  281. /// <summary>
  282. /// When overridden in a derived class, gets or sets the position within the current stream.
  283. /// </summary>
  284. /// <returns>
  285. /// The current position within the stream.
  286. /// </returns>
  287. /// <exception cref="NotSupportedException">The stream does not support seeking.</exception>
  288. public override long Position
  289. {
  290. get
  291. {
  292. lock (_buffer)
  293. {
  294. return _position;
  295. }
  296. }
  297. set
  298. {
  299. throw new NotSupportedException();
  300. }
  301. }
  302. #endregion
  303. public void CloseWrite()
  304. {
  305. lock (_buffer)
  306. {
  307. // This can be called while the stream is disposed, when the download has finishes
  308. // (TransferOut with Len=0 is processed) only after Close/Dispose is called
  309. if (!_closedWrite && !_isDisposed)
  310. {
  311. _closedWrite = true;
  312. Monitor.Pulse(_buffer);
  313. }
  314. }
  315. }
  316. private void CheckDisposed()
  317. {
  318. if (_isDisposed)
  319. {
  320. throw new ObjectDisposedException(GetType().FullName);
  321. }
  322. }
  323. private void Closed()
  324. {
  325. Action onDispose = OnDispose;
  326. OnDispose = null;
  327. onDispose?.Invoke();
  328. }
  329. }
  330. }