Marine Library  1.0
C++ library for Linux Networking Development
epoll.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Zhao DAI <daidodo@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or any
7  * later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see accompanying file LICENSE.txt
16  * or <http://www.gnu.org/licenses/>.
17  */
18 
25 #ifndef DOZERG_EPOLL_H_20130506
26 #define DOZERG_EPOLL_H_20130506
27 
28 #include <sys/epoll.h>
29 #include <vector>
30 #include "file.hh"
31 #include "tools/debug.hh" //tools::ToStringBits
32 #include "template.hh" //ARRAY_SIZE
33 
34 NS_SERVER_BEGIN
35 
36 class CEpoll;
37 
41 struct CEpollEvent : private epoll_event
42 {
43  friend class CEpoll;
49  static std::string EventsName(uint32_t ev){
50  const char * const kName[] = {
51  "EPOLLIN",
52  "EPOLLPRI",
53  "EPOLLOUT",
54  "EPOLLERR",
55  "EPOLLHUP",
56  NULL,
57  "EPOLLRDNORM",
58  "EPOLLRDBAND",
59  "EPOLLWRNORM",
60  "EPOLLWRBAND",
61  "EPOLLMSG"
62  };
63  return marine::tools::ToStringBits(ev, kName, ARRAY_SIZE(kName));
64  }
69  int fd() const{return data.fd;}
74  bool valid() const{return data.fd >= 0;}
79  bool canInput() const{return events & EPOLLIN;}
84  bool canOutput() const{return events & EPOLLOUT;}
89  bool error() const{return (events & EPOLLERR) || (events & EPOLLHUP);}
94  std::string toString() const{
95  CToString oss;
96  oss<<"{fd="<<data.fd
97  <<", events="<<EventsName(events)
98  <<"}";
99  return oss.str();
100  }
101 };
102 
106 class CEpoll : public IFileDesc
107 {
108 public:
109  static const int kFdType = 6;
114  int fdType() const{return kFdType;}
119  const char * fdTypeName() const{return "CEpoll";}
124  bool create(){
125  if(!valid())
126  fd_ = ::epoll_create(1000);
127  return valid();
128  }
138  bool addFd(int fd, uint32_t flags, bool mod = true){
139  return (valid()
140  && (ctrl(fd, flags, EPOLL_CTL_ADD)
141  || (mod && ctrl(fd, flags, EPOLL_CTL_MOD))));
142  }
152  bool modFd(int fd, uint32_t flags, bool add = true){
153  return (valid()
154  && (ctrl(fd, flags, EPOLL_CTL_MOD)
155  || (add && ctrl(fd, flags, EPOLL_CTL_ADD))));
156  }
162  bool delFd(int fd){return (valid() && ctrl(fd, 0, EPOLL_CTL_DEL));}
173  bool wait(int timeoutMs = -1){
174  if(!valid())
175  return false;
176  revents_.resize(128);
177  int n = ::epoll_wait(fd(), &revents_[0], revents_.size(), timeoutMs);
178  if(n < 0)
179  return false;
180  assert(size_t(n) <= revents_.size());
181  revents_.resize(n);
182  return true;
183  }
188  size_t size() const{return revents_.size();}
194  const CEpollEvent & operator [](size_t i) const{return revents_[i];}
199  std::string toString() const{
200  const size_t kMaxPrint = 4;
201  CToString oss;
202  oss<<"{IFileDesc="<<IFileDesc::toString()
203  <<", revents_={";
204  for(size_t i = 0;i < revents_.size() && i < kMaxPrint;++i){
205  if(i)
206  oss<<", ";
207  oss<<'['<<i<<"]="<<revents_[i].toString();
208  }
209  if(revents_.size() > kMaxPrint)
210  oss<<", ...";
211  oss<<"}}";
212  return oss.str();
213  }
214 private:
215  bool ctrl(int fd, uint32_t flags, int op){
216  assert(valid());
217  struct epoll_event ev;
218  memset(&ev, 0, sizeof ev);
219  ev.events = flags | EPOLLET;
220  ev.data.fd = fd;
221  return (0 == ::epoll_ctl(this->fd(), op, fd, &ev));
222  }
223  //field
224  std::vector<CEpollEvent> revents_;
225 };
226 
227 NS_SERVER_END
228 
229 #endif
230 
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