]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengines/socketengine_poll.cpp
Fix warning, thanks peavums
[user/henk/code/inspircd.git] / src / socketengines / socketengine_poll.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "exitcodes.h"
16 #include "socketengines/socketengine_poll.h"
17 #include <poll.h>
18 #include <ulimit.h>
19
20 PollEngine::PollEngine(InspIRCd* Instance) : SocketEngine(Instance)
21 {
22         // Poll requires no special setup (which is nice).
23         CurrentSetSize = 0;
24         MAX_DESCRIPTORS = 0;
25
26         ref = new EventHandler* [GetMaxFds()];
27         events = new struct pollfd[GetMaxFds()];
28
29         memset(events, 0, GetMaxFds() * sizeof(struct pollfd));
30         memset(ref, 0, GetMaxFds() * sizeof(EventHandler*));
31 }
32
33 PollEngine::~PollEngine()
34 {
35         // No destruction required, either.
36         delete[] ref;
37         delete[] events;
38 }
39
40 bool PollEngine::AddFd(EventHandler* eh)
41 {
42         int fd = eh->GetFd();
43         if ((fd < 0) || (fd > GetMaxFds() - 1))
44         {
45                 ServerInstance->Logs->Log("SOCKET",DEBUG,"AddFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
46                 return false;
47         }
48
49         if (GetRemainingFds() <= 1)
50         {
51                 ServerInstance->Logs->Log("SOCKET",DEBUG,"No remaining FDs cannot add fd: %d", fd);
52                 return false;
53         }
54
55         if (ref[fd])
56         {
57                 ServerInstance->Logs->Log("SOCKET",DEBUG,"Attempt to add duplicate fd: %d", fd);
58                 return false;
59         }
60
61         ref[fd] = eh;
62         events[fd].fd = fd;
63         if (eh->Readable())
64         {
65                 events[fd].events = POLLIN;
66         }
67         else
68         {
69                 events[fd].events = POLLOUT;
70         }
71
72         ServerInstance->Logs->Log("SOCKET", DEBUG,"New file descriptor: %d (%d)", fd, events[fd].events);
73         CurrentSetSize++;
74         return true;
75 }
76
77 void PollEngine::WantWrite(EventHandler* eh)
78 {
79         events[eh->GetFd()].events = POLLIN | POLLOUT;
80 }
81
82 bool PollEngine::DelFd(EventHandler* eh, bool force)
83 {
84         int fd = eh->GetFd();
85         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
86         {
87                 ServerInstance->Logs->Log("SOCKET", DEBUG, "DelFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
88                 return false;
89         }
90
91         events[fd].fd = -1;
92         events[fd].events = 0;
93
94         CurrentSetSize--;
95         ref[fd] = NULL;
96
97         ServerInstance->Logs->Log("SOCKET", DEBUG, "Remove file descriptor: %d", fd);
98         return true;
99 }
100
101 int PollEngine::GetMaxFds()
102 {
103         if (MAX_DESCRIPTORS)
104                 return MAX_DESCRIPTORS;
105
106         int max = ulimit(4, 0);
107         if (max > 0)
108         {
109                 MAX_DESCRIPTORS = max;
110                 return max;
111         }
112         else
113         {
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);
117         }
118         return 0;
119 }
120
121 int PollEngine::GetRemainingFds()
122 {
123         return MAX_DESCRIPTORS - CurrentSetSize;
124 }
125
126 int PollEngine::DispatchEvents()
127 {
128         int i = poll(events, GetMaxFds() - 1, 1000);
129         int fd = 0;
130         socklen_t codesize = sizeof(int);
131         int errcode;
132         int processed = 0;
133
134         if (i > 0)
135         {
136                 for (fd = 0; fd < GetMaxFds() - 1 && processed != i; fd++)
137                 {
138                         if (events[fd].revents)
139                                 processed++;
140
141                         if (events[fd].revents & POLLHUP)
142                         {
143                                 if (ref[fd])
144                                         ref[fd]->HandleEvent(EVENT_ERROR, 0);
145                                 continue;
146                         }
147
148                         if (events[fd].revents & POLLERR)
149                         {
150                                 // Get error number
151                                 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0)
152                                         errcode = errno;
153                                 if (ref[fd])
154                                         ref[fd]->HandleEvent(EVENT_ERROR, errcode);
155                                 continue;
156                         }
157
158                         if (events[fd].revents & POLLOUT)
159                         {
160                                 // Switch to wanting read again
161                                 // event handlers have to request to write again if they need it
162                                 events[fd].events = POLLIN;
163
164
165                                 if (ref[fd])
166                                         ref[fd]->HandleEvent(EVENT_WRITE);
167                         }
168
169                         if (events[fd].revents & POLLIN)
170                         {
171                                 if (ref[fd])
172                                         ref[fd]->HandleEvent(EVENT_READ);
173                         }
174                 }
175         }
176
177         return i;
178 }
179
180 std::string PollEngine::GetName()
181 {
182         return "poll";
183 }
184