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"
19 PollEngine::PollEngine(InspIRCd* Instance) : SocketEngine(Instance)
21 // Poll requires no special setup (which is nice).
25 ref = new EventHandler* [GetMaxFds()];
26 events = new struct pollfd[GetMaxFds()];
28 memset(events, 0, GetMaxFds() * sizeof(struct pollfd));
29 memset(ref, 0, GetMaxFds() * sizeof(EventHandler*));
32 PollEngine::~PollEngine()
34 // No destruction required, either.
39 bool PollEngine::AddFd(EventHandler* eh)
42 if ((fd < 0) || (fd > GetMaxFds() - 1))
44 ServerInstance->Logs->Log("SOCKET",DEBUG,"AddFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
48 if (GetRemainingFds() <= 1)
50 ServerInstance->Logs->Log("SOCKET",DEBUG,"No remaining FDs cannot add fd: %d", fd);
56 ServerInstance->Logs->Log("SOCKET",DEBUG,"Attempt to add duplicate fd: %d", fd);
64 events[fd].events = POLLIN;
68 events[fd].events = POLLOUT;
71 ServerInstance->Logs->Log("SOCKET", DEBUG,"New file descriptor: %d (%d)", fd, events[fd].events);
76 void PollEngine::WantWrite(EventHandler* eh)
78 events[eh->GetFd()].events = POLLIN | POLLOUT;
81 bool PollEngine::DelFd(EventHandler* eh, bool force)
84 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
86 ServerInstance->Logs->Log("SOCKET", DEBUG, "DelFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
91 events[fd].events = 0;
96 ServerInstance->Logs->Log("SOCKET", DEBUG, "Remove file descriptor: %d", fd);
100 int PollEngine::GetMaxFds()
103 return MAX_DESCRIPTORS;
105 int max = ulimit(4, 0);
108 MAX_DESCRIPTORS = max;
113 ServerInstance->Logs->Log("SOCKET", DEFAULT, "ERROR: Can't determine maximum number of open sockets!");
114 printf("ERROR: Can't determine maximum number of open sockets!\n");
115 ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
120 int PollEngine::GetRemainingFds()
122 return MAX_DESCRIPTORS - CurrentSetSize;
125 int PollEngine::DispatchEvents()
127 int i = poll(events, GetMaxFds() - 1, 1000);
129 socklen_t codesize = sizeof(int);
135 for (fd = 0; fd < GetMaxFds() - 1 && processed != i; fd++)
137 if (events[fd].revents)
140 if (events[fd].revents & POLLHUP)
143 ref[fd]->HandleEvent(EVENT_ERROR, 0);
147 if (events[fd].revents & POLLERR)
150 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0)
153 ref[fd]->HandleEvent(EVENT_ERROR, errcode);
157 if (events[fd].revents & POLLOUT)
159 // Switch to wanting read again
160 // event handlers have to request to write again if they need it
161 events[fd].events = POLLIN;
165 ref[fd]->HandleEvent(EVENT_WRITE);
168 if (events[fd].revents & POLLIN)
171 ref[fd]->HandleEvent(EVENT_READ);
179 std::string PollEngine::GetName()