1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/* RequiredFunctions: epoll_wait */
#include "module.h"
#include <sys/epoll.h>
#include <ulimit.h>
class SocketEngineEPoll : public SocketEngineBase
{
private:
long max;
int EngineHandle;
epoll_event *events;
unsigned SocketCount;
public:
SocketEngineEPoll()
{
SocketCount = 0;
max = ulimit(4, 0);
if (max <= 0)
{
Alog() << "Can't determine maximum number of open sockets";
throw ModuleException("Can't determine maximum number of open sockets");
}
EngineHandle = epoll_create(max / 4);
if (EngineHandle == -1)
{
Alog() << "Could not initialize epoll socket engine: " << strerror(errno);
throw ModuleException(Anope::string("Could not initialize epoll socket engine: ") + strerror(errno));
}
events = new epoll_event[max];
memset(events, 0, sizeof(epoll_event) * max);
}
~SocketEngineEPoll()
{
delete [] events;
}
void AddSocket(Socket *s)
{
epoll_event ev;
memset(&ev, 0, sizeof(ev));
ev.events = EPOLLIN | EPOLLOUT;
ev.data.fd = s->GetSock();
if (epoll_ctl(EngineHandle, EPOLL_CTL_ADD, ev.data.fd, &ev) == -1)
{
Alog() << "Unable to add fd " << ev.data.fd << " to socketengine epoll: " << strerror(errno);
return;
}
Sockets.insert(std::make_pair(ev.data.fd, s));
++SocketCount;
}
void DelSocket(Socket *s)
{
epoll_event ev;
memset(&ev, 0, sizeof(ev));
ev.data.fd = s->GetSock();
if (epoll_ctl(EngineHandle, EPOLL_CTL_DEL, ev.data.fd, &ev) == -1)
{
Alog() << "Unable to delete fd " << ev.data.fd << " from socketengine epoll: " << strerror(errno);
return;
}
Sockets.erase(ev.data.fd);
--SocketCount;
}
void Process()
{
int total = epoll_wait(EngineHandle, events, max - 1, Config.ReadTimeout * 1000);
if (total == -1)
{
Alog() << "SockEngine::Process(): error " << strerror(errno);
return;
}
for (int i = 0; i < total; ++i)
{
epoll_event *ev = &events[i];
Socket *s = Sockets[ev->data.fd];
if (ev->events & (EPOLLHUP | EPOLLERR))
{
s->ProcessError();
s->SetFlag(SF_DEAD);
continue;
}
if ((ev->events & EPOLLIN) && !s->ProcessRead())
s->SetFlag(SF_DEAD);
if ((ev->events & EPOLLOUT) && !s->ProcessWrite())
s->SetFlag(SF_DEAD);
}
for (std::map<int, Socket *>::iterator it = Sockets.begin(), it_end = Sockets.end(); it != it_end; )
{
Socket *s = it->second;
++it;
if (s->HasFlag(SF_DEAD))
delete s;
}
}
};
class ModuleSocketEngineEPoll : public Module
{
SocketEngineEPoll *engine;
public:
ModuleSocketEngineEPoll(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator)
{
this->SetAuthor("Anope");
this->SetPermanent(true);
this->SetType(SOCKETENGINE);
engine = new SocketEngineEPoll();
SocketEngine = engine;
}
~ModuleSocketEngineEPoll()
{
delete engine;
SocketEngine = NULL;
}
};
MODULE_INIT(ModuleSocketEngineEPoll)
|