Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve shell stream #1514

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/Renci.SshNet/IServiceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,44 @@ ShellStream CreateShellStream(ISession session,
IDictionary<TerminalModes, uint> terminalModeValues,
int bufferSize);

/// <summary>
/// Creates a shell stream.
/// </summary>
/// <param name="session">The SSH session.</param>
/// <param name="terminalName">The <c>TERM</c> environment variable.</param>
/// <param name="columns">The terminal width in columns.</param>
/// <param name="rows">The terminal width in rows.</param>
/// <param name="width">The terminal width in pixels.</param>
/// <param name="height">The terminal height in pixels.</param>
/// <param name="terminalModeValues">The terminal mode values.</param>
/// <param name="bufferSize">The size of the buffer.</param>
/// <param name="dataReceived">The DataReceived Handler.</param>
/// <param name="errorOccurred">The ErrorOccurred Handler.</param>
/// <returns>
/// The created <see cref="ShellStream"/> instance.
/// </returns>
/// <exception cref="SshConnectionException">Client is not connected.</exception>
/// <remarks>
/// <para>
/// The <c>TERM</c> environment variable contains an identifier for the text window's capabilities.
/// You can get a detailed list of these capabilities by using the ‘infocmp’ command.
/// </para>
/// <para>
/// The column/row dimensions override the pixel dimensions(when non-zero). Pixel dimensions refer
/// to the drawable area of the window.
/// </para>
/// </remarks>
ShellStream CreateShellStream(ISession session,
string terminalName,
uint columns,
uint rows,
uint width,
uint height,
IDictionary<TerminalModes, uint> terminalModeValues,
int bufferSize,
EventHandler<ShellDataEventArgs> dataReceived,
EventHandler<ExceptionEventArgs> errorOccurred);

/// <summary>
/// Creates a shell stream without allocating a pseudo terminal.
/// </summary>
Expand Down
32 changes: 32 additions & 0 deletions src/Renci.SshNet/ServiceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,38 @@ public ShellStream CreateShellStream(ISession session, string terminalName, uint
return new ShellStream(session, terminalName, columns, rows, width, height, terminalModeValues, bufferSize);
}

/// <summary>
/// Creates a shell stream.
/// </summary>
/// <param name="session">The SSH session.</param>
/// <param name="terminalName">The <c>TERM</c> environment variable.</param>
/// <param name="columns">The terminal width in columns.</param>
/// <param name="rows">The terminal width in rows.</param>
/// <param name="width">The terminal width in pixels.</param>
/// <param name="height">The terminal height in pixels.</param>
/// <param name="terminalModeValues">The terminal mode values.</param>
/// <param name="bufferSize">The size of the buffer.</param>
/// <param name="dataReceived">The DataReceived Handler.</param>
/// <param name="errorOccurred">The ErrorOccurred Handler.</param>
/// <returns>
/// The created <see cref="ShellStream"/> instance.
/// </returns>
/// <exception cref="SshConnectionException">Client is not connected.</exception>
/// <remarks>
/// <para>
/// The <c>TERM</c> environment variable contains an identifier for the text window's capabilities.
/// You can get a detailed list of these capabilities by using the ‘infocmp’ command.
/// </para>
/// <para>
/// The column/row dimensions override the pixel dimensions(when non-zero). Pixel dimensions refer
/// to the drawable area of the window.
/// </para>
/// </remarks>
public ShellStream CreateShellStream(ISession session, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary<TerminalModes, uint> terminalModeValues, int bufferSize, EventHandler<ShellDataEventArgs> dataReceived, EventHandler<ExceptionEventArgs> errorOccurred)
{
return new ShellStream(session, terminalName, columns, rows, width, height, terminalModeValues, bufferSize, dataReceived, errorOccurred);
}

/// <inheritdoc/>
public ShellStream CreateShellStreamNoTerminal(ISession session, int bufferSize)
{
Expand Down
38 changes: 38 additions & 0 deletions src/Renci.SshNet/ShellStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ private void AssertValid()
Debug.Assert(_readHead <= _readTail, $"Should have {nameof(_readHead)} <= {nameof(_readTail)} but have {_readHead.ToString(CultureInfo.InvariantCulture)} <= {_readTail.ToString(CultureInfo.InvariantCulture)}");
}

/// <summary>
/// Initializes a new instance of the <see cref="ShellStream"/> class.
/// </summary>
/// <param name="session">The SSH session.</param>
/// <param name="terminalName">The <c>TERM</c> environment variable.</param>
/// <param name="columns">The terminal width in columns.</param>
/// <param name="rows">The terminal width in rows.</param>
/// <param name="width">The terminal width in pixels.</param>
/// <param name="height">The terminal height in pixels.</param>
/// <param name="terminalModeValues">The terminal mode values.</param>
/// <param name="bufferSize">The size of the buffer.</param>
/// <param name="dataReceived">The DataReceived Handler.Please note that this event needs to be manually unsubscribed.</param>
/// <param name="errorOccurred">The ErrorOccurred Handler.Please note that this event needs to be manually unsubscribed.</param>
/// <exception cref="SshException">The channel could not be opened.</exception>
/// <exception cref="SshException">The pseudo-terminal request was not accepted by the server.</exception>
/// <exception cref="SshException">The request to start a shell was not accepted by the server.</exception>
internal ShellStream(ISession session, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary<TerminalModes, uint> terminalModeValues, int bufferSize, EventHandler<ShellDataEventArgs> dataReceived, EventHandler<ExceptionEventArgs> errorOccurred)
: this(session, terminalName, columns, rows, width, height, terminalModeValues, bufferSize)
{
DataReceived += dataReceived;
ErrorOccurred += errorOccurred;
}

/// <summary>
/// Initializes a new instance of the <see cref="ShellStream"/> class.
/// </summary>
Expand Down Expand Up @@ -872,6 +895,21 @@ public void WriteLine(string line)
Write(line + (_noTerminal ? "\n" : "\r"));
}

/// <summary>
/// Change the terminal size.
/// </summary>
/// <param name="columns">new columns of the terminal.</param>
/// <param name="rows">new rows of the terminal.</param>
/// <param name="width">new width of the terminal.</param>
/// <param name="height">new height of the terminal.</param>
/// <returns>
/// <see langword="true"/> if request was successful; otherwise <see langword="false"/>.
/// </returns>
public bool SendWindowChangeRequest(uint columns, uint rows, uint width, uint height)
{
return _channel.SendWindowChangeRequest(columns, rows, width, height);
}

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
Expand Down
33 changes: 33 additions & 0 deletions src/Renci.SshNet/SshClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,39 @@ public ShellStream CreateShellStream(string terminalName, uint columns, uint row
return ServiceFactory.CreateShellStream(Session, terminalName, columns, rows, width, height, terminalModeValues, bufferSize);
}

/// <summary>
/// Creates a shell stream.
/// </summary>
/// <param name="terminalName">The <c>TERM</c> environment variable.</param>
/// <param name="columns">The terminal width in columns.</param>
/// <param name="rows">The terminal width in rows.</param>
/// <param name="width">The terminal width in pixels.</param>
/// <param name="height">The terminal height in pixels.</param>
/// <param name="bufferSize">The size of the buffer.</param>
/// <param name="terminalModeValues">The terminal mode values.</param>
/// <param name="dataReceived">The DataReceived Handler.</param>
/// <param name="errorOccurred">The ErrorOccurred Handler.</param>
/// <returns>
/// The created <see cref="ShellStream"/> instance.
/// </returns>
/// <exception cref="SshConnectionException">Client is not connected.</exception>
/// <remarks>
/// <para>
/// The <c>TERM</c> environment variable contains an identifier for the text window's capabilities.
/// You can get a detailed list of these capabilities by using the ‘infocmp’ command.
/// </para>
/// <para>
/// The column/row dimensions override the pixel dimensions(when non-zero). Pixel dimensions refer
/// to the drawable area of the window.
/// </para>
/// </remarks>
public ShellStream CreateShellStream(string terminalName, uint columns, uint rows, uint width, uint height, int bufferSize, IDictionary<TerminalModes, uint>? terminalModeValues, EventHandler<ShellDataEventArgs> dataReceived, EventHandler<ExceptionEventArgs> errorOccurred)
{
EnsureSessionIsOpen();

return ServiceFactory.CreateShellStream(Session, terminalName, columns, rows, width, height, terminalModeValues, bufferSize, dataReceived, errorOccurred);
}

/// <inheritdoc />
public ShellStream CreateShellStreamNoTerminal(int bufferSize = -1)
{
Expand Down