From 353fee04940716bed5342f0a62b3bfb8b78fffa1 Mon Sep 17 00:00:00 2001 From: perry.werneck@gmail.com Date: Wed, 4 Dec 2013 17:36:08 +0000 Subject: [PATCH] Iniciando remoção das chamadas a gethostbyname --- connect.c | 10 +++++++++- iocalls.c | 280 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------- sources.mak | 1 + telnet.c | 2 +- utilc.h | 8 +++++--- 5 files changed, 233 insertions(+), 68 deletions(-) diff --git a/connect.c b/connect.c index a811302..b847c32 100644 --- a/connect.c +++ b/connect.c @@ -126,6 +126,9 @@ } } + #error Implementar + + /* if(sock > 0) { // Connection in progress, wait until socket is available for write @@ -203,6 +206,7 @@ } } } + */ #else fcntl(sock, F_SETFL,fcntl(sock,F_GETFL,0)|O_NONBLOCK); @@ -223,6 +227,9 @@ } } + #error Implementar + +/* if(sock > 0) { // Connection in progress, wait until socket is available for write @@ -301,6 +308,7 @@ } } } + */ #endif // WIN32 } @@ -308,7 +316,7 @@ status_changed(hSession,saved_status); - return sock; + return 0; } diff --git a/iocalls.c b/iocalls.c index 3fe031d..7097a6f 100644 --- a/iocalls.c +++ b/iocalls.c @@ -48,20 +48,19 @@ /*---[ Standard calls ]-------------------------------------------------------------------------------------*/ static void internal_remove_timeout(void *timer); +static void internal_remove_source(void *id); static void * internal_add_timeout(unsigned long interval_ms, H3270 *session, void (*proc)(H3270 *session)); #ifdef WIN32 static void * internal_add_input(HANDLE source, H3270 *session, void (*fn)(H3270 *session)); + static void * internal_add_output(HANDLE source, H3270 *session, void (*fn)(H3270 *session)); static void * internal_add_except(HANDLE source, H3270 *session, void (*fn)(H3270 *session)); #else static void * internal_add_input(int source, H3270 *session, void (*fn)(H3270 *session)); + static void * internal_add_output(int source, H3270 *session, void (*fn)(H3270 *session)); static void * internal_add_except(int source, H3270 *session, void (*fn)(H3270 *session)); #endif // WIN32 -static void internal_remove_input(void *id); - -// static int internal_process_events(int block); - static int internal_callthread(int(*callback)(H3270 *, void *), H3270 *session, void *parm); static int internal_wait(H3270 *hSession, int seconds); @@ -76,19 +75,25 @@ static void internal_ring_bell(H3270 *); static void (*remove_timeout)(void *timer) = internal_remove_timeout; - static void (*remove_input)(void *id) - = internal_remove_input; + static void (*remove_source)(void *id) + = internal_remove_source; #ifdef WIN32 static void * (*add_input)(HANDLE source, H3270 *session, void (*fn)(H3270 *session)) = internal_add_input; + static void * (*add_output)(HANDLE source, H3270 *session, void (*fn)(H3270 *session)) + = internal_add_output; + static void * (*add_except)(HANDLE source, H3270 *session, void (*fn)(H3270 *session)) = internal_add_except; #else static void * (*add_input)(int source, H3270 *session, void (*fn)(H3270 *session)) = internal_add_input; + static void * (*add_output)(int source, H3270 *session, void (*fn)(H3270 *session)) + = internal_add_output; + static void * (*add_except)(int source, H3270 *session, void (*fn)(H3270 *session)) = internal_add_except; #endif // WIN32 @@ -266,19 +271,39 @@ static void * internal_add_input(int source, H3270 *session, void (*fn)(H3270 *s trace("%s session=%p proc=%p",__FUNCTION__,session,fn); - ip->source = source; - ip->condition = InputReadMask; - ip->proc = fn; - ip->session = session; - ip->next = inputs; - inputs = ip; - inputs_changed = True; + ip->source = source; + ip->condition = InputReadMask; + ip->proc = fn; + ip->session = session; + ip->next = inputs; + inputs = ip; + inputs_changed = True; -// trace("%s: fd=%d callback=%p handle=%p",__FUNCTION__,source,fn,ip); + return ip; +} + +#ifdef WIN32 +static void * internal_add_output(HANDLE source, H3270 *session, void (*fn)(H3270 *session)) +#else +static void * internal_add_output(int source, H3270 *session, void (*fn)(H3270 *session)) +#endif // WIN32 +{ + input_t *ip = (input_t *) lib3270_malloc(sizeof(input_t)); + + trace("%s session=%p proc=%p",__FUNCTION__,session,fn); + + ip->source = source; + ip->condition = InputWriteMask; + ip->proc = fn; + ip->session = session; + ip->next = inputs; + inputs = ip; + inputs_changed = True; return ip; } + #if defined(_WIN32) static void * internal_add_except(HANDLE source, H3270 *session, void (*fn)(H3270 *session)) { @@ -305,7 +330,7 @@ static void * internal_add_except(int source, H3270 *session, void (*fn)(H3270 * } #endif // WIN32 -static void internal_remove_input(void *id) +static void internal_remove_source(void *id) { input_t *ip; input_t *prev = (input_t *)NULL; @@ -337,7 +362,7 @@ static int internal_event_dispatcher(H3270 *hSession, int block) { #if defined(_WIN32) HANDLE ha[MAX_HA]; - DWORD nha; + DWORD events; DWORD tmo; DWORD ret; unsigned long long now; @@ -346,19 +371,21 @@ static int internal_event_dispatcher(H3270 *hSession, int block) fd_set rfds, wfds, xfds; int ns; struct timeval now, twait, *tp; + int events; #endif input_t *ip, *ip_next; struct timeout *t; - Boolean any_events; int processed_any = 0; - retry: +retry: // If we've processed any input, then don't block again. - if(processed_any) block = 0; - any_events = False; + + events = 0; + +/* #if defined(_WIN32) nha = 0; #else @@ -378,12 +405,16 @@ static int internal_event_dispatcher(H3270 *hSession, int block) #endif any_events = True; } -#if !defined(_WIN32) if ((unsigned long)ip->condition & InputWriteMask) { +#if defined(_WIN32) + ha[nha++] = ip->source; +#else FD_SET(ip->source, &wfds); +#endif any_events = True; } +#if !defined(_WIN32) if ((unsigned long)ip->condition & InputExceptMask) { FD_SET(ip->source, &xfds); @@ -394,7 +425,8 @@ static int internal_event_dispatcher(H3270 *hSession, int block) if (block) { - if (timeouts != TN) { + if (timeouts != TN) + { #if defined(_WIN32) ms_ts(&now); if (now > timeouts->ts) @@ -414,7 +446,9 @@ static int internal_event_dispatcher(H3270 *hSession, int block) tp = &twait; #endif any_events = True; - } else { + } + else + { // Block for 1 second (at maximal) #if defined(_WIN32) tmo = 1; @@ -437,63 +471,170 @@ static int internal_event_dispatcher(H3270 *hSession, int block) if (!any_events) return processed_any; +*/ #if defined(_WIN32) - ret = WaitForMultipleObjects(nha, ha, FALSE, tmo); - if (ret == WAIT_FAILED) + + for (ip = inputs; ip != (input_t *)NULL; ip = ip->next) { -#else - ns = select(FD_SETSIZE, &rfds, &wfds, &xfds, tp); - if (ns < 0) + if ((unsigned long)ip->condition & InputReadMask) + ha[events++] = ip->source; + if ((unsigned long)ip->condition & InputWriteMask) + ha[events++] = ip->source; + } + + if (block) { - if (errno != EINTR) - lib3270_popup_an_errno(hSession,errno,"%s",_( "select() failed when dispatching events")); -#endif + if (timeouts != TN) + { + ms_ts(&now); + if (now > timeouts->ts) + tmo = 0; + else + tmo = timeouts->ts - now; + } + else + { + // Block for 1 second (at maximal) + tmo = 1; + } + } + else if (!events) + { + // No block & no events, return return processed_any; } + else + { + tmo = 1; + } + + ret = WaitForMultipleObjects(nha, ha, FALSE, tmo); + if (ret == WAIT_FAILED) + { + lib3270_popup_dialog( hSession, + LIB3270_NOTIFY_ERROR, + _( "Network error" ), + _( "WaitForMultipleObjects() failed when processing for events." ), + "Windows error %d", + GetLastError()); + } inputs_changed = False; -#if defined(_WIN32) for (i = 0, ip = inputs; ip != (input_t *)NULL; ip = ip_next, i++) { -#else - for (ip = inputs; ip != (input_t *) NULL; ip = ip_next) - { -#endif - ip_next = ip->next; - if (((unsigned long)ip->condition & InputReadMask) && -#if defined(_WIN32) - ret == WAIT_OBJECT_0 + i) - { -#else - FD_ISSET(ip->source, &rfds)) + if(ret == WAIT_OBJECT_0 + i) { -#endif (*ip->proc)(ip->session); processed_any = True; if (inputs_changed) goto retry; } -#if !defined(_WIN32) - if (((unsigned long)ip->condition & InputWriteMask) && FD_ISSET(ip->source, &wfds)) + } +#else + + FD_ZERO(&rfds); + FD_ZERO(&wfds); + FD_ZERO(&xfds); + + for (ip = inputs; ip != (input_t *)NULL; ip = ip->next) + { + if ((unsigned long)ip->condition & InputReadMask) { - (*ip->proc)(ip->session); - processed_any = True; - if (inputs_changed) - goto retry; + FD_SET(ip->source, &rfds); + events++; } - if (((unsigned long)ip->condition & InputExceptMask) && FD_ISSET(ip->source, &xfds)) + + if ((unsigned long)ip->condition & InputWriteMask) { - (*ip->proc)(ip->session); - processed_any = True; - if (inputs_changed) - goto retry; + FD_SET(ip->source, &wfds); + events++; + } + + if ((unsigned long)ip->condition & InputExceptMask) + { + FD_SET(ip->source, &xfds); + events++; } -#endif } + if (block) + { + if (timeouts != TN) + { + (void) gettimeofday(&now, (void *)NULL); + twait.tv_sec = timeouts->tv.tv_sec - now.tv_sec; + twait.tv_usec = timeouts->tv.tv_usec - now.tv_usec; + if (twait.tv_usec < 0L) { + twait.tv_sec--; + twait.tv_usec += MILLION; + } + if (twait.tv_sec < 0L) + twait.tv_sec = twait.tv_usec = 0L; + tp = &twait; + } + else + { + twait.tv_sec = 1; + twait.tv_usec = 0L; + tp = &twait; + } + } + else + { + twait.tv_sec = twait.tv_usec = 0L; + tp = &twait; + + if(!events) + return processed_any; + } + + ns = select(FD_SETSIZE, &rfds, &wfds, &xfds, tp); + if (ns < 0 && errno != EINTR) + { + lib3270_popup_dialog( hSession, + LIB3270_NOTIFY_ERROR, + _( "Network error" ), + _( "Select() failed when processing for events." ), + "%s", + strerror(errno)); + } + else + { + 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); + processed_any = True; + if (inputs_changed) + goto retry; + } + + if (((unsigned long)ip->condition & InputWriteMask) && FD_ISSET(ip->source, &wfds)) + { + (*ip->proc)(ip->session); + processed_any = True; + if (inputs_changed) + goto retry; + } + + if (((unsigned long)ip->condition & InputExceptMask) && FD_ISSET(ip->source, &xfds)) + { + (*ip->proc)(ip->session); + processed_any = True; + if (inputs_changed) + goto retry; + } + } + } + +#endif + // See what's expired. if (timeouts != TN) { #if defined(_WIN32) @@ -580,6 +721,16 @@ void * AddInput(int source, H3270 *session, void (*fn)(H3270 *session)) } #ifdef WIN32 +void * AddOutput(HANDLE source, H3270 *session, void (*fn)(H3270 *session)) +#else +void * AddOutput(int source, H3270 *session, void (*fn)(H3270 *session)) +#endif // WIN32 +{ + CHECK_SESSION_HANDLE(session); + return add_output(source,session,fn); +} + +#ifdef WIN32 void * AddExcept(HANDLE source, H3270 *session, void (*fn)(H3270 *session)) #else void * AddExcept(int source, H3270 *session, void (*fn)(H3270 *session)) @@ -589,9 +740,9 @@ void * AddExcept(int source, H3270 *session, void (*fn)(H3270 *session)) return add_except(source,session,fn); } -void RemoveInput(void * id) +void RemoveSource(void * id) { - remove_input(id); + remove_source(id); } void x_except_on(H3270 *h) @@ -600,7 +751,7 @@ void x_except_on(H3270 *h) return; if(h->reading) - RemoveInput(h->ns_read_id); + RemoveSource(h->ns_read_id); #ifdef WIN32 h->ns_exception_id = AddExcept(h->sockEvent, h, net_exception); @@ -621,13 +772,13 @@ void remove_input_calls(H3270 *session) { if(session->ns_read_id) { - RemoveInput(session->ns_read_id); + RemoveSource(session->ns_read_id); session->ns_read_id = NULL; session->reading = 0; } if(session->ns_exception_id) { - RemoveInput(session->ns_exception_id); + RemoveSource(session->ns_exception_id); session->ns_exception_id = NULL; session->excepting = 0; } @@ -656,8 +807,11 @@ LIB3270_EXPORT int lib3270_register_handlers(const struct lib3270_callbacks *cbk if(cbk->AddInput) add_input = cbk->AddInput; - if(cbk->RemoveInput) - remove_input = cbk->RemoveInput; + if(cbk->AddOutput) + add_output = cbk->AddOutput; + + if(cbk->RemoveSource) + remove_source = cbk->RemoveSource; if(cbk->AddExcept) add_except = cbk->AddExcept; diff --git a/sources.mak b/sources.mak index f8108d9..dfb06a2 100644 --- a/sources.mak +++ b/sources.mak @@ -34,6 +34,7 @@ TERMINAL_SOURCES = bounds.c ctlr.c util.c toggles.c screen.c selection.c kybd.c # Network I/O Sources NETWORK_SOURCES = iocalls.c proxy.c +# connect.c # Full library sources SOURCES = $(TERMINAL_SOURCES) $(NETWORK_SOURCES) ft.c ft_cut.c ft_dft.c glue.c resources.c \ diff --git a/telnet.c b/telnet.c index d4e6ae2..ab20fee 100644 --- a/telnet.c +++ b/telnet.c @@ -2052,7 +2052,7 @@ void net_exception(H3270 *session) if(session->excepting) { - RemoveInput(session->ns_exception_id); + RemoveSource(session->ns_exception_id); session->ns_exception_id = NULL; session->excepting = 0; } diff --git a/utilc.h b/utilc.h index a5725bb..1f66e35 100644 --- a/utilc.h +++ b/utilc.h @@ -38,14 +38,16 @@ LIB3270_INTERNAL void xs_warning(const char *fmt, ...) printflike(1, 2); #ifdef WIN32 LIB3270_INTERNAL void * AddInput(HANDLE, H3270 *session, void (*fn)(H3270 *session)); + LIB3270_INTERNAL void * AddOutput(HANDLE, H3270 *session, void (*fn)(H3270 *session)); LIB3270_INTERNAL void * AddExcept(HANDLE, H3270 *session, void (*fn)(H3270 *session)); #else LIB3270_INTERNAL void * AddInput(int, H3270 *session, void (*fn)(H3270 *session)); + LIB3270_INTERNAL void * AddOutput(int, H3270 *session, void (*fn)(H3270 *session)); LIB3270_INTERNAL void * AddExcept(int, H3270 *session, void (*fn)(H3270 *session)); #endif // WIN32 -LIB3270_INTERNAL void RemoveInput(void *); -LIB3270_INTERNAL void * AddTimeOut(unsigned long msec, H3270 *session, void (*fn)(H3270 *session)); -LIB3270_INTERNAL void RemoveTimeOut(void *cookie); +LIB3270_INTERNAL void RemoveSource(void *); +LIB3270_INTERNAL void * AddTimeOut(unsigned long msec, H3270 *session, void (*fn)(H3270 *session)); +LIB3270_INTERNAL void RemoveTimeOut(void *cookie); LIB3270_INTERNAL const char * KeysymToString(KeySym k); -- libgit2 0.21.2