前一篇介绍了epoll的LT模式,LT模式注意epollout事件在数据全部写成功后需要取消关注,或者更改为EPOLLIN。
而这次epoll的ET模式,要注意的是在读和写的过程中要在循环中写完或者读完所有数据,确保不要丢掉一些数据。
因为epoll ET模式只在两种边缘更改的时候触发,对于读事件只在内核缓冲区由空变为
非空通知一次用户,对于写事件,内核缓冲区只在由满变为非满的情况通知用户一次。
下面是代码
int main(){int eventsize = 20;struct epoll_event * epoll_eventsList = (struct epoll_event *)malloc(sizeof(struct epoll_event)*eventsize);//打开一个空的描述符int idlefd = open("/dev/null",O_RDONLY|O_CLOEXEC);cout << "idlefd" <<idlefd <<endl;//生成listen描述符int listenfd = socket(PF_INET, SOCK_CLOEXEC | SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);if(listenfd < 0){ERR_EXIT("socketfd");}//初始化地址信息struct sockaddr_in servaddr;memset(&servaddr,0 ,sizeof(struct sockaddr_in));servaddr.sin_family = AF_INET;servaddr.sin_port = htons(6667);servaddr.sin_addr.s_addr = htonl(INADDR_ANY);int on = 1;if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)ERR_EXIT("setsockopt");if(bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)))ERR_EXIT("bindError");if (listen(listenfd, SOMAXCONN) < 0)ERR_EXIT("listen");//记录客户端连接对应的socketstd::vector<int> clients;//创建epollfd, 用于管理epoll事件表int epollfd;epollfd = epoll_create1(EPOLL_CLOEXEC);struct epoll_event event;event.data.fd = listenfd;event.events = EPOLLIN|EPOLLET;//将listenfd加入到epollfd管理的表里epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &event);//用于接收新连接的客户端地址struct sockaddr_in peeraddr;socklen_t peerlen;int connfd;//为了简单起见写了个很大的数组,根据文件描述符存储内容//其实很多项目代码采epoll.data.ptr回调具体的读写vector<string> recievebuf;for(int i = 0 ; i < 22222; i++){recievebuf.push_back("");}while(1){int nready = epoll_wait(epollfd, epoll_eventsList, eventsize, -1);if (nready == -1){if (errno == EINTR)continue;ERR_EXIT("epoll_wait");}if (nready == 0)continue;//大小不够重新开辟if ((size_t)nready == eventsize){if(eventsize * 2 >= 22222){ERR_EXIT("too many fds");}struct epoll_event * epoll_eventsList2 = (struct epoll_event *)malloc(sizeof(struct epoll_event) *eventsize *2);if(epoll_eventsList2){memcpy(epoll_eventsList2,epoll_eventsList,sizeof(struct epoll_event) * eventsize);eventsize = eventsize * 2;free(epoll_eventsList);epoll_eventsList = epoll_eventsList2;}}for (int i = 0; i < nready; ++i){//判断wait返回的events数组状态是否正常if ((epoll_eventsList[i].events & EPOLLERR) ||(epoll_eventsList[i].events & EPOLLHUP)){fprintf (stderr, "epoll error\n");close (epoll_eventsList[i].data.fd);continue;}if (epoll_eventsList[i].data.fd == listenfd){peerlen = sizeof(peeraddr);//ET模式accept放在while循环里do{connfd = ::accept4(listenfd, (struct sockaddr*)&peeraddr,&peerlen, SOCK_NONBLOCK | SOCK_CLOEXEC);if(connfd <= 0)break;std::cout<<"ip="<<inet_ntoa(peeraddr.sin_addr)<<" port="<<ntohs(peeraddr.sin_port)<<std::endl;clients.push_back(connfd);//将connd加入epoll表里,关注读事件event.data.fd = connfd;event.events = EPOLLIN |EPOLLET;epoll_ctl(epollfd, EPOLL_CTL_ADD, connfd, &event);cout << "loop" <<endl;cout << "loop" << connfd << endl;}while(1);//accept失败,判断是否接收全所有的fdcout << connfd << endl;if (connfd == -1){if (errno != EAGAIN && errno != ECONNABORTED&& errno != EPROTO && errno != EINTR){cout << "error" <<endl;ERR_EXIT("accept");}}//所有请求都处理完成cout << "continue"<<endl;continue;}//endifelse if(epoll_eventsList[i].events & EPOLLIN){connfd = epoll_eventsList[i].data.fd;if(connfd > 22222){close(connfd);event = epoll_eventsList[i];epoll_ctl(epollfd, EPOLL_CTL_DEL, connfd, &event);clients.erase(std::remove(clients.begin(), clients.end(), connfd), clients.end());continue;}char buf[1024] = {0};if(connfd < 0)continue;int ret = 0;int total = 0;std::string strtemp;while(1){cout << "begin read" <<endl;ret = read(connfd, buf, 1024);if(ret <= 0){break;}strtemp += string(buf);total += ret;memset(buf, 0, 1024);if(ret < 1024){break;}}//endwhile(1)cout << "end read" <<endl;recievebuf[connfd] = strtemp.c_str();cout << "buff data :" << recievebuf[connfd]<<endl;if(ret == -1){if((errno == EAGAIN) ||(errno == EWOULDBLOCK)){//由于内核缓冲区空了,下次有数据到来是会触发epollincontinue;}ERR_EXIT("read");}//endif ret == -1//连接断开if(ret == 0){std::cout<<"client close"<<std::endl;close(connfd);event = epoll_eventsList[i];epoll_ctl(epollfd, EPOLL_CTL_DEL, connfd, &event);clients.erase(std::remove(clients.begin(), clients.end(), connfd), clients.end());continue;}cout << "turn to write" << endl;//更改为写模式event.data.fd = connfd;event.events = EPOLLOUT | EPOLLET;epoll_ctl(epollfd, EPOLL_CTL_MOD, connfd, &event);cout << "epoll mod change success" << endl;}//end elifelse //写事件{if(epoll_eventsList[i].events & EPOLLOUT){cout << "begin write" <<endl;connfd = epoll_eventsList[i].data.fd;int count = 0;int totalsend = 0;char buf[1024];strcpy(buf, recievebuf[connfd].c_str());cout << "write buff" <<buf<<endl;while(1){int totalcount = strlen(buf);int pos = 0;count = write(epoll_eventsList[i].data.fd, buf + pos, totalcount);cout << "write count:" << count;if(count < 0){break;}if(count < totalcount){totalcount = totalcount - count;pos += count;}else{break;}}//end whileif(count == -1){if((errno == EAGAIN) ||(errno == EWOULDBLOCK)){//由于内核缓冲区满了//于内核缓冲区满了continue;}ERR_EXIT("write");}if(count == 0){std::cout<<"client close"<<std::endl;close(connfd);event = epoll_eventsList[i];epoll_ctl(epollfd, EPOLL_CTL_DEL, connfd, &event);clients.erase(std::remove(clients.begin(), clients.end(), connfd),clients.end());continue;}event.data.fd = connfd;event.events = EPOLLIN|EPOLLET;epoll_ctl(epollfd, EPOLL_CTL_MOD, connfd, &event);}}//end eles 写事件}}}
源代码下载地址:http://download.csdn.net/detail/secondtonone1/9486222
我的微信公众号: