-
main里怎么实现
accept_cb 里的regist部分抽出来
// regist拉出来 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_cb;conn_list[fd].send_callback = send_cb;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); }// listenfd(sockfd) --> EPOLLIN --> accept_cb int accept_cb(int fd) {struct sockaddr_in clientaddr;socklen_t len = sizeof(clientaddr);int clientfd = accept(fd, (struct sockaddr*)&clientaddr, &len);printf("accept finshed: %d\n", clientfd);/* --------regist--------------conn_list[clientfd].fd = clientfd;conn_list[clientfd].r_action.recv_callback = recv_cb;conn_list[clientfd].send_callback = send_cb;memset(conn_list[clientfd].rbuffer, 0, BUFFER_LENGTH);conn_list[clientfd].rlength = 0;memset(conn_list[clientfd].wbuffer, 0, BUFFER_LENGTH);conn_list[clientfd].wlength = 0;set_event(clientfd, EPOLLIN);--------regist--------------*/event_register(fd, EPOLLIN);return 0; }
main里
int main(){unsigned short port = 2000;int sockfd = init_server(port);epfd = epoll_create(1);set_event(sockfd, EPOLLIN);while (1) { // mainloopstruct epoll_event events[1024] = {0};int nready = epoll_wait(epfd, events, 1024, -1);int i = 0;for (i = 0;i < nready;i ++) {int connfd = events[i].data.fd; #if 0 //读写二选一if (events[i].events & EPOLLIN) {conn_list[connfd].r_action.recv_callback(connfd);} else if (events[i].events & EPOLLOUT) {conn_list[connfd].send_callback(connfd);}#else // 读写共存if (events[i].events & EPOLLIN) {conn_list[connfd].r_action.recv_callback(connfd);} if (events[i].events & EPOLLOUT) {conn_list[connfd].send_callback(connfd);} #endif}}}
再改进一下set_event的flag 标志add 还是mod
#include <errno.h> #include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> #include <pthread.h> #include <unistd.h> #include <poll.h> #include <sys/epoll.h> #include <errno.h> #include <sys/time.h>#define BUFFER_LENGTH 1024 typedef int (*RCALLBACK)(int fd); //用于定义一个函数指针类型RCALLBACK。它表示一个接受一个int类型参数fd并返回int类型的函数指针#define CONNECTION_SIZE 1048576 // 1024 * 1024int accept_cb(int fd); int recv_cb(int fd); int send_cb(int fd);// global variable // 抄epoll里构建事件, 把io加入epoll里 不一样封装到set_event里 // int epfd = epoll_create(1); //不可以这样写,在里面改值 int epfd = 0;struct conn{int fd; //io是fdchar rbuffer[BUFFER_LENGTH];int rlength;char wbuffer[BUFFER_LENGTH];int wlength;RCALLBACK send_callback;// epollin事件对应callback要么accept或者recv/*这段代码定义了一个联合体(union)r_action,它包含两个成员:accept_callback和recv_callback,它们都是RCALLBACK类型的函数指针。在使用union时,各成员共享同一块内存空间,只能同时使用其中的一个成员。不同成员可以存储不同类型的数据,但在任意给定时间点上只能使用一个成员。该联合体的目的可能是为了提供某种灵活性,在特定情况下可以选择调用accept_callback或recv_callback函数来处理相应的操作。*/union{RCALLBACK accept_callback;RCALLBACK recv_callback;}r_action;};struct conn conn_list[CONNECTION_SIZE] = {0}; // fdint init_server(unsigned short port){// 创建抄network里// 创建socketint sockfd = socket(AF_INET, SOCK_STREAM, 0);// 绑定本地端口struct sockaddr_in servaddr;servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htons(INADDR_ANY); //绑网卡地址 默认0.0.0.0 绑本地地址;htons转成网络字节序 servaddr.sin_port = htons(port); //改这里 0-1023系统默认 大于1024都能用if(-1 == bind(sockfd, (struct sockaddr*)&servaddr, sizeof(struct sockaddr))){printf("bind failed: %s\n", strerror(errno));}listen(sockfd, 10);printf("listen finished: %d\n", sockfd);return sockfd; }int set_event(int fd, int event, int flag){if(flag){struct epoll_event ev; //构建事件,只用来add和delete,control里没用ev.events = event; //改这里不用epoll,reactor用eventev.data.fd = fd;epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev); }else{struct epoll_event ev; //构建事件,只用来add和delete,control里没用ev.events = event; //改这里不用epoll,reactor用eventev.data.fd = fd;epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &ev); } }// regist拉出来 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_cb;conn_list[fd].send_callback = send_cb;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); }// listenfd(sockfd) --> EPOLLIN --> accept_cb int accept_cb(int fd) {struct sockaddr_in clientaddr;socklen_t len = sizeof(clientaddr);int clientfd = accept(fd, (struct sockaddr*)&clientaddr, &len);printf("accept finshed: %d\n", clientfd);/* --------regist--------------conn_list[clientfd].fd = clientfd;conn_list[clientfd].r_action.recv_callback = recv_cb;conn_list[clientfd].send_callback = send_cb;memset(conn_list[clientfd].rbuffer, 0, BUFFER_LENGTH);conn_list[clientfd].rlength = 0;memset(conn_list[clientfd].wbuffer, 0, BUFFER_LENGTH);conn_list[clientfd].wlength = 0;set_event(clientfd, EPOLLIN);--------regist--------------*/event_register(fd, EPOLLIN);return 0; }int recv_cb(int fd) {int count = recv(fd, conn_list[fd].rbuffer, BUFFER_LENGTH, 0);if (count == 0) { // disconnectprintf("client disconnect: %d\n", fd);close(fd);epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL); // unfinishedreturn 0;} printf("RECV: %s\n", conn_list[fd].rbuffer);set_event(fd, EPOLLOUT, 0);return count; }int send_cb(int fd) {int count = send(fd, conn_list[fd].wbuffer, count, 0);set_event(fd, EPOLLIN, 0);return count; }int main(){unsigned short port = 2000;int sockfd = init_server(port);epfd = epoll_create(1);set_event(sockfd, EPOLLIN, 1);while (1) { // mainloopstruct epoll_event events[1024] = {0};int nready = epoll_wait(epfd, events, 1024, -1);int i = 0;for (i = 0;i < nready;i ++) {int connfd = events[i].data.fd; #if 0 //读写二选一if (events[i].events & EPOLLIN) {conn_list[connfd].r_action.recv_callback(connfd);} else if (events[i].events & EPOLLOUT) {conn_list[connfd].send_callback(connfd);}#else // 读写共存if (events[i].events & EPOLLIN) {conn_list[connfd].r_action.recv_callback(connfd);} if (events[i].events & EPOLLOUT) {conn_list[connfd].send_callback(connfd);} #endif}}}
然后编译
gcc -o reactor reactor.c ./reactor
-
调试
- 打开网络调试助手!——tcp client 改port addr
- 一直连接报segmentation fault
-
总结
- fd:对于client recv; 对于server是accept
- epoll与reactor区别
- reactor:读recv写send分开;每个io封装独立
- epoll:不能拿连续收存储
百万并发
没装3个client
看了一遍, reactor老是报错segmentation fault
这部分 跳了,后面再看吧,现在也没看懂,乱七八糟的
遇到问题不要慌,小问题
- too many open files:ulimit -n 1048576
- 分配ip不够