| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Text; |
| | | 5 | | using System.Text.RegularExpressions; |
| | | 6 | | using System.Threading; |
| | | 7 | | |
| | | 8 | | using Renci.SshNet.Abstractions; |
| | | 9 | | using Renci.SshNet.Channels; |
| | | 10 | | using Renci.SshNet.Common; |
| | | 11 | | |
| | | 12 | | namespace Renci.SshNet |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Contains operation for working with SSH Shell. |
| | | 16 | | /// </summary> |
| | | 17 | | public class ShellStream : Stream |
| | | 18 | | { |
| | | 19 | | private const string CrLf = "\r\n"; |
| | | 20 | | |
| | | 21 | | private readonly ISession _session; |
| | | 22 | | private readonly Encoding _encoding; |
| | | 23 | | private readonly int _bufferSize; |
| | | 24 | | private readonly Queue<byte> _incoming; |
| | | 25 | | private readonly Queue<byte> _outgoing; |
| | | 26 | | private IChannelSession _channel; |
| | 123 | 27 | | private AutoResetEvent _dataReceived = new AutoResetEvent(initialState: false); |
| | | 28 | | private bool _isDisposed; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Occurs when data was received. |
| | | 32 | | /// </summary> |
| | | 33 | | public event EventHandler<ShellDataEventArgs> DataReceived; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Occurs when an error occurred. |
| | | 37 | | /// </summary> |
| | | 38 | | public event EventHandler<ExceptionEventArgs> ErrorOccurred; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets a value indicating whether data is available on the <see cref="ShellStream"/> to be read. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <value> |
| | | 44 | | /// <see langword="true"/> if data is available to be read; otherwise, <see langword="false"/>. |
| | | 45 | | /// </value> |
| | | 46 | | public bool DataAvailable |
| | | 47 | | { |
| | | 48 | | get |
| | 0 | 49 | | { |
| | 0 | 50 | | lock (_incoming) |
| | 0 | 51 | | { |
| | 0 | 52 | | return _incoming.Count > 0; |
| | | 53 | | } |
| | 0 | 54 | | } |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets the number of bytes that will be written to the internal buffer. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <value> |
| | | 61 | | /// The number of bytes that will be written to the internal buffer. |
| | | 62 | | /// </value> |
| | | 63 | | internal int BufferSize |
| | | 64 | | { |
| | 9 | 65 | | get { return _bufferSize; } |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Initializes a new instance of the <see cref="ShellStream"/> class. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="session">The SSH session.</param> |
| | | 72 | | /// <param name="terminalName">The <c>TERM</c> environment variable.</param> |
| | | 73 | | /// <param name="columns">The terminal width in columns.</param> |
| | | 74 | | /// <param name="rows">The terminal width in rows.</param> |
| | | 75 | | /// <param name="width">The terminal width in pixels.</param> |
| | | 76 | | /// <param name="height">The terminal height in pixels.</param> |
| | | 77 | | /// <param name="terminalModeValues">The terminal mode values.</param> |
| | | 78 | | /// <param name="bufferSize">The size of the buffer.</param> |
| | | 79 | | /// <exception cref="SshException">The channel could not be opened.</exception> |
| | | 80 | | /// <exception cref="SshException">The pseudo-terminal request was not accepted by the server.</exception> |
| | | 81 | | /// <exception cref="SshException">The request to start a shell was not accepted by the server.</exception> |
| | 123 | 82 | | internal ShellStream(ISession session, string terminalName, uint columns, uint rows, uint width, uint height, ID |
| | 123 | 83 | | { |
| | 123 | 84 | | _encoding = session.ConnectionInfo.Encoding; |
| | 123 | 85 | | _session = session; |
| | 123 | 86 | | _bufferSize = bufferSize; |
| | 123 | 87 | | _incoming = new Queue<byte>(); |
| | 123 | 88 | | _outgoing = new Queue<byte>(); |
| | | 89 | | |
| | 123 | 90 | | _channel = _session.CreateChannelSession(); |
| | 123 | 91 | | _channel.DataReceived += Channel_DataReceived; |
| | 123 | 92 | | _channel.Closed += Channel_Closed; |
| | 123 | 93 | | _session.Disconnected += Session_Disconnected; |
| | 123 | 94 | | _session.ErrorOccured += Session_ErrorOccured; |
| | | 95 | | |
| | | 96 | | try |
| | 123 | 97 | | { |
| | 123 | 98 | | _channel.Open(); |
| | | 99 | | |
| | 117 | 100 | | if (!_channel.SendPseudoTerminalRequest(terminalName, columns, rows, width, height, terminalModeValues)) |
| | 6 | 101 | | { |
| | 6 | 102 | | throw new SshException("The pseudo-terminal request was not accepted by the server. Consult the serv |
| | | 103 | | } |
| | | 104 | | |
| | 105 | 105 | | if (!_channel.SendShellRequest()) |
| | 6 | 106 | | { |
| | 6 | 107 | | throw new SshException("The request to start a shell was not accepted by the server. Consult the ser |
| | | 108 | | } |
| | 93 | 109 | | } |
| | 30 | 110 | | catch |
| | 30 | 111 | | { |
| | 30 | 112 | | UnsubscribeFromSessionEvents(session); |
| | 30 | 113 | | _channel.Dispose(); |
| | 30 | 114 | | throw; |
| | | 115 | | } |
| | 93 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Gets a value indicating whether the current stream supports reading. |
| | | 120 | | /// </summary> |
| | | 121 | | /// <returns> |
| | | 122 | | /// <see langword="true"/> if the stream supports reading; otherwise, <see langword="false"/>. |
| | | 123 | | /// </returns> |
| | | 124 | | public override bool CanRead |
| | | 125 | | { |
| | 0 | 126 | | get { return true; } |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Gets a value indicating whether the current stream supports seeking. |
| | | 131 | | /// </summary> |
| | | 132 | | /// <returns> |
| | | 133 | | /// <see langword="true"/> if the stream supports seeking; otherwise, <see langword="false"/>. |
| | | 134 | | /// </returns> |
| | | 135 | | public override bool CanSeek |
| | | 136 | | { |
| | 0 | 137 | | get { return false; } |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Gets a value indicating whether the current stream supports writing. |
| | | 142 | | /// </summary> |
| | | 143 | | /// <returns> |
| | | 144 | | /// <see langword="true"/> if the stream supports writing; otherwise, <see langword="false"/>. |
| | | 145 | | /// </returns> |
| | | 146 | | public override bool CanWrite |
| | | 147 | | { |
| | 0 | 148 | | get { return true; } |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Clears all buffers for this stream and causes any buffered data to be written to the underlying device. |
| | | 153 | | /// </summary> |
| | | 154 | | /// <exception cref="IOException">An I/O error occurs.</exception> |
| | | 155 | | /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception> |
| | | 156 | | public override void Flush() |
| | 53 | 157 | | { |
| | | 158 | | #if NET7_0_OR_GREATER |
| | 36 | 159 | | ObjectDisposedException.ThrowIf(_channel is null, this); |
| | | 160 | | #else |
| | 17 | 161 | | if (_channel is null) |
| | 0 | 162 | | { |
| | 0 | 163 | | throw new ObjectDisposedException(GetType().FullName); |
| | | 164 | | } |
| | | 165 | | #endif // NET7_0_OR_GREATER |
| | | 166 | | |
| | 53 | 167 | | if (_outgoing.Count > 0) |
| | 48 | 168 | | { |
| | 48 | 169 | | _channel.SendData(_outgoing.ToArray()); |
| | 48 | 170 | | _outgoing.Clear(); |
| | 48 | 171 | | } |
| | 53 | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Gets the length in bytes of the stream. |
| | | 176 | | /// </summary> |
| | | 177 | | /// <returns>A long value representing the length of the stream in bytes.</returns> |
| | | 178 | | /// <exception cref="NotSupportedException">A class derived from Stream does not support seeking.</exception> |
| | | 179 | | /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception> |
| | | 180 | | public override long Length |
| | | 181 | | { |
| | | 182 | | get |
| | 0 | 183 | | { |
| | 0 | 184 | | lock (_incoming) |
| | 0 | 185 | | { |
| | 0 | 186 | | return _incoming.Count; |
| | | 187 | | } |
| | 0 | 188 | | } |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | /// <summary> |
| | | 192 | | /// Gets or sets the position within the current stream. |
| | | 193 | | /// </summary> |
| | | 194 | | /// <returns> |
| | | 195 | | /// The current position within the stream. |
| | | 196 | | /// </returns> |
| | | 197 | | /// <exception cref="IOException">An I/O error occurs.</exception> |
| | | 198 | | /// <exception cref="NotSupportedException">The stream does not support seeking.</exception> |
| | | 199 | | /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception> |
| | | 200 | | public override long Position |
| | | 201 | | { |
| | 0 | 202 | | get { return 0; } |
| | 0 | 203 | | set { throw new NotSupportedException(); } |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | /// <summary> |
| | | 207 | | /// This method is not supported. |
| | | 208 | | /// </summary> |
| | | 209 | | /// <param name="offset">A byte offset relative to the <paramref name="origin"/> parameter.</param> |
| | | 210 | | /// <param name="origin">A value of type <see cref="SeekOrigin"/> indicating the reference point used to obtain |
| | | 211 | | /// <returns> |
| | | 212 | | /// The new position within the current stream. |
| | | 213 | | /// </returns> |
| | | 214 | | /// <exception cref="IOException">An I/O error occurs.</exception> |
| | | 215 | | /// <exception cref="NotSupportedException">The stream does not support seeking, such as if the stream is constr |
| | | 216 | | /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception> |
| | | 217 | | public override long Seek(long offset, SeekOrigin origin) |
| | 0 | 218 | | { |
| | 0 | 219 | | throw new NotSupportedException(); |
| | | 220 | | } |
| | | 221 | | |
| | | 222 | | /// <summary> |
| | | 223 | | /// This method is not supported. |
| | | 224 | | /// </summary> |
| | | 225 | | /// <param name="value">The desired length of the current stream in bytes.</param> |
| | | 226 | | /// <exception cref="IOException">An I/O error occurs.</exception> |
| | | 227 | | /// <exception cref="NotSupportedException">The stream does not support both writing and seeking, such as if the |
| | | 228 | | /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception> |
| | | 229 | | public override void SetLength(long value) |
| | 0 | 230 | | { |
| | 0 | 231 | | throw new NotSupportedException(); |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | /// <summary> |
| | | 235 | | /// Expects the specified expression and performs action when one is found. |
| | | 236 | | /// </summary> |
| | | 237 | | /// <param name="expectActions">The expected expressions and actions to perform.</param> |
| | | 238 | | public void Expect(params ExpectAction[] expectActions) |
| | 0 | 239 | | { |
| | 0 | 240 | | Expect(TimeSpan.Zero, expectActions); |
| | 0 | 241 | | } |
| | | 242 | | |
| | | 243 | | /// <summary> |
| | | 244 | | /// Expects the specified expression and performs action when one is found. |
| | | 245 | | /// </summary> |
| | | 246 | | /// <param name="timeout">Time to wait for input.</param> |
| | | 247 | | /// <param name="expectActions">The expected expressions and actions to perform, if the specified time elapsed a |
| | | 248 | | public void Expect(TimeSpan timeout, params ExpectAction[] expectActions) |
| | 0 | 249 | | { |
| | 0 | 250 | | var expectedFound = false; |
| | 0 | 251 | | var text = string.Empty; |
| | | 252 | | |
| | | 253 | | do |
| | 0 | 254 | | { |
| | 0 | 255 | | lock (_incoming) |
| | 0 | 256 | | { |
| | 0 | 257 | | if (_incoming.Count > 0) |
| | 0 | 258 | | { |
| | 0 | 259 | | text = _encoding.GetString(_incoming.ToArray(), 0, _incoming.Count); |
| | 0 | 260 | | } |
| | | 261 | | |
| | 0 | 262 | | if (text.Length > 0) |
| | 0 | 263 | | { |
| | 0 | 264 | | foreach (var expectAction in expectActions) |
| | 0 | 265 | | { |
| | 0 | 266 | | var match = expectAction.Expect.Match(text); |
| | | 267 | | |
| | 0 | 268 | | if (match.Success) |
| | 0 | 269 | | { |
| | 0 | 270 | | var result = text.Substring(0, match.Index + match.Length); |
| | | 271 | | |
| | 0 | 272 | | for (var i = 0; i < match.Index + match.Length && _incoming.Count > 0; i++) |
| | 0 | 273 | | { |
| | | 274 | | // Remove processed items from the queue |
| | 0 | 275 | | _ = _incoming.Dequeue(); |
| | 0 | 276 | | } |
| | | 277 | | |
| | 0 | 278 | | expectAction.Action(result); |
| | 0 | 279 | | expectedFound = true; |
| | 0 | 280 | | } |
| | 0 | 281 | | } |
| | 0 | 282 | | } |
| | 0 | 283 | | } |
| | | 284 | | |
| | 0 | 285 | | if (!expectedFound) |
| | 0 | 286 | | { |
| | 0 | 287 | | if (timeout.Ticks > 0) |
| | 0 | 288 | | { |
| | 0 | 289 | | if (!_dataReceived.WaitOne(timeout)) |
| | 0 | 290 | | { |
| | 0 | 291 | | return; |
| | | 292 | | } |
| | 0 | 293 | | } |
| | | 294 | | else |
| | 0 | 295 | | { |
| | 0 | 296 | | _ = _dataReceived.WaitOne(); |
| | 0 | 297 | | } |
| | 0 | 298 | | } |
| | 0 | 299 | | } |
| | 0 | 300 | | while (!expectedFound); |
| | 0 | 301 | | } |
| | | 302 | | |
| | | 303 | | /// <summary> |
| | | 304 | | /// Expects the expression specified by text. |
| | | 305 | | /// </summary> |
| | | 306 | | /// <param name="text">The text to expect.</param> |
| | | 307 | | /// <returns> |
| | | 308 | | /// Text available in the shell that ends with expected text. |
| | | 309 | | /// </returns> |
| | | 310 | | public string Expect(string text) |
| | 4 | 311 | | { |
| | 4 | 312 | | return Expect(new Regex(Regex.Escape(text)), Session.InfiniteTimeSpan); |
| | 4 | 313 | | } |
| | | 314 | | |
| | | 315 | | /// <summary> |
| | | 316 | | /// Expects the expression specified by text. |
| | | 317 | | /// </summary> |
| | | 318 | | /// <param name="text">The text to expect.</param> |
| | | 319 | | /// <param name="timeout">Time to wait for input.</param> |
| | | 320 | | /// <returns> |
| | | 321 | | /// The text available in the shell that ends with expected text, or <see langword="null"/> if the specified tim |
| | | 322 | | /// </returns> |
| | | 323 | | public string Expect(string text, TimeSpan timeout) |
| | 0 | 324 | | { |
| | 0 | 325 | | return Expect(new Regex(Regex.Escape(text)), timeout); |
| | 0 | 326 | | } |
| | | 327 | | |
| | | 328 | | /// <summary> |
| | | 329 | | /// Expects the expression specified by regular expression. |
| | | 330 | | /// </summary> |
| | | 331 | | /// <param name="regex">The regular expression to expect.</param> |
| | | 332 | | /// <returns> |
| | | 333 | | /// The text available in the shell that contains all the text that ends with expected expression. |
| | | 334 | | /// </returns> |
| | | 335 | | public string Expect(Regex regex) |
| | 0 | 336 | | { |
| | 0 | 337 | | return Expect(regex, TimeSpan.Zero); |
| | 0 | 338 | | } |
| | | 339 | | |
| | | 340 | | /// <summary> |
| | | 341 | | /// Expects the expression specified by regular expression. |
| | | 342 | | /// </summary> |
| | | 343 | | /// <param name="regex">The regular expression to expect.</param> |
| | | 344 | | /// <param name="timeout">Time to wait for input.</param> |
| | | 345 | | /// <returns> |
| | | 346 | | /// The text available in the shell that contains all the text that ends with expected expression, |
| | | 347 | | /// or <see langword="null"/> if the specified time has elapsed. |
| | | 348 | | /// </returns> |
| | | 349 | | public string Expect(Regex regex, TimeSpan timeout) |
| | 4 | 350 | | { |
| | 4 | 351 | | var text = string.Empty; |
| | | 352 | | |
| | 16 | 353 | | while (true) |
| | 16 | 354 | | { |
| | 16 | 355 | | lock (_incoming) |
| | 16 | 356 | | { |
| | 16 | 357 | | if (_incoming.Count > 0) |
| | 13 | 358 | | { |
| | 13 | 359 | | text = _encoding.GetString(_incoming.ToArray(), 0, _incoming.Count); |
| | 13 | 360 | | } |
| | | 361 | | |
| | 16 | 362 | | var match = regex.Match(text); |
| | | 363 | | |
| | 16 | 364 | | if (match.Success) |
| | 4 | 365 | | { |
| | | 366 | | // Remove processed items from the queue |
| | 412 | 367 | | for (var i = 0; i < match.Index + match.Length && _incoming.Count > 0; i++) |
| | 202 | 368 | | { |
| | 202 | 369 | | _ = _incoming.Dequeue(); |
| | 202 | 370 | | } |
| | | 371 | | |
| | 4 | 372 | | break; |
| | | 373 | | } |
| | 12 | 374 | | } |
| | | 375 | | |
| | 12 | 376 | | if (timeout.Ticks > 0) |
| | 0 | 377 | | { |
| | 0 | 378 | | if (!_dataReceived.WaitOne(timeout)) |
| | 0 | 379 | | { |
| | 0 | 380 | | return null; |
| | | 381 | | } |
| | 0 | 382 | | } |
| | | 383 | | else |
| | 12 | 384 | | { |
| | 12 | 385 | | _ = _dataReceived.WaitOne(); |
| | 12 | 386 | | } |
| | 12 | 387 | | } |
| | | 388 | | |
| | 4 | 389 | | return text; |
| | 4 | 390 | | } |
| | | 391 | | |
| | | 392 | | /// <summary> |
| | | 393 | | /// Begins the expect. |
| | | 394 | | /// </summary> |
| | | 395 | | /// <param name="expectActions">The expect actions.</param> |
| | | 396 | | /// <returns> |
| | | 397 | | /// An <see cref="IAsyncResult" /> that references the asynchronous operation. |
| | | 398 | | /// </returns> |
| | | 399 | | public IAsyncResult BeginExpect(params ExpectAction[] expectActions) |
| | 0 | 400 | | { |
| | 0 | 401 | | return BeginExpect(TimeSpan.Zero, callback: null, state: null, expectActions); |
| | 0 | 402 | | } |
| | | 403 | | |
| | | 404 | | /// <summary> |
| | | 405 | | /// Begins the expect. |
| | | 406 | | /// </summary> |
| | | 407 | | /// <param name="callback">The callback.</param> |
| | | 408 | | /// <param name="expectActions">The expect actions.</param> |
| | | 409 | | /// <returns> |
| | | 410 | | /// An <see cref="IAsyncResult" /> that references the asynchronous operation. |
| | | 411 | | /// </returns> |
| | | 412 | | public IAsyncResult BeginExpect(AsyncCallback callback, params ExpectAction[] expectActions) |
| | 0 | 413 | | { |
| | 0 | 414 | | return BeginExpect(TimeSpan.Zero, callback, state: null, expectActions); |
| | 0 | 415 | | } |
| | | 416 | | |
| | | 417 | | /// <summary> |
| | | 418 | | /// Begins the expect. |
| | | 419 | | /// </summary> |
| | | 420 | | /// <param name="callback">The callback.</param> |
| | | 421 | | /// <param name="state">The state.</param> |
| | | 422 | | /// <param name="expectActions">The expect actions.</param> |
| | | 423 | | /// <returns> |
| | | 424 | | /// An <see cref="IAsyncResult" /> that references the asynchronous operation. |
| | | 425 | | /// </returns> |
| | | 426 | | public IAsyncResult BeginExpect(AsyncCallback callback, object state, params ExpectAction[] expectActions) |
| | 0 | 427 | | { |
| | 0 | 428 | | return BeginExpect(TimeSpan.Zero, callback, state, expectActions); |
| | 0 | 429 | | } |
| | | 430 | | |
| | | 431 | | /// <summary> |
| | | 432 | | /// Begins the expect. |
| | | 433 | | /// </summary> |
| | | 434 | | /// <param name="timeout">The timeout.</param> |
| | | 435 | | /// <param name="callback">The callback.</param> |
| | | 436 | | /// <param name="state">The state.</param> |
| | | 437 | | /// <param name="expectActions">The expect actions.</param> |
| | | 438 | | /// <returns> |
| | | 439 | | /// An <see cref="IAsyncResult" /> that references the asynchronous operation. |
| | | 440 | | /// </returns> |
| | | 441 | | #pragma warning disable CA1859 // Use concrete types when possible for improved performance |
| | | 442 | | public IAsyncResult BeginExpect(TimeSpan timeout, AsyncCallback callback, object state, params ExpectAction[] ex |
| | | 443 | | #pragma warning restore CA1859 // Use concrete types when possible for improved performance |
| | 0 | 444 | | { |
| | 0 | 445 | | var text = string.Empty; |
| | | 446 | | |
| | | 447 | | // Create new AsyncResult object |
| | 0 | 448 | | var asyncResult = new ExpectAsyncResult(callback, state); |
| | | 449 | | |
| | | 450 | | // Execute callback on different thread |
| | 0 | 451 | | ThreadAbstraction.ExecuteThread(() => |
| | 0 | 452 | | { |
| | 0 | 453 | | string expectActionResult = null; |
| | 0 | 454 | | try |
| | 0 | 455 | | { |
| | 0 | 456 | | do |
| | 0 | 457 | | { |
| | 0 | 458 | | lock (_incoming) |
| | 0 | 459 | | { |
| | 0 | 460 | | if (_incoming.Count > 0) |
| | 0 | 461 | | { |
| | 0 | 462 | | text = _encoding.GetString(_incoming.ToArray(), 0, _incoming.Count); |
| | 0 | 463 | | } |
| | 0 | 464 | | |
| | 0 | 465 | | if (text.Length > 0) |
| | 0 | 466 | | { |
| | 0 | 467 | | foreach (var expectAction in expectActions) |
| | 0 | 468 | | { |
| | 0 | 469 | | var match = expectAction.Expect.Match(text); |
| | 0 | 470 | | |
| | 0 | 471 | | if (match.Success) |
| | 0 | 472 | | { |
| | 0 | 473 | | var result = text.Substring(0, match.Index + match.Length); |
| | 0 | 474 | | |
| | 0 | 475 | | for (var i = 0; i < match.Index + match.Length && _incoming.Count > 0; i++) |
| | 0 | 476 | | { |
| | 0 | 477 | | // Remove processed items from the queue |
| | 0 | 478 | | _ = _incoming.Dequeue(); |
| | 0 | 479 | | } |
| | 0 | 480 | | |
| | 0 | 481 | | expectAction.Action(result); |
| | 0 | 482 | | callback?.Invoke(asyncResult); |
| | 0 | 483 | | expectActionResult = result; |
| | 0 | 484 | | } |
| | 0 | 485 | | } |
| | 0 | 486 | | } |
| | 0 | 487 | | } |
| | 0 | 488 | | |
| | 0 | 489 | | if (expectActionResult != null) |
| | 0 | 490 | | { |
| | 0 | 491 | | break; |
| | 0 | 492 | | } |
| | 0 | 493 | | |
| | 0 | 494 | | if (timeout.Ticks > 0) |
| | 0 | 495 | | { |
| | 0 | 496 | | if (!_dataReceived.WaitOne(timeout)) |
| | 0 | 497 | | { |
| | 0 | 498 | | callback?.Invoke(asyncResult); |
| | 0 | 499 | | break; |
| | 0 | 500 | | } |
| | 0 | 501 | | } |
| | 0 | 502 | | else |
| | 0 | 503 | | { |
| | 0 | 504 | | _ = _dataReceived.WaitOne(); |
| | 0 | 505 | | } |
| | 0 | 506 | | } |
| | 0 | 507 | | while (true); |
| | 0 | 508 | | |
| | 0 | 509 | | asyncResult.SetAsCompleted(expectActionResult, completedSynchronously: true); |
| | 0 | 510 | | } |
| | 0 | 511 | | catch (Exception exp) |
| | 0 | 512 | | { |
| | 0 | 513 | | asyncResult.SetAsCompleted(exp, completedSynchronously: true); |
| | 0 | 514 | | } |
| | 0 | 515 | | }); |
| | | 516 | | |
| | 0 | 517 | | return asyncResult; |
| | 0 | 518 | | } |
| | | 519 | | |
| | | 520 | | /// <summary> |
| | | 521 | | /// Ends the execute. |
| | | 522 | | /// </summary> |
| | | 523 | | /// <param name="asyncResult">The async result.</param> |
| | | 524 | | /// <returns> |
| | | 525 | | /// Text available in the shell that ends with expected text. |
| | | 526 | | /// </returns> |
| | | 527 | | /// <exception cref="ArgumentException">Either the IAsyncResult object did not come from the corresponding async |
| | | 528 | | public string EndExpect(IAsyncResult asyncResult) |
| | 0 | 529 | | { |
| | 0 | 530 | | if (asyncResult is not ExpectAsyncResult ar || ar.EndInvokeCalled) |
| | 0 | 531 | | { |
| | 0 | 532 | | throw new ArgumentException("Either the IAsyncResult object did not come from the corresponding async me |
| | | 533 | | } |
| | | 534 | | |
| | | 535 | | // Wait for operation to complete, then return result or throw exception |
| | 0 | 536 | | return ar.EndInvoke(); |
| | 0 | 537 | | } |
| | | 538 | | |
| | | 539 | | /// <summary> |
| | | 540 | | /// Reads the line from the shell. If line is not available it will block the execution and will wait for new li |
| | | 541 | | /// </summary> |
| | | 542 | | /// <returns> |
| | | 543 | | /// The line read from the shell. |
| | | 544 | | /// </returns> |
| | | 545 | | public string ReadLine() |
| | 11 | 546 | | { |
| | 11 | 547 | | return ReadLine(TimeSpan.Zero); |
| | 10 | 548 | | } |
| | | 549 | | |
| | | 550 | | /// <summary> |
| | | 551 | | /// Reads a line from the shell. If line is not available it will block the execution and will wait for new line |
| | | 552 | | /// </summary> |
| | | 553 | | /// <param name="timeout">Time to wait for input.</param> |
| | | 554 | | /// <returns> |
| | | 555 | | /// The line read from the shell, or <see langword="null"/> when no input is received for the specified timeout. |
| | | 556 | | /// </returns> |
| | | 557 | | public string ReadLine(TimeSpan timeout) |
| | 11 | 558 | | { |
| | 11 | 559 | | var text = string.Empty; |
| | | 560 | | |
| | 21 | 561 | | while (true) |
| | 21 | 562 | | { |
| | 21 | 563 | | lock (_incoming) |
| | 21 | 564 | | { |
| | 21 | 565 | | if (_incoming.Count > 0) |
| | 14 | 566 | | { |
| | 14 | 567 | | text = _encoding.GetString(_incoming.ToArray(), 0, _incoming.Count); |
| | 14 | 568 | | } |
| | | 569 | | |
| | 21 | 570 | | var index = text.IndexOf(CrLf, StringComparison.Ordinal); |
| | | 571 | | |
| | 21 | 572 | | if (index >= 0) |
| | 10 | 573 | | { |
| | 10 | 574 | | text = text.Substring(0, index); |
| | | 575 | | |
| | | 576 | | // determine how many bytes to remove from buffer |
| | 10 | 577 | | var bytesProcessed = _encoding.GetByteCount(text + CrLf); |
| | | 578 | | |
| | | 579 | | // remove processed bytes from the queue |
| | 532 | 580 | | for (var i = 0; i < bytesProcessed; i++) |
| | 256 | 581 | | { |
| | 256 | 582 | | _ = _incoming.Dequeue(); |
| | 256 | 583 | | } |
| | | 584 | | |
| | 10 | 585 | | break; |
| | | 586 | | } |
| | 11 | 587 | | } |
| | | 588 | | |
| | 11 | 589 | | if (timeout.Ticks > 0) |
| | 0 | 590 | | { |
| | 0 | 591 | | if (!_dataReceived.WaitOne(timeout)) |
| | 0 | 592 | | { |
| | 0 | 593 | | return null; |
| | | 594 | | } |
| | 0 | 595 | | } |
| | | 596 | | else |
| | 11 | 597 | | { |
| | 11 | 598 | | _ = _dataReceived.WaitOne(); |
| | 10 | 599 | | } |
| | 10 | 600 | | } |
| | | 601 | | |
| | 10 | 602 | | return text; |
| | 10 | 603 | | } |
| | | 604 | | |
| | | 605 | | /// <summary> |
| | | 606 | | /// Reads text available in the shell. |
| | | 607 | | /// </summary> |
| | | 608 | | /// <returns> |
| | | 609 | | /// The text available in the shell. |
| | | 610 | | /// </returns> |
| | | 611 | | public string Read() |
| | 0 | 612 | | { |
| | | 613 | | string text; |
| | | 614 | | |
| | 0 | 615 | | lock (_incoming) |
| | 0 | 616 | | { |
| | 0 | 617 | | text = _encoding.GetString(_incoming.ToArray(), 0, _incoming.Count); |
| | 0 | 618 | | _incoming.Clear(); |
| | 0 | 619 | | } |
| | | 620 | | |
| | 0 | 621 | | return text; |
| | 0 | 622 | | } |
| | | 623 | | |
| | | 624 | | /// <summary> |
| | | 625 | | /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number |
| | | 626 | | /// </summary> |
| | | 627 | | /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte arr |
| | | 628 | | /// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin storing the d |
| | | 629 | | /// <param name="count">The maximum number of bytes to be read from the current stream.</param> |
| | | 630 | | /// <returns> |
| | | 631 | | /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that |
| | | 632 | | /// </returns> |
| | | 633 | | /// <exception cref="ArgumentException">The sum of <paramref name="offset"/> and <paramref name="count"/> is lar |
| | | 634 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception> |
| | | 635 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> or <paramref name="count"/> is negat |
| | | 636 | | /// <exception cref="IOException">An I/O error occurs.</exception> |
| | | 637 | | /// <exception cref="NotSupportedException">The stream does not support reading.</exception> |
| | | 638 | | /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception> |
| | | 639 | | public override int Read(byte[] buffer, int offset, int count) |
| | 0 | 640 | | { |
| | 0 | 641 | | var i = 0; |
| | | 642 | | |
| | 0 | 643 | | lock (_incoming) |
| | 0 | 644 | | { |
| | 0 | 645 | | for (; i < count && _incoming.Count > 0; i++) |
| | 0 | 646 | | { |
| | 0 | 647 | | buffer[offset + i] = _incoming.Dequeue(); |
| | 0 | 648 | | } |
| | 0 | 649 | | } |
| | | 650 | | |
| | 0 | 651 | | return i; |
| | 0 | 652 | | } |
| | | 653 | | |
| | | 654 | | /// <summary> |
| | | 655 | | /// Writes the specified text to the shell. |
| | | 656 | | /// </summary> |
| | | 657 | | /// <param name="text">The text to be written to the shell.</param> |
| | | 658 | | /// <remarks> |
| | | 659 | | /// If <paramref name="text"/> is <see langword="null"/>, nothing is written. |
| | | 660 | | /// </remarks> |
| | | 661 | | public void Write(string text) |
| | 16 | 662 | | { |
| | 16 | 663 | | if (text is null) |
| | 3 | 664 | | { |
| | 3 | 665 | | return; |
| | | 666 | | } |
| | | 667 | | |
| | | 668 | | #if NET7_0_OR_GREATER |
| | 12 | 669 | | ObjectDisposedException.ThrowIf(_channel is null, this); |
| | | 670 | | #else |
| | 1 | 671 | | if (_channel is null) |
| | 0 | 672 | | { |
| | 0 | 673 | | throw new ObjectDisposedException(GetType().FullName); |
| | | 674 | | } |
| | | 675 | | #endif // NET7_0_OR_GREATER |
| | | 676 | | |
| | 12 | 677 | | var data = _encoding.GetBytes(text); |
| | 12 | 678 | | _channel.SendData(data); |
| | 15 | 679 | | } |
| | | 680 | | |
| | | 681 | | /// <summary> |
| | | 682 | | /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the |
| | | 683 | | /// </summary> |
| | | 684 | | /// <param name="buffer">An array of bytes. This method copies <paramref name="count"/> bytes from <paramref nam |
| | | 685 | | /// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin copying bytes |
| | | 686 | | /// <param name="count">The number of bytes to be written to the current stream.</param> |
| | | 687 | | /// <exception cref="ArgumentException">The sum of <paramref name="offset"/> and <paramref name="count"/> is gre |
| | | 688 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception> |
| | | 689 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> or <paramref name="count"/> is negat |
| | | 690 | | /// <exception cref="IOException">An I/O error occurs.</exception> |
| | | 691 | | /// <exception cref="NotSupportedException">The stream does not support writing.</exception> |
| | | 692 | | /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception> |
| | | 693 | | public override void Write(byte[] buffer, int offset, int count) |
| | 84 | 694 | | { |
| | 76414 | 695 | | foreach (var b in buffer.Take(offset, count)) |
| | 38081 | 696 | | { |
| | 38081 | 697 | | if (_outgoing.Count == _bufferSize) |
| | 24 | 698 | | { |
| | 24 | 699 | | Flush(); |
| | 24 | 700 | | } |
| | | 701 | | |
| | 38081 | 702 | | _outgoing.Enqueue(b); |
| | 38081 | 703 | | } |
| | 84 | 704 | | } |
| | | 705 | | |
| | | 706 | | /// <summary> |
| | | 707 | | /// Writes the line to the shell. |
| | | 708 | | /// </summary> |
| | | 709 | | /// <param name="line">The line to be written to the shell.</param> |
| | | 710 | | /// <remarks> |
| | | 711 | | /// If <paramref name="line"/> is <see langword="null"/>, only the line terminator is written. |
| | | 712 | | /// </remarks> |
| | | 713 | | public void WriteLine(string line) |
| | 12 | 714 | | { |
| | 12 | 715 | | Write(line + "\r"); |
| | 12 | 716 | | } |
| | | 717 | | |
| | | 718 | | /// <summary> |
| | | 719 | | /// Releases the unmanaged resources used by the <see cref="Stream"/> and optionally releases the managed resour |
| | | 720 | | /// </summary> |
| | | 721 | | /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor |
| | | 722 | | protected override void Dispose(bool disposing) |
| | 9 | 723 | | { |
| | 9 | 724 | | base.Dispose(disposing); |
| | | 725 | | |
| | 9 | 726 | | if (_isDisposed) |
| | 3 | 727 | | { |
| | 3 | 728 | | return; |
| | | 729 | | } |
| | | 730 | | |
| | 6 | 731 | | if (disposing) |
| | 6 | 732 | | { |
| | 6 | 733 | | UnsubscribeFromSessionEvents(_session); |
| | | 734 | | |
| | 6 | 735 | | if (_channel != null) |
| | 6 | 736 | | { |
| | 6 | 737 | | _channel.DataReceived -= Channel_DataReceived; |
| | 6 | 738 | | _channel.Closed -= Channel_Closed; |
| | 6 | 739 | | _channel.Dispose(); |
| | 6 | 740 | | _channel = null; |
| | 6 | 741 | | } |
| | | 742 | | |
| | 6 | 743 | | if (_dataReceived != null) |
| | 6 | 744 | | { |
| | 6 | 745 | | _dataReceived.Dispose(); |
| | 6 | 746 | | _dataReceived = null; |
| | 6 | 747 | | } |
| | | 748 | | |
| | 6 | 749 | | _isDisposed = true; |
| | 6 | 750 | | } |
| | | 751 | | else |
| | 0 | 752 | | { |
| | 0 | 753 | | UnsubscribeFromSessionEvents(_session); |
| | 0 | 754 | | } |
| | 9 | 755 | | } |
| | | 756 | | |
| | | 757 | | /// <summary> |
| | | 758 | | /// Unsubscribes the current <see cref="ShellStream"/> from session events. |
| | | 759 | | /// </summary> |
| | | 760 | | /// <param name="session">The session.</param> |
| | | 761 | | /// <remarks> |
| | | 762 | | /// Does nothing when <paramref name="session"/> is <see langword="null"/>. |
| | | 763 | | /// </remarks> |
| | | 764 | | private void UnsubscribeFromSessionEvents(ISession session) |
| | 36 | 765 | | { |
| | 36 | 766 | | if (session is null) |
| | 0 | 767 | | { |
| | 0 | 768 | | return; |
| | | 769 | | } |
| | | 770 | | |
| | 36 | 771 | | session.Disconnected -= Session_Disconnected; |
| | 36 | 772 | | session.ErrorOccured -= Session_ErrorOccured; |
| | 36 | 773 | | } |
| | | 774 | | |
| | | 775 | | private void Session_ErrorOccured(object sender, ExceptionEventArgs e) |
| | 0 | 776 | | { |
| | 0 | 777 | | OnRaiseError(e); |
| | 0 | 778 | | } |
| | | 779 | | |
| | | 780 | | private void Session_Disconnected(object sender, EventArgs e) |
| | 0 | 781 | | { |
| | 0 | 782 | | _channel?.Dispose(); |
| | 0 | 783 | | } |
| | | 784 | | |
| | | 785 | | private void Channel_Closed(object sender, ChannelEventArgs e) |
| | 1 | 786 | | { |
| | | 787 | | // TODO: Do we need to call dispose here ?? |
| | 1 | 788 | | Dispose(); |
| | 1 | 789 | | } |
| | | 790 | | |
| | | 791 | | private void Channel_DataReceived(object sender, ChannelDataEventArgs e) |
| | 26 | 792 | | { |
| | 26 | 793 | | lock (_incoming) |
| | 26 | 794 | | { |
| | 1094 | 795 | | foreach (var b in e.Data) |
| | 508 | 796 | | { |
| | 508 | 797 | | _incoming.Enqueue(b); |
| | 508 | 798 | | } |
| | 26 | 799 | | } |
| | | 800 | | |
| | 26 | 801 | | if (_dataReceived != null) |
| | 26 | 802 | | { |
| | 26 | 803 | | _ = _dataReceived.Set(); |
| | 26 | 804 | | } |
| | | 805 | | |
| | 26 | 806 | | OnDataReceived(e.Data); |
| | 26 | 807 | | } |
| | | 808 | | |
| | | 809 | | private void OnRaiseError(ExceptionEventArgs e) |
| | 0 | 810 | | { |
| | 0 | 811 | | ErrorOccurred?.Invoke(this, e); |
| | 0 | 812 | | } |
| | | 813 | | |
| | | 814 | | private void OnDataReceived(byte[] data) |
| | 26 | 815 | | { |
| | 26 | 816 | | DataReceived?.Invoke(this, new ShellDataEventArgs(data)); |
| | 26 | 817 | | } |
| | | 818 | | } |
| | | 819 | | } |