2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2009 Uli Schlachter <psychon@znc.in>
6 * Copyright (C) 2009 Craig Edwards <craigedwards@brainbox.cc>
7 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "exitcodes.h"
26 #ifndef SOCKETENGINE_POLL
27 #define SOCKETENGINE_POLL
33 #include "inspircd_config.h"
35 #include "socketengine.h"
39 # define __USE_XOPEN /* fuck every fucking OS ever made. needed by poll.h to work.*/
42 # include <sys/poll.h>
43 # include <sys/resource.h>
45 # define struct pollfd WSAPOLLFD
51 /** A specialisation of the SocketEngine class, designed to use poll().
53 class PollEngine : public SocketEngine
56 /** These are used by poll() to hold socket events
58 struct pollfd *events;
59 /** This map maps fds to an index in the events array.
61 std::map<int, unsigned int> fd_mappings;
63 /** Create a new PollEngine
66 /** Delete a PollEngine
68 virtual ~PollEngine();
69 virtual bool AddFd(EventHandler* eh, int event_mask);
70 virtual void OnSetEvent(EventHandler* eh, int old_mask, int new_mask);
71 virtual EventHandler* GetRef(int fd);
72 virtual void DelFd(EventHandler* eh);
73 virtual int DispatchEvents();
74 virtual std::string GetName();
79 PollEngine::PollEngine()
83 if (!getrlimit(RLIMIT_NOFILE, &limits))
85 MAX_DESCRIPTORS = limits.rlim_cur;
89 ServerInstance->Logs->Log("SOCKET", DEFAULT, "ERROR: Can't determine maximum number of open sockets: %s", strerror(errno));
90 std::cout << "ERROR: Can't determine maximum number of open sockets: " << strerror(errno) << std::endl;
91 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
94 ref = new EventHandler* [GetMaxFds()];
95 events = new struct pollfd[GetMaxFds()];
97 memset(events, 0, GetMaxFds() * sizeof(struct pollfd));
98 memset(ref, 0, GetMaxFds() * sizeof(EventHandler*));
101 PollEngine::~PollEngine()
103 // No destruction required, either.
108 static int mask_to_poll(int event_mask)
111 if (event_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
113 if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
118 bool PollEngine::AddFd(EventHandler* eh, int event_mask)
120 int fd = eh->GetFd();
121 if ((fd < 0) || (fd > GetMaxFds() - 1))
123 ServerInstance->Logs->Log("SOCKET",DEBUG,"AddFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
127 if (fd_mappings.find(fd) != fd_mappings.end())
129 ServerInstance->Logs->Log("SOCKET",DEBUG,"Attempt to add duplicate fd: %d", fd);
133 unsigned int index = CurrentSetSize;
135 fd_mappings[fd] = index;
137 events[index].fd = fd;
138 events[index].events = mask_to_poll(event_mask);
140 ServerInstance->Logs->Log("SOCKET", DEBUG,"New file descriptor: %d (%d; index %d)", fd, events[index].events, index);
141 SocketEngine::SetEventMask(eh, event_mask);
146 EventHandler* PollEngine::GetRef(int fd)
148 std::map<int, unsigned int>::iterator it = fd_mappings.find(fd);
149 if (it == fd_mappings.end())
151 return ref[it->second];
154 void PollEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
156 std::map<int, unsigned int>::iterator it = fd_mappings.find(eh->GetFd());
157 if (it == fd_mappings.end())
159 ServerInstance->Logs->Log("SOCKET",DEBUG,"SetEvents() on unknown fd: %d", eh->GetFd());
163 events[it->second].events = mask_to_poll(new_mask);
166 void PollEngine::DelFd(EventHandler* eh)
168 int fd = eh->GetFd();
169 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
171 ServerInstance->Logs->Log("SOCKET", DEBUG, "DelFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
175 std::map<int, unsigned int>::iterator it = fd_mappings.find(fd);
176 if (it == fd_mappings.end())
178 ServerInstance->Logs->Log("SOCKET",DEBUG,"DelFd() on unknown fd: %d", fd);
182 unsigned int index = it->second;
183 unsigned int last_index = CurrentSetSize - 1;
184 int last_fd = events[last_index].fd;
186 if (index != last_index)
188 // We need to move the last fd we got into this gap (gaps are evil!)
190 // So update the mapping for the last fd to its new position
191 fd_mappings[last_fd] = index;
193 // move last_fd from last_index into index
194 events[index].fd = last_fd;
195 events[index].events = events[last_index].events;
197 ref[index] = ref[last_index];
200 // Now remove all data for the last fd we got into out list.
201 // Above code made sure this always is right
202 fd_mappings.erase(it);
203 events[last_index].fd = 0;
204 events[last_index].events = 0;
205 ref[last_index] = NULL;
209 ServerInstance->Logs->Log("SOCKET", DEBUG, "Remove file descriptor: %d (index: %d) "
210 "(Filled gap with: %d (index: %d))", fd, index, last_fd, last_index);
213 int PollEngine::DispatchEvents()
215 int i = poll(events, CurrentSetSize, 1000);
217 socklen_t codesize = sizeof(int);
220 ServerInstance->UpdateTime();
224 for (index = 0; index < CurrentSetSize && processed != i; index++)
226 if (events[index].revents)
228 EventHandler* eh = ref[index];
232 if (events[index].revents & POLLHUP)
234 eh->HandleEvent(EVENT_ERROR, 0);
238 if (events[index].revents & POLLERR)
241 int fd = events[index].fd;
244 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0)
246 eh->HandleEvent(EVENT_ERROR, errcode);
250 if (events[index].revents & POLLIN)
252 SetEventMask(eh, eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
253 eh->HandleEvent(EVENT_READ);
254 if (eh != ref[index])
255 // whoops, deleted out from under us
259 if (events[index].revents & POLLOUT)
261 int mask = eh->GetEventMask();
262 mask &= ~(FD_WRITE_WILL_BLOCK | FD_WANT_SINGLE_WRITE);
263 SetEventMask(eh, mask);
264 events[index].events = mask_to_poll(mask);
265 eh->HandleEvent(EVENT_WRITE);
273 std::string PollEngine::GetName()
278 SocketEngine* CreateSocketEngine()
280 return new PollEngine;