From 731b0d340a1e5c50f31a6da0d7fd2d92cf8ea81b Mon Sep 17 00:00:00 2001 From: perry.werneck@gmail.com Date: Thu, 5 Dec 2013 01:47:19 +0000 Subject: [PATCH] Melhorando o processo de conexao ao host --- connect.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------- iocalls.c | 66 +++++++++++++++++++++++++++++++++--------------------------------- kybd.c | 2 +- lib3270.cbp | 12 +++++------- telnet.c | 12 ++++++------ testprogram.c | 17 ++++++++++------- util.c | 15 +++------------ 7 files changed, 112 insertions(+), 81 deletions(-) diff --git a/connect.c b/connect.c index e9c049b..d892886 100644 --- a/connect.c +++ b/connect.c @@ -67,9 +67,45 @@ static void net_connected(H3270 *hSession) { + int err; + socklen_t len = sizeof(err); + + trace("%s",__FUNCTION__); RemoveSource(hSession->ns_write_id); hSession->ns_write_id = NULL; + if(getsockopt(hSession->sock, SOL_SOCKET, SO_ERROR, (char *) &err, &len) < 0) + { + lib3270_disconnect(hSession); + lib3270_popup_dialog( hSession, + LIB3270_NOTIFY_ERROR, + _( "Network error" ), + _( "Unable to get connection state." ), +#ifdef _WIN32 + _( "Winsock Error %d"), WSAGetLastError() +#else + _( "%s" ), strerror(errno) +#endif // _WIN32 + ); + return; + } + else if(err) + { + lib3270_disconnect(hSession); + lib3270_popup_dialog( hSession, + LIB3270_NOTIFY_ERROR, + _( "Network error" ), + _( "Unable to connect to server." ), +#ifdef _WIN32 + _( "%s"), lib3270_win32_strerror(err) +#else + _( "%s" ), strerror(err) +#endif // _WIN32 + ); + return; + } + + #ifdef _WIN32 hSession->ns_exception_id = AddExcept(hSession->sockEvent, hSession, net_exception); hSession->ns_read_id = AddInput(hSession->sockEvent, hSession, net_input); @@ -114,7 +150,7 @@ static void net_connected(H3270 *hSession) LIB3270_NOTIFY_CRITICAL, N_( "Network startup error" ), N_( "WSAStartup failed" ), - "%s", win32_strerror(GetLastError()) ); + "%s", lib3270_win32_strerror(GetLastError()) ); _exit(1); } @@ -223,50 +259,51 @@ static void net_connected(H3270 *hSession) if(hSession->sock < 0) continue; + trace("sock=%d",hSession->sock); + #ifdef WIN32 - u_long block; - u_int len = sizeof(int); - if(session->sockEvent == NULL) + if(hSession->sockEvent == NULL) { char ename[256]; snprintf(ename, 255, "%s-%d", PACKAGE_NAME, getpid()); - session->sockEvent = CreateEvent(NULL, TRUE, FALSE, ename); - if(session->sockEvent == NULL) + hSession->sockEvent = CreateEvent(NULL, TRUE, FALSE, ename); + if(hSession->sockEvent == NULL) { - lib3270_popup_dialog( session, + lib3270_popup_dialog( hSession, LIB3270_NOTIFY_CRITICAL, N_( "Network startup error" ), N_( "Cannot create socket handle" ), - "%s", win32_strerror(GetLastError()) ); + "%s", lib3270_win32_strerror(GetLastError()) ); _exit(1); } } - if (WSAEventSelect(session->sock, session->sockEvent, FD_READ | FD_CONNECT | FD_CLOSE) != 0) + if (WSAEventSelect(hSession->sock, hSession->sockEvent, FD_READ | FD_CONNECT | FD_CLOSE) != 0) { - lib3270_popup_dialog( session, + lib3270_popup_dialog( hSession, LIB3270_NOTIFY_CRITICAL, N_( "Network startup error" ), N_( "WSAEventSelect failed" ), - "%s", win32_strerror(GetLastError()) ); + "%s", lib3270_win32_strerror(GetLastError()) ); _exit(1); } WSASetLastError(0); - block = 0; + u_long iMode=1; + trace("sock=%d",hSession->sock); - if(ioctlsocket(hSession->sock,FIONBIO,&block)) + if(ioctlsocket(hSession->sock,FIONBIO,&iMode)) { lib3270_popup_dialog( hSession, LIB3270_NOTIFY_ERROR, _( "Connection error" ), _( "ioctlsocket(FIONBIO) failed." ), - "%s", win32_strerror(GetLastError())); + "%s", lib3270_win32_strerror(GetLastError())); SOCK_CLOSE(hSession); } @@ -279,7 +316,7 @@ static void net_connected(H3270 *hSession) LIB3270_NOTIFY_ERROR, _( "Connection error" ), _( "Can't connect to host." ), - "%s", win32_strerror(GetLastError())); + "%s", lib3270_win32_strerror(GetLastError())); SOCK_CLOSE(hSession); } } @@ -342,11 +379,13 @@ static void net_connected(H3270 *hSession) lib3270_st_changed(hSession, LIB3270_STATE_HALF_CONNECT, True); #ifdef _WIN32 + trace("Sockevent=%08lx callback=%p",hSession->sockEvent,net_connected); hSession->ns_write_id = AddOutput(hSession->sockEvent, hSession, net_connected); #else hSession->ns_write_id = AddOutput(hSession->sock, hSession, net_connected); #endif // WIN32 + trace("%s: Connection in progress",__FUNCTION__); return 0; } diff --git a/iocalls.c b/iocalls.c index a3dd08d..9b71e82 100644 --- a/iocalls.c +++ b/iocalls.c @@ -269,8 +269,6 @@ static void * internal_add_input(int source, H3270 *session, void (*fn)(H3270 *s { input_t *ip = (input_t *) lib3270_malloc(sizeof(input_t)); - trace("%s session=%p proc=%p handle=%p",__FUNCTION__,session,fn,ip); - ip->source = source; ip->condition = InputReadMask; ip->proc = fn; @@ -290,8 +288,6 @@ static void * internal_add_output(int source, H3270 *session, void (*fn)(H3270 * { input_t *ip = (input_t *) lib3270_malloc(sizeof(input_t)); - trace("%s session=%p proc=%p handle=%p",__FUNCTION__,session,fn,ip); - ip->source = source; ip->condition = InputWriteMask; ip->proc = fn; @@ -314,8 +310,6 @@ static void * internal_add_except(int source, H3270 *session, void (*fn)(H3270 * { input_t *ip = (input_t *) lib3270_malloc(sizeof(input_t)); - trace("%s session=%p proc=%p",__FUNCTION__,session,fn); - ip->source = source; ip->condition = InputExceptMask; ip->proc = fn; @@ -324,7 +318,7 @@ static void * internal_add_except(int source, H3270 *session, void (*fn)(H3270 * inputs = ip; inputs_changed = True; - trace("%s: fd=%d callback=%p handle=%p",__FUNCTION__,source,fn,ip); + trace("%s session=%p proc=%p handle=%p",__FUNCTION__,ip->session,ip->proc,ip); return ip; } @@ -335,8 +329,6 @@ static void internal_remove_source(void *id) input_t *ip; input_t *prev = (input_t *)NULL; - trace("%s: fhandle=%p",__FUNCTION__,(input_t *) id); - for (ip = inputs; ip != (input_t *)NULL; ip = ip->next) { if (ip == (input_t *)id) @@ -347,7 +339,7 @@ static void internal_remove_source(void *id) if (ip == (input_t *)NULL) { - lib3270_write_log(NULL,"lib3270","Double removal on %s: Input %p wasnt found in the list",__FUNCTION__,id); + lib3270_write_log(NULL,"lib3270","Invalid call to (%s): Input %p wasnt found in the list",__FUNCTION__,id); return; } @@ -367,7 +359,6 @@ static int internal_event_dispatcher(H3270 *hSession, int block) HANDLE ha[MAX_HA]; DWORD events; DWORD tmo; - DWORD ret; unsigned long long now; int i; #else @@ -376,7 +367,7 @@ static int internal_event_dispatcher(H3270 *hSession, int block) struct timeval now, twait, *tp; int events; #endif - input_t *ip, *ip_next; + input_t *ip; struct timeout *t; int processed_any = 0; @@ -512,30 +503,41 @@ retry: tmo = 1; } - ret = WaitForMultipleObjects(nha, ha, FALSE, tmo); - if (ret == WAIT_FAILED) + if(events) { - lib3270_popup_dialog( hSession, - LIB3270_NOTIFY_ERROR, - _( "Network error" ), - _( "WaitForMultipleObjects() failed when processing for events." ), - "Windows error %d", - GetLastError()); - } - - inputs_changed = False; + DWORD ret = WaitForMultipleObjects(events, ha, FALSE, tmo); - for (i = 0, ip = inputs; ip != (input_t *)NULL; ip = ip_next, i++) - { - if(ret == WAIT_OBJECT_0 + i) + if (ret == WAIT_FAILED) { - (*ip->proc)(ip->session); - processed_any = True; - if (inputs_changed) - goto retry; + lib3270_popup_dialog( hSession, + LIB3270_NOTIFY_ERROR, + _( "Network error" ), + _( "WaitForMultipleObjects() failed when processing for events." ), + "%s", + lib3270_win32_strerror(GetLastError())); + lib3270_disconnect(hSession); } + else + { + inputs_changed = False; + for (i = 0, ip = inputs; ip != (input_t *)NULL; ip = ip->next, i++) + { + if(ret == WAIT_OBJECT_0 + i) + { + (*ip->proc)(ip->session); + processed_any = True; + if (inputs_changed) + goto retry; + } + } + } + } + else if(block) + { + Sleep(100); } + #else FD_ZERO(&rfds); @@ -606,10 +608,8 @@ retry: } else { - for (ip = inputs; ip != (input_t *) NULL; ip = ip_next) + for (ip = inputs; ip != (input_t *) NULL; ip = ip->next) { - ip_next = ip->next; - if (((unsigned long)ip->condition & InputReadMask) && FD_ISSET(ip->source, &rfds)) { (*ip->proc)(ip->session); diff --git a/kybd.c b/kybd.c index 9e8caa1..5dc967c 100644 --- a/kybd.c +++ b/kybd.c @@ -399,7 +399,7 @@ void lib3270_kybdlock_clear(H3270 *hSession, LIB3270_KL_STATE bits) { unsigned int n = hSession->kybdlock & ~( (unsigned int) bits); - trace("%s: kybdlock=%d",__FUNCTION__,n); +// trace("%s: kybdlock=%d",__FUNCTION__,n); if (n != hSession->kybdlock) { diff --git a/lib3270.cbp b/lib3270.cbp index 75713c5..c3cd57d 100644 --- a/lib3270.cbp +++ b/lib3270.cbp @@ -15,9 +15,6 @@ - - -