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"
20 #include <sys/types.h>
21 #include <sys/event.h>
25 #include "socketengine_kqueue.h"
26 #include "helperfuncs.h"
28 KQueueEngine::KQueueEngine(InspIRCd* Instance) : SocketEngine(Instance)
30 EngineHandle = kqueue();
31 if (EngineHandle == -1)
33 ServerInstance->Log(SPARSE,"ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
34 ServerInstance->Log(SPARSE,"ERROR: this is a fatal error, exiting now.");
35 printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
36 printf("ERROR: this is a fatal error, exiting now.");
37 InspIRCd::Exit(ERROR);
42 KQueueEngine::~KQueueEngine()
44 ServerInstance->Log(DEBUG,"KQueueEngine::~KQueueEngine()");
48 bool KQueueEngine::AddFd(int fd, bool readable, char type)
50 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
52 ServerInstance->Log(DEFAULT,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
55 if (GetRemainingFds() <= 1)
57 ServerInstance->Log(DEFAULT,"ERROR: System out of file descriptors!");
67 ServerInstance->Log(DEBUG,"Set readbit");
70 ServerInstance->Log(DEBUG,"Add socket %d",fd);
73 ServerInstance->Log(DEBUG,"kqueue: Add socket to events, kq=%d socket=%d",EngineHandle,fd);
74 EV_SET(&ke, fd, readable ? EVFILT_READ : EVFILT_WRITE, EV_ADD, 0, 0, NULL);
75 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
78 ServerInstance->Log(DEBUG,"kqueue: List insertion failure!");
86 bool KQueueEngine::DelFd(int fd)
88 ServerInstance->Log(DEBUG,"KQueueEngine::DelFd(%d)",fd);
90 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
94 EV_SET(&ke, fd, ref[fd] & X_READBIT ? EVFILT_READ : EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
95 int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
98 ServerInstance->Log(DEBUG,"kqueue: Failed to remove socket from queue!");
107 int KQueueEngine::GetMaxFds()
109 return MAX_DESCRIPTORS;
112 int KQueueEngine::GetRemainingFds()
114 return MAX_DESCRIPTORS - CurrentSetSize;
117 int KQueueEngine::Wait(int* fdlist)
123 int i = kevent(EngineHandle, NULL, 0, &ke_list[0], MAX_DESCRIPTORS, &ts);
124 for (int j = 0; j < i; j++)
125 fdlist[result++] = ke_list[j].ident;
130 std::string KQueueEngine::GetName()