Commit e162af43e15e0287376c3ee8302a41b744759729

Authored by perry.werneck@gmail.com
1 parent 84078687

Modificando funcionamento para compartilhar com websockets

Showing 4 changed files with 91 additions and 122 deletions   Show diff stats
connect.c
... ... @@ -76,13 +76,14 @@
76 76 /*---[ Implement ]-------------------------------------------------------------------------------*/
77 77  
78 78  
79   -static void net_connected(H3270 *hSession)
  79 +//static void net_connected(H3270 *hSession)
  80 +static void net_connected(H3270 *hSession, LIB3270_IO_FLAG flag, void *dunno)
80 81 {
81 82 int err;
82 83 socklen_t len = sizeof(err);
83 84  
84 85 trace("%s",__FUNCTION__);
85   - RemoveSource(hSession->ns_write_id);
  86 + lib3270_remove_poll(hSession->ns_write_id);
86 87 hSession->ns_write_id = NULL;
87 88  
88 89 if(getsockopt(hSession->sock, SOL_SOCKET, SO_ERROR, (char *) &err, &len) < 0)
... ... @@ -120,8 +121,12 @@ static void net_connected(H3270 *hSession)
120 121 return;
121 122 }
122 123  
123   - hSession->ns_exception_id = AddExcept(hSession->sock, hSession, net_exception);
124   - hSession->ns_read_id = AddInput(hSession->sock, hSession, net_input);
  124 +// hSession->ns_exception_id = AddExcept(hSession->sock, hSession, net_exception);
  125 +// hSession->ns_read_id = AddInput(hSession->sock, hSession, net_input);
  126 +
  127 + hSession->ns_exception_id = lib3270_add_poll_fd(hSession,hSession->sock,LIB3270_IO_FLAG_EXCEPTION,net_exception,0);
  128 + hSession->ns_read_id = lib3270_add_poll_fd(hSession,hSession->sock,LIB3270_IO_FLAG_READ,net_input,0);
  129 +
125 130 hSession->excepting = 1;
126 131 hSession->reading = 1;
127 132  
... ... @@ -498,7 +503,8 @@ static void net_connected(H3270 *hSession)
498 503 hSession->cstate = LIB3270_PENDING;
499 504 lib3270_st_changed(hSession, LIB3270_STATE_HALF_CONNECT, True);
500 505  
501   - hSession->ns_write_id = AddOutput(hSession->sock, hSession, net_connected);
  506 + hSession->ns_write_id = lib3270_add_poll_fd(hSession,hSession->sock,LIB3270_IO_FLAG_WRITE,net_connected,0);
  507 + // hSession->ns_write_id = AddOutput(hSession->sock, hSession, net_connected);
502 508  
503 509 trace("%s: Connection in progress",__FUNCTION__);
504 510  
... ...
iocalls.c
... ... @@ -37,9 +37,6 @@
37 37 #include "utilc.h"
38 38  
39 39 #define MILLION 1000000L
40   -#define InputReadMask 0x1
41   -#define InputExceptMask 0x2
42   -#define InputWriteMask 0x4
43 40  
44 41 #if defined(_WIN32)
45 42 #define MAX_HA 256
... ... @@ -47,18 +44,25 @@
47 44  
48 45 /*---[ Standard calls ]-------------------------------------------------------------------------------------*/
49 46  
50   -static void internal_remove_timeout(void *timer);
51   -static void internal_remove_source(void *id);
52   -static void * internal_add_timeout(unsigned long interval_ms, H3270 *session, void (*proc)(H3270 *session));
  47 +// Timeout calls
  48 +static void internal_remove_timeout(void *timer);
  49 +static void * internal_add_timeout(unsigned long interval_ms, H3270 *session, void (*proc)(H3270 *session));
53 50  
  51 +static void * internal_add_poll(H3270 *session, int fd, LIB3270_IO_FLAG flag, void(*proc)(H3270 *, int, LIB3270_IO_FLAG, void *), void *userdata );
  52 +static void internal_remove_poll(void *id);
  53 +
  54 +
  55 +/*
  56 +// fdcalls
  57 +static void internal_remove_source(void *id);
54 58 static void * internal_add_input(int source, H3270 *session, void (*fn)(H3270 *session));
55 59 static void * internal_add_output(int source, H3270 *session, void (*fn)(H3270 *session));
56 60 static void * internal_add_except(int source, H3270 *session, void (*fn)(H3270 *session));
  61 +*/
57 62  
58   -// static int internal_callthread(int(*callback)(H3270 *, void *), H3270 *session, void *parm);
59   -static int internal_wait(H3270 *hSession, int seconds);
  63 +static int internal_wait(H3270 *hSession, int seconds);
60 64  
61   -static int internal_event_dispatcher(H3270 *hSession, int block);
  65 +static int internal_event_dispatcher(H3270 *hSession, int block);
62 66 static void internal_ring_bell(H3270 *);
63 67  
64 68 /*---[ Active callbacks ]-----------------------------------------------------------------------------------*/
... ... @@ -69,19 +73,13 @@ static void internal_ring_bell(H3270 *);
69 73 static void (*remove_timeout)(void *timer)
70 74 = internal_remove_timeout;
71 75  
72   - static void (*remove_source)(void *id)
73   - = internal_remove_source;
74   -
75   - static void * (*add_input)(int source, H3270 *session, void (*fn)(H3270 *session))
76   - = internal_add_input;
  76 + static void * (*add_poll)(H3270 *session, int fd, LIB3270_IO_FLAG flag, void(*proc)(H3270 *, int, LIB3270_IO_FLAG, void *), void *userdata)
  77 + = internal_add_poll;
77 78  
78   - static void * (*add_output)(int source, H3270 *session, void (*fn)(H3270 *session))
79   - = internal_add_output;
  79 + static void (*remove_poll)(void *id)
  80 + = internal_remove_poll;
80 81  
81   - static void * (*add_except)(int source, H3270 *session, void (*fn)(H3270 *session))
82   - = internal_add_except;
83   -
84   - static int (*wait)(H3270 *hSession, int seconds)
  82 + static int (*wait)(H3270 *hSession, int seconds)
85 83 = internal_wait;
86 84  
87 85 static int (*event_dispatcher)(H3270 *hSession,int wait)
... ... @@ -107,23 +105,25 @@ static void internal_ring_bell(H3270 *);
107 105  
108 106 #define TN (timeout_t *)NULL
109 107  
110   - /* Input events. */
  108 +/* I/O events. */
111 109 typedef struct input
112 110 {
113   - struct input *next;
114   - int source;
115   - int condition;
116   - void (*proc)(H3270 *session);
117   - H3270 *session;
118   -} input_t;
  111 + struct input * next;
  112 + H3270 * session;
  113 + int fd;
  114 + LIB3270_IO_FLAG flag;
  115 + void * userdata;
119 116  
  117 + void (*call)(H3270 *, int, LIB3270_IO_FLAG, void *);
  118 +
  119 +} input_t;
120 120  
121 121  
122 122 /*---[ Statics ]--------------------------------------------------------------------------------------------*/
123 123  
124 124 static timeout_t * timeouts = NULL;
125 125 static input_t * inputs = NULL;
126   - static Boolean inputs_changed = False;
  126 + static Boolean inputs_changed = False;
127 127  
128 128 /*---[ Implement ]------------------------------------------------------------------------------------------*/
129 129  
... ... @@ -235,56 +235,25 @@ static void internal_remove_timeout(void * timer)
235 235 }
236 236 }
237 237  
238   -/* Input events. */
  238 +/* I/O events. */
239 239  
240   -static void * internal_add_input(int source, H3270 *session, void (*fn)(H3270 *session))
  240 +static void * internal_add_poll(H3270 *session, int fd, LIB3270_IO_FLAG flag, void(*call)(H3270 *, int, LIB3270_IO_FLAG, void *), void *userdata )
241 241 {
242 242 input_t *ip = (input_t *) lib3270_malloc(sizeof(input_t));
243 243  
244   - ip->source = source;
245   - ip->condition = InputReadMask;
246   - ip->proc = fn;
247   - ip->session = session;
248   - ip->next = inputs;
249   - inputs = ip;
250   - inputs_changed = True;
251   -
252   - return ip;
253   -}
254   -
255   -static void * internal_add_output(int source, H3270 *session, void (*fn)(H3270 *session))
256   -{
257   - input_t *ip = (input_t *) lib3270_malloc(sizeof(input_t));
  244 + ip->session = session;
  245 + ip->fd = fd;
  246 + ip->flag = flag;
  247 + ip->userdata = userdata;
  248 + ip->call = call;
258 249  
259   - ip->source = source;
260   - ip->condition = InputWriteMask;
261   - ip->proc = fn;
262   - ip->session = session;
263   - ip->next = inputs;
264 250 inputs = ip;
265 251 inputs_changed = True;
266 252  
267 253 return ip;
268 254 }
269 255  
270   -static void * internal_add_except(int source, H3270 *session, void (*fn)(H3270 *session))
271   -{
272   - input_t *ip = (input_t *) lib3270_malloc(sizeof(input_t));
273   -
274   - ip->source = source;
275   - ip->condition = InputExceptMask;
276   - ip->proc = fn;
277   - ip->session = session;
278   - ip->next = inputs;
279   - inputs = ip;
280   - inputs_changed = True;
281   -
282   - trace("%s session=%p proc=%p handle=%p",__FUNCTION__,ip->session,ip->proc,ip);
283   -
284   - return ip;
285   -}
286   -
287   -static void internal_remove_source(void *id)
  256 +static void internal_remove_poll(void *id)
288 257 {
289 258 input_t *ip;
290 259 input_t *prev = (input_t *)NULL;
... ... @@ -349,22 +318,22 @@ retry:
349 318  
350 319 for (ip = inputs; ip != (input_t *)NULL; ip = ip->next)
351 320 {
352   - if ((unsigned long)ip->condition & InputReadMask)
  321 + if(ip->flag & LIB3270_IO_FLAG_READ)
353 322 {
354   - FD_SET(ip->source, &rfds);
355   - maxSock = max(ip->source,maxSock);
  323 + FD_SET(ip->fd, &rfds);
  324 + maxSock = max(ip->fd,maxSock);
356 325 }
357 326  
358   - if ((unsigned long)ip->condition & InputWriteMask)
  327 + if(ip->flag & LIB3270_IO_FLAG_WRITE)
359 328 {
360   - FD_SET(ip->source, &wfds);
361   - maxSock = max(ip->source,maxSock);
  329 + FD_SET(ip->fd, &wfds);
  330 + maxSock = max(ip->fd,maxSock);
362 331 }
363 332  
364   - if ((unsigned long)ip->condition & InputExceptMask)
  333 + if(ip->flag & LIB3270_IO_FLAG_EXCEPTION)
365 334 {
366   - FD_SET(ip->source, &xfds);
367   - maxSock = max(ip->source,maxSock);
  335 + FD_SET(ip->fd, &xfds);
  336 + maxSock = max(ip->fd,maxSock);
368 337 }
369 338 }
370 339  
... ... @@ -410,7 +379,7 @@ retry:
410 379 {
411 380 for (ip = inputs; ip != (input_t *) NULL; ip = ip->next)
412 381 {
413   - if (((unsigned long)ip->condition & InputReadMask) && FD_ISSET(ip->source, &rfds))
  382 + if((ip->flag & LIB3270_IO_FLAG_READ) && FD_ISSET(ip->fd, &rfds))
414 383 {
415 384 (*ip->proc)(ip->session);
416 385 processed_any = True;
... ... @@ -418,7 +387,7 @@ retry:
418 387 goto retry;
419 388 }
420 389  
421   - if (((unsigned long)ip->condition & InputWriteMask) && FD_ISSET(ip->source, &wfds))
  390 + if ((ip->flag & LIB3270_IO_FLAG_WRITE) && FD_ISSET(ip->fd, &wfds))
422 391 {
423 392 (*ip->proc)(ip->session);
424 393 processed_any = True;
... ... @@ -426,7 +395,7 @@ retry:
426 395 goto retry;
427 396 }
428 397  
429   - if (((unsigned long)ip->condition & InputExceptMask) && FD_ISSET(ip->source, &xfds))
  398 + if ((ip->flag & LIB3270_IO_FLAG_EXCEPTION) && FD_ISSET(ip->fd, &xfds))
430 399 {
431 400 (*ip->proc)(ip->session);
432 401 processed_any = True;
... ... @@ -452,21 +421,21 @@ retry:
452 421  
453 422 for (ip = inputs; ip != (input_t *)NULL; ip = ip->next)
454 423 {
455   - if ((unsigned long)ip->condition & InputReadMask)
  424 + if(ip->flag & LIB3270_IO_FLAG_READ)
456 425 {
457   - FD_SET(ip->source, &rfds);
  426 + FD_SET(ip->fd, &rfds);
458 427 events++;
459 428 }
460 429  
461   - if ((unsigned long)ip->condition & InputWriteMask)
  430 + if(ip->flag & LIB3270_IO_FLAG_WRITE)
462 431 {
463   - FD_SET(ip->source, &wfds);
  432 + FD_SET(ip->fd, &wfds);
464 433 events++;
465 434 }
466 435  
467   - if ((unsigned long)ip->condition & InputExceptMask)
  436 + if(ip->flag & LIB3270_IO_FLAG_EXCEPTION)
468 437 {
469   - FD_SET(ip->source, &xfds);
  438 + FD_SET(ip->fd, &xfds);
470 439 events++;
471 440 }
472 441 }
... ... @@ -518,25 +487,25 @@ retry:
518 487 {
519 488 for (ip = inputs; ip != (input_t *) NULL; ip = ip->next)
520 489 {
521   - if (((unsigned long)ip->condition & InputReadMask) && FD_ISSET(ip->source, &rfds))
  490 + if((ip->flag & LIB3270_IO_FLAG_READ) && FD_ISSET(ip->fd, &rfds))
522 491 {
523   - (*ip->proc)(ip->session);
  492 + (*ip->call)(ip->session,ip->fd,LIB3270_IO_FLAG_READ,ip->userdata);
524 493 processed_any = True;
525 494 if (inputs_changed)
526 495 goto retry;
527 496 }
528 497  
529   - if (((unsigned long)ip->condition & InputWriteMask) && FD_ISSET(ip->source, &wfds))
  498 + if((ip->flag & LIB3270_IO_FLAG_WRITE) && FD_ISSET(ip->fd, &wfds))
530 499 {
531   - (*ip->proc)(ip->session);
  500 + (*ip->call)(ip->session,ip->fd,LIB3270_IO_FLAG_WRITE,ip->userdata);
532 501 processed_any = True;
533 502 if (inputs_changed)
534 503 goto retry;
535 504 }
536 505  
537   - if (((unsigned long)ip->condition & InputExceptMask) && FD_ISSET(ip->source, &xfds))
  506 + if((ip->flag & LIB3270_IO_FLAG_EXCEPTION) && FD_ISSET(ip->fd, &xfds))
538 507 {
539   - (*ip->proc)(ip->session);
  508 + (*ip->call)(ip->session,ip->fd,LIB3270_IO_FLAG_EXCEPTION,ip->userdata);
540 509 processed_any = True;
541 510 if (inputs_changed)
542 511 goto retry;
... ... @@ -619,6 +588,7 @@ void RemoveTimeOut(void * timer)
619 588 return remove_timeout(timer);
620 589 }
621 590  
  591 +/*
622 592 void * AddInput(int source, H3270 *session, void (*fn)(H3270 *session))
623 593 {
624 594 CHECK_SESSION_HANDLE(session);
... ... @@ -641,6 +611,7 @@ void RemoveSource(void * id)
641 611 {
642 612 remove_source(id);
643 613 }
  614 +*/
644 615  
645 616 void x_except_on(H3270 *h)
646 617 {
... ... @@ -648,32 +619,36 @@ void x_except_on(H3270 *h)
648 619 return;
649 620  
650 621 if(h->reading)
651   - RemoveSource(h->ns_read_id);
  622 + lib3270_remove_poll(h->ns_read_id);
  623 +
  624 + h->ns_exception_id = lib3270_add_poll_fd(h,h->sock,LIB3270_IO_FLAG_EXCEPTION,net_exception,0);
  625 +// h->ns_exception_id = AddExcept(h->sock, h, net_exception);
652 626  
653   - h->ns_exception_id = AddExcept(h->sock, h, net_exception);
654 627 h->excepting = 1;
655 628  
656 629 if(h->reading)
657   - h->ns_read_id = AddInput(h->sock, h, net_input);
  630 + h->ns_read_id = lib3270_add_poll_fd(h,h->sock,LIB3270_IO_FLAG_READ,net_input,0);
  631 +
  632 +// h->ns_read_id = AddInput(h->sock, h, net_input);
658 633 }
659 634  
660 635 void remove_input_calls(H3270 *session)
661 636 {
662 637 if(session->ns_read_id)
663 638 {
664   - RemoveSource(session->ns_read_id);
  639 + lib3270_remove_poll(session->ns_read_id);
665 640 session->ns_read_id = NULL;
666 641 session->reading = 0;
667 642 }
668 643 if(session->ns_exception_id)
669 644 {
670   - RemoveSource(session->ns_exception_id);
  645 + lib3270_remove_poll(session->ns_exception_id);
671 646 session->ns_exception_id = NULL;
672 647 session->excepting = 0;
673 648 }
674 649 if(session->ns_write_id)
675 650 {
676   - RemoveSource(session->ns_write_id);
  651 + lib3270_remove_poll(session->ns_write_id);
677 652 session->ns_write_id = NULL;
678 653 session->writing = 0;
679 654 }
... ... @@ -689,6 +664,7 @@ LIB3270_EXPORT void lib3270_register_time_handlers(void * (*add)(unsigned long i
689 664  
690 665 }
691 666  
  667 +/*
692 668 LIB3270_EXPORT int lib3270_register_handlers(const struct lib3270_callbacks *cbk)
693 669 {
694 670 if(!cbk)
... ... @@ -723,6 +699,7 @@ LIB3270_EXPORT int lib3270_register_handlers(const struct lib3270_callbacks *cbk
723 699 return 0;
724 700  
725 701 }
  702 +*/
726 703  
727 704 LIB3270_EXPORT void lib3270_iterate(int block) {
728 705 event_dispatcher(NULL,block);
... ...
telnet.c
... ... @@ -909,7 +909,7 @@ LIB3270_INTERNAL void lib3270_sock_disconnect(H3270 *hSession)
909 909  
910 910 if(hSession->ns_write_id)
911 911 {
912   - RemoveSource(hSession->ns_write_id);
  912 + lib3270_remove_poll(hSession->ns_write_id);
913 913 hSession->ns_write_id = 0;
914 914 }
915 915  
... ... @@ -988,7 +988,7 @@ LIB3270_EXPORT void lib3270_data_recv(H3270 *hSession, size_t nr, const unsigned
988 988 * @param hSession Session handle
989 989 *
990 990 */
991   -void net_input(H3270 *hSession)
  991 +void net_input(H3270 *hSession, LIB3270_IO_FLAG flag, void *dunno)
992 992 {
993 993 // register unsigned char * cp;
994 994 int nr;
... ... @@ -1950,7 +1950,7 @@ process_eor(H3270 *hSession)
1950 1950 * net_exception
1951 1951 * Called when there is an exceptional condition on the socket.
1952 1952 */
1953   -void net_exception(H3270 *session)
  1953 +void net_exception(H3270 *session, LIB3270_IO_FLAG flag, void *dunno)
1954 1954 {
1955 1955 CHECK_SESSION_HANDLE(session);
1956 1956  
... ... @@ -1961,7 +1961,7 @@ void net_exception(H3270 *session)
1961 1961  
1962 1962 if(session->excepting)
1963 1963 {
1964   - RemoveSource(session->ns_exception_id);
  1964 + lib3270_remove_poll(session->ns_exception_id);
1965 1965 session->ns_exception_id = NULL;
1966 1966 session->excepting = 0;
1967 1967 }
... ...
telnetc.h
... ... @@ -30,34 +30,20 @@ struct ctl_char {
30 30 };
31 31  
32 32 LIB3270_INTERNAL void net_abort(H3270 *hSession);
33   -// LIB3270_INTERNAL Boolean net_add_dummy_tn3270e(H3270 *hSession);
34 33 LIB3270_INTERNAL void net_add_eor(unsigned char *buf, int len);
35 34 LIB3270_INTERNAL void net_break(H3270 *hSession);
36   -// LIB3270_INTERNAL void net_charmode(H3270 *hSession);
37 35 LIB3270_INTERNAL int net_connect(H3270 *session, const char *, char *, Boolean, Boolean *, Boolean *);
38 36 LIB3270_INTERNAL void net_disconnect(H3270 *session);
39   -LIB3270_INTERNAL void net_exception(H3270 *session);
40   -// LIB3270_INTERNAL void net_hexansi_out(unsigned char *buf, int len);
41   -LIB3270_INTERNAL void net_input(H3270 *session);
  37 +LIB3270_INTERNAL void net_exception(H3270 *session, LIB3270_IO_FLAG flag, void *dunno);
  38 +LIB3270_INTERNAL void net_input(H3270 *session, LIB3270_IO_FLAG flag, void *dunno);
42 39 LIB3270_INTERNAL void net_interrupt(H3270 *hSession);
43   -// LIB3270_INTERNAL void net_linemode(void);
44   -// LIB3270_INTERNAL struct ctl_char *net_linemode_chars(void);
45 40 LIB3270_INTERNAL void net_output(H3270 *hSession);
46   -//LIB3270_INTERNAL const char *net_query_bind_plu_name(void);
47   -//LIB3270_INTERNAL const char *net_query_connection_state(void);
48   -//LIB3270_INTERNAL const char *net_query_host(void);
49   -//LIB3270_INTERNAL const char *net_query_lu_name(void);
50 41 LIB3270_INTERNAL void net_sendc(H3270 *hSession, char c);
51 42 LIB3270_INTERNAL void net_sends(H3270 *hSession, const char *s);
52 43 LIB3270_INTERNAL void net_send_erase(H3270 *hSession);
53 44 LIB3270_INTERNAL void net_send_kill(H3270 *hSession);
54 45 LIB3270_INTERNAL void net_send_werase(H3270 *hSession);
55   -// LIB3270_INTERNAL Boolean net_snap_options(void);
56 46 LIB3270_INTERNAL void space3270out(H3270 *hSession, int n);
57   -// LIB3270_INTERNAL const char *tn3270e_current_opts(void);
58   -// LIB3270_INTERNAL char *net_proxy_type(void);
59   -//LIB3270_INTERNAL char *net_proxy_host(void);
60   -// LIB3270_INTERNAL char *net_proxy_port(void);
61 47  
62 48 #if defined(X3270_TRACE)
63 49 LIB3270_INTERNAL void trace_netdata(H3270 *hSession, char direction, unsigned const char *buf, int len);
... ...