1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include "exitcodes.h"
16 #include <sys/types.h>
17 #include <sys/event.h>
19 #include "socketengine_kqueue.h"
22 KQueueEngine::KQueueEngine(InspIRCd* Instance) : SocketEngine(Instance)
24 EngineHandle = kqueue();
25 if (EngineHandle == -1)
27 ServerInstance->Log(SPARSE,"ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
28 ServerInstance->Log(SPARSE,"ERROR: this is a fatal error, exiting now.");
29 printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
30 printf("ERROR: this is a fatal error, exiting now.");
31 InspIRCd::Exit(EXIT_STATUS_SOCKETENGINE);
36 KQueueEngine::~KQueueEngine()
41 bool KQueueEngine::AddFd(EventHandler* eh)
45 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
48 if (GetRemainingFds() <= 1)
57 EV_SET(&ke, fd, eh->Readable() ? EVFILT_READ : EVFILT_WRITE, EV_ADD, 0, 0, NULL);
59 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
65 ServerInstance->Log(DEBUG,"New file descriptor: %d", fd);
69 bool KQueueEngine::DelFd(EventHandler* eh, bool force)
73 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
77 EV_SET(&ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
79 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
81 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
83 int j = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
85 if ((j < 0) && (i < 0) && !force)
91 ServerInstance->Log(DEBUG,"Remove file descriptor: %d", fd);
95 void KQueueEngine::WantWrite(EventHandler* eh)
97 /** When changing an item in a kqueue, there is no 'modify' call
98 * as in epoll. Instead, we add the item again, and this overwrites
99 * the original setting rather than adding it twice. See man kqueue.
102 EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL);
103 kevent(EngineHandle, &ke, 1, 0, 0, NULL);
106 int KQueueEngine::GetMaxFds()
108 return MAX_DESCRIPTORS;
111 int KQueueEngine::GetRemainingFds()
113 return MAX_DESCRIPTORS - CurrentSetSize;
116 int KQueueEngine::DispatchEvents()
120 int i = kevent(EngineHandle, NULL, 0, &ke_list[0], MAX_DESCRIPTORS, &ts);
121 for (int j = 0; j < i; j++)
123 if (ke_list[j].flags & EV_EOF)
125 /* We love you kqueue, oh yes we do *sings*!
126 * kqueue gives us the error number directly in the EOF state!
127 * Unlike smelly epoll and select, where we have to getsockopt
128 * to get the error, this saves us time and cpu cycles. Go BSD!
130 if (ref[ke_list[j].ident])
131 ref[ke_list[j].ident]->HandleEvent(EVENT_ERROR, ke_list[j].fflags);
134 if (ke_list[j].flags & EVFILT_WRITE)
136 /* This looks wrong but its right. As above, theres no modify
137 * call in kqueue. See the manpage.
140 EV_SET(&ke, ke_list[j].ident, EVFILT_READ, EV_ADD, 0, 0, NULL);
141 kevent(EngineHandle, &ke, 1, 0, 0, NULL);
142 if (ref[ke_list[j].ident])
143 ref[ke_list[j].ident]->HandleEvent(EVENT_WRITE);
147 if (ref[ke_list[j].ident])
148 ref[ke_list[j].ident]->HandleEvent(EVENT_READ);
155 std::string KQueueEngine::GetName()