From ddb1c1838f35818db92809d92c999a1352be5521 Mon Sep 17 00:00:00 2001 From: milo Date: Sun, 20 Feb 2011 14:06:57 +0100 Subject: poll: Added a function to poll the events. --- include/libssh/libssh.h | 1 + src/poll.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h index e714eca..504aac9 100644 --- a/include/libssh/libssh.h +++ b/include/libssh/libssh.h @@ -482,6 +482,7 @@ LIBSSH_API ssh_event ssh_event_new(void); LIBSSH_API int ssh_event_add_fd(ssh_event event, socket_t fd, short events, ssh_event_callback cb, void *userdata); LIBSSH_API int ssh_event_add_session(ssh_event event, ssh_session session); +LIBSSH_API int ssh_event_dopoll(ssh_event event, int timeout); LIBSSH_API int ssh_event_remove_fd(ssh_event event, socket_t fd); LIBSSH_API int ssh_event_remove_session(ssh_event event, ssh_session session); LIBSSH_API void ssh_event_free(ssh_event event); diff --git a/src/poll.c b/src/poll.c index bfb54e6..79dbb49 100644 --- a/src/poll.c +++ b/src/poll.c @@ -835,6 +835,44 @@ int ssh_event_add_session(ssh_event event, ssh_session session) { return SSH_OK; } +/** + * @brief Poll all the sockets and sessions associated through an event object. + * If any of the events are set after the poll, the + * call back functions of the sessions or sockets will be called. + * This function should be called once within the programs main loop. + * + * @param event The ssh_event object to poll. + * @param timeout An upper limit on the time for which the poll will + * block, in milliseconds. Specifying a negative value + * means an infinite timeout. This parameter is passed to + * the poll() function. + * @returns SSH_OK No error. + * SSH_ERROR Error happened during the poll. + */ +int ssh_event_dopoll(ssh_event event, int timeout) { + int rc; +#ifdef WITH_SERVER + ssh_session session; + struct ssh_iterator *iterator; +#endif + + if(event == NULL || event->ctx == NULL) { + return SSH_ERROR; + } + rc = ssh_poll_ctx_dopoll(event->ctx, timeout); +#ifdef WITH_SERVER + if(rc == SSH_OK) { + iterator = ssh_list_get_iterator(event->sessions); + while(iterator != NULL) { + session = (ssh_session)iterator->data; + ssh_execute_message_callbacks(session); + iterator = iterator->next; + } + } +#endif + return rc; +} + /** * @brief Remove a socket fd from an event context. * -- cgit v1.2.3