Commit 8d613af13478f1a943eb3b65cd76f95de3ee9c26

Authored by perry.werneck@gmail.com
1 parent 23867c9e

Melhorando o processo de conexao ao host

src/classlib/session.cc
... ... @@ -469,6 +469,17 @@
469 469 return EINVAL;
470 470 }
471 471  
  472 +#ifdef WIN32
  473 + string session::win32_strerror(int e)
  474 + {
  475 + static char buffer[4096];
  476 +
  477 + if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,e,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),buffer,sizeof(buffer),NULL) == 0)
  478 + snprintf(buffer,4095,"Windows error %d", e);
  479 +
  480 + return string(buffer);
  481 + }
  482 +#endif // WIN32
472 483  
473 484 }
474 485  
... ...
src/include/lib3270.h
... ... @@ -1033,6 +1033,9 @@
1033 1033 */
1034 1034 LIB3270_EXPORT int lib3270_is_tso(H3270 *hSession);
1035 1035  
  1036 +#ifdef WIN32
  1037 + LIB3270_EXPORT const char * lib3270_win32_strerror(int e);
  1038 +#endif // WIn32
1036 1039  
1037 1040 #ifdef __cplusplus
1038 1041 }
... ...
src/include/pw3270/class.h
... ... @@ -117,9 +117,10 @@
117 117  
118 118 // charset
119 119 #ifdef WIN32
120   - void set_display_charset(const char *remote = 0, const char *local = "CP1252");
  120 + void set_display_charset(const char *remote = 0, const char *local = "CP1252");
  121 + string win32_strerror(int e);
121 122 #else
122   - void set_display_charset(const char *remote = 0, const char *local = "UTF-8");
  123 + void set_display_charset(const char *remote = 0, const char *local = "UTF-8");
123 124 #endif // WIN32
124 125  
125 126 virtual int set_host_charset(const char *charset) = 0;
... ...
src/lib3270/connect.c
... ... @@ -67,9 +67,45 @@
67 67  
68 68 static void net_connected(H3270 *hSession)
69 69 {
  70 + int err;
  71 + socklen_t len = sizeof(err);
  72 +
  73 + trace("%s",__FUNCTION__);
70 74 RemoveSource(hSession->ns_write_id);
71 75 hSession->ns_write_id = NULL;
72 76  
  77 + if(getsockopt(hSession->sock, SOL_SOCKET, SO_ERROR, (char *) &err, &len) < 0)
  78 + {
  79 + lib3270_disconnect(hSession);
  80 + lib3270_popup_dialog( hSession,
  81 + LIB3270_NOTIFY_ERROR,
  82 + _( "Network error" ),
  83 + _( "Unable to get connection state." ),
  84 +#ifdef _WIN32
  85 + _( "Winsock Error %d"), WSAGetLastError()
  86 +#else
  87 + _( "%s" ), strerror(errno)
  88 +#endif // _WIN32
  89 + );
  90 + return;
  91 + }
  92 + else if(err)
  93 + {
  94 + lib3270_disconnect(hSession);
  95 + lib3270_popup_dialog( hSession,
  96 + LIB3270_NOTIFY_ERROR,
  97 + _( "Network error" ),
  98 + _( "Unable to connect to server." ),
  99 +#ifdef _WIN32
  100 + _( "%s"), lib3270_win32_strerror(err)
  101 +#else
  102 + _( "%s" ), strerror(err)
  103 +#endif // _WIN32
  104 + );
  105 + return;
  106 + }
  107 +
  108 +
73 109 #ifdef _WIN32
74 110 hSession->ns_exception_id = AddExcept(hSession->sockEvent, hSession, net_exception);
75 111 hSession->ns_read_id = AddInput(hSession->sockEvent, hSession, net_input);
... ... @@ -114,7 +150,7 @@ static void net_connected(H3270 *hSession)
114 150 LIB3270_NOTIFY_CRITICAL,
115 151 N_( "Network startup error" ),
116 152 N_( "WSAStartup failed" ),
117   - "%s", win32_strerror(GetLastError()) );
  153 + "%s", lib3270_win32_strerror(GetLastError()) );
118 154  
119 155 _exit(1);
120 156 }
... ... @@ -223,50 +259,51 @@ static void net_connected(H3270 *hSession)
223 259 if(hSession->sock < 0)
224 260 continue;
225 261  
  262 + trace("sock=%d",hSession->sock);
  263 +
226 264 #ifdef WIN32
227   - u_long block;
228   - u_int len = sizeof(int);
229 265  
230   - if(session->sockEvent == NULL)
  266 + if(hSession->sockEvent == NULL)
231 267 {
232 268 char ename[256];
233 269  
234 270 snprintf(ename, 255, "%s-%d", PACKAGE_NAME, getpid());
235 271  
236   - session->sockEvent = CreateEvent(NULL, TRUE, FALSE, ename);
237   - if(session->sockEvent == NULL)
  272 + hSession->sockEvent = CreateEvent(NULL, TRUE, FALSE, ename);
  273 + if(hSession->sockEvent == NULL)
238 274 {
239   - lib3270_popup_dialog( session,
  275 + lib3270_popup_dialog( hSession,
240 276 LIB3270_NOTIFY_CRITICAL,
241 277 N_( "Network startup error" ),
242 278 N_( "Cannot create socket handle" ),
243   - "%s", win32_strerror(GetLastError()) );
  279 + "%s", lib3270_win32_strerror(GetLastError()) );
244 280 _exit(1);
245 281 }
246 282 }
247 283  
248   - if (WSAEventSelect(session->sock, session->sockEvent, FD_READ | FD_CONNECT | FD_CLOSE) != 0)
  284 + if (WSAEventSelect(hSession->sock, hSession->sockEvent, FD_READ | FD_CONNECT | FD_CLOSE) != 0)
249 285 {
250   - lib3270_popup_dialog( session,
  286 + lib3270_popup_dialog( hSession,
251 287 LIB3270_NOTIFY_CRITICAL,
252 288 N_( "Network startup error" ),
253 289 N_( "WSAEventSelect failed" ),
254   - "%s", win32_strerror(GetLastError()) );
  290 + "%s", lib3270_win32_strerror(GetLastError()) );
255 291 _exit(1);
256 292 }
257 293  
258 294  
259 295  
260 296 WSASetLastError(0);
261   - block = 0;
  297 + u_long iMode=1;
  298 + trace("sock=%d",hSession->sock);
262 299  
263   - if(ioctlsocket(hSession->sock,FIONBIO,&block))
  300 + if(ioctlsocket(hSession->sock,FIONBIO,&iMode))
264 301 {
265 302 lib3270_popup_dialog( hSession,
266 303 LIB3270_NOTIFY_ERROR,
267 304 _( "Connection error" ),
268 305 _( "ioctlsocket(FIONBIO) failed." ),
269   - "%s", win32_strerror(GetLastError()));
  306 + "%s", lib3270_win32_strerror(GetLastError()));
270 307  
271 308 SOCK_CLOSE(hSession);
272 309 }
... ... @@ -279,7 +316,7 @@ static void net_connected(H3270 *hSession)
279 316 LIB3270_NOTIFY_ERROR,
280 317 _( "Connection error" ),
281 318 _( "Can't connect to host." ),
282   - "%s", win32_strerror(GetLastError()));
  319 + "%s", lib3270_win32_strerror(GetLastError()));
283 320 SOCK_CLOSE(hSession);
284 321 }
285 322 }
... ... @@ -342,11 +379,13 @@ static void net_connected(H3270 *hSession)
342 379 lib3270_st_changed(hSession, LIB3270_STATE_HALF_CONNECT, True);
343 380  
344 381 #ifdef _WIN32
  382 + trace("Sockevent=%08lx callback=%p",hSession->sockEvent,net_connected);
345 383 hSession->ns_write_id = AddOutput(hSession->sockEvent, hSession, net_connected);
346 384 #else
347 385 hSession->ns_write_id = AddOutput(hSession->sock, hSession, net_connected);
348 386 #endif // WIN32
349 387  
  388 + trace("%s: Connection in progress",__FUNCTION__);
350 389 return 0;
351 390  
352 391 }
... ...
src/lib3270/iocalls.c
... ... @@ -269,8 +269,6 @@ static void * internal_add_input(int source, H3270 *session, void (*fn)(H3270 *s
269 269 {
270 270 input_t *ip = (input_t *) lib3270_malloc(sizeof(input_t));
271 271  
272   - trace("%s session=%p proc=%p handle=%p",__FUNCTION__,session,fn,ip);
273   -
274 272 ip->source = source;
275 273 ip->condition = InputReadMask;
276 274 ip->proc = fn;
... ... @@ -290,8 +288,6 @@ static void * internal_add_output(int source, H3270 *session, void (*fn)(H3270 *
290 288 {
291 289 input_t *ip = (input_t *) lib3270_malloc(sizeof(input_t));
292 290  
293   - trace("%s session=%p proc=%p handle=%p",__FUNCTION__,session,fn,ip);
294   -
295 291 ip->source = source;
296 292 ip->condition = InputWriteMask;
297 293 ip->proc = fn;
... ... @@ -314,8 +310,6 @@ static void * internal_add_except(int source, H3270 *session, void (*fn)(H3270 *
314 310 {
315 311 input_t *ip = (input_t *) lib3270_malloc(sizeof(input_t));
316 312  
317   - trace("%s session=%p proc=%p",__FUNCTION__,session,fn);
318   -
319 313 ip->source = source;
320 314 ip->condition = InputExceptMask;
321 315 ip->proc = fn;
... ... @@ -324,7 +318,7 @@ static void * internal_add_except(int source, H3270 *session, void (*fn)(H3270 *
324 318 inputs = ip;
325 319 inputs_changed = True;
326 320  
327   - trace("%s: fd=%d callback=%p handle=%p",__FUNCTION__,source,fn,ip);
  321 + trace("%s session=%p proc=%p handle=%p",__FUNCTION__,ip->session,ip->proc,ip);
328 322  
329 323 return ip;
330 324 }
... ... @@ -335,8 +329,6 @@ static void internal_remove_source(void *id)
335 329 input_t *ip;
336 330 input_t *prev = (input_t *)NULL;
337 331  
338   - trace("%s: fhandle=%p",__FUNCTION__,(input_t *) id);
339   -
340 332 for (ip = inputs; ip != (input_t *)NULL; ip = ip->next)
341 333 {
342 334 if (ip == (input_t *)id)
... ... @@ -347,7 +339,7 @@ static void internal_remove_source(void *id)
347 339  
348 340 if (ip == (input_t *)NULL)
349 341 {
350   - lib3270_write_log(NULL,"lib3270","Double removal on %s: Input %p wasnt found in the list",__FUNCTION__,id);
  342 + lib3270_write_log(NULL,"lib3270","Invalid call to (%s): Input %p wasnt found in the list",__FUNCTION__,id);
351 343 return;
352 344 }
353 345  
... ... @@ -367,7 +359,6 @@ static int internal_event_dispatcher(H3270 *hSession, int block)
367 359 HANDLE ha[MAX_HA];
368 360 DWORD events;
369 361 DWORD tmo;
370   - DWORD ret;
371 362 unsigned long long now;
372 363 int i;
373 364 #else
... ... @@ -376,7 +367,7 @@ static int internal_event_dispatcher(H3270 *hSession, int block)
376 367 struct timeval now, twait, *tp;
377 368 int events;
378 369 #endif
379   - input_t *ip, *ip_next;
  370 + input_t *ip;
380 371 struct timeout *t;
381 372 int processed_any = 0;
382 373  
... ... @@ -512,30 +503,41 @@ retry:
512 503 tmo = 1;
513 504 }
514 505  
515   - ret = WaitForMultipleObjects(nha, ha, FALSE, tmo);
516   - if (ret == WAIT_FAILED)
  506 + if(events)
517 507 {
518   - lib3270_popup_dialog( hSession,
519   - LIB3270_NOTIFY_ERROR,
520   - _( "Network error" ),
521   - _( "WaitForMultipleObjects() failed when processing for events." ),
522   - "Windows error %d",
523   - GetLastError());
524   - }
525   -
526   - inputs_changed = False;
  508 + DWORD ret = WaitForMultipleObjects(events, ha, FALSE, tmo);
527 509  
528   - for (i = 0, ip = inputs; ip != (input_t *)NULL; ip = ip_next, i++)
529   - {
530   - if(ret == WAIT_OBJECT_0 + i)
  510 + if (ret == WAIT_FAILED)
531 511 {
532   - (*ip->proc)(ip->session);
533   - processed_any = True;
534   - if (inputs_changed)
535   - goto retry;
  512 + lib3270_popup_dialog( hSession,
  513 + LIB3270_NOTIFY_ERROR,
  514 + _( "Network error" ),
  515 + _( "WaitForMultipleObjects() failed when processing for events." ),
  516 + "%s",
  517 + lib3270_win32_strerror(GetLastError()));
  518 + lib3270_disconnect(hSession);
536 519 }
  520 + else
  521 + {
  522 + inputs_changed = False;
537 523  
  524 + for (i = 0, ip = inputs; ip != (input_t *)NULL; ip = ip->next, i++)
  525 + {
  526 + if(ret == WAIT_OBJECT_0 + i)
  527 + {
  528 + (*ip->proc)(ip->session);
  529 + processed_any = True;
  530 + if (inputs_changed)
  531 + goto retry;
  532 + }
  533 + }
  534 + }
  535 + }
  536 + else if(block)
  537 + {
  538 + Sleep(100);
538 539 }
  540 +
539 541 #else
540 542  
541 543 FD_ZERO(&rfds);
... ... @@ -606,10 +608,8 @@ retry:
606 608 }
607 609 else
608 610 {
609   - for (ip = inputs; ip != (input_t *) NULL; ip = ip_next)
  611 + for (ip = inputs; ip != (input_t *) NULL; ip = ip->next)
610 612 {
611   - ip_next = ip->next;
612   -
613 613 if (((unsigned long)ip->condition & InputReadMask) && FD_ISSET(ip->source, &rfds))
614 614 {
615 615 (*ip->proc)(ip->session);
... ...
src/lib3270/kybd.c
... ... @@ -399,7 +399,7 @@ void lib3270_kybdlock_clear(H3270 *hSession, LIB3270_KL_STATE bits)
399 399 {
400 400 unsigned int n = hSession->kybdlock & ~( (unsigned int) bits);
401 401  
402   - trace("%s: kybdlock=%d",__FUNCTION__,n);
  402 +// trace("%s: kybdlock=%d",__FUNCTION__,n);
403 403  
404 404 if (n != hSession->kybdlock)
405 405 {
... ...
src/lib3270/lib3270.cbp
... ... @@ -15,9 +15,6 @@
15 15 <Add option="-g" />
16 16 <Add option="-DDEBUG=1" />
17 17 </Compiler>
18   - <Linker>
19   - <Add library="pthread" />
20   - </Linker>
21 18 </Target>
22 19 <Target title="Release">
23 20 <Option output=".bin/Release/3270" prefix_auto="1" extension_auto="1" />
... ... @@ -50,16 +47,17 @@
50 47 <Add option="-Wredundant-decls" />
51 48 <Add option="-Wunreachable-code" />
52 49 <Add option="-Wmissing-declarations" />
53   - <Add option="-Wall" />
54 50 <Add option="-pthread" />
  51 + <Add option="-Wno-redundant-decls" />
55 52 <Add option="-DLIB3270=1" />
56 53 <Add directory="../include" />
57 54 <Add directory="../include/lib3270" />
58 55 </Compiler>
59 56 <Linker>
60   - <Add option="-pthread" />
61   - <Add option="-lssl" />
62   - <Add option="-lcrypto" />
  57 + <Add library="intl" />
  58 + <Add library="ws2_32" />
  59 + <Add library="ssl" />
  60 + <Add library="crypto" />
63 61 </Linker>
64 62 <Unit filename="../include/lib3270.h" />
65 63 <Unit filename="../include/lib3270/action_table.h" />
... ...
src/lib3270/telnet.c
... ... @@ -315,7 +315,7 @@ void sockstart(H3270 *session)
315 315 LIB3270_NOTIFY_CRITICAL,
316 316 N_( "Network startup error" ),
317 317 N_( "WSAStartup failed" ),
318   - "%s", win32_strerror(GetLastError()) );
  318 + "%s", lib3270_win32_strerror(GetLastError()) );
319 319  
320 320 _exit(1);
321 321 }
... ... @@ -344,7 +344,7 @@ socklen_t ha_len = sizeof(haddr);
344 344 void popup_a_sockerr(H3270 *hSession, char *fmt, ...)
345 345 {
346 346 #if defined(_WIN32)
347   - const char *msg = win32_strerror(socket_errno());
  347 + const char *msg = lib3270_win32_strerror(socket_errno());
348 348 #else
349 349 const char *msg = strerror(errno);
350 350 #endif // WIN32
... ... @@ -719,7 +719,7 @@ int net_connect(H3270 *session, const char *host, char *portname, Boolean ls, Bo
719 719 LIB3270_NOTIFY_CRITICAL,
720 720 N_( "Network startup error" ),
721 721 N_( "Cannot create socket handle" ),
722   - "%s", win32_strerror(GetLastError()) );
  722 + "%s", lib3270_win32_strerror(GetLastError()) );
723 723 _exit(1);
724 724 }
725 725 }
... ... @@ -730,7 +730,7 @@ int net_connect(H3270 *session, const char *host, char *portname, Boolean ls, Bo
730 730 LIB3270_NOTIFY_CRITICAL,
731 731 N_( "Network startup error" ),
732 732 N_( "WSAEventSelect failed" ),
733   - "%s", win32_strerror(GetLastError()) );
  733 + "%s", lib3270_win32_strerror(GetLastError()) );
734 734 _exit(1);
735 735 }
736 736  
... ... @@ -1069,7 +1069,7 @@ LIB3270_EXPORT void lib3270_data_recv(H3270 *hSession, size_t nr, const unsigned
1069 1069 {
1070 1070 register const unsigned char * cp;
1071 1071  
1072   - trace("%s: nr=%d",__FUNCTION__,(int) nr);
  1072 +// trace("%s: nr=%d",__FUNCTION__,(int) nr);
1073 1073  
1074 1074 trace_netdata(hSession, '<', netrbuf, nr);
1075 1075  
... ... @@ -3284,7 +3284,7 @@ static void ssl_info_callback(INFO_CONST SSL *s, int where, int ret)
3284 3284 #if defined(_WIN32)
3285 3285 else if (GetLastError() != 0)
3286 3286 {
3287   - strncpy(err_buf,win32_strerror(GetLastError()),1023);
  3287 + strncpy(err_buf,lib3270_win32_strerror(GetLastError()),1023);
3288 3288 }
3289 3289 #else
3290 3290 else if (errno != 0)
... ...
src/lib3270/testprogram.c
1 1  
2 2 #include <stdio.h>
3 3 #include <string.h>
4   -#include <pthread.h>
  4 +// #include <pthread.h>
5 5  
6 6 #include "globals.h"
7 7 #include <lib3270/macros.h>
8 8  
9 9 #define MAX_ARGS 10
10 10  
11   -#include <pthread.h>
12   -
13 11 static H3270 *session = NULL;
14 12  
15 13 static void * mainloop(void *dunno)
... ... @@ -25,18 +23,22 @@ int main(int numpar, char *param[])
25 23 {
26 24 H3270 * h;
27 25 char line[4096];
28   - pthread_t thread;
  26 +// pthread_t thread;
29 27  
30 28 lib3270_initialize();
31 29  
32 30 session = h = lib3270_session_new("");
33 31 printf("3270 session %p created\n]",h);
34 32  
35   - pthread_create(&thread, NULL, mainloop, NULL);
36   - pthread_detach(thread);
  33 +// pthread_create(&thread, NULL, mainloop, NULL);
  34 +// pthread_detach(thread);
  35 +
  36 + lib3270_connect_host(h, "fandezhi.efglobe.com", "telnet");
  37 +// lib3270_connect_host(h, "127.0.0.1", "9090");
37 38  
38   - lib3270_connect_host(h, "127.0.0.1", "80");
  39 + mainloop(0);
39 40  
  41 +/*
40 42 while(fgets(line,4095,stdin))
41 43 {
42 44 // const LIB3270_MACRO_LIST *cmd = get_3270_calls();
... ... @@ -85,6 +87,7 @@ int main(int numpar, char *param[])
85 87  
86 88 printf("Ending 3270 session %p\n",h);
87 89 lib3270_session_free(h);
  90 +*/
88 91  
89 92 return 0;
90 93 }
... ...
src/lib3270/util.c
... ... @@ -146,25 +146,16 @@ const char * inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
146 146 }
147 147  
148 148 // Decode a Win32 error number.
149   -const char * win32_strerror(int e)
  149 +LIB3270_EXPORT const char * lib3270_win32_strerror(int e)
150 150 {
151 151 static char buffer[4096];
152 152  
153   - if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
154   - NULL,
155   - e,
156   - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
157   - buffer,
158   - sizeof(buffer),
159   - NULL) == 0) {
160   -
161   - sprintf(buffer, "Windows error %d", e);
162   - }
  153 + if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,e,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),buffer,sizeof(buffer),NULL) == 0)
  154 + snprintf(buffer, 4095, "Windows error %d", e);
163 155  
164 156 return buffer;
165 157 }
166 158  
167   -
168 159 #endif // _WIN32
169 160  
170 161 /*
... ...