1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| #include "../base_tool.h" #include <sys/epoll.h> #include <signal.h> #include <string.h>
int epfd = 0; #define BUFFER_LENGTH 1024 #define CONN_SIZE 1048576
typedef int (*RCALLBACK)(int fd);
struct conn { int fd; char rbuffer[BUFFER_LENGTH]; char rlength; char wbuffer[BUFFER_LENGTH]; char wlength;
RCALLBACK send_callback; union { RCALLBACK recv_callback; RCALLBACK accept_callback; } r_action; };
struct conn conn_list[CONN_SIZE] = {0};
long GetUnixTime() { auto now = std::chrono::system_clock::now(); auto timestamp = std::chrono::system_clock::to_time_t(now); return timestamp; }
void set_event(int fd, int event, int flag) { if (flag) { struct epoll_event ev; ev.events = event; ev.data.fd = fd; epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev); } else { struct epoll_event ev; ev.events = event; ev.data.fd = fd; epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &ev); } }
int init_server(unsigned short port) { int sockfd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(port);
if (-1 == bind(sockfd, (struct sockaddr *)&servaddr, sizeof(struct sockaddr))) { std::cout << "bind failed" << std::endl; return -1; } listen(sockfd, 10); printf("listen finished\n"); return sockfd; }
int recv_callback(int fd) { int count = recv(fd, conn_list[fd].rbuffer, BUFFER_LENGTH, 0); conn_list[fd].rlength = count; if (count == 0) { printf("client disconnect%d\n", fd); epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL); close(fd); return -1; } printf("RECV :%s\n", conn_list[fd].rbuffer);
memcpy(conn_list[fd].wbuffer, conn_list[fd].rbuffer, conn_list[fd].rlength); conn_list[fd].wlength = conn_list[fd].rlength; set_event(fd, EPOLLOUT, 0); return count; }
int send_callback(int fd) { int count = send(fd, conn_list[fd].wbuffer, BUFFER_LENGTH, 0); printf("SEND :%s\n", conn_list[fd].wbuffer); set_event(fd, EPOLLIN, 0); return count; }
int event_register(int fd, int event) { if (fd < 0) { return -1; } conn_list[fd].fd = fd; conn_list[fd].r_action.recv_callback = recv_callback; conn_list[fd].send_callback = send_callback; memset(conn_list[fd].rbuffer, 0, BUFFER_LENGTH); conn_list[fd].rlength = 0;
memset(conn_list[fd].wbuffer, 0, BUFFER_LENGTH); conn_list[fd].wlength = 0;
set_event(fd, event, 1); return 0; }
int accept_callback(int fd) { struct sockaddr client_addr; socklen_t client_addr_len = sizeof(client_addr); int clientfd = accept(fd, &client_addr, &client_addr_len);
printf("accept clienfd=%d\n", clientfd); if (clientfd < 0) { printf("accept failed %s\n", strerror(errno)); return -1; } event_register(clientfd, EPOLLIN); return 0; }
int main(int argc, char **argv) { if (argc <= 1) { printf("Usage: %s port\n", argv[0]); exit(0); } unsigned short port = atoi(argv[1]); int sockfd = init_server(port);
epfd = epoll_create(1); conn_list[sockfd].fd = sockfd; conn_list[sockfd].r_action.recv_callback = accept_callback; set_event(sockfd, EPOLLIN, 1); while (1) { struct epoll_event events[1024] = {0}; int nready = epoll_wait(epfd, events, 1024, -1); for (int i = 0; i < nready; i++) { int connfd = events[i].data.fd; if (events[i].events & EPOLLIN) { conn_list[connfd].r_action.recv_callback(connfd); }
if (events[i].events & EPOLLOUT) { conn_list[connfd].send_callback(connfd); } } } }
|