Subject: Re: sftp_read problems with large buffer

Re: sftp_read problems with large buffer

From: Daniel Stenberg <>
Date: Tue, 4 Aug 2009 23:03:26 +0200 (CEST)

On Tue, 4 Aug 2009, Peter Stuge wrote:

> Daniel, can you describe the theory around _wait_socket() and


_libssh2_wait_socket() is simply an internal utility function that is used for
waiting on a non-blocking socket until something happens. It uses the
libssh2_session_block_directions() function to find out for what direction we
can expect data to happen and thus what to wait for.

> BLOCK_ADJUST() a little?

BLOCK_ADJUST and BLOCK_ADJUST_ERRNO are two helper-macros that I added to make
it easier to write functions that are _either_ blocking or non-blocking.

Almost all the entry-point libssh2 functions now work in a manner like for
example libssh2_session_startup(). Its function body looks like this:

     int rc;

     BLOCK_ADJUST(rc, session, session_startup(session, sock) );

     return rc;

BLOCK_ADJUST() is then a macro that calls session_startup(session, sock) and
based on what the non-blocking option in 'session' is set to and what this
function returns it acts. If blocking is set, it will detect a
LIBSSH2_ERROR_EAGAIN return code and call _libssh2_wait_socket() to wait for
"action" and then loop to call session_startup() again.

BLOCK_ADJUST_ERRNO() is basically the same function, but it is used for
internal functions that don't return the "normal" error code, but for
functions that return pointers so to detect an EAGAIN situation we see that if
NULL was returned, if blocking is then selected we call
libssh2_session_last_errno() to see if the reason was a blocking situation. If
it is, it calls _libssh2_wait_socket() to wait for "action" and then loop to
call the function again.

Received on 2009-08-04