BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file BReactor_glib.h |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
30 | #ifndef BADVPN_SYSTEM_BREACTOR_H |
||
31 | #define BADVPN_SYSTEM_BREACTOR_H |
||
32 | |||
33 | #include <stdint.h> |
||
34 | |||
35 | #include <glib.h> |
||
36 | |||
37 | #include <misc/debug.h> |
||
38 | #include <misc/debugcounter.h> |
||
39 | #include <misc/offset.h> |
||
40 | #include <structure/LinkedList1.h> |
||
41 | #include <base/DebugObject.h> |
||
42 | #include <base/BPending.h> |
||
43 | #include <system/BTime.h> |
||
44 | |||
45 | typedef struct BReactor_s BReactor; |
||
46 | |||
47 | struct BSmallTimer_t; |
||
48 | |||
49 | #define BTIMER_SET_ABSOLUTE 1 |
||
50 | #define BTIMER_SET_RELATIVE 2 |
||
51 | |||
52 | typedef void (*BSmallTimer_handler) (struct BSmallTimer_t *timer); |
||
53 | typedef void (*BTimer_handler) (void *user); |
||
54 | |||
55 | typedef struct BSmallTimer_t { |
||
56 | union { |
||
57 | BSmallTimer_handler smalll; // MSVC doesn't like "small" |
||
58 | BTimer_handler heavy; |
||
59 | } handler; |
||
60 | BReactor *reactor; |
||
61 | GSource *source; |
||
62 | uint8_t active; |
||
63 | uint8_t is_small; |
||
64 | } BSmallTimer; |
||
65 | |||
66 | void BSmallTimer_Init (BSmallTimer *bt, BSmallTimer_handler handler); |
||
67 | int BSmallTimer_IsRunning (BSmallTimer *bt); |
||
68 | |||
69 | typedef struct { |
||
70 | BSmallTimer base; |
||
71 | void *user; |
||
72 | btime_t msTime; |
||
73 | } BTimer; |
||
74 | |||
75 | void BTimer_Init (BTimer *bt, btime_t msTime, BTimer_handler handler, void *user); |
||
76 | int BTimer_IsRunning (BTimer *bt); |
||
77 | |||
78 | struct BFileDescriptor_t; |
||
79 | |||
80 | #define BREACTOR_READ (1 << 0) |
||
81 | #define BREACTOR_WRITE (1 << 1) |
||
82 | #define BREACTOR_ERROR (1 << 2) |
||
83 | #define BREACTOR_HUP (1 << 3) |
||
84 | |||
85 | typedef void (*BFileDescriptor_handler) (void *user, int events); |
||
86 | |||
87 | typedef struct BFileDescriptor_t { |
||
88 | int fd; |
||
89 | BFileDescriptor_handler handler; |
||
90 | void *user; |
||
91 | int active; |
||
92 | int waitEvents; |
||
93 | BReactor *reactor; |
||
94 | GSource *source; |
||
95 | GPollFD pollfd; |
||
96 | } BFileDescriptor; |
||
97 | |||
98 | void BFileDescriptor_Init (BFileDescriptor *bs, int fd, BFileDescriptor_handler handler, void *user); |
||
99 | |||
100 | struct BReactor_s { |
||
101 | int exiting; |
||
102 | int exit_code; |
||
103 | GMainLoop *gloop; |
||
104 | int unref_gloop_on_free; |
||
105 | GSourceFuncs fd_source_funcs; |
||
106 | BPendingGroup pending_jobs; |
||
107 | LinkedList1 active_limits_list; |
||
108 | |||
109 | DebugCounter d_fds_counter; |
||
110 | DebugCounter d_limits_ctr; |
||
111 | DebugCounter d_timers_ctr; |
||
112 | DebugObject d_obj; |
||
113 | }; |
||
114 | |||
115 | int BReactor_Init (BReactor *bsys) WARN_UNUSED; |
||
116 | void BReactor_Free (BReactor *bsys); |
||
117 | int BReactor_Exec (BReactor *bsys); |
||
118 | void BReactor_Quit (BReactor *bsys, int code); |
||
119 | void BReactor_SetSmallTimer (BReactor *bsys, BSmallTimer *bt, int mode, btime_t time); |
||
120 | void BReactor_RemoveSmallTimer (BReactor *bsys, BSmallTimer *bt); |
||
121 | void BReactor_SetTimer (BReactor *bsys, BTimer *bt); |
||
122 | void BReactor_SetTimerAfter (BReactor *bsys, BTimer *bt, btime_t after); |
||
123 | void BReactor_SetTimerAbsolute (BReactor *bsys, BTimer *bt, btime_t time); |
||
124 | void BReactor_RemoveTimer (BReactor *bsys, BTimer *bt); |
||
125 | BPendingGroup * BReactor_PendingGroup (BReactor *bsys); |
||
126 | int BReactor_Synchronize (BReactor *bsys, BSmallPending *ref); |
||
127 | int BReactor_AddFileDescriptor (BReactor *bsys, BFileDescriptor *bs) WARN_UNUSED; |
||
128 | void BReactor_RemoveFileDescriptor (BReactor *bsys, BFileDescriptor *bs); |
||
129 | void BReactor_SetFileDescriptorEvents (BReactor *bsys, BFileDescriptor *bs, int events); |
||
130 | |||
131 | int BReactor_InitFromExistingGMainLoop (BReactor *bsys, GMainLoop *gloop, int unref_gloop_on_free); |
||
132 | GMainLoop * BReactor_GetGMainLoop (BReactor *bsys); |
||
133 | int BReactor_SynchronizeAll (BReactor *bsys); |
||
134 | |||
135 | typedef struct { |
||
136 | BReactor *reactor; |
||
137 | int limit; |
||
138 | int count; |
||
139 | LinkedList1Node active_limits_list_node; |
||
140 | DebugObject d_obj; |
||
141 | } BReactorLimit; |
||
142 | |||
143 | void BReactorLimit_Init (BReactorLimit *o, BReactor *reactor, int limit); |
||
144 | void BReactorLimit_Free (BReactorLimit *o); |
||
145 | int BReactorLimit_Increment (BReactorLimit *o); |
||
146 | void BReactorLimit_SetLimit (BReactorLimit *o, int limit); |
||
147 | |||
148 | #endif |