Commit 76612edf670312af09306df5b028a4f2632145e2
1 parent
5b8675fe
Exists in
master
and in
3 other branches
Improving "auto-reconnect" toggle.
Showing
7 changed files
with
65 additions
and
27 deletions
Show diff stats
src/include/ansic.h
src/include/lib3270.h
... | ... | @@ -167,7 +167,7 @@ |
167 | 167 | } LIB3270_DIRECTION; |
168 | 168 | |
169 | 169 | /** |
170 | - * Toggle types. | |
170 | + * @brief Toggle types. | |
171 | 171 | * |
172 | 172 | */ |
173 | 173 | typedef enum _LIB3270_TOGGLE_TYPE |
... | ... | @@ -751,7 +751,7 @@ |
751 | 751 | * @param ix Toggle id. |
752 | 752 | * @param value New toggle state (non zero for true). |
753 | 753 | * |
754 | - * @returns 0 if the toggle is already at the state, 1 if the toggle was changed; < 0 on invalid toggle id | |
754 | + * @returns 0 if the toggle is already at the state, 1 if the toggle was changed; < 0 on error (sets errno). | |
755 | 755 | */ |
756 | 756 | LIB3270_EXPORT int lib3270_set_toggle(H3270 *h, LIB3270_TOGGLE ix, int value); |
757 | 757 | ... | ... |
src/lib3270/host.c
... | ... | @@ -55,19 +55,14 @@ |
55 | 55 | #include <lib3270/internals.h> |
56 | 56 | #include <lib3270/properties.h> |
57 | 57 | |
58 | -#define RECONNECT_MS 2000 /* 2 sec before reconnecting to host */ | |
59 | -#define RECONNECT_ERR_MS 5000 /* 5 sec before reconnecting to host */ | |
60 | - | |
61 | -static void try_reconnect(H3270 *session); | |
62 | - | |
63 | -/* | |
64 | - * Called from timer to attempt an automatic reconnection. | |
58 | +/** | |
59 | + * @brief Called from timer to attempt an automatic reconnection. | |
65 | 60 | */ |
66 | -static void try_reconnect(H3270 *session) | |
61 | +void lib3270_reconnect(H3270 *hSession) | |
67 | 62 | { |
68 | - lib3270_write_log(session,"3270","Starting auto-reconnect (Host: %s)",session->host.full ? session->host.full : "-"); | |
69 | - session->auto_reconnect_inprogress = 0; | |
70 | - lib3270_connect(session,0); | |
63 | + lib3270_write_log(hSession,"3270","Starting auto-reconnect on %s",lib3270_get_url(hSession)); | |
64 | + hSession->auto_reconnect_inprogress = 0; | |
65 | + lib3270_connect(hSession,0); | |
71 | 66 | } |
72 | 67 | |
73 | 68 | LIB3270_EXPORT int lib3270_disconnect(H3270 *h) |
... | ... | @@ -90,7 +85,7 @@ int host_disconnect(H3270 *hSession, int failed) |
90 | 85 | { |
91 | 86 | /* Schedule an automatic reconnection. */ |
92 | 87 | hSession->auto_reconnect_inprogress = 1; |
93 | - (void) AddTimeOut(failed ? RECONNECT_ERR_MS: RECONNECT_MS, hSession, try_reconnect); | |
88 | + (void) AddTimeOut(failed ? RECONNECT_ERR_MS : RECONNECT_MS, hSession, lib3270_reconnect); | |
94 | 89 | } |
95 | 90 | |
96 | 91 | /* | ... | ... |
src/lib3270/linux/connect.c
... | ... | @@ -229,7 +229,6 @@ static void net_connected(H3270 *hSession, int fd unused, LIB3270_IO_FLAG flag u |
229 | 229 | struct resolver host; |
230 | 230 | |
231 | 231 | CHECK_SESSION_HANDLE(hSession); |
232 | - | |
233 | 232 | memset(&host,0,sizeof(host)); |
234 | 233 | |
235 | 234 | lib3270_main_iterate(hSession,0); |
... | ... | @@ -240,6 +239,16 @@ static void net_connected(H3270 *hSession, int fd unused, LIB3270_IO_FLAG flag u |
240 | 239 | if(hSession->sock > 0) |
241 | 240 | return errno = EBUSY; |
242 | 241 | |
242 | + if(!(hSession->host.current && hSession->host.srvc)) | |
243 | + { | |
244 | + // No host info, try the default one. | |
245 | + lib3270_set_url(hSession,NULL); | |
246 | + if(!(hSession->host.current && hSession->host.srvc)) | |
247 | + { | |
248 | + return errno = ENOENT; | |
249 | + } | |
250 | + } | |
251 | + | |
243 | 252 | #if defined(HAVE_LIBSSL) |
244 | 253 | set_ssl_state(hSession,LIB3270_SSL_UNSECURE); |
245 | 254 | #endif // HAVE_LIBSSL | ... | ... |
src/lib3270/private.h
... | ... | @@ -119,6 +119,9 @@ |
119 | 119 | #undef X3270_MENUS |
120 | 120 | #endif /*]*/ |
121 | 121 | |
122 | +#define RECONNECT_MS 2000 /**< @brief 2 sec before reconnecting to host. */ | |
123 | +#define RECONNECT_ERR_MS 5000 /**< @brief 5 sec before reconnecting to host when failed */ | |
124 | + | |
122 | 125 | /* types of internal actions */ |
123 | 126 | enum iaction { |
124 | 127 | IA_STRING, IA_PASTE, IA_REDRAW, |
... | ... | @@ -641,6 +644,11 @@ LIB3270_INTERNAL void lib3270_sock_disconnect(H3270 *hSession); |
641 | 644 | |
642 | 645 | LIB3270_INTERNAL int lib3270_default_event_dispatcher(H3270 *hSession, int block); |
643 | 646 | |
647 | +/** | |
648 | + * @brief Called from timer to attempt an automatic reconnection. | |
649 | + */ | |
650 | +LIB3270_INTERNAL void lib3270_reconnect(H3270 *hSession); | |
651 | + | |
644 | 652 | #if defined(DEBUG) |
645 | 653 | #define CHECK_SESSION_HANDLE(x) check_session_handle(&x,__FUNCTION__); |
646 | 654 | LIB3270_INTERNAL void check_session_handle(H3270 **hSession, const char *fname); | ... | ... |
src/lib3270/testprogram/testprogram.c
... | ... | @@ -42,16 +42,15 @@ int main(int argc, char *argv[]) |
42 | 42 | |
43 | 43 | } |
44 | 44 | |
45 | - printf("HOST URL: %s\HOST CRL: %s\n",lib3270_get_url(h),lib3270_get_crl_url(h)); | |
45 | +// printf("HOST URL: %s\HOST CRL: %s\n",lib3270_get_url(h),lib3270_get_crl_url(h)); | |
46 | 46 | |
47 | - if(lib3270_set_url(h,NULL)) | |
48 | - lib3270_set_url(h,"tn3270://fandezhi.efglobe.com"); | |
47 | +// if(lib3270_set_url(h,NULL)) | |
48 | +// lib3270_set_url(h,"tn3270://fandezhi.efglobe.com"); | |
49 | 49 | |
50 | -// lib3270_set_toggle(h,LIB3270_TOGGLE_DS_TRACE,1); | |
50 | + //lib3270_set_toggle(h,LIB3270_TOGGLE_DS_TRACE,1); | |
51 | 51 | lib3270_set_toggle(h,LIB3270_TOGGLE_SSL_TRACE,1); |
52 | 52 | |
53 | 53 | rc = lib3270_connect(h,120); |
54 | - | |
55 | 54 | printf("\nConnect %s exits with rc=%d\n",lib3270_get_url(h),rc); |
56 | 55 | |
57 | 56 | lib3270_wait_for_ready(h,10); | ... | ... |
src/lib3270/toggles.c
... | ... | @@ -57,7 +57,8 @@ |
57 | 57 | #include "screenc.h" |
58 | 58 | #include "trace_dsc.h" |
59 | 59 | #include "togglesc.h" |
60 | -#include "api.h" | |
60 | +//#include "api.h" | |
61 | +#include "utilc.h" | |
61 | 62 | |
62 | 63 | static const struct _toggle_info |
63 | 64 | { |
... | ... | @@ -255,6 +256,15 @@ static void toggle_notify(H3270 *session, struct lib3270_toggle *t, LIB3270_TOGG |
255 | 256 | |
256 | 257 | } |
257 | 258 | |
259 | +/** | |
260 | + * @brief Set toggle state. | |
261 | + * | |
262 | + * @param h Session handle. | |
263 | + * @param ix Toggle id. | |
264 | + * @param value New toggle state (non zero for true). | |
265 | + * | |
266 | + * @returns 0 if the toggle is already at the state, 1 if the toggle was changed; < 0 on error (sets errno). | |
267 | + */ | |
258 | 268 | LIB3270_EXPORT int lib3270_set_toggle(H3270 *session, LIB3270_TOGGLE ix, int value) |
259 | 269 | { |
260 | 270 | char v = value ? True : False; |
... | ... | @@ -263,7 +273,7 @@ LIB3270_EXPORT int lib3270_set_toggle(H3270 *session, LIB3270_TOGGLE ix, int val |
263 | 273 | CHECK_SESSION_HANDLE(session); |
264 | 274 | |
265 | 275 | if(ix < 0 || ix >= LIB3270_TOGGLE_COUNT) |
266 | - return -EINVAL; | |
276 | + return -(errno = EINVAL); | |
267 | 277 | |
268 | 278 | t = &session->toggle[ix]; |
269 | 279 | |
... | ... | @@ -330,6 +340,23 @@ static void toggle_keepalive(H3270 *session, struct lib3270_toggle *t unused, LI |
330 | 340 | } |
331 | 341 | } |
332 | 342 | |
343 | +static void toggle_reconnect(H3270 *hSession, struct lib3270_toggle *t unused, LIB3270_TOGGLE_TYPE type ) | |
344 | +{ | |
345 | + | |
346 | + // If already connected or not interactive returns. | |
347 | + if(hSession->sock > 0 || type != TT_INTERACTIVE) | |
348 | + return; | |
349 | + | |
350 | + if(t->value && !hSession->auto_reconnect_inprogress) | |
351 | + { | |
352 | + /* Schedule an automatic reconnection. */ | |
353 | + lib3270_write_log(hSession,"toggle","Auto-reconnect toggle was activated when offline, reconnecting"); | |
354 | + hSession->auto_reconnect_inprogress = 1; | |
355 | + (void) AddTimeOut(RECONNECT_MS, hSession, lib3270_reconnect); | |
356 | + } | |
357 | + | |
358 | +} | |
359 | + | |
333 | 360 | /** |
334 | 361 | * @brief Called from system initialization code to handle initial toggle settings. |
335 | 362 | */ |
... | ... | @@ -345,6 +372,7 @@ void initialize_toggles(H3270 *session) |
345 | 372 | session->toggle[LIB3270_TOGGLE_UNDERLINE].upcall = toggle_redraw; |
346 | 373 | session->toggle[LIB3270_TOGGLE_ALTSCREEN].upcall = toggle_altscreen; |
347 | 374 | session->toggle[LIB3270_TOGGLE_KEEP_ALIVE].upcall = toggle_keepalive; |
375 | + session->toggle[LIB3270_TOGGLE_RECONNECT].upcall = toggle_reconnect; | |
348 | 376 | |
349 | 377 | for(f=0;f<LIB3270_TOGGLE_COUNT;f++) |
350 | 378 | { |
... | ... | @@ -355,8 +383,8 @@ void initialize_toggles(H3270 *session) |
355 | 383 | |
356 | 384 | } |
357 | 385 | |
358 | -/* | |
359 | - * Called from system exit code to handle toggles. | |
386 | +/** | |
387 | + * @brief Called from system exit code to handle toggles. | |
360 | 388 | */ |
361 | 389 | void shutdown_toggles(H3270 *session) |
362 | 390 | { | ... | ... |