25 #ifndef DOZERG_EPOLL_H_20130506 26 #define DOZERG_EPOLL_H_20130506 28 #include <sys/epoll.h> 31 #include "tools/debug.hh" 32 #include "template.hh" 50 const char *
const kName[] = {
63 return marine::tools::ToStringBits(ev, kName, ARRAY_SIZE(kName));
69 int fd()
const{
return data.fd;}
74 bool valid()
const{
return data.fd >= 0;}
79 bool canInput()
const{
return events & EPOLLIN;}
89 bool error()
const{
return (events & EPOLLERR) || (events & EPOLLHUP);}
109 static const int kFdType = 6;
126 fd_ = ::epoll_create(1000);
138 bool addFd(
int fd, uint32_t flags,
bool mod =
true){
140 && (ctrl(fd, flags, EPOLL_CTL_ADD)
141 || (mod && ctrl(fd, flags, EPOLL_CTL_MOD))));
152 bool modFd(
int fd, uint32_t flags,
bool add =
true){
154 && (ctrl(fd, flags, EPOLL_CTL_MOD)
155 || (add && ctrl(fd, flags, EPOLL_CTL_ADD))));
176 revents_.resize(128);
177 int n = ::epoll_wait(
fd(), &revents_[0], revents_.size(), timeoutMs);
180 assert(
size_t(n) <= revents_.size());
188 size_t size()
const{
return revents_.size();}
194 const CEpollEvent & operator [](
size_t i)
const{
return revents_[i];}
200 const size_t kMaxPrint = 4;
204 for(
size_t i = 0;i < revents_.size() && i < kMaxPrint;++i){
207 oss<<
'['<<i<<
"]="<<revents_[i].toString();
209 if(revents_.size() > kMaxPrint)
215 bool ctrl(
int fd, uint32_t flags,
int op){
217 struct epoll_event ev;
218 memset(&ev, 0,
sizeof ev);
219 ev.events = flags | EPOLLET;
221 return (0 == ::epoll_ctl(this->
fd(), op, fd, &ev));
224 std::vector<CEpollEvent> revents_;
virtual std::string toString() const
Get readable description of this object.
Definition: file.hh:163
const char * fdTypeName() const
Get fd (file identifier) type name.
Definition: epoll.hh:119
size_t size() const
Get number of file descriptors that have pending events.
Definition: epoll.hh:188
bool wait(int timeoutMs=-1)
Wait for epoll events.
Definition: epoll.hh:173
Abstract interface of fd (file descriptor).
Definition: file.hh:43
Definition: to_string.hh:43
std::string toString() const
Get readable description.
Definition: epoll.hh:199
bool delFd(int fd)
Remove an fd (file descriptor) from epoll.
Definition: epoll.hh:162
bool create()
Initialize epoll.
Definition: epoll.hh:124
bool modFd(int fd, uint32_t flags, bool add=true)
Modify flags of an fd (file descriptor).
Definition: epoll.hh:152
bool canOutput() const
Test if there are write events.
Definition: epoll.hh:84
Encapsulation for fd (file descriptor) and regular files.
bool valid() const
Test if fd is valid.
Definition: epoll.hh:74
bool canInput() const
Test if there are read events.
Definition: epoll.hh:79
bool error() const
Test if there are errors.
Definition: epoll.hh:89
std::string toString() const
Get readable description.
Definition: epoll.hh:94
int fdType() const
Get fd (file descriptor) type identifier.
Definition: epoll.hh:114
Representation of epoll(7).
Definition: epoll.hh:106
bool addFd(int fd, uint32_t flags, bool mod=true)
Add fd (file descriptor) to epoll.
Definition: epoll.hh:138
Representation of epoll_event.
Definition: epoll.hh:41
int fd() const
Get fd (file descriptor).
Definition: epoll.hh:69
static std::string EventsName(uint32_t ev)
Get events description.
Definition: epoll.hh:49