From 93053fe45f4810c9c6cfb00bf2b049e49ca44289 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Sun, 26 Jul 2020 14:22:30 -0300 Subject: [PATCH] Refactoring and centralizing auto-reconnect feature. --- src/core/connect.c | 10 +++------- src/core/host.c | 31 ++++++++++++++++++++++++------- src/core/telnet.c | 9 ++++----- src/include/internals.h | 17 ++++++++++++----- src/ssl/state.c | 1 + 5 files changed, 44 insertions(+), 24 deletions(-) diff --git a/src/core/connect.c b/src/core/connect.c index 5c4c7ed..58d4938 100644 --- a/src/core/connect.c +++ b/src/core/connect.c @@ -83,19 +83,15 @@ LIB3270_POPUP popup = { .name = "CantConnect", - .title = _( "Connection failed" ), +// .title = _( "Connection failed" ), .type = LIB3270_NOTIFY_INFO, .summary = summary, .body = message, .label = _("Try again") }; -// if(hSession->cbk.popup_show(hSession,&popup,lib3270_get_toggle(hSession,LIB3270_TOGGLE_RECONNECT) && !hSession->auto_reconnect_inprogress) == 0) { - if(hSession->cbk.popup_show(hSession,&popup,!hSession->auto_reconnect_inprogress) == 0) { - // Schedule an automatic reconnection. - hSession->auto_reconnect_inprogress = 1; - (void) AddTimer(1000, hSession, lib3270_check_for_auto_reconnect); - } + if(hSession->cbk.popup_show(hSession,&popup,!hSession->auto_reconnect_inprogress) == 0) + lib3270_activate_auto_reconnect(hSession,1000); } diff --git a/src/core/host.c b/src/core/host.c index 16334f0..d182d99 100644 --- a/src/core/host.c +++ b/src/core/host.c @@ -64,7 +64,7 @@ /** * @brief Called from timer to attempt an automatic reconnection. */ -int lib3270_check_for_auto_reconnect(H3270 *hSession) +static int check_for_auto_reconnect(H3270 *hSession) { if(hSession->auto_reconnect_inprogress) @@ -78,6 +78,27 @@ int lib3270_check_for_auto_reconnect(H3270 *hSession) return 0; } +/** + * @brief Activate auto-reconnect timer. + * + * @param hSession TN3270 Session handle. + * @param msec Time to reconnect. + * + * @return 0 if ok or error code if not. + * + * @retval EBUSY Auto reconnect is already active. + */ +int lib3270_activate_auto_reconnect(H3270 *hSession, unsigned long msec) +{ + if(hSession->auto_reconnect_inprogress) + return EBUSY; + + hSession->auto_reconnect_inprogress = 1; + (void) AddTimer(msec, hSession, check_for_auto_reconnect); + + return 0; +} + LIB3270_EXPORT int lib3270_disconnect(H3270 *h) { return host_disconnect(h,0); @@ -95,12 +116,8 @@ int host_disconnect(H3270 *hSession, int failed) trace("Disconnected (Failed: %d Reconnect: %d in_progress: %d)",failed,lib3270_get_toggle(hSession,LIB3270_TOGGLE_RECONNECT),hSession->auto_reconnect_inprogress); - if(failed && lib3270_get_toggle(hSession,LIB3270_TOGGLE_RECONNECT) && !hSession->auto_reconnect_inprogress) - { - /* Schedule an automatic reconnection. */ - hSession->auto_reconnect_inprogress = 1; - (void) AddTimer(failed ? RECONNECT_ERR_MS : RECONNECT_MS, hSession, lib3270_check_for_auto_reconnect); - } + if(failed && lib3270_get_toggle(hSession,LIB3270_TOGGLE_RECONNECT)) + lib3270_activate_auto_reconnect(hSession,failed ? RECONNECT_ERR_MS : RECONNECT_MS); /* * Remember a disconnect from ANSI mode, to keep screen tracing diff --git a/src/core/telnet.c b/src/core/telnet.c index c19db4f..bd34c2a 100644 --- a/src/core/telnet.c +++ b/src/core/telnet.c @@ -2223,12 +2223,11 @@ static const char * cmd(int c) return nnn(c); } -/* - * opt - * Expands a TELNET option into a character string. +/*** + * + * @brief Expands a TELNET option into a character string. */ -static const char * -opt(unsigned char c) +static const char * opt(unsigned char c) { if (TELOPT_OK(c)) return TELOPT(c); diff --git a/src/include/internals.h b/src/include/internals.h index 39f772f..cffed11 100644 --- a/src/include/internals.h +++ b/src/include/internals.h @@ -753,11 +753,6 @@ LIB3270_INTERNAL int do_select(H3270 *h, unsigned int start, unsigned int end, LIB3270_INTERNAL void connection_failed(H3270 *hSession, const char *message); -/** - * @brief Called from timer to attempt an automatic reconnection. - */ -LIB3270_INTERNAL int lib3270_check_for_auto_reconnect(H3270 *hSession); - #if defined(DEBUG) #define CHECK_SESSION_HANDLE(x) check_session_handle(&x,__FUNCTION__); LIB3270_INTERNAL void check_session_handle(H3270 **hSession, const char *fname); @@ -766,6 +761,18 @@ LIB3270_INTERNAL int lib3270_check_for_auto_reconnect(H3270 *hSession); LIB3270_INTERNAL void check_session_handle(H3270 **hSession); #endif // DEBUG +/** + * @brief Activate auto-reconnect timer. + * + * @param hSession TN3270 Session handle. + * @param msec Time to reconnect. + * + * @return 0 if ok or error code if not. + * + * @retval EBUSY Auto reconnect is already active. + */ +LIB3270_INTERNAL int lib3270_activate_auto_reconnect(H3270 *hSession, unsigned long msec); + LIB3270_INTERNAL int check_online_session(const H3270 *hSession); LIB3270_INTERNAL int check_offline_session(const H3270 *hSession); diff --git a/src/ssl/state.c b/src/ssl/state.c index 20017d7..aa09cf9 100644 --- a/src/ssl/state.c +++ b/src/ssl/state.c @@ -78,6 +78,7 @@ void set_ssl_state(H3270 *hSession, LIB3270_SSL_STATE state) hSession->ssl.state = state; trace_dsn(hSession,"SSL state changes to %d\n",(int) state); + debug("SSL state changes to %d\n",(int) state); hSession->cbk.update_ssl(hSession,hSession->ssl.state); } -- libgit2 0.21.2