]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengine.cpp
Remove an extern, partly because it's unused, partly because it then gets shadowed...
[user/henk/code/inspircd.git] / src / socketengine.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include "inspircd_config.h"
18 #include "globals.h"
19 #include "inspircd.h"
20 #ifdef USE_EPOLL
21 #include <sys/epoll.h>
22 #endif
23 #ifdef USE_KQUEUE
24 #include <sys/types.h>
25 #include <sys/event.h>
26 #include <sys/time.h>
27 #endif
28 #include <vector>
29 #include <string>
30 #include "socketengine.h"
31
32 char ref[MAX_DESCRIPTORS];
33
34 SocketEngine::SocketEngine()
35 {
36         log(DEBUG,"SocketEngine::SocketEngine()");
37 #ifdef USE_EPOLL
38         EngineHandle = epoll_create(MAX_DESCRIPTORS);
39 #endif
40 #ifdef USE_KQUEUE
41         EngineHandle = kqueue();
42 #endif
43         CurrentSetSize = 0;
44 }
45
46 SocketEngine::~SocketEngine()
47 {
48         log(DEBUG,"SocketEngine::~SocketEngine()");
49 #ifdef USE_EPOLL
50         close(EngineHandle);
51 #endif
52 #ifdef USE_KQUEUE
53         close(EngineHandle);
54 #endif
55 }
56
57 char SocketEngine::GetType(int fd)
58 {
59         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
60                 return X_EMPTY_SLOT;
61         /* Mask off the top bit used for 'read/write' state */
62         return (ref[fd] & ~0x80);
63 }
64
65 bool SocketEngine::AddFd(int fd, bool readable, char type)
66 {
67         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
68         {
69                 log(DEFAULT,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
70                 return false;
71         }
72         if (GetRemainingFds() <= 1)
73         {
74                 log(DEFAULT,"ERROR: System out of file descriptors!");
75                 return false;
76         }
77 #ifdef USE_SELECT
78         fds[fd] = fd;
79 #endif
80         ref[fd] = type;
81         if (readable)
82         {
83                 log(DEBUG,"Set readbit");
84                 ref[fd] |= X_READBIT;
85         }
86         log(DEBUG,"Add socket %d",fd);
87 #ifdef USE_EPOLL
88         struct epoll_event ev;
89         memset(&ev,0,sizeof(struct epoll_event));
90         log(DEBUG,"epoll: Add socket to events, ep=%d socket=%d",EngineHandle,fd);
91         readable ? ev.events = EPOLLIN : ev.events = EPOLLOUT;
92         ev.data.fd = fd;
93         int i = epoll_ctl(EngineHandle, EPOLL_CTL_ADD, fd, &ev);
94         if (i < 0)
95         {
96                 log(DEBUG,"epoll: List insertion failure!");
97                 return false;
98         }
99 #endif
100 #ifdef USE_KQUEUE
101         struct kevent ke;
102         log(DEBUG,"kqueue: Add socket to events, kq=%d socket=%d",EngineHandle,fd);
103         EV_SET(&ke, fd, readable ? EVFILT_READ : EVFILT_WRITE, EV_ADD, 0, 0, NULL);
104         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
105         if (i == -1)
106         {
107                 log(DEBUG,"kqueue: List insertion failure!");
108                 return false;
109         }
110 #endif
111         CurrentSetSize++;
112         return true;
113 }
114
115 bool SocketEngine::HasFd(int fd)
116 {
117         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
118                 return false;
119         return (ref[fd] != 0);
120 }
121
122 bool SocketEngine::DelFd(int fd)
123 {
124         log(DEBUG,"SocketEngine::DelFd(%d)",fd);
125
126         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
127                 return false;
128
129 #ifdef USE_SELECT
130         std::map<int,int>::iterator t = fds.find(fd);
131         if (t != fds.end())
132         {
133                 fds.erase(t);
134                 log(DEBUG,"Deleted fd %d",fd);
135         }
136 #endif
137 #ifdef USE_KQUEUE
138         struct kevent ke;
139         EV_SET(&ke, fd, ref[fd] & X_READBIT ? EVFILT_READ : EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
140         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
141         if (i == -1)
142         {
143                 log(DEBUG,"kqueue: Failed to remove socket from queue!");
144                 return false;
145         }
146 #endif
147 #ifdef USE_EPOLL
148         struct epoll_event ev;
149         memset(&ev,0,sizeof(struct epoll_event));
150         ref[fd] && X_READBIT ? ev.events = EPOLLIN : ev.events = EPOLLOUT;
151         ev.data.fd = fd;
152         int i = epoll_ctl(EngineHandle, EPOLL_CTL_DEL, fd, &ev);
153         if (i < 0)
154         {
155                 log(DEBUG,"epoll: List deletion failure!");
156                 return false;
157         }
158 #endif
159         CurrentSetSize--;
160         ref[fd] = 0;
161         return true;
162 }
163
164 int SocketEngine::GetMaxFds()
165 {
166 #ifdef USE_SELECT
167         return FD_SETSIZE;
168 #endif
169 #ifdef USE_KQUEUE
170         return MAX_DESCRIPTORS;
171 #endif
172 #ifdef USE_EPOLL
173         return MAX_DESCRIPTORS;
174 #endif
175 }
176
177 int SocketEngine::GetRemainingFds()
178 {
179 #ifdef USE_SELECT
180         return FD_SETSIZE - CurrentSetSize;
181 #endif
182 #ifdef USE_KQUEUE
183         return MAX_DESCRIPTORS - CurrentSetSize;
184 #endif
185 #ifdef USE_EPOLL
186         return MAX_DESCRIPTORS - CurrentSetSize;
187 #endif
188 }
189
190 int SocketEngine::Wait(int* fdlist)
191 {
192         int result = 0;
193 #ifdef USE_SELECT
194         FD_ZERO(&wfdset);
195         FD_ZERO(&rfdset);
196         timeval tval;
197         int sresult;
198         for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
199         {
200                 if (ref[a->second] & X_READBIT)
201                 {
202                         FD_SET (a->second, &rfdset);
203                 }
204                 else
205                 {
206                         FD_SET (a->second, &wfdset);
207                 }
208                 
209         }
210         tval.tv_sec = 0;
211         tval.tv_usec = 50L;
212         sresult = select(FD_SETSIZE, &rfdset, &wfdset, NULL, &tval);
213         if (sresult > 0)
214         {
215                 for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
216                 {
217                         if ((FD_ISSET (a->second, &rfdset)) || (FD_ISSET (a->second, &wfdset)))
218                                 fdlist[result++] = a->second;
219                 }
220         }
221 #endif
222 #ifdef USE_KQUEUE
223         ts.tv_nsec = 5000L;
224         ts.tv_sec = 0;
225         int i = kevent(EngineHandle, NULL, 0, &ke_list[0], MAX_DESCRIPTORS, &ts);
226         for (int j = 0; j < i; j++)
227                 fdlist[result++] = ke_list[j].ident;
228 #endif
229 #ifdef USE_EPOLL
230         int i = epoll_wait(EngineHandle, events, MAX_DESCRIPTORS, 50);
231         for (int j = 0; j < i; j++)
232                 fdlist[result++] = events[j].data.fd;
233 #endif
234         return result;
235 }
236
237 std::string SocketEngine::GetName()
238 {
239 #ifdef USE_SELECT
240         return "select";
241 #endif
242 #ifdef USE_KQUEUE
243         return "kqueue";
244 #endif
245 #ifdef USE_EPOLL
246         return "epoll";
247 #endif
248         return "misconfigured";
249 }