Commit c092d84ebb23c7377973f52e387c050faa1486f6
1 parent
2171f01c
Exists in
master
and in
5 other branches
Ainda implementando novo mecanismo de conexão sem threads e com mensagems melhores em caso de erro
Showing
4 changed files
with
94 additions
and
109 deletions
Show diff stats
src/lib3270/connect.c
@@ -99,7 +99,7 @@ static void net_connected(H3270 *hSession) | @@ -99,7 +99,7 @@ static void net_connected(H3270 *hSession) | ||
99 | lib3270_popup_dialog( hSession, | 99 | lib3270_popup_dialog( hSession, |
100 | LIB3270_NOTIFY_ERROR, | 100 | LIB3270_NOTIFY_ERROR, |
101 | _( "Network error" ), | 101 | _( "Network error" ), |
102 | - _( "Unable to connect to server." ), | 102 | + _( "Unable to connect to host." ), |
103 | #ifdef _WIN32 | 103 | #ifdef _WIN32 |
104 | _( "%s"), lib3270_win32_strerror(err) | 104 | _( "%s"), lib3270_win32_strerror(err) |
105 | #else | 105 | #else |
@@ -176,6 +176,7 @@ static void net_connected(H3270 *hSession) | @@ -176,6 +176,7 @@ static void net_connected(H3270 *hSession) | ||
176 | LIB3270_EXPORT int lib3270_connect_host(H3270 *hSession, const char *hostname, const char *srvc, LIB3270_CONNECT_OPTION opt) | 176 | LIB3270_EXPORT int lib3270_connect_host(H3270 *hSession, const char *hostname, const char *srvc, LIB3270_CONNECT_OPTION opt) |
177 | { | 177 | { |
178 | int s; | 178 | int s; |
179 | + int optval; | ||
179 | struct addrinfo hints; | 180 | struct addrinfo hints; |
180 | struct addrinfo * result = NULL; | 181 | struct addrinfo * result = NULL; |
181 | struct addrinfo * rp = NULL; | 182 | struct addrinfo * rp = NULL; |
@@ -366,6 +367,39 @@ static void net_connected(H3270 *hSession) | @@ -366,6 +367,39 @@ static void net_connected(H3270 *hSession) | ||
366 | } | 367 | } |
367 | } | 368 | } |
368 | 369 | ||
370 | + optval = 1; | ||
371 | + if (setsockopt(hSession->sock, SOL_SOCKET, SO_OOBINLINE, (char *)&optval,sizeof(optval)) < 0) | ||
372 | + { | ||
373 | + lib3270_popup_dialog( hSession, | ||
374 | + LIB3270_NOTIFY_ERROR, | ||
375 | + _( "Connection error" ), | ||
376 | + _( "setsockopt(SO_OOBINLINE) has failed" ), | ||
377 | + "%s", | ||
378 | + strerror(errno)); | ||
379 | + SOCK_CLOSE(hSession); | ||
380 | + } | ||
381 | + | ||
382 | + optval = lib3270_get_toggle(hSession,LIB3270_TOGGLE_KEEP_ALIVE) ? 1 : 0; | ||
383 | + if (setsockopt(hSession->sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&optval, sizeof(optval)) < 0) | ||
384 | + { | ||
385 | + char buffer[4096]; | ||
386 | + snprintf(buffer,4095,N_( "Can't %s network keep-alive" ), optval ? _( "enable" ) : _( "disable" )); | ||
387 | + | ||
388 | + popup_a_sockerr(session, ); | ||
389 | + | ||
390 | + lib3270_popup_dialog( hSession, | ||
391 | + LIB3270_NOTIFY_ERROR, | ||
392 | + _( "Connection error" ), | ||
393 | + buffer, | ||
394 | + "%s", | ||
395 | + strerror(errno)); | ||
396 | + SOCK_CLOSE(hSession); | ||
397 | + } | ||
398 | + else | ||
399 | + { | ||
400 | + trace_dsn(hSession,"Network keep-alive is %s\n",optval ? "enabled" : "disabled" ); | ||
401 | + } | ||
402 | + | ||
369 | #else | 403 | #else |
370 | fcntl(hSession->sock, F_SETFL,fcntl(hSession->sock,F_GETFL,0)|O_NONBLOCK); | 404 | fcntl(hSession->sock, F_SETFL,fcntl(hSession->sock,F_GETFL,0)|O_NONBLOCK); |
371 | 405 | ||
@@ -387,6 +421,37 @@ static void net_connected(H3270 *hSession) | @@ -387,6 +421,37 @@ static void net_connected(H3270 *hSession) | ||
387 | } | 421 | } |
388 | } | 422 | } |
389 | 423 | ||
424 | + optval = 1; | ||
425 | + if (setsockopt(hSession->sock, SOL_SOCKET, SO_OOBINLINE, (char *)&optval,sizeof(optval)) < 0) | ||
426 | + { | ||
427 | + lib3270_popup_dialog( hSession, | ||
428 | + LIB3270_NOTIFY_ERROR, | ||
429 | + _( "Connection error" ), | ||
430 | + _( "setsockopt(SO_OOBINLINE) has failed" ), | ||
431 | + "%s", | ||
432 | + strerror(errno)); | ||
433 | + SOCK_CLOSE(hSession); | ||
434 | + } | ||
435 | + | ||
436 | + optval = lib3270_get_toggle(hSession,LIB3270_TOGGLE_KEEP_ALIVE) ? 1 : 0; | ||
437 | + if (setsockopt(hSession->sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&optval, sizeof(optval)) < 0) | ||
438 | + { | ||
439 | + char buffer[4096]; | ||
440 | + snprintf(buffer,4095,N_( "Can't %s network keep-alive" ), optval ? _( "enable" ) : _( "disable" )); | ||
441 | + | ||
442 | + lib3270_popup_dialog( hSession, | ||
443 | + LIB3270_NOTIFY_ERROR, | ||
444 | + _( "Connection error" ), | ||
445 | + buffer, | ||
446 | + "%s", | ||
447 | + strerror(errno)); | ||
448 | + SOCK_CLOSE(hSession); | ||
449 | + } | ||
450 | + else | ||
451 | + { | ||
452 | + trace_dsn(hSession,"Network keep-alive is %s\n",optval ? "enabled" : "disabled" ); | ||
453 | + } | ||
454 | + | ||
390 | #endif // WIN32 | 455 | #endif // WIN32 |
391 | } | 456 | } |
392 | 457 | ||
@@ -395,13 +460,6 @@ static void net_connected(H3270 *hSession) | @@ -395,13 +460,6 @@ static void net_connected(H3270 *hSession) | ||
395 | // set options for inline out-of-band data and keepalives | 460 | // set options for inline out-of-band data and keepalives |
396 | 461 | ||
397 | /* | 462 | /* |
398 | - int on = 1; | ||
399 | - if (setsockopt(hSession->sock, SOL_SOCKET, SO_OOBINLINE, (char *)&on,sizeof(on)) < 0) | ||
400 | - { | ||
401 | - popup_a_sockerr(hSession, N_( "setsockopt(%s)" ), "SO_OOBINLINE"); | ||
402 | - SOCK_CLOSE(hSession); | ||
403 | - } | ||
404 | - | ||
405 | #if defined(OMTU) | 463 | #if defined(OMTU) |
406 | else if (setsockopt(hSession->sock, SOL_SOCKET, SO_SNDBUF, (char *)&mtu,sizeof(mtu)) < 0) | 464 | else if (setsockopt(hSession->sock, SOL_SOCKET, SO_SNDBUF, (char *)&mtu,sizeof(mtu)) < 0) |
407 | { | 465 | { |
@@ -433,7 +491,7 @@ static void net_connected(H3270 *hSession) | @@ -433,7 +491,7 @@ static void net_connected(H3270 *hSession) | ||
433 | 491 | ||
434 | if(opt&LIB3270_CONNECT_OPTION_WAIT) | 492 | if(opt&LIB3270_CONNECT_OPTION_WAIT) |
435 | { | 493 | { |
436 | - time_t end = time(0)+120; | 494 | + time_t end = time(0)+120; |
437 | 495 | ||
438 | while(time(0) < end) | 496 | while(time(0) < end) |
439 | { | 497 | { |
@@ -442,18 +500,28 @@ static void net_connected(H3270 *hSession) | @@ -442,18 +500,28 @@ static void net_connected(H3270 *hSession) | ||
442 | switch(hSession->cstate) | 500 | switch(hSession->cstate) |
443 | { | 501 | { |
444 | case LIB3270_PENDING: | 502 | case LIB3270_PENDING: |
503 | + case LIB3270_CONNECTED_INITIAL: | ||
504 | + case LIB3270_CONNECTED_ANSI: | ||
505 | + case LIB3270_CONNECTED_3270: | ||
506 | + case LIB3270_CONNECTED_INITIAL_E: | ||
507 | + case LIB3270_CONNECTED_NVT: | ||
508 | + case LIB3270_CONNECTED_SSCP: | ||
445 | break; | 509 | break; |
446 | 510 | ||
447 | - case CONNECTED_INITIAL: | ||
448 | - trace("%s: Connected, exiting wait",__FUNCTION__); | 511 | + case LIB3270_NOT_CONNECTED: |
512 | + return ENOTCONN; | ||
513 | + | ||
514 | + case LIB3270_CONNECTED_TN3270E: | ||
449 | return 0; | 515 | return 0; |
450 | 516 | ||
451 | default: | 517 | default: |
452 | - trace("%s: State changed to %d",__FUNCTION__,hSession->cstate); | 518 | + lib3270_write_log(hSession,"connect", "%s: State changed to unexpected state %d",__FUNCTION__,hSession->cstate); |
453 | return -1; | 519 | return -1; |
454 | } | 520 | } |
455 | 521 | ||
456 | } | 522 | } |
523 | + | ||
524 | + lib3270_disconnect(hSession); | ||
457 | return ETIMEDOUT; | 525 | return ETIMEDOUT; |
458 | } | 526 | } |
459 | 527 |
src/lib3270/iocalls.c
@@ -373,100 +373,14 @@ static int internal_event_dispatcher(H3270 *hSession, int block) | @@ -373,100 +373,14 @@ static int internal_event_dispatcher(H3270 *hSession, int block) | ||
373 | 373 | ||
374 | retry: | 374 | retry: |
375 | 375 | ||
376 | + inputs_changed = 0; | ||
377 | + | ||
376 | // If we've processed any input, then don't block again. | 378 | // If we've processed any input, then don't block again. |
377 | if(processed_any) | 379 | if(processed_any) |
378 | block = 0; | 380 | block = 0; |
379 | 381 | ||
380 | events = 0; | 382 | events = 0; |
381 | 383 | ||
382 | -/* | ||
383 | -#if defined(_WIN32) | ||
384 | - nha = 0; | ||
385 | -#else | ||
386 | - FD_ZERO(&rfds); | ||
387 | - FD_ZERO(&wfds); | ||
388 | - FD_ZERO(&xfds); | ||
389 | -#endif | ||
390 | - | ||
391 | - for (ip = inputs; ip != (input_t *)NULL; ip = ip->next) | ||
392 | - { | ||
393 | - if ((unsigned long)ip->condition & InputReadMask) | ||
394 | - { | ||
395 | -#if defined(_WIN32) | ||
396 | - ha[nha++] = ip->source; | ||
397 | -#else | ||
398 | - FD_SET(ip->source, &rfds); | ||
399 | -#endif | ||
400 | - any_events = True; | ||
401 | - } | ||
402 | - if ((unsigned long)ip->condition & InputWriteMask) | ||
403 | - { | ||
404 | -#if defined(_WIN32) | ||
405 | - ha[nha++] = ip->source; | ||
406 | -#else | ||
407 | - FD_SET(ip->source, &wfds); | ||
408 | -#endif | ||
409 | - any_events = True; | ||
410 | - } | ||
411 | -#if !defined(_WIN32) | ||
412 | - if ((unsigned long)ip->condition & InputExceptMask) | ||
413 | - { | ||
414 | - FD_SET(ip->source, &xfds); | ||
415 | - any_events = True; | ||
416 | - } | ||
417 | -#endif | ||
418 | - } | ||
419 | - | ||
420 | - if (block) | ||
421 | - { | ||
422 | - if (timeouts != TN) | ||
423 | - { | ||
424 | -#if defined(_WIN32) | ||
425 | - ms_ts(&now); | ||
426 | - if (now > timeouts->ts) | ||
427 | - tmo = 0; | ||
428 | - else | ||
429 | - tmo = timeouts->ts - now; | ||
430 | -#else | ||
431 | - (void) gettimeofday(&now, (void *)NULL); | ||
432 | - twait.tv_sec = timeouts->tv.tv_sec - now.tv_sec; | ||
433 | - twait.tv_usec = timeouts->tv.tv_usec - now.tv_usec; | ||
434 | - if (twait.tv_usec < 0L) { | ||
435 | - twait.tv_sec--; | ||
436 | - twait.tv_usec += MILLION; | ||
437 | - } | ||
438 | - if (twait.tv_sec < 0L) | ||
439 | - twait.tv_sec = twait.tv_usec = 0L; | ||
440 | - tp = &twait; | ||
441 | -#endif | ||
442 | - any_events = True; | ||
443 | - } | ||
444 | - else | ||
445 | - { | ||
446 | - // Block for 1 second (at maximal) | ||
447 | -#if defined(_WIN32) | ||
448 | - tmo = 1; | ||
449 | -#else | ||
450 | - twait.tv_sec = 1; | ||
451 | - twait.tv_usec = 0L; | ||
452 | - tp = &twait; | ||
453 | -#endif | ||
454 | - } | ||
455 | - } | ||
456 | - else | ||
457 | - { | ||
458 | -#if defined(_WIN32) | ||
459 | - tmo = 1; | ||
460 | -#else | ||
461 | - twait.tv_sec = twait.tv_usec = 0L; | ||
462 | - tp = &twait; | ||
463 | -#endif | ||
464 | - } | ||
465 | - | ||
466 | - if (!any_events) | ||
467 | - return processed_any; | ||
468 | -*/ | ||
469 | - | ||
470 | #if defined(_WIN32) | 384 | #if defined(_WIN32) |
471 | 385 | ||
472 | for (ip = inputs; ip != (input_t *)NULL; ip = ip->next) | 386 | for (ip = inputs; ip != (input_t *)NULL; ip = ip->next) |
@@ -589,7 +503,8 @@ retry: | @@ -589,7 +503,8 @@ retry: | ||
589 | } | 503 | } |
590 | else | 504 | else |
591 | { | 505 | { |
592 | - twait.tv_sec = twait.tv_usec = 0L; | 506 | + twait.tv_sec = 1; |
507 | + twait.tv_usec = 0L; | ||
593 | tp = &twait; | 508 | tp = &twait; |
594 | 509 | ||
595 | if(!events) | 510 | if(!events) |
@@ -597,6 +512,7 @@ retry: | @@ -597,6 +512,7 @@ retry: | ||
597 | } | 512 | } |
598 | 513 | ||
599 | ns = select(FD_SETSIZE, &rfds, &wfds, &xfds, tp); | 514 | ns = select(FD_SETSIZE, &rfds, &wfds, &xfds, tp); |
515 | + | ||
600 | if (ns < 0 && errno != EINTR) | 516 | if (ns < 0 && errno != EINTR) |
601 | { | 517 | { |
602 | lib3270_popup_dialog( hSession, | 518 | lib3270_popup_dialog( hSession, |
@@ -639,7 +555,8 @@ retry: | @@ -639,7 +555,8 @@ retry: | ||
639 | #endif | 555 | #endif |
640 | 556 | ||
641 | // See what's expired. | 557 | // See what's expired. |
642 | - if (timeouts != TN) { | 558 | + if (timeouts != TN) |
559 | + { | ||
643 | #if defined(_WIN32) | 560 | #if defined(_WIN32) |
644 | ms_ts(&now); | 561 | ms_ts(&now); |
645 | #else | 562 | #else |
@@ -649,11 +566,11 @@ retry: | @@ -649,11 +566,11 @@ retry: | ||
649 | while ((t = timeouts) != TN) | 566 | while ((t = timeouts) != TN) |
650 | { | 567 | { |
651 | #if defined(_WIN32) | 568 | #if defined(_WIN32) |
652 | - if (t->ts <= now) { | 569 | + if (t->ts <= now) |
653 | #else | 570 | #else |
654 | if (t->tv.tv_sec < now.tv_sec ||(t->tv.tv_sec == now.tv_sec && t->tv.tv_usec < now.tv_usec)) | 571 | if (t->tv.tv_sec < now.tv_sec ||(t->tv.tv_sec == now.tv_sec && t->tv.tv_usec < now.tv_usec)) |
655 | - { | ||
656 | #endif | 572 | #endif |
573 | + { | ||
657 | timeouts = t->next; | 574 | timeouts = t->next; |
658 | t->in_play = True; | 575 | t->in_play = True; |
659 | (*t->proc)(t->session); | 576 | (*t->proc)(t->session); |
src/lib3270/lib3270.cbp
@@ -96,9 +96,6 @@ | @@ -96,9 +96,6 @@ | ||
96 | <Option compilerVar="CC" /> | 96 | <Option compilerVar="CC" /> |
97 | </Unit> | 97 | </Unit> |
98 | <Unit filename="ctlrc.h" /> | 98 | <Unit filename="ctlrc.h" /> |
99 | - <Unit filename="fallbacks.c"> | ||
100 | - <Option compilerVar="CC" /> | ||
101 | - </Unit> | ||
102 | <Unit filename="ft.c"> | 99 | <Unit filename="ft.c"> |
103 | <Option compilerVar="CC" /> | 100 | <Option compilerVar="CC" /> |
104 | </Unit> | 101 | </Unit> |
src/lib3270/testprogram.c
@@ -22,6 +22,7 @@ static void * mainloop(void *dunno) | @@ -22,6 +22,7 @@ static void * mainloop(void *dunno) | ||
22 | int main(int numpar, char *param[]) | 22 | int main(int numpar, char *param[]) |
23 | { | 23 | { |
24 | H3270 * h; | 24 | H3270 * h; |
25 | + int rc; | ||
25 | // char line[4096]; | 26 | // char line[4096]; |
26 | // pthread_t thread; | 27 | // pthread_t thread; |
27 | 28 | ||
@@ -35,8 +36,10 @@ int main(int numpar, char *param[]) | @@ -35,8 +36,10 @@ int main(int numpar, char *param[]) | ||
35 | // pthread_create(&thread, NULL, mainloop, NULL); | 36 | // pthread_create(&thread, NULL, mainloop, NULL); |
36 | // pthread_detach(thread); | 37 | // pthread_detach(thread); |
37 | 38 | ||
38 | -// lib3270_connect_host(h, "fandezhi.efglobe.com", "telnet", LIB3270_CONNECT_OPTION_WAIT); | ||
39 | - lib3270_connect_host(h, "127.0.0.1", "9090", LIB3270_CONNECT_OPTION_WAIT); | 39 | +// rc = lib3270_connect_host(h, "fandezhi.efglobe.com", "telnet", LIB3270_CONNECT_OPTION_WAIT); |
40 | + rc = lib3270_connect_host(h, "127.0.0.1", "9090", LIB3270_CONNECT_OPTION_WAIT); | ||
41 | + | ||
42 | + printf("\nConnect exits with rc=%d\n",rc); | ||
40 | 43 | ||
41 | mainloop(0); | 44 | mainloop(0); |
42 | 45 |