|
47 | 47 | #include "mstats.h" |
48 | 48 |
|
49 | 49 | #include <sys/select.h> |
| 50 | +#include <sys/socket.h> |
50 | 51 | #include <sys/time.h> |
| 52 | +#include <pthread.h> |
51 | 53 |
|
52 | 54 | counter_type link_read_bytes_global; /* GLOBAL */ |
53 | 55 | counter_type link_write_bytes_global; /* GLOBAL */ |
@@ -2570,3 +2572,72 @@ process_io(struct context *c, struct link_socket *sock) |
2570 | 2572 | } |
2571 | 2573 | } |
2572 | 2574 | } |
| 2575 | + |
| 2576 | +bool threaded_lock(struct thread_pointer *b) |
| 2577 | +{ |
| 2578 | + if (pthread_mutex_trylock(&(b->p->m)) == 0) |
| 2579 | + { |
| 2580 | + if (b->p->f == 0) |
| 2581 | + { |
| 2582 | + b->p->f = b->n; |
| 2583 | + } |
| 2584 | + pthread_mutex_unlock(&(b->p->m)); |
| 2585 | + } |
| 2586 | + if (b->p->f == b->n) { return true; } |
| 2587 | + return false; |
| 2588 | +} |
| 2589 | + |
| 2590 | +void threaded_io(struct context *c, struct link_socket *sock, struct thread_pointer *b) |
| 2591 | +{ |
| 2592 | + const unsigned int status = c->c2.event_set_status; |
| 2593 | + |
| 2594 | +#ifdef ENABLE_MANAGEMENT |
| 2595 | + if (status & (MANAGEMENT_READ | MANAGEMENT_WRITE)) |
| 2596 | + { |
| 2597 | + ASSERT(management); |
| 2598 | + management_io(management); |
| 2599 | + } |
| 2600 | +#endif |
| 2601 | + |
| 2602 | + //msg(M_INFO, "MTIO MODE DEBUG PROC [%d] [%d][%d][%d][%d] [%d] [%d][%d][%p]",status,SOCKET_READ,SOCKET_WRITE,TUN_READ,TUN_WRITE,sock->sd,b->n,b->l,c->c2.buffers); |
| 2603 | + |
| 2604 | + /* TCP/UDP port ready to accept write */ |
| 2605 | + if (status & SOCKET_WRITE) |
| 2606 | + { |
| 2607 | + process_outgoing_link(c, sock); |
| 2608 | + } |
| 2609 | + /* TUN device ready to accept write */ |
| 2610 | + else if (status & TUN_WRITE) |
| 2611 | + { |
| 2612 | + process_outgoing_tun(c, sock); |
| 2613 | + } |
| 2614 | + /* Incoming data on TCP/UDP port */ |
| 2615 | + else if (status & SOCKET_READ) |
| 2616 | + { |
| 2617 | + read_incoming_link(c, sock); |
| 2618 | + if (!IS_SIG(c)) |
| 2619 | + { |
| 2620 | + process_incoming_link(c, sock); |
| 2621 | + } |
| 2622 | + } |
| 2623 | + /* Incoming data on TUN device */ |
| 2624 | + else if (status & TUN_READ) |
| 2625 | + { |
| 2626 | + if (threaded_lock(b)) |
| 2627 | + { |
| 2628 | + read_incoming_tun(c); |
| 2629 | + b->p->f = 0; |
| 2630 | + if (!IS_SIG(c)) |
| 2631 | + { |
| 2632 | + process_incoming_tun(c, sock); |
| 2633 | + } |
| 2634 | + } |
| 2635 | + } |
| 2636 | + else if (status & DCO_READ) |
| 2637 | + { |
| 2638 | + if (!IS_SIG(c)) |
| 2639 | + { |
| 2640 | + process_incoming_dco(c); |
| 2641 | + } |
| 2642 | + } |
| 2643 | +} |
0 commit comments