summaryrefslogtreecommitdiff
path: root/src/socketengines/socketengine_poll.cpp
blob: 773a898396518c1d226a6f2ea109e83b8dff4ff0 (plain)
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
#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

static long max;
static pollfd *events;
static int SocketCount;
static std::map<int, int> socket_positions;

void SocketEngine::Init()
{
	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 CoreException("Can't determine maximum number of open sockets");
	}

	events = new pollfd[max];
}

void SocketEngine::Shutdown()
{
	Process();

	for (std::map<int, Socket *>::const_iterator it = Sockets.begin(), it_end = Sockets.end(); it != it_end;)
	{
		Socket *s = it->second;
		++it;
		delete s;
	}
	Sockets.clear();

	delete [] events;
}

void SocketEngine::AddSocket(Socket *s)
{
	if (SocketCount == max)
	{
		Log() << "Unable to add fd " << s->GetFD() << " to socketengine poll, engine is full";
		return;
	}

	pollfd *ev = &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 SocketEngine::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 - 1)
	{
		pollfd *ev = &events[pos->second],
			*last_ev = &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());
	socket_positions.erase(pos);

	--SocketCount;
}

void SocketEngine::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 = &events[pos->second];
	ev->events |= POLLOUT;
	
	s->SetFlag(SF_WRITABLE);
}

void SocketEngine::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 = &events[pos->second];
	ev->events &= ~POLLOUT;

	s->UnsetFlag(SF_WRITABLE);
}

void SocketEngine::Process()
{
	int total = poll(events, SocketCount, Config->ReadTimeout * 1000);
	Anope::CurTime = time(NULL);

	/* EINTR can be given if the read timeout expires */
	if (total == -1)
	{
		if (errno != EINTR)
			Log() << "SockEngine::Process(): error: " << Anope::LastError();
		return;
	}

	for (int i = 0, processed = 0; i < SocketCount && processed != total; ++i)
	{
		pollfd *ev = &events[i];
		
		if (ev->revents != 0)
			++processed;

		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 < SocketCount; ++i)
	{
		pollfd *ev = &events[i];
		Socket *s = Sockets[ev->fd];

		if (s->HasFlag(SF_DEAD))
			delete s;
	}
}