From 91a01b450ce761ba6f8b9ba59a74d1029297d237 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Fri, 31 Jul 2020 12:28:28 -0300 Subject: [PATCH] Adjustments for performance. --- src/core/iocalls.c | 9 ++++++--- src/core/linux/connect.c | 10 ++++++++++ src/core/properties/boolean.c | 7 +++++-- src/core/wait.c | 37 ++++++++++++++++++++++++++++++++++++- src/core/windows/connect.c | 13 +++++++++++++ src/include/lib3270.h | 16 +++++++++++++++- src/include/lib3270/properties.h | 7 ++++++- src/testprogram/testprogram.c | 16 ++++++++++++++-- 8 files changed, 105 insertions(+), 10 deletions(-) diff --git a/src/core/iocalls.c b/src/core/iocalls.c index 9c55cf2..49649e9 100644 --- a/src/core/iocalls.c +++ b/src/core/iocalls.c @@ -325,9 +325,12 @@ static void internal_ring_bell(H3270 GNUC_UNUSED(*session)) void * AddTimer(unsigned long interval_ms, H3270 *session, int (*proc)(H3270 *session, void *userdata), void *userdata) { - void *timer; - CHECK_SESSION_HANDLE(session); - timer = add_timer(session,interval_ms,proc,userdata); + void *timer = add_timer( + session, + interval_ms ? interval_ms : 100, // Prevents a zero-value timer. + proc, + userdata + ); trace("Timeout %p created with %ld ms",timer,interval_ms); return timer; } diff --git a/src/core/linux/connect.c b/src/core/linux/connect.c index 88b38ff..462ca37 100644 --- a/src/core/linux/connect.c +++ b/src/core/linux/connect.c @@ -245,6 +245,15 @@ static void net_connected(H3270 *hSession, int GNUC_UNUSED(fd), LIB3270_IO_FLAG if(seconds) { + int rc = lib3270_wait_for_cstate(hSession,LIB3270_CONNECTED_TN3270E,seconds); + if(rc) + { + lib3270_disconnect(hSession); + lib3270_write_log(hSession,"connect", "%s: %s",__FUNCTION__,strerror(ETIMEDOUT)); + return errno = rc; + } + + /* time_t end = time(0)+seconds; while(time(0) < end) @@ -282,6 +291,7 @@ static void net_connected(H3270 *hSession, int GNUC_UNUSED(fd), LIB3270_IO_FLAG lib3270_write_log(hSession,"connect", "%s: %s",__FUNCTION__,strerror(ETIMEDOUT)); return errno = ETIMEDOUT; + */ } return 0; diff --git a/src/core/properties/boolean.c b/src/core/properties/boolean.c index ffe901b..6b198d7 100644 --- a/src/core/properties/boolean.c +++ b/src/core/properties/boolean.c @@ -45,10 +45,13 @@ return hSession->starting != 0; } - void lib3270_disable_crl_download(H3270 *hSession) + int lib3270_disable_crl_download(H3270 *hSession, int enabled) { #ifdef SSL_ENABLE_CRL_CHECK - hSession->ssl.crl.download = 0; + hSession->ssl.crl.download = enabled ? 1 : 0; + return 0; +#else + return errno = ENOTSUP; #endif // SSL_ENABLE_CRL_CHECK } diff --git a/src/core/wait.c b/src/core/wait.c index 5973f75..7adb4fd 100644 --- a/src/core/wait.c +++ b/src/core/wait.c @@ -84,6 +84,7 @@ LIB3270_EXPORT int lib3270_wait_for_ready(H3270 *hSession, int seconds) break; } + debug("%s: Waiting",__FUNCTION__); lib3270_main_iterate(hSession,1); } RemoveTimer(hSession,timer); @@ -189,7 +190,7 @@ int lib3270_wait_for_string_at_address(H3270 *hSession, int baddr, const char *k } -int lib3270_wait_for_string_at(H3270 *hSession, unsigned int row, unsigned int col, const char *key, int seconds) +LIB3270_EXPORT int lib3270_wait_for_string_at(H3270 *hSession, unsigned int row, unsigned int col, const char *key, int seconds) { int baddr = lib3270_translate_to_address(hSession,row,col); if(baddr < 0) @@ -197,3 +198,37 @@ int lib3270_wait_for_string_at(H3270 *hSession, unsigned int row, unsigned int c return lib3270_wait_for_string_at_address(hSession,baddr,key,seconds); } + +LIB3270_EXPORT int lib3270_wait_for_cstate(H3270 *hSession, LIB3270_CSTATE cstate, int seconds) +{ + + int rc = -1; + int timeout = 0; + void * timer = AddTimer(seconds * 1000, hSession, timer_expired, &timeout); + + while(rc == -1) + { + if(timeout) { + // Timeout! The timer was destroyed. + return errno = ETIMEDOUT; + } + + if(hSession->connection.state == LIB3270_NOT_CONNECTED) + { + rc = ENOTCONN; + break; + } + + if(!hSession->starting && hSession->connection.state == cstate) + { + rc = 0; + break; + } + + lib3270_main_iterate(hSession,1); + + } + RemoveTimer(hSession,timer); + + return errno = rc; +} diff --git a/src/core/windows/connect.c b/src/core/windows/connect.c index 375e294..6e3f1c5 100644 --- a/src/core/windows/connect.c +++ b/src/core/windows/connect.c @@ -347,6 +347,18 @@ int net_reconnect(H3270 *hSession, int seconds) if(seconds) { + int rc = lib3270_wait_for_cstate(hSession,LIB3270_CONNECTED_TN3270E,seconds); + if(rc) + { + lib3270_disconnect(hSession); + lib3270_write_log(hSession,"connect", "%s: %s",__FUNCTION__,strerror(rc)); + return errno = rc; + } + } + + /* + if(seconds) + { time_t end = time(0)+seconds; while(time(0) < end) @@ -382,6 +394,7 @@ int net_reconnect(H3270 *hSession, int seconds) lib3270_write_log(hSession,"connect", "%s: %s",__FUNCTION__,strerror(ETIMEDOUT)); return errno = ETIMEDOUT; } + */ return 0; diff --git a/src/include/lib3270.h b/src/include/lib3270.h index 8fb74b5..d07bbb4 100644 --- a/src/include/lib3270.h +++ b/src/include/lib3270.h @@ -1111,7 +1111,7 @@ LIB3270_EXPORT int lib3270_wait_for_update(H3270 *hSession, int seconds); /** - * Wait "N" seconds for "ready" state. + * @brief Wait "N" seconds for "ready" state. * * @param seconds Number of seconds to wait. * @@ -1121,6 +1121,20 @@ LIB3270_EXPORT int lib3270_wait_for_ready(H3270 *hSession, int seconds); /** + * @brief Wait "N" seconds for online state. + * + * @param seconds Number of seconds to wait. + * + * @return 0 if ok, errno code if not. + * + * @retval ETIMEDOUT Timeout waiting. + * @retval ENOTCONN Not connected to host. + * @retval 0 Session is online and in required state. + * + */ + LIB3270_EXPORT int lib3270_wait_for_cstate(H3270 *hSession, LIB3270_CSTATE cstate, int seconds); + + /** * "beep" to notify user. * * If available play a sound signal do alert user. diff --git a/src/include/lib3270/properties.h b/src/include/lib3270/properties.h index 5a2f097..5674d26 100644 --- a/src/include/lib3270/properties.h +++ b/src/include/lib3270/properties.h @@ -216,9 +216,14 @@ * @brief Disable automatic download of the CRL. * * @param hSession Session handle. + * @param Value Non zero to enable automatic download of CRL. * + * @return 0 if ok or error code if not (Sets errno). + * + * @retval 0 Download of the CRL was disabled. + * @retval ENOTSUP No SSL/TLS support. */ - LIB3270_EXPORT void lib3270_disable_crl_download(H3270 *hSession); + LIB3270_EXPORT int lib3270_ssl_set_crl_download(H3270 *hSession, int enabled); /** * @brief Get lib3270 version info. diff --git a/src/testprogram/testprogram.c b/src/testprogram/testprogram.c index 21a7440..c58a4c4 100644 --- a/src/testprogram/testprogram.c +++ b/src/testprogram/testprogram.c @@ -154,13 +154,25 @@ int main(int argc, char *argv[]) const void * online_listener = lib3270_register_action_group_listener(h,LIB3270_ACTION_GROUP_ONLINE,online_group_state_changed,NULL); - rc = lib3270_reconnect(h,120); + rc = lib3270_reconnect(h,0); printf("\n\nConnect exits with rc=%d (%s)\n\n",rc,strerror(rc)); if(!rc) { + rc = lib3270_wait_for_cstate(h,LIB3270_CONNECTED_TN3270E, 60); + printf("\n\nWait for LIB3270_CONNECTED_TN3270E exits with rc=%d (%s)\n\n",rc,strerror(rc)); + } + + if(!rc) + { + rc = lib3270_wait_for_ready(h,60); + printf("\n\nWait for ready exits with rc=%d (%s)\n\n",rc,strerror(rc)); + } + + if(!rc) + { + printf("\n\nWaiting starts %u\n",(unsigned int) time(NULL)); - lib3270_wait_for_ready(h,10); { // Performance checks -- libgit2 0.21.2