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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
#include "module.h"
#ifndef _WIN32
# include <ulimit.h>
# include <sys/poll.h>
# include <poll.h>
# ifndef POLLRDHUP
# define POLLRDHUP 0
# endif
#else
# define poll WSAPoll
# define POLLRDHUP POLLHUP
#endif
class SocketEnginePoll : public SocketEngineBase
{
private:
long max;
pollfd *events;
int SocketCount;
std::map<int, int> socket_positions;
public:
SocketEnginePoll()
{
SocketCount = 0;
#ifndef _WIN32
max = ulimit(4, 0);
#else
max = 1024;
#endif
if (max <= 0)
{
Log() << "Can't determine maximum number of open sockets";
throw ModuleException("Can't determine maximum number of open sockets");
}
events = new pollfd[max];
}
~SocketEnginePoll()
{
delete [] events;
}
void AddSocket(Socket *s)
{
if (SocketCount == max)
{
Log() << "Unable to add fd " << s->GetFD() << " to socketengine poll, engine is full";
return;
}
pollfd *ev = &this->events[SocketCount];
ev->fd = s->GetFD();
ev->events = POLLIN;
ev->revents = 0;
Sockets.insert(std::make_pair(ev->fd, s));
socket_positions.insert(std::make_pair(ev->fd, SocketCount));
++SocketCount;
}
void DelSocket(Socket *s)
{
std::map<int, int>::iterator pos = socket_positions.find(s->GetFD());
if (pos == socket_positions.end())
{
Log() << "Unable to delete unknown fd " << s->GetFD() << " from socketengine poll";
return;
}
if (pos->second != SocketCount)
{
pollfd *ev = &this->events[pos->second],
*last_ev = &this->events[SocketCount - 1];
ev->fd = last_ev->fd;
ev->events = last_ev->events;
ev->revents = last_ev->revents;
socket_positions[ev->fd] = pos->second;
}
Sockets.erase(s->GetFD());
this->socket_positions.erase(pos);
--SocketCount;
}
void MarkWritable(Socket *s)
{
if (s->HasFlag(SF_WRITABLE))
return;
std::map<int, int>::iterator pos = socket_positions.find(s->GetFD());
if (pos == socket_positions.end())
{
Log() << "Unable to mark unknown fd " << s->GetFD() << " as writable";
return;
}
pollfd *ev = &this->events[pos->second];
ev->events |= POLLOUT;
s->SetFlag(SF_WRITABLE);
}
void ClearWritable(Socket *s)
{
if (!s->HasFlag(SF_WRITABLE))
return;
std::map<int, int>::iterator pos = socket_positions.find(s->GetFD());
if (pos == socket_positions.end())
{
Log() << "Unable to mark unknown fd " << s->GetFD() << " as writable";
return;
}
pollfd *ev = &this->events[pos->second];
ev->events &= ~POLLOUT;
s->UnsetFlag(SF_WRITABLE);
}
void Process()
{
int total = poll(this->events, this->SocketCount, Config->ReadTimeout * 1000);
Anope::CurTime = time(NULL);
if (total == -1)
{
Log() << "SockEngine::Process(): error: " << Anope::LastError();
return;
}
for (int i = 0; i < total; ++i)
{
pollfd *ev = &this->events[i];
Socket *s = Sockets[ev->fd];
if (s->HasFlag(SF_DEAD))
continue;
if (ev->revents & (POLLERR | POLLRDHUP))
{
s->ProcessError();
s->SetFlag(SF_DEAD);
continue;
}
if ((ev->revents & POLLIN) && !s->ProcessRead())
s->SetFlag(SF_DEAD);
if ((ev->revents & POLLOUT) && !s->ProcessWrite())
s->SetFlag(SF_DEAD);
}
for (int i = 0; i < total; ++i)
{
pollfd *ev = &this->events[i];
Socket *s = Sockets[ev->fd];
if (s->HasFlag(SF_DEAD))
delete s;
}
}
};
class ModuleSocketEnginePoll : public Module
{
SocketEnginePoll engine;
public:
ModuleSocketEnginePoll(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator)
{
this->SetAuthor("Anope");
this->SetPermanent(true);
this->SetType(SOCKETENGINE);
SocketEngine = &engine;
}
~ModuleSocketEnginePoll()
{
SocketEngine = NULL;
}
};
MODULE_INIT(ModuleSocketEnginePoll)
|