Add PipeStream class to handle command output

Improve Shell input and output streams
This commit is contained in:
olegkap_cp
2011-05-09 14:19:08 +00:00
parent 20fb009c84
commit 10f2ff2624
6 changed files with 422 additions and 40 deletions
@@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshClient.Common;
using Renci.SshClient.Tests.Properties;
using System.IO;
namespace Renci.SshClient.Tests.SshClientTests
{
@@ -164,7 +165,8 @@ namespace Renci.SshClient.Tests.SshClientTests
client.Connect();
var cmd = client.CreateCommand("echo 12345; echo 654321 >&2");
cmd.Execute();
var extendedData = Encoding.ASCII.GetString(cmd.ExtendedOutputStream.ToArray());
//var extendedData = Encoding.ASCII.GetString(cmd.ExtendedOutputStream.ToArray());
var extendedData = new StreamReader(cmd.ExtendedOutputStream, Encoding.ASCII).ReadToEnd();
client.Disconnect();
Assert.AreEqual("12345\n", cmd.Result);
@@ -324,10 +326,9 @@ namespace Renci.SshClient.Tests.SshClientTests
{
var testValue = Guid.NewGuid().ToString();
var command = string.Format("echo {0}", testValue);
//var command = string.Format("echo {0};sleep 2s", testValue);
var cmd = s.CreateCommand(command);
var result = cmd.Execute();
result = result.Substring(0, result.Length - 1); // Remove \n chararacter returned by command
result = result.Substring(0, result.Length - 1); // Remove \n character returned by command
return result.Equals(testValue);
}
@@ -0,0 +1,322 @@
namespace Renci.SshClient.Common
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
/// <summary>
/// PipeStream is a thread-safe read/write data stream for use between two threads in a
/// single-producer/single-consumer type problem.
/// </summary>
/// <version>2006/10/13 1.0</version>
/// <remarks>Update on 2008/10/9 1.1 - uses Monitor instead of Manual Reset events for more elegant synchronicity.</remarks>
/// <license>
/// 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.
/// </license>
public class PipeStream : Stream
{
#region Private members
/// <summary>
/// Queue of bytes provides the datastructure for transmitting from an
/// input stream to an output stream.
/// </summary>
/// <remarks>Possible more effecient ways to accomplish this.</remarks>
private readonly Queue<byte> _buffer = new Queue<byte>();
/// <summary>
/// Indicates that the input stream has been flushed and that
/// all remaining data should be written to the output stream.
/// </summary>
private bool _isFlushed;
/// <summary>
/// Maximum number of bytes to store in the buffer.
/// </summary>
private long _maxBufferLength = 200 * 1024 * 1024;
/// <summary>
/// Setting this to true will cause Read() to block if it appears
/// that it will run out of data.
/// </summary>
private bool _canBlockLastRead;
#endregion
#region Public properties
/// <summary>
/// Gets or sets the maximum number of bytes to store in the buffer.
/// </summary>
/// <value>The length of the max buffer.</value>
public long MaxBufferLength
{
get { return this._maxBufferLength; }
set { this._maxBufferLength = value; }
}
/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// Setting to true will remove the possibility of ending a stream reader prematurely.
/// </remarks>
/// <value>
/// <c>true</c> if block last read method before the buffer is empty; otherwise, <c>false</c>.
/// </value>
public bool BlockLastReadBuffer
{
get { return this._canBlockLastRead; }
set
{
this._canBlockLastRead = value;
// when turning off the block last read, signal Read() that it may now read the rest of the buffer.
if (!this._canBlockLastRead)
lock (this._buffer)
Monitor.Pulse(this._buffer);
}
}
#endregion
#region Stream overide methods
///<summary>
///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///</summary>
///<filterpriority>2</filterpriority>
public new void Dispose()
{
// clear the internal buffer
_buffer.Clear();
}
///<summary>
///When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.
///</summary>
///
///<exception cref="T:System.IO.IOException">An I/O error occurs. </exception><filterpriority>2</filterpriority>
public override void Flush()
{
this._isFlushed = true;
lock (this._buffer)
Monitor.Pulse(this._buffer);
}
///<summary>
///When overridden in a derived class, sets the position within the current stream.
///</summary>
///<returns>
///The new position within the current stream.
///</returns>
///<param name="offset">A byte offset relative to the origin parameter. </param>
///<param name="origin">A value of type <see cref="T:System.IO.SeekOrigin"></see> indicating the reference point used to obtain the new position. </param>
///<exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
///<exception cref="T:System.NotSupportedException">The stream does not support seeking, such as if the stream is constructed from a pipe or console output. </exception>
///<exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception><filterpriority>1</filterpriority>
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
///<summary>
///When overridden in a derived class, sets the length of the current stream.
///</summary>
///<param name="value">The desired length of the current stream in bytes. </param>
///<exception cref="T:System.NotSupportedException">The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. </exception>
///<exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
///<exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception><filterpriority>2</filterpriority>
public override void SetLength(long value)
{
throw new NotImplementedException();
}
///<summary>
///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.
///</summary>
///<returns>
///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.
///</returns>
///<param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream. </param>
///<param name="count">The maximum number of bytes to be read from the current stream. </param>
///<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>
///<exception cref="T:System.ArgumentException">The sum of offset and count is larger than the buffer length. </exception>
///<exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
///<exception cref="T:System.NotSupportedException">The stream does not support reading. </exception>
///<exception cref="T:System.ArgumentNullException">buffer is null. </exception>
///<exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
///<exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception><filterpriority>1</filterpriority>
public override int Read(byte[] buffer, int offset, int count)
{
if (offset != 0)
throw new NotImplementedException("Offsets with value of non-zero are not supported");
if (buffer == null)
throw new ArgumentException("Buffer is null");
if (offset + count > buffer.Length)
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("count({0}) > mMaxBufferLength({1})", count, _maxBufferLength));
if (count == 0)
return 0;
int readLength = 0;
lock (this._buffer)
{
while (!this.ReadAvailable(count))
Monitor.Wait(this._buffer);
// fill the read buffer
for (; readLength < count && Length > 0; readLength++)
{
buffer[readLength] = this._buffer.Dequeue();
}
Monitor.Pulse(this._buffer);
}
return readLength;
}
/// <summary>
/// Returns true if there are
/// </summary>
/// <param name="count"></param>
/// <returns></returns>
private bool ReadAvailable(int count)
{
return (this.Length >= count || this._isFlushed) &&
(this.Length >= (count + 1) || !this.BlockLastReadBuffer);
}
///<summary>
///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.
///</summary>
///<param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream. </param>
///<param name="count">The number of bytes to be written to the current stream. </param>
///<param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream. </param>
///<exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
///<exception cref="T:System.NotSupportedException">The stream does not support writing. </exception>
///<exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
///<exception cref="T:System.ArgumentNullException">buffer is null. </exception>
///<exception cref="T:System.ArgumentException">The sum of offset and count is greater than the buffer length. </exception>
///<exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception><filterpriority>1</filterpriority>
public override void Write(byte[] buffer, int offset, int count)
{
if (buffer == null)
throw new ArgumentException("Buffer is null");
if (offset + count > buffer.Length)
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 (count == 0)
return;
lock (this._buffer)
{
// wait until the buffer isn't full
while (this.Length >= this._maxBufferLength)
Monitor.Wait(this._buffer);
this._isFlushed = false; // if it were flushed before, it soon will not be.
// queue up the buffer data
for (int i = offset; i < offset + count; i++)
{
this._buffer.Enqueue(buffer[i]);
}
Monitor.Pulse(this._buffer); // signal that write has occurred
}
}
///<summary>
///When overridden in a derived class, gets a value indicating whether the current stream supports reading.
///</summary>
///<returns>
///true if the stream supports reading; otherwise, false.
///</returns>
///<filterpriority>1</filterpriority>
public override bool CanRead
{
get { return true; }
}
///<summary>
///When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
///</summary>
///<returns>
///true if the stream supports seeking; otherwise, false.
///</returns>
///<filterpriority>1</filterpriority>
public override bool CanSeek
{
get { return false; }
}
///<summary>
///When overridden in a derived class, gets a value indicating whether the current stream supports writing.
///</summary>
///<returns>
///true if the stream supports writing; otherwise, false.
///</returns>
///<filterpriority>1</filterpriority>
public override bool CanWrite
{
get { return true; }
}
///<summary>
///When overridden in a derived class, gets the length in bytes of the stream.
///</summary>
///<returns>
///A long value representing the length of the stream in bytes.
///</returns>
///
///<exception cref="T:System.NotSupportedException">A class derived from Stream does not support seeking. </exception>
///<exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception><filterpriority>1</filterpriority>
public override long Length
{
get { return this._buffer.Count; }
}
///<summary>
///When overridden in a derived class, gets or sets the position within the current stream.
///</summary>
///<returns>
///The current position within the stream.
///</returns>
///<exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
///<exception cref="T:System.NotSupportedException">The stream does not support seeking. </exception>
///<exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception><filterpriority>1</filterpriority>
public override long Position
{
get { return 0; }
set { throw new NotImplementedException(); }
}
#endregion
}
}
@@ -72,6 +72,7 @@
<Compile Include="Common\ChannelEventArgs.cs" />
<Compile Include="Common\ChannelOpenFailedEventArgs.cs" />
<Compile Include="Common\ChannelRequestEventArgs.cs" />
<Compile Include="Common\PipeStream.cs" />
<Compile Include="Common\PortForwardEventArgs.cs" />
<Compile Include="Common\SshAuthenticationException.cs" />
<Compile Include="Common\SshConnectionException.cs" />
+28 -28
View File
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Diagnostics;
using System.IO;
using System.Text;
@@ -19,11 +20,7 @@ namespace Renci.SshClient
private ChannelSession _channel;
private Stream _channelInput;
private TextWriter _channelOutput;
private TextWriter _channelExtendedOutput;
private Stream _input;
private string _terminalName;
@@ -39,7 +36,11 @@ namespace Renci.SshClient
private Task _dataReaderTask;
private Encoding _encoding;
private Stream _outputStream;
private Stream _extendedOutputStream;
private int _bufferSize;
/// <summary>
/// Gets a value indicating whether this shell is started.
@@ -87,19 +88,20 @@ namespace Renci.SshClient
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="terminalMode">The terminal mode.</param>
internal Shell(Session session, Stream input, TextWriter output, TextWriter extendedOutput, string terminalName, uint columns, uint rows, uint width, uint height, string terminalMode)
/// <param name="bufferSize">Size of the buffer for output stream.</param>
internal Shell(Session session, Stream input, Stream output, Stream extendedOutput, string terminalName, uint columns, uint rows, uint width, uint height, string terminalMode, int bufferSize)
{
this._session = session;
this._channelInput = input;
this._channelOutput = output;
this._channelExtendedOutput = extendedOutput;
this._input = input;
this._outputStream = output;
this._extendedOutputStream = extendedOutput;
this._terminalName = terminalName;
this._columns = columns;
this._rows = rows;
this._width = width;
this._height = height;
this._terminalMode = terminalMode;
this._encoding = Encoding.ASCII;
this._bufferSize = bufferSize;
}
/// <summary>
@@ -133,21 +135,19 @@ namespace Renci.SshClient
{
try
{
var buffer = new byte[this._bufferSize];
while (this._channel.IsOpen)
{
var ch = this._channelInput.ReadByte();
var read = this._input.Read(buffer, 0, buffer.Length);
if (ch > 0)
{
Debug.WriteLine(ch);
this._session.SendMessage(new ChannelDataMessage(this._channel.RemoteChannelNumber, new byte[] { (byte)ch }));
}
else
{
// Wait for data become available
Thread.Sleep(30);
if (read > 0)
{
this._session.SendMessage(new ChannelDataMessage(this._channel.RemoteChannelNumber, buffer.Take(read).ToArray()));
}
// Wait for data become available
Thread.Sleep(30);
}
}
catch (Exception exp)
@@ -162,7 +162,6 @@ namespace Renci.SshClient
{
this.Started(this, new EventArgs());
}
}
/// <summary>
@@ -183,7 +182,7 @@ namespace Renci.SshClient
}
this._channel.Close();
this._channelInput.Close();
this._input.Close();
this._dataReaderTask.Wait();
@@ -222,17 +221,18 @@ namespace Renci.SshClient
private void Channel_ExtendedDataReceived(object sender, Common.ChannelDataEventArgs e)
{
if (this._channelExtendedOutput != null)
if (this._extendedOutputStream != null)
{
this._channelExtendedOutput.Write(e.Data);
this._extendedOutputStream.Write(e.Data, 0, e.Data.Length);
}
}
private void Channel_DataReceived(object sender, Common.ChannelDataEventArgs e)
{
if (this._channelOutput != null)
if (this._outputStream != null)
{
this._channelOutput.Write(this._channelOutput.Encoding.GetString(e.Data));
//this._channelOutput.Write(this._channelOutput.Encoding.GetString(e.Data));
this._extendedOutputStream.Write(e.Data, 0, e.Data.Length);
}
}
+2 -2
View File
@@ -202,12 +202,12 @@ namespace Renci.SshClient
/// <param name="height">The height.</param>
/// <param name="terminalMode">The terminal mode.</param>
/// <returns></returns>
public Shell CreateShell(Stream input, TextWriter output, TextWriter extendedOutput, string terminalName, uint columns, uint rows, uint width, uint height, string terminalMode)
public Shell CreateShell(Stream input, Stream output, Stream extendedOutput, string terminalName, uint columns, uint rows, uint width, uint height, string terminalMode, int bufferSize = 1024)
{
// Ensure that connection is established.
this.EnsureConnection();
return new Shell(this.Session, input, output, extendedOutput, terminalName, columns, rows, width, height, terminalMode);
return new Shell(this.Session, input, output, extendedOutput, terminalName, columns, rows, width, height, terminalMode, bufferSize);
}
}
}
+65 -7
View File
@@ -17,6 +17,14 @@ namespace Renci.SshClient
/// </summary>
public class SshCommand : IDisposable
{
//private StreamReader _outputSteamReader;
//private StreamReader _extendedOutputSteamReader;
//private StreamWriter _outputSteamWriter;
//private StreamWriter _extendedOutputSteamWriter;
private Encoding _encoding;
private Session _session;
@@ -54,13 +62,14 @@ namespace Renci.SshClient
/// <summary>
/// Gets the output stream.
/// </summary>
public MemoryStream OutputStream { get; private set; }
public Stream OutputStream { get; private set; }
/// <summary>
/// Gets the extended output stream.
/// </summary>
public MemoryStream ExtendedOutputStream { get; private set; }
public Stream ExtendedOutputStream { get; private set; }
private StringBuilder _result;
/// <summary>
/// Gets the command execution result.
/// </summary>
@@ -68,10 +77,24 @@ namespace Renci.SshClient
{
get
{
return this._encoding.GetString(this.OutputStream.ToArray());
if (this._result == null)
{
this._result = new StringBuilder();
}
if (this.OutputStream.Length > 0)
{
using (var sr = new StreamReader(this.OutputStream, this._encoding))
{
this._result.Append(sr.ReadToEnd());
}
}
return this._result.ToString();
}
}
private StringBuilder _error;
/// <summary>
/// Gets the command execution error.
/// </summary>
@@ -80,7 +103,22 @@ namespace Renci.SshClient
get
{
if (this._hasError)
return this._encoding.GetString(this.ExtendedOutputStream.ToArray());
{
if (this._error == null)
{
this._error = new StringBuilder();
}
if (this.ExtendedOutputStream.Length > 0)
{
using (var sr = new StreamReader(this.ExtendedOutputStream, this._encoding))
{
this._error.Append(sr.ReadToEnd());
}
}
return this._error.ToString();
}
else
return string.Empty;
}
@@ -197,6 +235,15 @@ namespace Renci.SshClient
return this.EndExecute(this.BeginExecute(null, null));
}
public void Cancel()
{
if (this._channel != null && this._channel.IsOpen)
{
this._session.SendMessage(new ChannelEofMessage(this._channel.RemoteChannelNumber));
this._session.SendMessage(new ChannelCloseMessage(this._channel.RemoteChannelNumber));
}
}
/// <summary>
/// Executes the specified command text.
/// </summary>
@@ -217,8 +264,13 @@ namespace Renci.SshClient
this._channel.ExtendedDataReceived += Channel_ExtendedDataReceived;
this._channel.RequestReceived += Channel_RequestReceived;
this._channel.Closed += Channel_Closed;
this.OutputStream = new MemoryStream();
this.ExtendedOutputStream = new MemoryStream();
this.OutputStream = new PipeStream();
this.ExtendedOutputStream = new PipeStream();
//this._outputSteamReader = new StreamReader(this.OutputStream);
//this._extendedOutputSteamReader = new StreamReader(this.ExtendedOutputStream);
//this._outputSteamWriter = new StreamWriter(this.OutputStream);
//this._extendedOutputSteamWriter = new StreamWriter(this.ExtendedOutputStream);
}
private void Session_Disconnected(object sender, EventArgs e)
@@ -236,6 +288,11 @@ namespace Renci.SshClient
}
private void Channel_Closed(object sender, Common.ChannelEventArgs e)
{
Cancel1();
}
private void Cancel1()
{
if (this.OutputStream != null)
{
@@ -294,6 +351,7 @@ namespace Renci.SshClient
if (this.OutputStream != null)
{
this.OutputStream.Write(e.Data, 0, e.Data.Length);
//this._outputSteamWriter.Write(this._encoding.GetString(e.Data, 0, e.Data.Length));
this.OutputStream.Flush();
}
@@ -391,7 +449,7 @@ namespace Renci.SshClient
this._channel = null;
}
}
// Note disposing has been done.
this._isDisposed = true;
}