]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengine_kqueue.cpp
Remove unused debug
[user/henk/code/inspircd.git] / src / socketengine_kqueue.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.h"
18 #include <sys/types.h>
19 #include <sys/event.h>
20 #include <sys/time.h>
21 #include "socketengine_kqueue.h"
22
23
24 KQueueEngine::KQueueEngine(InspIRCd* Instance) : SocketEngine(Instance)
25 {
26         EngineHandle = kqueue();
27         if (EngineHandle == -1)
28         {
29                 ServerInstance->Log(SPARSE,"ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
30                 ServerInstance->Log(SPARSE,"ERROR: this is a fatal error, exiting now.");
31                 printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
32                 printf("ERROR: this is a fatal error, exiting now.");
33                 InspIRCd::Exit(ERROR);
34         }
35         CurrentSetSize = 0;
36 }
37
38 KQueueEngine::~KQueueEngine()
39 {
40         ServerInstance->Log(DEBUG,"KQueueEngine::~KQueueEngine()");
41         close(EngineHandle);
42 }
43
44 bool KQueueEngine::AddFd(EventHandler* eh)
45 {
46         int fd = eh->GetFd();
47
48         ServerInstance->Log(DEBUG,"KQueueEngine::AddFd(%d)",fd);
49
50         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
51         {
52                 ServerInstance->Log(DEBUG,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
53                 return false;
54         }
55         if (GetRemainingFds() <= 1)
56         {
57                 ServerInstance->Log(DEBUG,"ERROR: System out of file descriptors!");
58                 return false;
59         }
60
61         if (ref[fd])
62         {
63                 ServerInstance->Log(DEBUG,"ERROR: Slot already occupied");
64                 return false;
65         }
66
67         ref[fd] = eh;
68         ServerInstance->Log(DEBUG,"Add socket %d",fd);
69
70         struct kevent ke;
71         ServerInstance->Log(DEBUG,"kqueue: Add socket to events, kq=%d socket=%d",EngineHandle,fd);
72         EV_SET(&ke, fd, eh->Readable() ? EVFILT_READ : EVFILT_WRITE, EV_ADD, 0, 0, NULL);
73
74         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
75         if (i == -1)
76         {
77                 ServerInstance->Log(DEBUG,"kqueue: List insertion failure!");
78                 return false;
79         }
80
81         CurrentSetSize++;
82         return true;
83 }
84
85 bool KQueueEngine::DelFd(EventHandler* eh)
86 {
87         int fd = eh->GetFd();
88
89         ServerInstance->Log(DEBUG,"KQueueEngine::DelFd(%d)",fd);
90
91         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
92                 return false;
93
94         struct kevent ke;
95         EV_SET(&ke, eh->GetFd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
96
97         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
98         
99         EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
100
101         int j = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
102
103         if ((j < 0) && (i < 0))
104                 return false;
105
106         CurrentSetSize--;
107         ref[fd] = NULL;
108
109         return true;
110 }
111
112 void KQueueEngine::WantWrite(EventHandler* eh)
113 {
114         /** When changing an item in a kqueue, there is no 'modify' call
115          * as in epoll. Instead, we add the item again, and this overwrites
116          * the original setting rather than adding it twice. See man kqueue.
117          */
118         struct kevent ke;
119         EV_SET(&ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL);
120         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
121         if (i == -1)
122         {
123                 ServerInstance->Log(DEBUG,"kqueue: Unable to set fd %d for wanting write", eh->GetFd());
124         }
125 }
126
127 int KQueueEngine::GetMaxFds()
128 {
129         return MAX_DESCRIPTORS;
130 }
131
132 int KQueueEngine::GetRemainingFds()
133 {
134         return MAX_DESCRIPTORS - CurrentSetSize;
135 }
136
137 int KQueueEngine::DispatchEvents()
138 {
139         ts.tv_nsec = 5000L;
140         ts.tv_sec = 0;
141         int i = kevent(EngineHandle, NULL, 0, &ke_list[0], MAX_DESCRIPTORS, &ts);
142         for (int j = 0; j < i; j++)
143         {
144                 ServerInstance->Log(DEBUG,"Handle %s event on fd %d",ke_list[j].flags & EVFILT_WRITE ? "write" : "read", ke_list[j].ident);
145                 if (ke_list[j].flags & EV_EOF)
146                 {
147                         ServerInstance->Log(DEBUG,"kqueue: Error on FD %d", ke_list[j].ident);
148                         /* We love you kqueue, oh yes we do *sings*!
149                          * kqueue gives us the error number directly in the EOF state!
150                          * Unlike smelly epoll and select, where we have to getsockopt
151                          * to get the error, this saves us time and cpu cycles. Go BSD!
152                          */
153                         if (ref[ke_list[j].ident])
154                                 ref[ke_list[j].ident]->HandleEvent(EVENT_ERROR, ke_list[j].fflags);
155                         continue;
156                 }
157                 if (ke_list[j].flags & EVFILT_WRITE)
158                 {
159                         /* This looks wrong but its right. As above, theres no modify 
160                          * call in kqueue. See the manpage.
161                          */
162                         struct kevent ke;
163                         EV_SET(&ke, ke_list[j].ident, EVFILT_READ, EV_ADD, 0, 0, NULL);
164                         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
165                         if (i == -1)
166                         {
167                                 ServerInstance->Log(DEBUG,"kqueue: Unable to set fd %d back to just wanting to read!", ke_list[j].ident);
168                         }
169                         if (ref[ke_list[j].ident])
170                                 ref[ke_list[j].ident]->HandleEvent(EVENT_WRITE);
171                 }
172                 else
173                 {
174                         if (ref[ke_list[j].ident])
175                                 ref[ke_list[j].ident]->HandleEvent(EVENT_READ);
176                 }
177         }
178
179         return i;
180 }
181
182 std::string KQueueEngine::GetName()
183 {
184         return "kqueue";
185 }