资源简介
一个Linux下对epoll封装的类,方便初学者
代码片段和文件信息
#ifndef CMYDEVREACTOR_H_
#define CMYDEVREACTOR_H_
#include
#include
#define DEFUALT_EVENTS 1024
class CMyDevReactor {
public:
CMyDevReactor();
virtual ~CMyDevReactor();
int open(int Max_Events=DEFUALT_EVENTS);
virtual int register_handler(int fd);
virtual int register_handler(int fdint fd_mask);
virtual int remove_handler(int fdint fd_mask);
virtual int remove_handler(int fd);
virtual int update_handler(int fdint fd_mask);
virtual int handle_events(int *timeout=0);
virtual int Eevent_Accept(int fdchar *ipint port);
virtual int Eevent_Handle_In(int fd);
virtual int Eevent_Handle_Out(int fd);
virtual int Eevent_Handle_close(int fd);
virtual int Eevent_Handle_Error(int fd);
protected:
virtual int Close();
private:
int DispactchEvent(int nfds);
public:
enum
{
ACCEPT_MASK= 1
/// Set value of bits to new mask (changes the entire mask).
READ_MASK= 2
/// Bitwise “or“ the value into the mask (only changes enabled
/// bits).
WRITE_MASK =3
/// Bitwise “and“ the negation of the value out of the mask (only
/// changes enabled bits).
CLR_MASK =4
};
private:
int epfd;
int _listenfd;
struct epoll_event *events;
int maxevents;
};
#endif /* CMYDEVREACTOR_H_ */
#include
#include “CMyDevReactor.h“
#include “CPuball.h“
#include
/*extern int Eevent_Accept(int fdchar *ipint port);
extern int Eevent_Handle_In(int fd);
extern int Eevent_Handle_Out(int fd);
extern int Eevent_Handle_close(int fd);
extern int Eevent_Handle_Error(int fd);*/
CMyDevReactor::CMyDevReactor():_listenfd(-1)
{
// TODO Auto-generated constructor stub
}
CMyDevReactor::~CMyDevReactor() {
// TODO Auto-generated destructor stub
Close();
}
int CMyDevReactor:pen(int Max_Events)
{
FUNC_ENTER;
maxevents=Max_Events;
struct rlimit rt;
rt.rlim_max = rt.rlim_cur = maxevents;
errno=0;
if (setrlimit(RLIMIT_NOFILE &rt) == -1) {
MY_ERROR(“setrlimit fail: %d %s\n“errnostrerror(errno));
return -1;
}
events = new struct epoll_event[maxevents];
if ((epfd = epoll_create(maxevents)) < 0)
{
delete [] events;
return -1;
}
FUNC_LEAVE;
return 0;
}
int CMyDevReactor::Close()
{
FUNC_ENTER;
if(_listenfd!=-1) close(_listenfd);
close(epfd);
if(events!=NULL) delete [] events;
FUNC_LEAVE;
return 0;
}
int CMyDevReactor::register_handler(int fd)
{
FUNC_ENTER;
struct epoll_event ev;
ev.data.fd=fd;
/
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 9095 2010-01-12 18:06 epoll1.cpp
----------- --------- ---------- ----- ----
9095 1
评论
共有 条评论