blob: 5bd39f188b1554b3a9606fd4149f8799972bc39b [file] [log] [blame]
b.liueb040652023-09-25 18:50:56 +08001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <errno.h>
5#include <sys/socket.h>
6#include <sys/un.h>
7#include <netinet/in.h>
8#include <pthread.h>
9#include <sys/epoll.h>
10#include <string.h>
11#include <fcntl.h>
12#include <signal.h>
13
14#include "mbtk_tcpip.h"
15#include "mbtk_log.h"
16
17/*
18 int link_id;
19 char ser_addr[256];
20 int ser_port;
21 mbtk_sock_type prot_type; // TCP/UDP
22 mbtk_tcpip_type_enum tcpip_type; // Only support client.
23 int local_port;
24 bool ack_support;
25 bool ssl_support;
26 bool ignore_cert;
27 uint32 heartbeat_time;
28 uint32 delay_time;
29
30 mbtk_tcpip_read_callback_func read_cb;
31
32*/
33static void help()
34{
35 printf("\n************************************************************************\n");
36 printf("net_open: Open network.\n");
37 printf("net_close: Close network.\n");
38 printf("link_open <link_id> <ser_addr> <ser_port> <loc_port> <TCP/UDP> <CLI/SER> <ack> <ssl> <ignore_cert> <heartbeat_time> <delay_time> <read_cb>: Open link.\n");
39 printf("link_close <link_id>: Close link.\n");
40 printf("send <link_id> <data>:Send data.\n");
41 printf("recv <link_id>:Recv data.\n");
42 printf("traffic_reset <link_id>:Traffic reset.\n");
43 printf("traffic_get <link_id>:Traffic get.\n");
44 printf("state_get <link_id>:Link state get.\n");
45 printf("\n************************************************************************\n");
46}
47
48static void sig_process(int sig)
49{
50 LOGI("I got signal %d\n", sig);
51 switch(sig)
52 {
53 case SIGINT: // Ctrl + C
54 {
55 LOGI("Exit by SIGINT.\n");
56 mbtk_tcpip_err_enum err = mbtk_tcpip_net_close();
57 if(err == MBTK_TCPIP_ERR_SUCCESS) {
58 printf("Net close success.\n");
59 } else {
60 printf("Net close fail:%d\n", err);
61 }
62 exit(0);
63 }
64 case SIGQUIT: // Ctrl + \ (类似 SIGINT ,但要产生core文件)
65 {
66 LOGI("Exit by SIGQUIT.\n");
67 mbtk_tcpip_err_enum err = mbtk_tcpip_net_close();
68 if(err == MBTK_TCPIP_ERR_SUCCESS) {
69 printf("Net close success.\n");
70 } else {
71 printf("Net close fail:%d\n", err);
72 }
73 exit(0);
74 }
75 case SIGTERM:// 默认kill (同 SIGKILL ,但 SIGKILL 不可捕获)
76 {
77 LOGI("Exit by SIGTERM.\n");
78 mbtk_tcpip_err_enum err = mbtk_tcpip_net_close();
79 if(err == MBTK_TCPIP_ERR_SUCCESS) {
80 printf("Net close success.\n");
81 } else {
82 printf("Net close fail:%d\n", err);
83 }
84 exit(0);
85 }
86 case SIGTSTP:// Ctrl + Z (同 SIGSTOP ,但 SIGSTOP 不可捕获)
87 {
88 LOGI("Exit by SIGTSTP.\n");
89 exit(0);
90 }
91 case SIGSEGV: // 如空指针
92 {
93 LOGI("Exit by SIGSEGV.\n");
94 exit(0);
95 }
96 default:
97 {
98 LOGI("Unknown sig:%d\n",sig);
99 break;
100 }
101 }
102}
103
104void tcpip_read_cb(int link_id, const char* data, int data_len)
105{
106 printf("\nRECV(%d-%d):%s\n", link_id, data_len, data);
107}
108
109void tcpip_net_callback_func(int state, const char* addr)
110{
111 if(state) {
112 printf("Net conncect, IP : %s\n", addr);
113 } else {
114 printf("Net disconnect.\n");
115 }
116}
117
118void tcpip_sock_callback_func(int link_id, int state)
119{
120 if(state == 0) {
121 printf("Link[%d] disconnected.\n", link_id);
122 }
123}
124
125int main(int argc, char *argv[])
126{
127 signal(SIGINT, sig_process);
128 signal(SIGQUIT, sig_process);
129 signal(SIGTERM, sig_process);
130 //signal(SIGTSTP, sig_process);
131 //signal(SIGSEGV, sig_process);
132
133 mbtk_log_init("radio","MBTK_TCPIP");
134
135 printf(">>>>>>>>>>>>>>>>>>>>>>>>Enter cmd:\n");
136 char cmd[100];
137 while(1)
138 {
139 memset(cmd, 0, 100);
140 mbtk_tcpip_err_enum err;
141 if(fgets(cmd, 100, stdin))
142 {
143 char *ptr = cmd + strlen(cmd) - 1;
144 while(ptr >= cmd && (*ptr == '\r' || *ptr == '\n'))
145 {
146 *ptr-- = '\0';
147 }
148 // net_open
149 if(!strncasecmp(cmd, "net_open", 8)){
150 err = mbtk_tcpip_net_open(tcpip_net_callback_func, tcpip_sock_callback_func);
151 if(err == MBTK_TCPIP_ERR_SUCCESS) {
152 printf("Net open success.\n");
153 } else {
154 printf("Net open fail:%d\n", err);
155 }
156 } else if(!strncasecmp(cmd, "net_close", 9)){ // net_close
157 err = mbtk_tcpip_net_close();
158 if(err == MBTK_TCPIP_ERR_SUCCESS) {
159 printf("Net close success.\n");
160 } else {
161 printf("Net close fail:%d\n", err);
162 }
163 }
164 // link_open <link_id> <ser_addr> <ser_port> <loc_port> <TCP/UDP> <CLI/SER> <ack> <ssl> <ignore_cert> <heartbeat_time> <delay_time> <read_cb>
165 else if(!strncasecmp(cmd, "link_open", 9)){
166 mbtk_tcpip_info_t info;
167 char prot_type[10] = {0};
168 char type[10] = {0};
169 int read_cb_set;
170 memset(&info, 0x0, sizeof(mbtk_tcpip_info_t));
171 int count = sscanf(cmd, "link_open %d %s %d %d %s %s %d %d %d %d %d %d", &(info.link_id),
172 info.ser_addr, &(info.ser_port), &(info.local_port), prot_type, type,
173 &(info.ack_support), &(info.ssl_support), &(info.ignore_cert), &(info.heartbeat_time),
174 &(info.delay_time), &read_cb_set);
175 if(count == 12) {
176 if(!strncasecmp(prot_type, "UDP", 3)) {
177 info.prot_type = MBTK_SOCK_UDP;
178 } else {
179 info.prot_type = MBTK_SOCK_TCP;
180 }
181
182 if(!strncasecmp(type, "SER", 3)) {
183 info.tcpip_type = MBTK_TCPIP_TYPE_SERVER;
184 } else {
185 info.tcpip_type = MBTK_TCPIP_TYPE_CLIENT;
186 }
187
188 if(read_cb_set) {
189 info.read_cb = tcpip_read_cb;
190 } else {
191 info.read_cb = NULL;
192 }
193
194 err = mbtk_tcpip_sock_open((const mbtk_tcpip_info_t*)(&info));
195 if(err == MBTK_TCPIP_ERR_SUCCESS) {
196 printf("Link open success.\n");
197 } else {
198 printf("Link open fail:%d\n", err);
199 }
200 } else {
201 printf("ARG error.\n");
202 }
203 } else if(!strncasecmp(cmd, "link_close", 10)){ // link_close <link_id>
204 int link_id;
205 int count = sscanf(cmd, "link_close %d", &link_id);
206 if(count == 1) {
207 err = mbtk_tcpip_sock_close(link_id);
208 if(err == MBTK_TCPIP_ERR_SUCCESS) {
209 printf("Link close success.\n");
210 } else {
211 printf("Link close fail:%d\n", err);
212 }
213 } else {
214 printf("ARG error.\n");
215 }
216 } else if(!strncasecmp(cmd, "send", 4)){ // send <link_id> <data>
217 int link_id;
218 char data[100] = {0};
219 int count = sscanf(cmd, "send %d %s", &link_id, data);
220 if(count == 2) {
221 int len = mbtk_tcpip_send(link_id, data, strlen(data), NULL, 0);
222 if(len == strlen(data)) {
223 printf("Send success:%d.\n", len);
224 } else {
225 printf("Send fail:%d/%d\n", len, strlen(data));
226 }
227 } else {
228 printf("ARG error.\n");
229 }
230 } else if(!strncasecmp(cmd, "recv", 4)){ // recv <link_id>
231 int link_id;
232 int count = sscanf(cmd, "recv %d", &link_id);
233 if(count == 1) {
234 char buff[2048] = {0};
235 int len = mbtk_tcpip_read(link_id, buff, 2048);
236 if(len > 0) {
237 printf("RECV[%d]:%s\n", len, buff);
238 } else {
239 printf("RECV fail:%d\n", len);
240 }
241 } else {
242 printf("ARG error.\n");
243 }
244 } else if(!strncasecmp(cmd, "traffic_reset", 13)){ // traffic_reset <link_id>
245 int link_id;
246 int count = sscanf(cmd, "traffic_reset %d", &link_id);
247 if(count == 1) {
248 err = mbtk_tcpip_data_traffic_reset(link_id);
249 if(err == MBTK_TCPIP_ERR_SUCCESS) {
250 printf("Traffic reset success.\n");
251 } else {
252 printf("Traffic reset fail:%d\n", err);
253 }
254 } else {
255 printf("ARG error.\n");
256 }
257 } else if(!strncasecmp(cmd, "traffic_get", 11)){ // traffic_get <link_id>
258 int link_id;
259 int count = sscanf(cmd, "traffic_get %d", &link_id);
260 if(count == 1) {
261 int traffic = mbtk_tcpip_data_traffic_get(link_id);
262 if(traffic >= 0) {
263 printf("Traffic : %d\n", traffic);
264 } else {
265 printf("Get raffic fail:%d\n", traffic);
266 }
267 } else {
268 printf("ARG error.\n");
269 }
270 } else if(!strncasecmp(cmd, "state_get", 9)){ // state_get <link_id>
271 int link_id;
272 int count = sscanf(cmd, "state_get %d", &link_id);
273 if(count == 1) {
274 int state = mbtk_tcpip_link_state_get(link_id);
275 if(state == 1) {
276 printf("Link %d connected.\n", link_id);
277 } else if(state == 0) {
278 printf("Link %d disconnected.\n", link_id);
279 } else {
280 printf("Get link state fail:%d\n", state);
281 }
282 } else {
283 printf("ARG error.\n");
284 }
285 }
286 else if(!strcasecmp(cmd, "h") || !strcasecmp(cmd, "help")) {
287 help();
288 } else if(!strcasecmp(cmd, "q")) {
289 err = mbtk_tcpip_net_close();
290 if(err == MBTK_TCPIP_ERR_SUCCESS) {
291 printf("Net close success.\n");
292 } else {
293 printf("Net close fail:%d\n", err);
294 }
295 break;
296 } else {
297 printf("\n");
298 }
299 }
300 }
301
302 LOGD("Client exec complete.");
303#if 1
304 while(1)
305 {
306 sleep(1000 * 365 * 24 * 60 * 60);
307 }
308#else
309 sleep(1);
310#endif
311 return 0;
312}
313