BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file PacketPassInactivityMonitor.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 "PacketPassInactivityMonitor.h" |
||
31 | |||
32 | static void input_handler_send (PacketPassInactivityMonitor *o, uint8_t *data, int data_len) |
||
33 | { |
||
34 | DebugObject_Access(&o->d_obj); |
||
35 | |||
36 | // schedule send |
||
37 | PacketPassInterface_Sender_Send(o->output, data, data_len); |
||
38 | |||
39 | // stop timer |
||
40 | BReactor_RemoveTimer(o->reactor, &o->timer); |
||
41 | } |
||
42 | |||
43 | static void input_handler_requestcancel (PacketPassInactivityMonitor *o) |
||
44 | { |
||
45 | DebugObject_Access(&o->d_obj); |
||
46 | |||
47 | // request cancel |
||
48 | PacketPassInterface_Sender_RequestCancel(o->output); |
||
49 | } |
||
50 | |||
51 | static void output_handler_done (PacketPassInactivityMonitor *o) |
||
52 | { |
||
53 | DebugObject_Access(&o->d_obj); |
||
54 | |||
55 | // output no longer busy, restart timer |
||
56 | BReactor_SetTimer(o->reactor, &o->timer); |
||
57 | |||
58 | // call done |
||
59 | PacketPassInterface_Done(&o->input); |
||
60 | } |
||
61 | |||
62 | static void timer_handler (PacketPassInactivityMonitor *o) |
||
63 | { |
||
64 | DebugObject_Access(&o->d_obj); |
||
65 | |||
66 | // restart timer |
||
67 | BReactor_SetTimer(o->reactor, &o->timer); |
||
68 | |||
69 | // call handler |
||
70 | if (o->handler) { |
||
71 | o->handler(o->user); |
||
72 | return; |
||
73 | } |
||
74 | } |
||
75 | |||
76 | void PacketPassInactivityMonitor_Init (PacketPassInactivityMonitor *o, PacketPassInterface *output, BReactor *reactor, btime_t interval, PacketPassInactivityMonitor_handler handler, void *user) |
||
77 | { |
||
78 | // init arguments |
||
79 | o->output = output; |
||
80 | o->reactor = reactor; |
||
81 | o->handler = handler; |
||
82 | o->user = user; |
||
83 | |||
84 | // init input |
||
85 | PacketPassInterface_Init(&o->input, PacketPassInterface_GetMTU(o->output), (PacketPassInterface_handler_send)input_handler_send, o, BReactor_PendingGroup(o->reactor)); |
||
86 | if (PacketPassInterface_HasCancel(o->output)) { |
||
87 | PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_requestcancel)input_handler_requestcancel); |
||
88 | } |
||
89 | |||
90 | // init output |
||
91 | PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o); |
||
92 | |||
93 | // init timer |
||
94 | BTimer_Init(&o->timer, interval, (BTimer_handler)timer_handler, o); |
||
95 | BReactor_SetTimer(o->reactor, &o->timer); |
||
96 | |||
97 | DebugObject_Init(&o->d_obj); |
||
98 | } |
||
99 | |||
100 | void PacketPassInactivityMonitor_Free (PacketPassInactivityMonitor *o) |
||
101 | { |
||
102 | DebugObject_Free(&o->d_obj); |
||
103 | |||
104 | // free timer |
||
105 | BReactor_RemoveTimer(o->reactor, &o->timer); |
||
106 | |||
107 | // free input |
||
108 | PacketPassInterface_Free(&o->input); |
||
109 | } |
||
110 | |||
111 | PacketPassInterface * PacketPassInactivityMonitor_GetInput (PacketPassInactivityMonitor *o) |
||
112 | { |
||
113 | DebugObject_Access(&o->d_obj); |
||
114 | |||
115 | return &o->input; |
||
116 | } |
||
117 | |||
118 | void PacketPassInactivityMonitor_SetHandler (PacketPassInactivityMonitor *o, PacketPassInactivityMonitor_handler handler, void *user) |
||
119 | { |
||
120 | DebugObject_Access(&o->d_obj); |
||
121 | |||
122 | o->handler = handler; |
||
123 | o->user = user; |
||
124 | } |
||
125 | |||
126 | void PacketPassInactivityMonitor_Force (PacketPassInactivityMonitor *o) |
||
127 | { |
||
128 | DebugObject_Access(&o->d_obj); |
||
129 | |||
130 | BReactor_SetTimerAfter(o->reactor, &o->timer, 0); |
||
131 | } |