BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file BReactor_emscripten.c |
||
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 | #include <stdio.h> |
||
31 | #include <inttypes.h> |
||
32 | |||
33 | #include <emscripten/emscripten.h> |
||
34 | |||
35 | #include "BReactor_emscripten.h" |
||
36 | |||
37 | #include <misc/debug.h> |
||
38 | |||
39 | #include <generated/blog_channel_BReactor.h> |
||
40 | |||
41 | static void assert_timer (BTimer *bt, BReactor *reactor) |
||
42 | { |
||
43 | ASSERT(bt->active == 0 || bt->active == 1); |
||
44 | ASSERT(bt->active == 0 || bt->reactor); |
||
45 | ASSERT(bt->active == 0 || (!reactor || bt->reactor == reactor)); |
||
46 | } |
||
47 | |||
48 | static void dispatch_pending (BReactor *o) |
||
49 | { |
||
50 | while (BPendingGroup_HasJobs(&o->pending_jobs)) { |
||
51 | BPendingGroup_ExecuteJob(&o->pending_jobs); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | __attribute__((used)) |
||
56 | void breactor_timer_cb (BReactor *reactor, BTimer *bt) |
||
57 | { |
||
58 | assert_timer(bt, reactor); |
||
59 | ASSERT(bt->active); |
||
60 | ASSERT(!BPendingGroup_HasJobs(&reactor->pending_jobs)); |
||
61 | |||
62 | bt->active = 0; |
||
63 | |||
64 | bt->handler(bt->handler_pointer); |
||
65 | dispatch_pending(reactor); |
||
66 | } |
||
67 | |||
68 | static void small_timer_handler (void *vbt) |
||
69 | { |
||
70 | BSmallTimer *bt = vbt; |
||
71 | |||
72 | bt->handler(bt); |
||
73 | } |
||
74 | |||
75 | void BTimer_Init (BTimer *bt, btime_t msTime, BTimer_handler handler, void *user) |
||
76 | { |
||
77 | bt->msTime = msTime; |
||
78 | bt->handler = handler; |
||
79 | bt->handler_pointer = user; |
||
80 | bt->active = 0; |
||
81 | } |
||
82 | |||
83 | int BTimer_IsRunning (BTimer *bt) |
||
84 | { |
||
85 | assert_timer(bt, NULL); |
||
86 | |||
87 | return bt->active; |
||
88 | } |
||
89 | |||
90 | void BSmallTimer_Init (BSmallTimer *bt, BSmallTimer_handler handler) |
||
91 | { |
||
92 | BTimer_Init(&bt->timer, 0, small_timer_handler, bt); |
||
93 | bt->handler = handler; |
||
94 | } |
||
95 | |||
96 | int BSmallTimer_IsRunning (BSmallTimer *bt) |
||
97 | { |
||
98 | return BTimer_IsRunning(&bt->timer); |
||
99 | } |
||
100 | |||
101 | void BReactor_EmscriptenInit (BReactor *bsys) |
||
102 | { |
||
103 | BPendingGroup_Init(&bsys->pending_jobs); |
||
104 | |||
105 | DebugObject_Init(&bsys->d_obj); |
||
106 | } |
||
107 | |||
108 | void BReactor_EmscriptenFree (BReactor *bsys) |
||
109 | { |
||
110 | DebugObject_Free(&bsys->d_obj); |
||
111 | ASSERT(!BPendingGroup_HasJobs(&bsys->pending_jobs)); |
||
112 | } |
||
113 | |||
114 | void BReactor_EmscriptenSync (BReactor *bsys) |
||
115 | { |
||
116 | DebugObject_Access(&bsys->d_obj); |
||
117 | |||
118 | dispatch_pending(bsys); |
||
119 | } |
||
120 | |||
121 | BPendingGroup * BReactor_PendingGroup (BReactor *bsys) |
||
122 | { |
||
123 | DebugObject_Access(&bsys->d_obj); |
||
124 | |||
125 | return &bsys->pending_jobs; |
||
126 | } |
||
127 | |||
128 | void BReactor_SetTimer (BReactor *bsys, BTimer *bt) |
||
129 | { |
||
130 | BReactor_SetTimerAfter(bsys, bt, bt->msTime); |
||
131 | } |
||
132 | |||
133 | void BReactor_SetTimerAfter (BReactor *bsys, BTimer *bt, btime_t after) |
||
134 | { |
||
135 | DebugObject_Access(&bsys->d_obj); |
||
136 | assert_timer(bt, bsys); |
||
137 | |||
138 | if (bt->active) { |
||
139 | BReactor_RemoveTimer(bsys, bt); |
||
140 | } |
||
141 | |||
142 | char cmd[120]; |
||
143 | sprintf(cmd, "setTimeout(function(){Module.ccall('breactor_timer_cb','null',['number','number'],[%d,%d]);},%"PRIi64")", (int)bsys, (int)bt, after); |
||
144 | |||
145 | bt->active = 1; |
||
146 | bt->timerid = emscripten_run_script_int(cmd); |
||
147 | bt->reactor = bsys; |
||
148 | } |
||
149 | |||
150 | void BReactor_RemoveTimer (BReactor *bsys, BTimer *bt) |
||
151 | { |
||
152 | DebugObject_Access(&bsys->d_obj); |
||
153 | assert_timer(bt, bsys); |
||
154 | |||
155 | if (!bt->active) { |
||
156 | return; |
||
157 | } |
||
158 | |||
159 | char cmd[30]; |
||
160 | sprintf(cmd, "clearTimeout(%d)", bt->timerid); |
||
161 | |||
162 | emscripten_run_script(cmd); |
||
163 | bt->active = 0; |
||
164 | } |
||
165 | |||
166 | void BReactor_SetSmallTimer (BReactor *bsys, BSmallTimer *bt, int mode, btime_t time) |
||
167 | { |
||
168 | ASSERT(mode == BTIMER_SET_RELATIVE) |
||
169 | |||
170 | BReactor_SetTimerAfter(bsys, &bt->timer, time); |
||
171 | } |
||
172 | |||
173 | void BReactor_RemoveSmallTimer (BReactor *bsys, BSmallTimer *bt) |
||
174 | { |
||
175 | BReactor_RemoveTimer(bsys, &bt->timer); |
||
176 | } |