summaryrefslogtreecommitdiff
path: root/src/socketengines/socketengine_kqueue.cpp
blob: ef592646f07a79aca19611cbefd1d1c7bc19ed3e (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
#include "module.h"
#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>

static int kq_fd, max_fds;
static struct kevent *change_events, *event_events;
static int change_count;

static struct kevent *GetChangeEvent()
{
	if (change_count == max_fds)
	{
		timespec zero_timespec = { 0, 0 };
		for (int i = 0; i < change_count; ++i)
			kevent(kq_fd, &change_events[i], 1, NULL, 0, &zero_timespec);
		change_count = 0;
	}

	return &change_events[change_count++];
}

void SocketEngine::Init()
{
	kq_fd = kqueue();
	max_fds = getdtablesize();

	if (kq_fd < 0)
		throw SocketException("Unable to create kqueue engine: " + Anope::LastError());
	else if (max_fds <= 0)
		throw SocketException("Can't determine maximum number of open sockets");

	change_events = new struct kevent[max_fds];
	event_events = new struct kevent[max_fds];

	change_count = 0;
}

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 [] change_events;
	delete [] event_events;
}

void SocketEngine::AddSocket(Socket *s)
{
	struct kevent *event = GetChangeEvent();
	EV_SET(event, s->GetFD(), EVFILT_READ, EV_ADD, 0, 0, NULL);

	Sockets.insert(std::make_pair(s->GetFD(), s));
}

void SocketEngine::DelSocket(Socket *s)
{
	struct kevent *event = GetChangeEvent();
	EV_SET(event, s->GetFD(), EVFILT_READ, EV_DELETE, 0, 0, NULL);

	event = GetChangeEvent();
	EV_SET(event, s->GetFD(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);

	Sockets.erase(s->GetFD());
}

void SocketEngine::MarkWritable(Socket *s)
{
	if (s->HasFlag(SF_WRITABLE))
		return;

	struct kevent *event = GetChangeEvent();
	EV_SET(event, s->GetFD(), EVFILT_WRITE, EV_ADD, 0, 0, NULL);

	s->SetFlag(SF_WRITABLE);
}

void SocketEngine::ClearWritable(Socket *s)
{
	if (!s->HasFlag(SF_WRITABLE))
		return;

	struct kevent *event = GetChangeEvent();
	EV_SET(event, s->GetFD(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);

	s->UnsetFlag(SF_WRITABLE);
}

void SocketEngine::Process()
{
	static timespec kq_timespec = { Config->ReadTimeout, 0 };
	int total = kevent(kq_fd, change_events, change_count, event_events, max_fds, &kq_timespec);
	change_count = 0;
	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; i < total; ++i)
	{
		struct kevent *event = &event_events[i];
		if (event->flags & EV_ERROR)
			continue;

		std::map<int, Socket *>::iterator it = Sockets.find(event->ident);
		if (it == Sockets.end())
			continue;
		Socket *s = it->second;

		if (event->flags & EV_EOF)
		{
			socklen_t sz = sizeof(errno);
			getsockopt(s->GetFD(), SOL_SOCKET, SO_ERROR, &errno, &sz);
			s->ProcessError();
			s->SetFlag(SF_DEAD);
			delete s;
			continue;
		}

		if (!s->Process())
			continue;

		if (event->filter == EVFILT_READ && !s->ProcessRead())
			s->SetFlag(SF_DEAD);
		else if (event->filter == EVFILT_WRITE && !s->ProcessWrite())
			s->SetFlag(SF_DEAD);

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