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 | 99 | lib3270_popup_dialog( hSession, |
100 | 100 | LIB3270_NOTIFY_ERROR, |
101 | 101 | _( "Network error" ), |
102 | - _( "Unable to connect to server." ), | |
102 | + _( "Unable to connect to host." ), | |
103 | 103 | #ifdef _WIN32 |
104 | 104 | _( "%s"), lib3270_win32_strerror(err) |
105 | 105 | #else |
... | ... | @@ -176,6 +176,7 @@ static void net_connected(H3270 *hSession) |
176 | 176 | LIB3270_EXPORT int lib3270_connect_host(H3270 *hSession, const char *hostname, const char *srvc, LIB3270_CONNECT_OPTION opt) |
177 | 177 | { |
178 | 178 | int s; |
179 | + int optval; | |
179 | 180 | struct addrinfo hints; |
180 | 181 | struct addrinfo * result = NULL; |
181 | 182 | struct addrinfo * rp = NULL; |
... | ... | @@ -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 | 403 | #else |
370 | 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 | 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 | 455 | #endif // WIN32 |
391 | 456 | } |
392 | 457 | |
... | ... | @@ -395,13 +460,6 @@ static void net_connected(H3270 *hSession) |
395 | 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 | 463 | #if defined(OMTU) |
406 | 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 | 491 | |
434 | 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 | 496 | while(time(0) < end) |
439 | 497 | { |
... | ... | @@ -442,18 +500,28 @@ static void net_connected(H3270 *hSession) |
442 | 500 | switch(hSession->cstate) |
443 | 501 | { |
444 | 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 | 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 | 515 | return 0; |
450 | 516 | |
451 | 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 | 519 | return -1; |
454 | 520 | } |
455 | 521 | |
456 | 522 | } |
523 | + | |
524 | + lib3270_disconnect(hSession); | |
457 | 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 | 373 | |
374 | 374 | retry: |
375 | 375 | |
376 | + inputs_changed = 0; | |
377 | + | |
376 | 378 | // If we've processed any input, then don't block again. |
377 | 379 | if(processed_any) |
378 | 380 | block = 0; |
379 | 381 | |
380 | 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 | 384 | #if defined(_WIN32) |
471 | 385 | |
472 | 386 | for (ip = inputs; ip != (input_t *)NULL; ip = ip->next) |
... | ... | @@ -589,7 +503,8 @@ retry: |
589 | 503 | } |
590 | 504 | else |
591 | 505 | { |
592 | - twait.tv_sec = twait.tv_usec = 0L; | |
506 | + twait.tv_sec = 1; | |
507 | + twait.tv_usec = 0L; | |
593 | 508 | tp = &twait; |
594 | 509 | |
595 | 510 | if(!events) |
... | ... | @@ -597,6 +512,7 @@ retry: |
597 | 512 | } |
598 | 513 | |
599 | 514 | ns = select(FD_SETSIZE, &rfds, &wfds, &xfds, tp); |
515 | + | |
600 | 516 | if (ns < 0 && errno != EINTR) |
601 | 517 | { |
602 | 518 | lib3270_popup_dialog( hSession, |
... | ... | @@ -639,7 +555,8 @@ retry: |
639 | 555 | #endif |
640 | 556 | |
641 | 557 | // See what's expired. |
642 | - if (timeouts != TN) { | |
558 | + if (timeouts != TN) | |
559 | + { | |
643 | 560 | #if defined(_WIN32) |
644 | 561 | ms_ts(&now); |
645 | 562 | #else |
... | ... | @@ -649,11 +566,11 @@ retry: |
649 | 566 | while ((t = timeouts) != TN) |
650 | 567 | { |
651 | 568 | #if defined(_WIN32) |
652 | - if (t->ts <= now) { | |
569 | + if (t->ts <= now) | |
653 | 570 | #else |
654 | 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 | 572 | #endif |
573 | + { | |
657 | 574 | timeouts = t->next; |
658 | 575 | t->in_play = True; |
659 | 576 | (*t->proc)(t->session); | ... | ... |
src/lib3270/lib3270.cbp
src/lib3270/testprogram.c
... | ... | @@ -22,6 +22,7 @@ static void * mainloop(void *dunno) |
22 | 22 | int main(int numpar, char *param[]) |
23 | 23 | { |
24 | 24 | H3270 * h; |
25 | + int rc; | |
25 | 26 | // char line[4096]; |
26 | 27 | // pthread_t thread; |
27 | 28 | |
... | ... | @@ -35,8 +36,10 @@ int main(int numpar, char *param[]) |
35 | 36 | // pthread_create(&thread, NULL, mainloop, NULL); |
36 | 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 | 44 | mainloop(0); |
42 | 45 | ... | ... |