1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 "socketengines/socketengine_poll.h"
20 PollEngine::PollEngine(InspIRCd* Instance) : SocketEngine(Instance)
22 // Poll requires no special setup (which is nice).
26 ref = new EventHandler* [GetMaxFds()];
27 events = new struct pollfd[GetMaxFds()];
29 memset(events, 0, GetMaxFds() * sizeof(struct pollfd));
30 memset(ref, 0, GetMaxFds() * sizeof(EventHandler*));
33 PollEngine::~PollEngine()
35 // No destruction required, either.
40 bool PollEngine::AddFd(EventHandler* eh)
43 if ((fd < 0) || (fd > GetMaxFds() - 1))
45 ServerInstance->Logs->Log("SOCKET",DEBUG,"AddFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
49 if (GetRemainingFds() <= 1)
51 ServerInstance->Logs->Log("SOCKET",DEBUG,"No remaining FDs cannot add fd: %d", fd);
57 ServerInstance->Logs->Log("SOCKET",DEBUG,"Attempt to add duplicate fd: %d", fd);
65 events[fd].events = POLLIN;
69 events[fd].events = POLLOUT;
72 ServerInstance->Logs->Log("SOCKET", DEBUG,"New file descriptor: %d (%d)", fd, events[fd].events);
77 void PollEngine::WantWrite(EventHandler* eh)
79 events[eh->GetFd()].events = POLLIN | POLLOUT;
82 bool PollEngine::DelFd(EventHandler* eh, bool force)
85 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
87 ServerInstance->Logs->Log("SOCKET", DEBUG, "DelFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
92 events[fd].events = 0;
97 ServerInstance->Logs->Log("SOCKET", DEBUG, "Remove file descriptor: %d", fd);
101 int PollEngine::GetMaxFds()
104 return MAX_DESCRIPTORS;
106 int max = ulimit(4, 0);
109 MAX_DESCRIPTORS = max;
114 ServerInstance->Logs->Log("SOCKET", DEFAULT, "ERROR: Can't determine maximum number of open sockets!");
115 printf("ERROR: Can't determine maximum number of open sockets!\n");
116 ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
121 int PollEngine::GetRemainingFds()
123 return MAX_DESCRIPTORS - CurrentSetSize;
126 int PollEngine::DispatchEvents()
128 int i = poll(events, GetMaxFds() - 1, 1000);
130 socklen_t codesize = sizeof(int);
136 for (fd = 0; fd < GetMaxFds() - 1 && processed != i; fd++)
138 if (events[fd].revents)
141 if (events[fd].revents & POLLHUP)
144 ref[fd]->HandleEvent(EVENT_ERROR, 0);
148 if (events[fd].revents & POLLERR)
151 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0)
154 ref[fd]->HandleEvent(EVENT_ERROR, errcode);
158 if (events[fd].revents & POLLOUT)
160 // Switch to wanting read again
161 // event handlers have to request to write again if they need it
162 events[fd].events = POLLIN;
166 ref[fd]->HandleEvent(EVENT_WRITE);
169 if (events[fd].revents & POLLIN)
172 ref[fd]->HandleEvent(EVENT_READ);
180 std::string PollEngine::GetName()