sshnet / SSH.NET

SSH.NET is a Secure Shell (SSH) library for .NET, optimized for parallelism.
http://sshnet.github.io/SSH.NET/
MIT License
3.88k stars 919 forks source link

Improve ShellStream Expect #1315

Closed WojciechNagorski closed 4 months ago

WojciechNagorski commented 4 months ago

There are three implementation of Expect method:

public string Expect(Regex regex, TimeSpan timeout);
public void Expect(TimeSpan timeout, params ExpectAction[] expectActions);
public IAsyncResult BeginExpect(TimeSpan timeout, AsyncCallback callback, object state, params ExpectAction[] expectActions);

Two of them return string ends with the expected expression. Only the first one works differently. In this PR I fixed this problem.

Moreover, the _incoming array contains bytes and regex works on UTF8 encoding, so we should count how many bytes we should remove from the incoming array.

Continuation of #1313 Added test failures for #1207 (does not fix performance issue)

WojciechNagorski commented 4 months ago

@Rob-Hague Can I ask for a review?

WojciechNagorski commented 4 months ago

@Rob-Hague Thanks! I haven't touched the other tests and I won't.

Rob-Hague commented 4 months ago

Btw, working on ShellStream makes me wish it derived from StreamReader rather than Stream... it would save a lot of repeated encoding work for the Expect methods (and just make more sense in general). But perhaps not worth the break.

jscarle commented 4 months ago

I believe that ShellStream still has it's place, however perhaps a new ShellStreamReader could be added that would consume the a ShellStream and we could slowly migrate functionality between the two classes.

It could even be done in two steps. First by adding the new methods into ShellStreamReader, and then later deprecating them on ShellStream.

WojciechNagorski commented 4 months ago

This issue has been fixed in the 2024.0.0 version.