blob: 8a7d279909fc79d914b23f718d3b6785ab6de6ca [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));
b.liu9e8584b2024-11-06 19:21:28 +0800180 int ack_support,ssl_support,ignore_cert;
b.liueb040652023-09-25 18:50:56 +0800181 int count = sscanf(cmd, "link_open %d %s %d %d %s %s %d %d %d %d %d %d", &(info.link_id),
182 info.ser_addr, &(info.ser_port), &(info.local_port), prot_type, type,
b.liu9e8584b2024-11-06 19:21:28 +0800183 &ack_support, &ssl_support, &ignore_cert, &(info.heartbeat_time),
b.liueb040652023-09-25 18:50:56 +0800184 &(info.delay_time), &read_cb_set);
185 if(count == 12) {
b.liu9e8584b2024-11-06 19:21:28 +0800186 info.ack_support = (bool)ack_support;
187 info.ssl_support = (bool)ssl_support;
188 info.ignore_cert = (bool)ignore_cert;
b.liueb040652023-09-25 18:50:56 +0800189 if(!strncasecmp(prot_type, "UDP", 3)) {
190 info.prot_type = MBTK_SOCK_UDP;
191 } else {
192 info.prot_type = MBTK_SOCK_TCP;
193 }
194
195 if(!strncasecmp(type, "SER", 3)) {
196 info.tcpip_type = MBTK_TCPIP_TYPE_SERVER;
197 } else {
198 info.tcpip_type = MBTK_TCPIP_TYPE_CLIENT;
199 }
200
201 if(read_cb_set) {
202 info.read_cb = tcpip_read_cb;
203 } else {
204 info.read_cb = NULL;
205 }
206
207 err = mbtk_tcpip_sock_open((const mbtk_tcpip_info_t*)(&info));
208 if(err == MBTK_TCPIP_ERR_SUCCESS) {
209 printf("Link open success.\n");
210 } else {
211 printf("Link open fail:%d\n", err);
212 }
213 } else {
214 printf("ARG error.\n");
215 }
216 } else if(!strncasecmp(cmd, "link_close", 10)){ // link_close <link_id>
217 int link_id;
218 int count = sscanf(cmd, "link_close %d", &link_id);
219 if(count == 1) {
220 err = mbtk_tcpip_sock_close(link_id);
221 if(err == MBTK_TCPIP_ERR_SUCCESS) {
222 printf("Link close success.\n");
223 } else {
224 printf("Link close fail:%d\n", err);
225 }
226 } else {
227 printf("ARG error.\n");
228 }
229 } else if(!strncasecmp(cmd, "send", 4)){ // send <link_id> <data>
230 int link_id;
231 char data[100] = {0};
232 int count = sscanf(cmd, "send %d %s", &link_id, data);
233 if(count == 2) {
234 int len = mbtk_tcpip_send(link_id, data, strlen(data), NULL, 0);
235 if(len == strlen(data)) {
236 printf("Send success:%d.\n", len);
237 } else {
238 printf("Send fail:%d/%d\n", len, strlen(data));
239 }
240 } else {
241 printf("ARG error.\n");
242 }
b.liu94baa7c2023-09-26 16:26:10 +0800243 } else if(!strncasecmp(cmd, "recv", 4)){ // recv <link_id> <len>
b.liueb040652023-09-25 18:50:56 +0800244 int link_id;
b.liu94baa7c2023-09-26 16:26:10 +0800245 int read_size;
246 int count = sscanf(cmd, "recv %d %d", &link_id, &read_size);
247 if(count == 2 && read_size > 0 && read_size <= 2048) {
b.liueb040652023-09-25 18:50:56 +0800248 char buff[2048] = {0};
b.liu94baa7c2023-09-26 16:26:10 +0800249 int len = mbtk_tcpip_read(link_id, buff, read_size);
b.liueb040652023-09-25 18:50:56 +0800250 if(len > 0) {
251 printf("RECV[%d]:%s\n", len, buff);
252 } else {
253 printf("RECV fail:%d\n", len);
254 }
255 } else {
256 printf("ARG error.\n");
257 }
258 } else if(!strncasecmp(cmd, "traffic_reset", 13)){ // traffic_reset <link_id>
259 int link_id;
260 int count = sscanf(cmd, "traffic_reset %d", &link_id);
261 if(count == 1) {
262 err = mbtk_tcpip_data_traffic_reset(link_id);
263 if(err == MBTK_TCPIP_ERR_SUCCESS) {
264 printf("Traffic reset success.\n");
265 } else {
266 printf("Traffic reset fail:%d\n", err);
267 }
268 } else {
269 printf("ARG error.\n");
270 }
271 } else if(!strncasecmp(cmd, "traffic_get", 11)){ // traffic_get <link_id>
272 int link_id;
273 int count = sscanf(cmd, "traffic_get %d", &link_id);
274 if(count == 1) {
275 int traffic = mbtk_tcpip_data_traffic_get(link_id);
276 if(traffic >= 0) {
277 printf("Traffic : %d\n", traffic);
278 } else {
279 printf("Get raffic fail:%d\n", traffic);
280 }
281 } else {
282 printf("ARG error.\n");
283 }
284 } else if(!strncasecmp(cmd, "state_get", 9)){ // state_get <link_id>
285 int link_id;
286 int count = sscanf(cmd, "state_get %d", &link_id);
287 if(count == 1) {
288 int state = mbtk_tcpip_link_state_get(link_id);
289 if(state == 1) {
290 printf("Link %d connected.\n", link_id);
291 } else if(state == 0) {
292 printf("Link %d disconnected.\n", link_id);
293 } else {
294 printf("Get link state fail:%d\n", state);
295 }
296 } else {
297 printf("ARG error.\n");
298 }
299 }
b.liu94baa7c2023-09-26 16:26:10 +0800300 else if(!strncasecmp(cmd, "tcp_info", 8)){ // tcp_info <link_id>
301 int link_id;
302 int count = sscanf(cmd, "tcp_info %d", &link_id);
303 if(count == 1) {
304 mbtk_tcpip_tcp_state_info_s tcp_info;
305 if(mbtk_tcpip_info_get(link_id, &tcp_info)) {
306 printf("mbtk_tcpip_info_get() fail.\n");
307 } else {
308 tcpip_print_tcp_info(&tcp_info);
309 }
310 } else {
311 printf("ARG error.\n");
312 }
313 }
b.liueb040652023-09-25 18:50:56 +0800314 else if(!strcasecmp(cmd, "h") || !strcasecmp(cmd, "help")) {
315 help();
316 } else if(!strcasecmp(cmd, "q")) {
317 err = mbtk_tcpip_net_close();
318 if(err == MBTK_TCPIP_ERR_SUCCESS) {
319 printf("Net close success.\n");
320 } else {
321 printf("Net close fail:%d\n", err);
322 }
323 break;
324 } else {
325 printf("\n");
326 }
327 }
328 }
329
330 LOGD("Client exec complete.");
331#if 1
332 while(1)
333 {
b.liu9e8584b2024-11-06 19:21:28 +0800334 sleep(365 * 24 * 60 * 60);
b.liueb040652023-09-25 18:50:56 +0800335 }
336#else
337 sleep(1);
338#endif
339 return 0;
340}
341