blob: d30d7066d8f21c3b637b802b2efbcb976da220e2 [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");
b.liu94baa7c2023-09-26 16:26:10 +080041 printf("recv <link_id> <len>:Recv data.\n");
b.liueb040652023-09-25 18:50:56 +080042 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");
b.liu94baa7c2023-09-26 16:26:10 +080045 printf("tcp_info <link_id>:TCP information state get.\n");
b.liueb040652023-09-25 18:50:56 +080046 printf("\n************************************************************************\n");
47}
48
49static void sig_process(int sig)
50{
51 LOGI("I got signal %d\n", sig);
52 switch(sig)
53 {
54 case SIGINT: // Ctrl + C
55 {
56 LOGI("Exit by SIGINT.\n");
57 mbtk_tcpip_err_enum err = mbtk_tcpip_net_close();
58 if(err == MBTK_TCPIP_ERR_SUCCESS) {
59 printf("Net close success.\n");
60 } else {
61 printf("Net close fail:%d\n", err);
62 }
63 exit(0);
64 }
65 case SIGQUIT: // Ctrl + \ (类似 SIGINT ,但要产生core文件)
66 {
67 LOGI("Exit by SIGQUIT.\n");
68 mbtk_tcpip_err_enum err = mbtk_tcpip_net_close();
69 if(err == MBTK_TCPIP_ERR_SUCCESS) {
70 printf("Net close success.\n");
71 } else {
72 printf("Net close fail:%d\n", err);
73 }
74 exit(0);
75 }
76 case SIGTERM:// 默认kill (同 SIGKILL ,但 SIGKILL 不可捕获)
77 {
78 LOGI("Exit by SIGTERM.\n");
79 mbtk_tcpip_err_enum err = mbtk_tcpip_net_close();
80 if(err == MBTK_TCPIP_ERR_SUCCESS) {
81 printf("Net close success.\n");
82 } else {
83 printf("Net close fail:%d\n", err);
84 }
85 exit(0);
86 }
87 case SIGTSTP:// Ctrl + Z (同 SIGSTOP ,但 SIGSTOP 不可捕获)
88 {
89 LOGI("Exit by SIGTSTP.\n");
90 exit(0);
91 }
92 case SIGSEGV: // 如空指针
93 {
94 LOGI("Exit by SIGSEGV.\n");
95 exit(0);
96 }
97 default:
98 {
99 LOGI("Unknown sig:%d\n",sig);
100 break;
101 }
102 }
103}
104
b.liu94baa7c2023-09-26 16:26:10 +0800105void tcpip_print_tcp_info(mbtk_tcpip_tcp_state_info_s *info)
106{
107 printf("Link - %d\n", info->link_id);
108 printf("fd - %d\n", info->sock_fd);
109 printf("State - %d\n", info->state);
110 printf("Recv data length - %d\n", info->recv_data_len);
111}
112
b.liueb040652023-09-25 18:50:56 +0800113void tcpip_read_cb(int link_id, const char* data, int data_len)
114{
115 printf("\nRECV(%d-%d):%s\n", link_id, data_len, data);
116}
117
118void tcpip_net_callback_func(int state, const char* addr)
119{
120 if(state) {
121 printf("Net conncect, IP : %s\n", addr);
122 } else {
123 printf("Net disconnect.\n");
124 }
125}
126
127void tcpip_sock_callback_func(int link_id, int state)
128{
129 if(state == 0) {
130 printf("Link[%d] disconnected.\n", link_id);
131 }
132}
133
134int main(int argc, char *argv[])
135{
136 signal(SIGINT, sig_process);
137 signal(SIGQUIT, sig_process);
138 signal(SIGTERM, sig_process);
139 //signal(SIGTSTP, sig_process);
140 //signal(SIGSEGV, sig_process);
141
142 mbtk_log_init("radio","MBTK_TCPIP");
143
144 printf(">>>>>>>>>>>>>>>>>>>>>>>>Enter cmd:\n");
145 char cmd[100];
146 while(1)
147 {
148 memset(cmd, 0, 100);
149 mbtk_tcpip_err_enum err;
150 if(fgets(cmd, 100, stdin))
151 {
152 char *ptr = cmd + strlen(cmd) - 1;
153 while(ptr >= cmd && (*ptr == '\r' || *ptr == '\n'))
154 {
155 *ptr-- = '\0';
156 }
157 // net_open
158 if(!strncasecmp(cmd, "net_open", 8)){
159 err = mbtk_tcpip_net_open(tcpip_net_callback_func, tcpip_sock_callback_func);
160 if(err == MBTK_TCPIP_ERR_SUCCESS) {
161 printf("Net open success.\n");
162 } else {
163 printf("Net open fail:%d\n", err);
164 }
165 } else if(!strncasecmp(cmd, "net_close", 9)){ // net_close
166 err = mbtk_tcpip_net_close();
167 if(err == MBTK_TCPIP_ERR_SUCCESS) {
168 printf("Net close success.\n");
169 } else {
170 printf("Net close fail:%d\n", err);
171 }
172 }
173 // link_open <link_id> <ser_addr> <ser_port> <loc_port> <TCP/UDP> <CLI/SER> <ack> <ssl> <ignore_cert> <heartbeat_time> <delay_time> <read_cb>
174 else if(!strncasecmp(cmd, "link_open", 9)){
175 mbtk_tcpip_info_t info;
176 char prot_type[10] = {0};
177 char type[10] = {0};
178 int read_cb_set;
179 memset(&info, 0x0, sizeof(mbtk_tcpip_info_t));
180 int count = sscanf(cmd, "link_open %d %s %d %d %s %s %d %d %d %d %d %d", &(info.link_id),
181 info.ser_addr, &(info.ser_port), &(info.local_port), prot_type, type,
182 &(info.ack_support), &(info.ssl_support), &(info.ignore_cert), &(info.heartbeat_time),
183 &(info.delay_time), &read_cb_set);
184 if(count == 12) {
185 if(!strncasecmp(prot_type, "UDP", 3)) {
186 info.prot_type = MBTK_SOCK_UDP;
187 } else {
188 info.prot_type = MBTK_SOCK_TCP;
189 }
190
191 if(!strncasecmp(type, "SER", 3)) {
192 info.tcpip_type = MBTK_TCPIP_TYPE_SERVER;
193 } else {
194 info.tcpip_type = MBTK_TCPIP_TYPE_CLIENT;
195 }
196
197 if(read_cb_set) {
198 info.read_cb = tcpip_read_cb;
199 } else {
200 info.read_cb = NULL;
201 }
202
203 err = mbtk_tcpip_sock_open((const mbtk_tcpip_info_t*)(&info));
204 if(err == MBTK_TCPIP_ERR_SUCCESS) {
205 printf("Link open success.\n");
206 } else {
207 printf("Link open fail:%d\n", err);
208 }
209 } else {
210 printf("ARG error.\n");
211 }
212 } else if(!strncasecmp(cmd, "link_close", 10)){ // link_close <link_id>
213 int link_id;
214 int count = sscanf(cmd, "link_close %d", &link_id);
215 if(count == 1) {
216 err = mbtk_tcpip_sock_close(link_id);
217 if(err == MBTK_TCPIP_ERR_SUCCESS) {
218 printf("Link close success.\n");
219 } else {
220 printf("Link close fail:%d\n", err);
221 }
222 } else {
223 printf("ARG error.\n");
224 }
225 } else if(!strncasecmp(cmd, "send", 4)){ // send <link_id> <data>
226 int link_id;
227 char data[100] = {0};
228 int count = sscanf(cmd, "send %d %s", &link_id, data);
229 if(count == 2) {
230 int len = mbtk_tcpip_send(link_id, data, strlen(data), NULL, 0);
231 if(len == strlen(data)) {
232 printf("Send success:%d.\n", len);
233 } else {
234 printf("Send fail:%d/%d\n", len, strlen(data));
235 }
236 } else {
237 printf("ARG error.\n");
238 }
b.liu94baa7c2023-09-26 16:26:10 +0800239 } else if(!strncasecmp(cmd, "recv", 4)){ // recv <link_id> <len>
b.liueb040652023-09-25 18:50:56 +0800240 int link_id;
b.liu94baa7c2023-09-26 16:26:10 +0800241 int read_size;
242 int count = sscanf(cmd, "recv %d %d", &link_id, &read_size);
243 if(count == 2 && read_size > 0 && read_size <= 2048) {
b.liueb040652023-09-25 18:50:56 +0800244 char buff[2048] = {0};
b.liu94baa7c2023-09-26 16:26:10 +0800245 int len = mbtk_tcpip_read(link_id, buff, read_size);
b.liueb040652023-09-25 18:50:56 +0800246 if(len > 0) {
247 printf("RECV[%d]:%s\n", len, buff);
248 } else {
249 printf("RECV fail:%d\n", len);
250 }
251 } else {
252 printf("ARG error.\n");
253 }
254 } else if(!strncasecmp(cmd, "traffic_reset", 13)){ // traffic_reset <link_id>
255 int link_id;
256 int count = sscanf(cmd, "traffic_reset %d", &link_id);
257 if(count == 1) {
258 err = mbtk_tcpip_data_traffic_reset(link_id);
259 if(err == MBTK_TCPIP_ERR_SUCCESS) {
260 printf("Traffic reset success.\n");
261 } else {
262 printf("Traffic reset fail:%d\n", err);
263 }
264 } else {
265 printf("ARG error.\n");
266 }
267 } else if(!strncasecmp(cmd, "traffic_get", 11)){ // traffic_get <link_id>
268 int link_id;
269 int count = sscanf(cmd, "traffic_get %d", &link_id);
270 if(count == 1) {
271 int traffic = mbtk_tcpip_data_traffic_get(link_id);
272 if(traffic >= 0) {
273 printf("Traffic : %d\n", traffic);
274 } else {
275 printf("Get raffic fail:%d\n", traffic);
276 }
277 } else {
278 printf("ARG error.\n");
279 }
280 } else if(!strncasecmp(cmd, "state_get", 9)){ // state_get <link_id>
281 int link_id;
282 int count = sscanf(cmd, "state_get %d", &link_id);
283 if(count == 1) {
284 int state = mbtk_tcpip_link_state_get(link_id);
285 if(state == 1) {
286 printf("Link %d connected.\n", link_id);
287 } else if(state == 0) {
288 printf("Link %d disconnected.\n", link_id);
289 } else {
290 printf("Get link state fail:%d\n", state);
291 }
292 } else {
293 printf("ARG error.\n");
294 }
295 }
b.liu94baa7c2023-09-26 16:26:10 +0800296 else if(!strncasecmp(cmd, "tcp_info", 8)){ // tcp_info <link_id>
297 int link_id;
298 int count = sscanf(cmd, "tcp_info %d", &link_id);
299 if(count == 1) {
300 mbtk_tcpip_tcp_state_info_s tcp_info;
301 if(mbtk_tcpip_info_get(link_id, &tcp_info)) {
302 printf("mbtk_tcpip_info_get() fail.\n");
303 } else {
304 tcpip_print_tcp_info(&tcp_info);
305 }
306 } else {
307 printf("ARG error.\n");
308 }
309 }
b.liueb040652023-09-25 18:50:56 +0800310 else if(!strcasecmp(cmd, "h") || !strcasecmp(cmd, "help")) {
311 help();
312 } else if(!strcasecmp(cmd, "q")) {
313 err = mbtk_tcpip_net_close();
314 if(err == MBTK_TCPIP_ERR_SUCCESS) {
315 printf("Net close success.\n");
316 } else {
317 printf("Net close fail:%d\n", err);
318 }
319 break;
320 } else {
321 printf("\n");
322 }
323 }
324 }
325
326 LOGD("Client exec complete.");
327#if 1
328 while(1)
329 {
330 sleep(1000 * 365 * 24 * 60 * 60);
331 }
332#else
333 sleep(1);
334#endif
335 return 0;
336}
337