b.liu | 4e243dc | 2023-11-27 11:20:00 +0800 | [diff] [blame] | 1 | #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 | static void help() |
| 19 | { |
| 20 | printf("\n************************************************************************\n"); |
| 21 | printf("net_open: Open network.\n"); |
| 22 | printf("net_close: Close network.\n"); |
| 23 | 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"); |
| 24 | printf("link_close <link_id>: Close link.\n"); |
| 25 | printf("send <link_id> <data>:Send data.\n"); |
| 26 | printf("recv <link_id> <len>:Recv data.\n"); |
| 27 | printf("traffic_reset <link_id>:Traffic reset.\n"); |
| 28 | printf("traffic_get <link_id>:Traffic get.\n"); |
| 29 | printf("state_get <link_id>:Link state get.\n"); |
| 30 | printf("tcp_info <link_id>:TCP information state get.\n"); |
| 31 | printf("\n************************************************************************\n"); |
| 32 | } |
| 33 | |
| 34 | static void sig_process(int sig) |
| 35 | { |
| 36 | LOGI("I got signal %d\n", sig); |
| 37 | switch(sig) |
| 38 | { |
| 39 | case SIGINT: // Ctrl + C |
| 40 | { |
| 41 | LOGI("Exit by SIGINT.\n"); |
| 42 | mbtk_tcpip_err_enum err = mbtk_tcpip_net_close(); |
| 43 | if(err == MBTK_TCPIP_ERR_SUCCESS) { |
| 44 | printf("Net close success.\n"); |
| 45 | } else { |
| 46 | printf("Net close fail:%d\n", err); |
| 47 | } |
| 48 | exit(0); |
| 49 | } |
| 50 | case SIGQUIT: // Ctrl + \ (类似 SIGINT ,但要产生core文件) |
| 51 | { |
| 52 | LOGI("Exit by SIGQUIT.\n"); |
| 53 | mbtk_tcpip_err_enum err = mbtk_tcpip_net_close(); |
| 54 | if(err == MBTK_TCPIP_ERR_SUCCESS) { |
| 55 | printf("Net close success.\n"); |
| 56 | } else { |
| 57 | printf("Net close fail:%d\n", err); |
| 58 | } |
| 59 | exit(0); |
| 60 | } |
| 61 | case SIGTERM:// 默认kill (同 SIGKILL ,但 SIGKILL 不可捕获) |
| 62 | { |
| 63 | LOGI("Exit by SIGTERM.\n"); |
| 64 | mbtk_tcpip_err_enum err = mbtk_tcpip_net_close(); |
| 65 | if(err == MBTK_TCPIP_ERR_SUCCESS) { |
| 66 | printf("Net close success.\n"); |
| 67 | } else { |
| 68 | printf("Net close fail:%d\n", err); |
| 69 | } |
| 70 | exit(0); |
| 71 | } |
| 72 | case SIGTSTP:// Ctrl + Z (同 SIGSTOP ,但 SIGSTOP 不可捕获) |
| 73 | { |
| 74 | LOGI("Exit by SIGTSTP.\n"); |
| 75 | exit(0); |
| 76 | } |
| 77 | case SIGSEGV: // 如空指针 |
| 78 | { |
| 79 | LOGI("Exit by SIGSEGV.\n"); |
| 80 | exit(0); |
| 81 | } |
| 82 | default: |
| 83 | { |
| 84 | LOGI("Unknown sig:%d\n",sig); |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void tcpip_print_tcp_info(mbtk_tcpip_tcp_state_info_s *info) |
| 91 | { |
| 92 | printf("Link - %d\n", info->link_id); |
| 93 | printf("fd - %d\n", info->sock_fd); |
| 94 | printf("State - %d\n", info->state); |
| 95 | printf("Recv data length - %d\n", info->recv_data_len); |
| 96 | } |
| 97 | |
| 98 | void tcpip_read_cb(int link_id, const char* data, int data_len) |
| 99 | { |
| 100 | printf("\nRECV(%d-%d):%s\n", link_id, data_len, data); |
| 101 | } |
| 102 | |
| 103 | void tcpip_net_callback_func(int state, const char* addr) |
| 104 | { |
| 105 | if(state) { |
| 106 | printf("Net conncect, IP : %s\n", addr); |
| 107 | } else { |
| 108 | printf("Net disconnect.\n"); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
| 113 | int main(int argc, char *argv[]) |
| 114 | { |
| 115 | |
| 116 | |
| 117 | printf(">>>>>>>>>>>START TCP TEST>>>>>>>>>>>>>\r\n"); |
| 118 | mbtk_tcpip_err_enum err; |
| 119 | |
| 120 | mbtk_tcpip_info_t info; |
| 121 | memset(&info, 0x0, sizeof(mbtk_tcpip_info_t)); |
| 122 | char *ip = "www.chaixiongfeng.group"; |
| 123 | strcpy(&info.ser_addr,ip); |
| 124 | info.link_id=1; |
| 125 | info.ser_port =45002; |
| 126 | info.prot_type = MBTK_SOCK_TCP; |
| 127 | info.tcpip_type = MBTK_TCPIP_TYPE_CLIENT; |
| 128 | info.read_cb = NULL; |
| 129 | |
| 130 | |
| 131 | err = mbtk_tcpip_sock_open((const mbtk_tcpip_info_t*)(&info)); |
| 132 | if(err == MBTK_TCPIP_ERR_SUCCESS) { |
| 133 | printf("Link open success.\n"); |
| 134 | } else { |
| 135 | printf("Link open fail:%d\n", err); |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | while(1) |
| 140 | { |
| 141 | char *data="stargcharge connect\r\n"; |
| 142 | int len = mbtk_tcpip_send(1, data, strlen(data), NULL, 0); |
| 143 | if(len == strlen(data)) |
| 144 | { |
| 145 | printf("Send success:%d.\n", len); |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | printf("Send fail:%d/%d\n", len, strlen(data)); |
| 150 | } |
| 151 | |
| 152 | char buff[128]; |
| 153 | len = mbtk_tcpip_read(1, buff, 128); |
| 154 | if(len > 0) |
| 155 | { |
| 156 | printf("RECV[%d]:%s\n", len, buff); |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | printf("RECV fail:%d\n", len); |
| 161 | } |
| 162 | |
| 163 | err = mbtk_tcpip_sock_close(1); |
| 164 | if(err == MBTK_TCPIP_ERR_SUCCESS) |
| 165 | { |
| 166 | printf("Link close success.\n"); |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | printf("Link close fail:%d\n", err); |
| 171 | } |
| 172 | sleep(5); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | |