- #pragma once
- #include <errno.h>
- #include <sys/epoll.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- #include "log.h"
- #ifndef EPOLL_SIZE
- #define EPOLL_SIZE 4096
- #define EPOLL_CONTEXT_SIZE (4096+256)
- #endif
- typedef void * EPollerContext;
- class Epoller
- {
- public:
- Epoller(){}
- virtual ~Epoller(){}
- bool create()
- {
- memset(_contexts,0,sizeof(_contexts));
- _handle=epoll_create(EPOLL_SIZE);
- if(_handle==-1)
- {
- glog.log(__FUNCTION__,"Epoll create error,errno is %d",errno);
- return false;
- }
- else
- return true;
- }
- void handle_events()
- {
- for(int i=0;i<_lastntfd;i++)
- {
- on_event(_events[i].data.fd,_events[i].events);
- }
- }
- int get_online_users()
- {
- int result=0;
- for(int i=0;i<EPOLL_CONTEXT_SIZE;i++)
- {
- if(_contexts[i]!=0)
- result++;
- }
封装的EPOLL 模型
最新推荐文章于 2023-08-03 16:28:54 发布