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

will method read_stream block? #1

Open
yangshoulai opened this issue Aug 4, 2023 · 4 comments
Open

will method read_stream block? #1

yangshoulai opened this issue Aug 4, 2023 · 4 comments

Comments

@yangshoulai
Copy link

yangshoulai commented Aug 4, 2023

In async-ssh2-lite/src/main.js line 49, if the read stream size exactly equal BUFFER_SIZE, method read_buf_bytes will return true, will current method block in next loop? I am a newer to Rust and I am searching for ssh local port forwarding, Thank you very much if you could answer my question。

@mihirsamdarshi
Copy link
Owner

Hi @yangshoulai, thanks for the question.

If I understand correctly, you’re asking about this bit of code

match stream.read(&mut buffer).await {
Ok(n) => {
if !read_buf_bytes(&mut request_len, &mut request_buffer, n, buffer) {
break;
}
}
Err(e) => {
println!("Error in reading request data: {e:?}");
break;
}
}
}

No, the method will not block in the next loop. The method read_buf_bytes() checks how many bytes (n) were read from the stream. If n is 0, this basically translates to there being no more data to read on the stream. Let's say BUFFER_SIZE is 4KB and your file is also 4KB. This does not mean the last read() will block, because the operating system typically knows the file size ahead of the read operations, thus it will read 4KB and then report EOF (end of file), i.e., it will return 0 to the read() on the next loop indicating there is nothing left to read, and hence that will not block. If it's 0, it's usually because the file has already finished (EOF has been reached), and subsequent read() calls will therefore return 0 immediately. The behavior of the code is essentially relying on the read() system call's behavior on an EOF.

As such, the function will run through the loop until it reads the entire stream and once that's done, the next stream.read call will immediately return 0, this returns false from read_buf_bytes() and the loop breaks. Therefore, read_stream() won't block even if the read stream size is exactly equal to BUFFER_SIZE.

@yangshoulai
Copy link
Author

Thank you very much for your detailed explanation. I have learned a lot. Finally, I found this lib ssh_jumper, It helped me solved my problem, but thanks again for your help.

@TheBlindM
Copy link

Thank you very much for your detailed explanation. I have learned a lot. Finally, I found this lib ssh_jumper, It helped me solved my problem, but thanks again for your help.

May I ask how "ssh_jumper" is used together with async ssh2 lite?

@mihirsamdarshi
Copy link
Owner

Hi @TheBlindM I think that ssh_jumper uses async ssh2 lite under the hood

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants