1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 #include "inspircd_config.h"
21 #include <sys/epoll.h>
24 #include <sys/types.h>
25 #include <sys/event.h>
30 #include "socketengine.h"
32 char ref[MAX_DESCRIPTORS];
34 SocketEngine::SocketEngine()
36 log(DEBUG,"SocketEngine::SocketEngine()");
38 EngineHandle = epoll_create(MAX_DESCRIPTORS);
41 EngineHandle = kqueue();
46 SocketEngine::~SocketEngine()
48 log(DEBUG,"SocketEngine::~SocketEngine()");
57 char SocketEngine::GetType(int fd)
59 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
61 /* Mask off the top bit used for 'read/write' state */
62 return (ref[fd] & ~0x80);
65 bool SocketEngine::AddFd(int fd, bool readable, char type)
67 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
69 log(DEFAULT,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
72 if (GetRemainingFds() <= 1)
74 log(DEFAULT,"ERROR: System out of file descriptors!");
83 log(DEBUG,"Set readbit");
86 log(DEBUG,"Add socket %d",fd);
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;
93 int i = epoll_ctl(EngineHandle, EPOLL_CTL_ADD, fd, &ev);
96 log(DEBUG,"epoll: List insertion failure!");
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);
107 log(DEBUG,"kqueue: List insertion failure!");
115 bool SocketEngine::DelFd(int fd)
117 log(DEBUG,"SocketEngine::DelFd(%d)",fd);
119 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
123 std::map<int,int>::iterator t = fds.find(fd);
127 log(DEBUG,"Deleted fd %d",fd);
132 EV_SET(&ke, fd, ref[fd] & X_READBIT ? EVFILT_READ : EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
133 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
136 log(DEBUG,"kqueue: Failed to remove socket from queue!");
141 struct epoll_event ev;
142 memset(&ev,0,sizeof(struct epoll_event));
143 ref[fd] && X_READBIT ? ev.events = EPOLLIN : ev.events = EPOLLOUT;
145 int i = epoll_ctl(EngineHandle, EPOLL_CTL_DEL, fd, &ev);
148 log(DEBUG,"epoll: List deletion failure!");
157 int SocketEngine::GetMaxFds()
163 return MAX_DESCRIPTORS;
166 return MAX_DESCRIPTORS;
170 int SocketEngine::GetRemainingFds()
173 return FD_SETSIZE - CurrentSetSize;
176 return MAX_DESCRIPTORS - CurrentSetSize;
179 return MAX_DESCRIPTORS - CurrentSetSize;
183 int SocketEngine::Wait(int* fdlist)
191 for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
193 if (ref[a->second] & X_READBIT)
195 FD_SET (a->second, &rfdset);
199 FD_SET (a->second, &wfdset);
205 sresult = select(FD_SETSIZE, &rfdset, &wfdset, NULL, &tval);
208 for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
210 if ((FD_ISSET (a->second, &rfdset)) || (FD_ISSET (a->second, &wfdset)))
211 fdlist[result++] = a->second;
218 int i = kevent(EngineHandle, NULL, 0, &ke_list[0], MAX_DESCRIPTORS, &ts);
219 for (int j = 0; j < i; j++)
220 fdlist[result++] = ke_list[j].ident;
223 int i = epoll_wait(EngineHandle, events, MAX_DESCRIPTORS, 100);
224 for (int j = 0; j < i; j++)
225 fdlist[result++] = events[j].data.fd;
230 std::string SocketEngine::GetName()
241 return "misconfigured";