summaryrefslogtreecommitdiff
path: root/src/socketengine.cpp
blob: ec34bf74e6c1c1fa81fc0daf98a2678bc22fbc86 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*       +------------------------------------+
 *       | Inspire Internet Relay Chat Daemon |
 *       +------------------------------------+
 *
 *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
 *                       E-mail:
 *                <brain@chatspike.net>
 *                <Craig@chatspike.net>
 *
 * Written by Craig Edwards, Craig McLure, and others.
 * This program is free but copyrighted software; see
 *            the file COPYING for details.
 *
 * ---------------------------------------------------
 */

#include "inspircd_config.h"
#include "globals.h"
#include "inspircd.h"
#ifdef USE_EPOLL
#include <sys/epoll.h>
#endif
#ifdef USE_KQUEUE
#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
#endif
#include <vector>
#include <string>
#include "socketengine.h"

char ref[MAX_DESCRIPTORS];

SocketEngine::SocketEngine()
{
	log(DEBUG,"SocketEngine::SocketEngine()");
#ifdef USE_EPOLL
	EngineHandle = epoll_create(MAX_DESCRIPTORS);
#endif
#ifdef USE_KQUEUE
	EngineHandle = kqueue();
#endif
	CurrentSetSize = 0;
}

SocketEngine::~SocketEngine()
{
	log(DEBUG,"SocketEngine::~SocketEngine()");
#ifdef USE_EPOLL
	close(EngineHandle);
#endif
#ifdef USE_KQUEUE
	close(EngineHandle);
#endif
}

char SocketEngine::GetType(int fd)
{
	if ((fd < 0) || (fd > MAX_DESCRIPTORS))
		return X_EMPTY_SLOT;
	/* Mask off the top bit used for 'read/write' state */
	return (ref[fd] & ~0x80);
}

bool SocketEngine::AddFd(int fd, bool readable, char type)
{
	if ((fd < 0) || (fd > MAX_DESCRIPTORS))
	{
		log(DEFAULT,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
		return false;
	}
	if (GetRemainingFds() <= 1)
	{
		log(DEFAULT,"ERROR: System out of file descriptors!");
		return false;
	}
#ifdef USE_SELECT
	fds[fd] = fd;
#endif
	ref[fd] = type;
	if (readable)
	{
		log(DEBUG,"Set readbit");
		ref[fd] |= X_READBIT;
	}
	log(DEBUG,"Add socket %d",fd);
#ifdef USE_EPOLL
	struct epoll_event ev;
	log(DEBUG,"epoll: Add socket to events, ep=%d socket=%d",EngineHandle,fd);
	readable ? ev.events = EPOLLIN : ev.events = EPOLLOUT;
	ev.data.fd = fd;
	int i = epoll_ctl(EngineHandle, EPOLL_CTL_ADD, fd, &ev);
	if (i < 0)
	{
		log(DEBUG,"epoll: List insertion failure!");
		return false;
	}
#endif
#ifdef USE_KQUEUE
	struct kevent ke;
	log(DEBUG,"kqueue: Add socket to events, kq=%d socket=%d",EngineHandle,fd);
	EV_SET(&ke, fd, readable ? EVFILT_READ : EVFILT_WRITE, EV_ADD, 0, 0, NULL);
	int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
	if (i == -1)
	{
		log(DEBUG,"kqueue: List insertion failure!");
		return false;
	}
#endif
	CurrentSetSize++;
	return true;
}

bool SocketEngine::DelFd(int fd)
{
	log(DEBUG,"SocketEngine::DelFd(%d)",fd);

	if ((fd < 0) || (fd > MAX_DESCRIPTORS))
		return false;

#ifdef USE_SELECT
	std::map<int,int>::iterator t = fds.find(fd);
	if (t != fds.end())
	{
		fds.erase(t);
		log(DEBUG,"Deleted fd %d",fd);
	}
#endif
#ifdef USE_KQUEUE
	struct kevent ke;
	EV_SET(&ke, fd, ref[fd] & X_READBIT ? EVFILT_READ : EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
	int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
	if (i == -1)
	{
		log(DEBUG,"kqueue: Failed to remove socket from queue!");
		return false;
	}
#endif
#ifdef USE_EPOLL
	struct epoll_event ev;
	ref[fd] && X_READBIT ? ev.events = EPOLLIN : ev.events = EPOLLOUT;
	ev.data.fd = fd;
	int i = epoll_ctl(EngineHandle, EPOLL_CTL_DEL, fd, &ev);
	if (i < 0)
	{
		log(DEBUG,"epoll: List deletion failure!");
		return false;
	}
#endif
	CurrentSetSize--;
	ref[fd] = 0;
	return true;
}

int SocketEngine::GetMaxFds()
{
#ifdef USE_SELECT
	return FD_SETSIZE;
#endif
#ifdef USE_KQUEUE
	return MAX_DESCRIPTORS;
#endif
#ifdef USE_EPOLL
	return MAX_DESCRIPTORS;
#endif
}

int SocketEngine::GetRemainingFds()
{
#ifdef USE_SELECT
	return FD_SETSIZE - CurrentSetSize;
#endif
#ifdef USE_KQUEUE
	return MAX_DESCRIPTORS - CurrentSetSize;
#endif
#ifdef USE_EPOLL
	return MAX_DESCRIPTORS - CurrentSetSize;
#endif
}

int SocketEngine::Wait(int* fdlist)
{
	int result = 0;
#ifdef USE_SELECT
	FD_ZERO(&wfdset);
	FD_ZERO(&rfdset);
	timeval tval;
	int sresult;
	for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
	{
		if (ref[a->second] & X_READBIT)
		{
			FD_SET (a->second, &rfdset);
		}
		else
		{
			FD_SET (a->second, &wfdset);
		}
		
	}
	tval.tv_sec = 0;
	tval.tv_usec = 100L;
	sresult = select(FD_SETSIZE, &rfdset, &wfdset, NULL, &tval);
	if (sresult > 0)
	{
		for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
		{
			if ((FD_ISSET (a->second, &rfdset)) || (FD_ISSET (a->second, &wfdset)))
				fdlist[result++] = a->second;
		}
	}
#endif
#ifdef USE_KQUEUE
	ts.tv_nsec = 10000L;
	ts.tv_sec = 0;
	int i = kevent(EngineHandle, NULL, 0, &ke_list[0], MAX_DESCRIPTORS, &ts);
	for (int j = 0; j < i; j++)
		fdlist[result++] = ke_list[j].ident;
#endif
#ifdef USE_EPOLL
	int i = epoll_wait(EngineHandle, events, MAX_DESCRIPTORS, 100);
	for (int j = 0; j < i; j++)
		fdlist[result++] = events[j].data.fd;
#endif
	return result;
}

std::string SocketEngine::GetName()
{
#ifdef USE_SELECT
	return "select";
#endif
#ifdef USE_KQUEUE
	return "kqueue";
#endif
#ifdef USE_EPOLL
	return "epoll";
#endif
	return "misconfigured";
}