Anope IRC Services  Version 2.0
socketengine_epoll.cpp
Go to the documentation of this file.
1 /*
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  */
11 
12 #include "services.h"
13 #include "anope.h"
14 #include "sockets.h"
15 #include "socketengine.h"
16 #include "config.h"
17 
18 #include <sys/epoll.h>
19 #include <ulimit.h>
20 #include <errno.h>
21 
22 static int EngineHandle;
23 static std::vector<epoll_event> events;
24 
26 {
27  EngineHandle = epoll_create(4);
28 
29  if (EngineHandle == -1)
30  throw SocketException("Could not initialize epoll socket engine: " + Anope::LastError());
31 
32  events.resize(DefaultSize);
33 }
34 
36 {
37  while (!Sockets.empty())
38  delete Sockets.begin()->second;
39 }
40 
41 void SocketEngine::Change(Socket *s, bool set, SocketFlag flag)
42 {
43  if (set == s->flags[flag])
44  return;
45 
46  bool before_registered = s->flags[SF_READABLE] || s->flags[SF_WRITABLE];
47 
48  s->flags[flag] = set;
49 
50  bool now_registered = s->flags[SF_READABLE] || s->flags[SF_WRITABLE];
51 
52  epoll_event ev;
53 
54  memset(&ev, 0, sizeof(ev));
55 
56  ev.events = (s->flags[SF_READABLE] ? EPOLLIN : 0) | (s->flags[SF_WRITABLE] ? EPOLLOUT : 0);
57  ev.data.fd = s->GetFD();
58 
59  int mod;
60  if (!before_registered && now_registered)
61  mod = EPOLL_CTL_ADD;
62  else if (before_registered && !now_registered)
63  mod = EPOLL_CTL_DEL;
64  else if (before_registered && now_registered)
65  mod = EPOLL_CTL_MOD;
66  else
67  return;
68 
69  if (epoll_ctl(EngineHandle, mod, ev.data.fd, &ev) == -1)
70  throw SocketException("Unable to epoll_ctl() fd " + stringify(ev.data.fd) + " to epoll: " + Anope::LastError());
71 }
72 
74 {
75  if (Sockets.size() > events.size())
76  events.resize(events.size() * 2);
77 
78  int total = epoll_wait(EngineHandle, &events.front(), events.size(), Config->ReadTimeout * 1000);
79  Anope::CurTime = time(NULL);
80 
81  /* EINTR can be given if the read timeout expires */
82  if (total == -1)
83  {
84  if (errno != EINTR)
85  Log() << "SockEngine::Process(): error: " << Anope::LastError();
86  return;
87  }
88 
89  for (int i = 0; i < total; ++i)
90  {
91  epoll_event &ev = events[i];
92 
93  std::map<int, Socket *>::iterator it = Sockets.find(ev.data.fd);
94  if (it == Sockets.end())
95  continue;
96  Socket *s = it->second;
97 
98  if (ev.events & (EPOLLHUP | EPOLLERR))
99  {
100  s->ProcessError();
101  delete s;
102  continue;
103  }
104 
105  if (!s->Process())
106  {
107  if (s->flags[SF_DEAD])
108  delete s;
109  continue;
110  }
111 
112  if ((ev.events & EPOLLIN) && !s->ProcessRead())
113  s->flags[SF_DEAD] = true;
114 
115  if ((ev.events & EPOLLOUT) && !s->ProcessWrite())
116  s->flags[SF_DEAD] = true;
117 
118  if (s->flags[SF_DEAD])
119  delete s;
120  }
121 }
122 
static std::vector< epoll_event > events
std::bitset< SF_SIZE > flags
Definition: sockets.h:203
SocketFlag
Definition: sockets.h:124
static void Init()
CoreExport const string LastError()
Definition: misc.cpp:606
virtual bool Process()
Definition: sockets.cpp:498
virtual bool ProcessRead()
Definition: sockets.cpp:503
virtual bool ProcessWrite()
Definition: sockets.cpp:508
static void Process()
static std::map< int, Socket * > Sockets
Definition: socketengine.h:24
static void Change(Socket *s, bool set, SocketFlag flag)
CoreExport time_t CurTime
Definition: main.cpp:41
int GetFD() const
Definition: sockets.cpp:474
Definition: Config.cs:26
static const int DefaultSize
Definition: socketengine.h:21
Anope::string stringify(const T &x)
Definition: anope.h:710
static int EngineHandle
static void Shutdown()
Definition: logger.h:53
virtual void ProcessError()
Definition: sockets.cpp:513