diff options
author | Aris Adamantiadis <aris@0xbadc0de.be> | 2011-08-30 21:27:36 +0300 |
---|---|---|
committer | Aris Adamantiadis <aris@0xbadc0de.be> | 2011-09-02 11:43:11 +0300 |
commit | cb2a111fa141f158baaae0f2fa6e474e6a2aca49 (patch) | |
tree | 3056fe93b79c8c8c2fc4c0e2c9b2d6800a5217b5 | |
parent | 555d1a43470d184cdfb0deedc39415c59b14a133 (diff) | |
download | libssh-cb2a111fa141f158baaae0f2fa6e474e6a2aca49.tar.gz libssh-cb2a111fa141f158baaae0f2fa6e474e6a2aca49.tar.xz libssh-cb2a111fa141f158baaae0f2fa6e474e6a2aca49.zip |
channel: implement ssh_channel_poll_timeout
Resolves bug #57
-rw-r--r-- | include/libssh/libssh.h | 1 | ||||
-rw-r--r-- | src/channels.c | 55 |
2 files changed, 56 insertions, 0 deletions
diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h index 6aaffdb8..819318b1 100644 --- a/include/libssh/libssh.h +++ b/include/libssh/libssh.h @@ -341,6 +341,7 @@ LIBSSH_API int ssh_channel_open_forward(ssh_channel channel, const char *remoteh int remoteport, const char *sourcehost, int localport); LIBSSH_API int ssh_channel_open_session(ssh_channel channel); LIBSSH_API int ssh_channel_poll(ssh_channel channel, int is_stderr); +int ssh_channel_poll_timeout(ssh_channel channel, int timeout, int is_stderr); LIBSSH_API int ssh_channel_read(ssh_channel channel, void *dest, uint32_t count, int is_stderr); LIBSSH_API int ssh_channel_read_nonblocking(ssh_channel channel, void *dest, uint32_t count, int is_stderr); diff --git a/src/channels.c b/src/channels.c index aa81c399..9f869564 100644 --- a/src/channels.c +++ b/src/channels.c @@ -2731,6 +2731,61 @@ int ssh_channel_poll(ssh_channel channel, int is_stderr){ } /** + * @brief Polls a channel for data to read, waiting for a certain timeout. + * + * @param[in] channel The channel to poll. + * @param[in] timeout Set an upper limit on the time for which this function + * will block, in milliseconds. Specifying a negative value + * means an infinite timeout. This parameter is passed to + * the poll() function. + * @param[in] is_stderr A boolean to select the stderr stream. + * + * @return The number of bytes available for reading, + * 0 if nothing is available (timeout elapsed), + * SSH_EOF on end of file, + * SSH_ERROR on error. + * + * @warning When the channel is in EOF state, the function returns SSH_EOF. + * + * @see channel_is_eof() + */ +int ssh_channel_poll_timeout(ssh_channel channel, int timeout, int is_stderr){ + ssh_session session; + ssh_buffer stdbuf; + struct ssh_channel_read_termination_struct ctx; + int rc; + + if(channel == NULL) { + return SSH_ERROR; + } + + session = channel->session; + stdbuf = channel->stdout_buffer; + enter_function(); + + if (is_stderr) { + stdbuf = channel->stderr_buffer; + } + ctx.buffer = stdbuf; + ctx.channel = channel; + ctx.count = 1; + rc = ssh_handle_packets_termination(channel->session, timeout, + ssh_channel_read_termination, &ctx); + if(rc ==SSH_ERROR || session->session_state == SSH_SESSION_STATE_ERROR){ + rc = SSH_ERROR; + goto end; + } + rc = buffer_get_rest_len(stdbuf); + if(rc > 0) + goto end; + if (channel->remote_eof) + rc = SSH_EOF; +end: + leave_function(); + return rc; +} + +/** * @brief Recover the session in which belongs a channel. * * @param[in] channel The channel to recover the session from. |