From 76612edf670312af09306df5b028a4f2632145e2 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Wed, 16 Jan 2019 10:01:43 -0200 Subject: [PATCH] Improving "auto-reconnect" toggle. --- src/include/ansic.h | 5 ++--- src/include/lib3270.h | 4 ++-- src/lib3270/host.c | 19 +++++++------------ src/lib3270/linux/connect.c | 11 ++++++++++- src/lib3270/private.h | 8 ++++++++ src/lib3270/testprogram/testprogram.c | 9 ++++----- src/lib3270/toggles.c | 36 ++++++++++++++++++++++++++++++++---- 7 files changed, 65 insertions(+), 27 deletions(-) diff --git a/src/include/ansic.h b/src/include/ansic.h index e8f1120..628874a 100644 --- a/src/include/ansic.h +++ b/src/include/ansic.h @@ -12,9 +12,8 @@ * for more details. */ -/* - * ansic.h - * Global declarations for ansi.c. +/** + * @brief Global declarations for ansi.c. */ #if defined(X3270_ANSI) /*[*/ diff --git a/src/include/lib3270.h b/src/include/lib3270.h index ce851ae..c1daef2 100644 --- a/src/include/lib3270.h +++ b/src/include/lib3270.h @@ -167,7 +167,7 @@ } LIB3270_DIRECTION; /** - * Toggle types. + * @brief Toggle types. * */ typedef enum _LIB3270_TOGGLE_TYPE @@ -751,7 +751,7 @@ * @param ix Toggle id. * @param value New toggle state (non zero for true). * - * @returns 0 if the toggle is already at the state, 1 if the toggle was changed; < 0 on invalid toggle id + * @returns 0 if the toggle is already at the state, 1 if the toggle was changed; < 0 on error (sets errno). */ LIB3270_EXPORT int lib3270_set_toggle(H3270 *h, LIB3270_TOGGLE ix, int value); diff --git a/src/lib3270/host.c b/src/lib3270/host.c index eec0bc9..f452901 100644 --- a/src/lib3270/host.c +++ b/src/lib3270/host.c @@ -55,19 +55,14 @@ #include #include -#define RECONNECT_MS 2000 /* 2 sec before reconnecting to host */ -#define RECONNECT_ERR_MS 5000 /* 5 sec before reconnecting to host */ - -static void try_reconnect(H3270 *session); - -/* - * Called from timer to attempt an automatic reconnection. +/** + * @brief Called from timer to attempt an automatic reconnection. */ -static void try_reconnect(H3270 *session) +void lib3270_reconnect(H3270 *hSession) { - lib3270_write_log(session,"3270","Starting auto-reconnect (Host: %s)",session->host.full ? session->host.full : "-"); - session->auto_reconnect_inprogress = 0; - lib3270_connect(session,0); + lib3270_write_log(hSession,"3270","Starting auto-reconnect on %s",lib3270_get_url(hSession)); + hSession->auto_reconnect_inprogress = 0; + lib3270_connect(hSession,0); } LIB3270_EXPORT int lib3270_disconnect(H3270 *h) @@ -90,7 +85,7 @@ int host_disconnect(H3270 *hSession, int failed) { /* Schedule an automatic reconnection. */ hSession->auto_reconnect_inprogress = 1; - (void) AddTimeOut(failed ? RECONNECT_ERR_MS: RECONNECT_MS, hSession, try_reconnect); + (void) AddTimeOut(failed ? RECONNECT_ERR_MS : RECONNECT_MS, hSession, lib3270_reconnect); } /* diff --git a/src/lib3270/linux/connect.c b/src/lib3270/linux/connect.c index dbea3a1..0aa6d59 100644 --- a/src/lib3270/linux/connect.c +++ b/src/lib3270/linux/connect.c @@ -229,7 +229,6 @@ static void net_connected(H3270 *hSession, int fd unused, LIB3270_IO_FLAG flag u struct resolver host; CHECK_SESSION_HANDLE(hSession); - memset(&host,0,sizeof(host)); lib3270_main_iterate(hSession,0); @@ -240,6 +239,16 @@ static void net_connected(H3270 *hSession, int fd unused, LIB3270_IO_FLAG flag u if(hSession->sock > 0) return errno = EBUSY; + if(!(hSession->host.current && hSession->host.srvc)) + { + // No host info, try the default one. + lib3270_set_url(hSession,NULL); + if(!(hSession->host.current && hSession->host.srvc)) + { + return errno = ENOENT; + } + } + #if defined(HAVE_LIBSSL) set_ssl_state(hSession,LIB3270_SSL_UNSECURE); #endif // HAVE_LIBSSL diff --git a/src/lib3270/private.h b/src/lib3270/private.h index 82caa52..3f6bb22 100644 --- a/src/lib3270/private.h +++ b/src/lib3270/private.h @@ -119,6 +119,9 @@ #undef X3270_MENUS #endif /*]*/ +#define RECONNECT_MS 2000 /**< @brief 2 sec before reconnecting to host. */ +#define RECONNECT_ERR_MS 5000 /**< @brief 5 sec before reconnecting to host when failed */ + /* types of internal actions */ enum iaction { IA_STRING, IA_PASTE, IA_REDRAW, @@ -641,6 +644,11 @@ LIB3270_INTERNAL void lib3270_sock_disconnect(H3270 *hSession); LIB3270_INTERNAL int lib3270_default_event_dispatcher(H3270 *hSession, int block); +/** + * @brief Called from timer to attempt an automatic reconnection. + */ +LIB3270_INTERNAL void lib3270_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); diff --git a/src/lib3270/testprogram/testprogram.c b/src/lib3270/testprogram/testprogram.c index 8bc4f96..29820cd 100644 --- a/src/lib3270/testprogram/testprogram.c +++ b/src/lib3270/testprogram/testprogram.c @@ -42,16 +42,15 @@ int main(int argc, char *argv[]) } - printf("HOST URL: %s\HOST CRL: %s\n",lib3270_get_url(h),lib3270_get_crl_url(h)); +// printf("HOST URL: %s\HOST CRL: %s\n",lib3270_get_url(h),lib3270_get_crl_url(h)); - if(lib3270_set_url(h,NULL)) - lib3270_set_url(h,"tn3270://fandezhi.efglobe.com"); +// if(lib3270_set_url(h,NULL)) +// lib3270_set_url(h,"tn3270://fandezhi.efglobe.com"); -// lib3270_set_toggle(h,LIB3270_TOGGLE_DS_TRACE,1); + //lib3270_set_toggle(h,LIB3270_TOGGLE_DS_TRACE,1); lib3270_set_toggle(h,LIB3270_TOGGLE_SSL_TRACE,1); rc = lib3270_connect(h,120); - printf("\nConnect %s exits with rc=%d\n",lib3270_get_url(h),rc); lib3270_wait_for_ready(h,10); diff --git a/src/lib3270/toggles.c b/src/lib3270/toggles.c index 7422e9a..c5daabf 100644 --- a/src/lib3270/toggles.c +++ b/src/lib3270/toggles.c @@ -57,7 +57,8 @@ #include "screenc.h" #include "trace_dsc.h" #include "togglesc.h" -#include "api.h" +//#include "api.h" +#include "utilc.h" static const struct _toggle_info { @@ -255,6 +256,15 @@ static void toggle_notify(H3270 *session, struct lib3270_toggle *t, LIB3270_TOGG } +/** + * @brief Set toggle state. + * + * @param h Session handle. + * @param ix Toggle id. + * @param value New toggle state (non zero for true). + * + * @returns 0 if the toggle is already at the state, 1 if the toggle was changed; < 0 on error (sets errno). + */ LIB3270_EXPORT int lib3270_set_toggle(H3270 *session, LIB3270_TOGGLE ix, int value) { char v = value ? True : False; @@ -263,7 +273,7 @@ LIB3270_EXPORT int lib3270_set_toggle(H3270 *session, LIB3270_TOGGLE ix, int val CHECK_SESSION_HANDLE(session); if(ix < 0 || ix >= LIB3270_TOGGLE_COUNT) - return -EINVAL; + return -(errno = EINVAL); t = &session->toggle[ix]; @@ -330,6 +340,23 @@ static void toggle_keepalive(H3270 *session, struct lib3270_toggle *t unused, LI } } +static void toggle_reconnect(H3270 *hSession, struct lib3270_toggle *t unused, LIB3270_TOGGLE_TYPE type ) +{ + + // If already connected or not interactive returns. + if(hSession->sock > 0 || type != TT_INTERACTIVE) + return; + + if(t->value && !hSession->auto_reconnect_inprogress) + { + /* Schedule an automatic reconnection. */ + lib3270_write_log(hSession,"toggle","Auto-reconnect toggle was activated when offline, reconnecting"); + hSession->auto_reconnect_inprogress = 1; + (void) AddTimeOut(RECONNECT_MS, hSession, lib3270_reconnect); + } + +} + /** * @brief Called from system initialization code to handle initial toggle settings. */ @@ -345,6 +372,7 @@ void initialize_toggles(H3270 *session) session->toggle[LIB3270_TOGGLE_UNDERLINE].upcall = toggle_redraw; session->toggle[LIB3270_TOGGLE_ALTSCREEN].upcall = toggle_altscreen; session->toggle[LIB3270_TOGGLE_KEEP_ALIVE].upcall = toggle_keepalive; + session->toggle[LIB3270_TOGGLE_RECONNECT].upcall = toggle_reconnect; for(f=0;f