www.libssh2.org | Daily snapshots | Mailing list archive | Docs | Examples

Archive Index This month's Index

Subject: [libssh2] RFC on non-blocking changes and suggestions - part 1

[libssh2] RFC on non-blocking changes and suggestions - part 1

From: James Housley <jim_at_thehousleys.net>
Date: Thu, 19 Apr 2007 14:15:27 -0400

I created a non-blocking version of libssh2_sftp_readdir(), like
libssh2_sftp_read() and libssh2_sftp_write(). While these functions
"set" the mode to non-blocking, it isn't even close. The
libssh2_chanel_read() function seems to be non-blocking capable, so I
started there and migrated the non-blocking up. In most of the
places I while loops on PACKET_EAGAIN, which still blocks in non-
blocking mode just like before, but the code still works. I have
been testing between two machines, the target machine is FreeBSD with
ipfw(8) and traffic shaping. To test the response on a slow link I
have the bandwidth limited to 2500bit/s in both directions on tcp/22.

To make libssh2_sftp_packet_requirev() able to be called repeatably
with out calling flush and still being able to honor the value of
LIBSSH2_READ_TIMEOUT I added a new variable to the _LIBSSH2_SFTP
structure to track if this was the first time in or subsequent calls.

My most serious problem is in the bottom while loop in
libssh2_sftp_packet_read(). If I get PACKET_EAGAIN from
_libssh2_channel_read() to be non-blocking something has to be done
with the data that has been read. My first thought was to just call
libssh2_sftp_packet_add() with the data and number of bytes that have
been received. This crashes nicely, because the header of the packet
gives a different length then what is there. My second option is to
temporarily store the packet in the _LIBSSH_SFTP structure and use
that to allow the bypassing of the top portion of
libssh2_sftp_packet_read() and just start reading. Then the
temporary storage would need to be removed in case of other errors,
might be hard to get right.

Attached is my code changes as they stand, edited with TAB for indent
set a 4 spaces (like the original code in the library).

Jim

Index: include/libssh2_sftp.h
===================================================================
RCS file: /cvsroot/libssh2/libssh2/include/libssh2_sftp.h,v
retrieving revision 1.12
diff -u -r1.12 libssh2_sftp.h
--- include/libssh2_sftp.h 18 Apr 2007 18:51:03 -0000 1.12
+++ include/libssh2_sftp.h 19 Apr 2007 16:44:21 -0000
@@ -191,7 +191,11 @@
LIBSSH2_API ssize_t libssh2_sftp_readnb(LIBSSH2_SFTP_HANDLE *handle,
                                        char *buffer, size_t buffer_maxlen);
-LIBSSH2_API int libssh2_sftp_readdir(LIBSSH2_SFTP_HANDLE *handle,
char *buffer, size_t buffer_maxlen, LIBSSH2_SFTP_ATTRIBUTES *attrs);
+LIBSSH2_API int libssh2_sftp_readdir(LIBSSH2_SFTP_HANDLE *handle,
char *buffer,
+ size_t buffer_maxlen,
LIBSSH2_SFTP_ATTRIBUTES *attrs);
+LIBSSH2_API int libssh2_sftp_readdirnb(LIBSSH2_SFTP_HANDLE *handle,
char *buffer,
+ size_t buffer_maxlen,
LIBSSH2_SFTP_ATTRIBUTES *attrs);
+
LIBSSH2_API ssize_t libssh2_sftp_write(LIBSSH2_SFTP_HANDLE *handle,
                                       const char *buffer, size_t count);
LIBSSH2_API ssize_t libssh2_sftp_writenb(LIBSSH2_SFTP_HANDLE *handle,
@@ -238,6 +242,9 @@
#define libssh2_sftp_readlink(sftp, path, target, maxlen)
libssh2_sftp_symlink_ex((sftp), (path), strlen(path), (target),
(maxlen), LIBSSH2_SFTP_READLINK)
#define libssh2_sftp_realpath(sftp, path, target, maxlen)
libssh2_sftp_symlink_ex((sftp), (path), strlen(path), (target),
(maxlen), LIBSSH2_SFTP_REALPATH)
+LIBSSH2_API void libssh2_sftp_set_blocking(LIBSSH2_SFTP *session,
int blocking);
+LIBSSH2_API int libssh2_sftp_get_blocking(LIBSSH2_SFTP *session);
+
#ifdef __cplusplus
} /* extern "C" */
#endif

--
/"\   ASCII Ribbon Campaign  .
\ / - NO HTML/RTF in e-mail  .
  X  - NO Word docs in e-mail .
/ \ -----------------------------------------------------------------
jeh@FreeBSD.org      http://www.FreeBSD.org     The Power to Serve
jim@TheHousleys.Net  http://www.TheHousleys.net
---------------------------------------------------------------------
Fortune Not Found:
Abort, Retry, Ignore?
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
libssh2-devel mailing list
libssh2-devel_at_lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libssh2-devel
Received on 2007-04-19

the libssh2 team