BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file stdin_input.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 | * @section DESCRIPTION |
||
30 | * |
||
31 | * Example program which reads stdin and waits for SIGINT and SIGTERM. |
||
32 | */ |
||
33 | |||
34 | #include <stdio.h> |
||
35 | #include <stddef.h> |
||
36 | |||
37 | #include <base/DebugObject.h> |
||
38 | #include <system/BReactor.h> |
||
39 | #include <system/BNetwork.h> |
||
40 | #include <system/BConnection.h> |
||
41 | #include <system/BUnixSignal.h> |
||
42 | |||
43 | #define BUF_SIZE 64 |
||
44 | |||
45 | BReactor reactor; |
||
46 | BConnection pipe_con; |
||
47 | BUnixSignal usignal; |
||
48 | StreamRecvInterface *source_if; |
||
49 | uint8_t buf[BUF_SIZE + 1]; |
||
50 | |||
51 | static void signal_handler (void *user, int signo) |
||
52 | { |
||
53 | fprintf(stderr, "received %s, exiting\n", (signo == SIGINT ? "SIGINT" : "SIGTERM")); |
||
54 | |||
55 | // exit event loop |
||
56 | BReactor_Quit(&reactor, 1); |
||
57 | } |
||
58 | |||
59 | static void connection_handler (void *user, int event) |
||
60 | { |
||
61 | if (event == BCONNECTION_EVENT_RECVCLOSED) { |
||
62 | fprintf(stderr, "pipe closed\n"); |
||
63 | } else { |
||
64 | fprintf(stderr, "pipe error\n"); |
||
65 | } |
||
66 | |||
67 | // exit event loop |
||
68 | BReactor_Quit(&reactor, (event == BCONNECTION_EVENT_RECVCLOSED ? 0 : 1)); |
||
69 | } |
||
70 | |||
71 | static void input_handler_done (void *user, int data_len) |
||
72 | { |
||
73 | // receive next chunk |
||
74 | StreamRecvInterface_Receiver_Recv(source_if, buf, BUF_SIZE); |
||
75 | |||
76 | // print this chunk |
||
77 | buf[data_len] = '\0'; |
||
78 | printf("Received: '%s'\n", buf); |
||
79 | } |
||
80 | |||
81 | int main () |
||
82 | { |
||
83 | int ret = 1; |
||
84 | |||
85 | BLog_InitStdout(); |
||
86 | |||
87 | // init network |
||
88 | if (!BNetwork_GlobalInit()) { |
||
89 | fprintf(stderr, "BNetwork_GlobalInit failed\n"); |
||
90 | goto fail1; |
||
91 | } |
||
92 | |||
93 | // init reactor (event loop) |
||
94 | if (!BReactor_Init(&reactor)) { |
||
95 | fprintf(stderr, "BReactor_Init failed\n"); |
||
96 | goto fail1; |
||
97 | } |
||
98 | |||
99 | // init signal handling |
||
100 | sigset_t set; |
||
101 | sigemptyset(&set); |
||
102 | sigaddset(&set, SIGINT); |
||
103 | sigaddset(&set, SIGTERM); |
||
104 | if (!BUnixSignal_Init(&usignal, &reactor, set, signal_handler, NULL)) { |
||
105 | fprintf(stderr, "BUnixSignal_Init failed\n"); |
||
106 | goto fail2; |
||
107 | } |
||
108 | |||
109 | // init BConnection object backed by the stdin fd |
||
110 | if (!BConnection_Init(&pipe_con, BConnection_source_pipe(0, 0), &reactor, NULL, connection_handler)) { |
||
111 | fprintf(stderr, "BConnection_Init failed\n"); |
||
112 | goto fail3; |
||
113 | } |
||
114 | |||
115 | // init connection receive interface |
||
116 | BConnection_RecvAsync_Init(&pipe_con); |
||
117 | source_if = BConnection_RecvAsync_GetIf(&pipe_con); |
||
118 | |||
119 | // init receive done callback |
||
120 | StreamRecvInterface_Receiver_Init(source_if, input_handler_done, NULL); |
||
121 | |||
122 | // receive first chunk |
||
123 | StreamRecvInterface_Receiver_Recv(source_if, buf, BUF_SIZE); |
||
124 | |||
125 | // run event loop |
||
126 | ret = BReactor_Exec(&reactor); |
||
127 | |||
128 | BConnection_RecvAsync_Free(&pipe_con); |
||
129 | BConnection_Free(&pipe_con); |
||
130 | fail3: |
||
131 | BUnixSignal_Free(&usignal, 0); |
||
132 | fail2: |
||
133 | BReactor_Free(&reactor); |
||
134 | fail1: |
||
135 | BLog_Free(); |
||
136 | DebugObjectGlobal_Finish(); |
||
137 | return ret; |
||
138 | } |