SSH.NET icon indicating copy to clipboard operation
SSH.NET copied to clipboard

Improve shell stream

Open 294797392 opened this issue 1 year ago • 8 comments

Improve ShellStream

  • Added SendWindowChangeRequest to ShellStream

  • Added a new constructor to ShellStream

  • SshClient adds an overload function for CreateShellStream

When creating ShellStream, the channel will be immediately opened in the constructor of ShellStream. At this time, the calling module may not have subscribed to ShellStream's DataReceived event, which may result in the loss of a small piece of data immediately after connection. You can use ShellStream's new constructor to solve this problem

294797392 avatar Oct 07 '24 07:10 294797392

Why does AppVeyor fail?

294797392 avatar Oct 07 '24 08:10 294797392

Hi @294797392

Are you able solve your problem using the Read method instead of the DataReceived event? e.g.

byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = shellStream.Read(buffer) != 0)
{
    // Process "bytesRead" bytes from buffer
}

Rob-Hague avatar Oct 08 '24 18:10 Rob-Hague

Yes, using the Read function can solve my problem. But I think it's best to optimize it, as other developers may also encounter this issue when using it. Do you think it is necessary to add the SendWindowChangeRequest function? @Rob-Hague

294797392 avatar Oct 09 '24 06:10 294797392

Yes it seems reasonable to add SendWindowChangeRequest - there have been previous requests for it. Personally I would suggest to:

  1. make it return void: the server does not respond to a window-change request, and our implementation in ChannelSession just returns true so it is not much use to return bool
  2. add ThrowHelper.ThrowObjectDisposedIf(_disposed, this); at the start of the method
  3. add at least a basic test. For example, in test\Renci.SshNet.Tests\Classes\ShellStreamTest.cs there could be a test which verifies the call to IChannelSession mock, and verifies the ObjectDisposedException when the ShellStream is closed

Let me know what you think

Rob-Hague avatar Oct 09 '24 19:10 Rob-Hague

I agree with your idea. I tried to modify the code and added the SendWindowChangeRequest method, but found that if it returns void, IDE0058 compilation error will occur. If void is returned, there may be many modifications and it will also affect the consistency of the code (there are many methods in ChannelSession that directly return true). Perhaps having a return value would be better than having no return value, as a return value can prompt the caller about the execution result of the method you called, and in the future we can also add more error handling to the method (if necessary)! And I believe that when developing the ChannelSession module initially, there must have been a reason for returning a boolean. @Rob-Hague

294797392 avatar Oct 11 '24 11:10 294797392

Ok, bool seems fine to me - I don't feel strongly about it

Rob-Hague avatar Oct 13 '24 15:10 Rob-Hague

Ok, bool seems fine to me - I don't feel strongly about it

Do you agree with me changing it to a boolean return value? If you agree, I will submit another PR

294797392 avatar Oct 14 '24 01:10 294797392

Yes

Rob-Hague avatar Oct 14 '24 10:10 Rob-Hague