summaryrefslogtreecommitdiffstats
path: root/src/session.c
diff options
context:
space:
mode:
authorAris Adamantiadis <aris@0xbadc0de.be>2011-05-24 23:26:18 +0200
committerAris Adamantiadis <aris@0xbadc0de.be>2011-05-24 23:26:18 +0200
commit59f7647cd97c62ab7a26725e5a166dcb54b27bc6 (patch)
tree34dad1189e803af0b8d1d3caa83cd220a950bc3f /src/session.c
parent4d6b1aa2c7ea67d6909680254b7ae68aa437a6ef (diff)
downloadlibssh-59f7647cd97c62ab7a26725e5a166dcb54b27bc6.tar.gz
libssh-59f7647cd97c62ab7a26725e5a166dcb54b27bc6.tar.xz
libssh-59f7647cd97c62ab7a26725e5a166dcb54b27bc6.zip
Introduced ssh_timeout_elapsed functions
Functions to mesure elapsed time before and after a serie of calls. Introduces a dependancy to clock_gettime() and librt, hope this doesn't break anything. Porting to gettimeofday() should not be too hard.
Diffstat (limited to 'src/session.c')
-rw-r--r--src/session.c31
1 files changed, 22 insertions, 9 deletions
diff --git a/src/session.c b/src/session.c
index 35fb0b2..27b91c9 100644
--- a/src/session.c
+++ b/src/session.c
@@ -304,18 +304,27 @@ int ssh_is_blocking(ssh_session session){
* will block, in milliseconds. Specifying a negative value
* means an infinite timeout. This parameter is passed to
* the poll() function.
- * @returns SSH_OK on success, SSH_ERROR otherwise.
+ * @returns SSH_OK on success, SSH_AGAIN if timeout occurred,
+ * SSH_ERROR otherwise.
*/
int ssh_blocking_flush(ssh_session session, int timeout){
ssh_socket s;
+ struct ssh_timestamp ts;
+ int rc;
if(session==NULL)
return SSH_ERROR;
enter_function();
s=session->socket;
+ ssh_timestamp_init(&ts);
while (ssh_socket_buffered_write_bytes(s) > 0 && session->alive) {
- ssh_handle_packets(session, timeout);
+ rc=ssh_handle_packets(session, timeout);
+ if(ssh_timeout_elapsed(&ts,timeout)){
+ rc=SSH_AGAIN;
+ break;
+ }
+ timeout = ssh_timeout_update(&ts, timeout);
}
leave_function();
@@ -415,6 +424,7 @@ void ssh_set_fd_except(ssh_session session) {
int ssh_handle_packets(ssh_session session, int timeout) {
ssh_poll_handle spoll_in,spoll_out;
ssh_poll_ctx ctx;
+ int rc;
if(session==NULL || session->socket==NULL)
return SSH_ERROR;
enter_function();
@@ -429,7 +439,8 @@ int ssh_handle_packets(ssh_session session, int timeout) {
if(spoll_in != spoll_out)
ssh_poll_ctx_add(ctx,spoll_out);
}
- if( ssh_poll_ctx_dopoll(ctx,timeout) )
+ rc = ssh_poll_ctx_dopoll(ctx,timeout);
+ if(rc == SSH_ERROR)
session->session_state = SSH_SESSION_STATE_ERROR;
leave_function();
if (session->session_state != SSH_SESSION_STATE_ERROR)
@@ -460,17 +471,19 @@ int ssh_handle_packets(ssh_session session, int timeout) {
int ssh_handle_packets_termination(ssh_session session, int timeout,
ssh_termination_function fct, void *user){
int ret = SSH_ERROR;
+ struct ssh_timestamp ts;
+ ssh_timestamp_init(&ts);
+
while(!fct(user)){
ret = ssh_handle_packets(session, timeout);
if(ret == SSH_ERROR)
return SSH_ERROR;
- if(timeout == 0){
- if(fct(user))
- return SSH_OK;
- else
- return SSH_AGAIN;
+ if(fct(user)) {
+ return SSH_OK;
+ } else if (ssh_timeout_elapsed(&ts, timeout)) {
+ return SSH_AGAIN;
}
- /* TODO: verify that total timeout has not expired and then return SSH_AGAIN */
+ timeout = ssh_timeout_update(&ts,timeout);
}
return ret;
}