]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengine_select.cpp
a26c28e517d77e2c0badd8bbf507a602f6aef5d4
[user/henk/code/inspircd.git] / src / socketengine_select.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/select.h>
19 #include "socketengine_select.h"
20
21
22 SelectEngine::SelectEngine(InspIRCd* Instance) : SocketEngine(Instance)
23 {
24         ServerInstance->Log(DEBUG,"SelectEngine::SelectEngine()");
25         EngineHandle = 0;
26         CurrentSetSize = 0;
27         memset(writeable, 0, sizeof(writeable));
28 }
29
30 SelectEngine::~SelectEngine()
31 {
32         ServerInstance->Log(DEBUG,"SelectEngine::~SelectEngine()");
33 }
34
35 bool SelectEngine::AddFd(EventHandler* eh)
36 {
37         int fd = eh->GetFd();
38         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
39         {
40                 ServerInstance->Log(DEFAULT,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
41                 return false;
42         }
43         if (GetRemainingFds() <= 1)
44         {
45                 ServerInstance->Log(DEFAULT,"ERROR: System out of file descriptors!");
46                 return false;
47         }
48
49         fds[fd] = fd;
50
51         if (ref[fd])
52                 return false;
53
54         ref[fd] = eh;
55         ServerInstance->Log(DEBUG,"Add socket %d",fd);
56
57         CurrentSetSize++;
58         return true;
59 }
60
61 void SelectEngine::WantWrite(EventHandler* eh)
62 {
63         writeable[eh->GetFd()] = true;
64 }
65
66 bool SelectEngine::DelFd(EventHandler* eh)
67 {
68         int fd = eh->GetFd();
69
70         ServerInstance->Log(DEBUG,"SelectEngine::DelFd(%d)",fd);
71
72         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
73                 return false;
74
75         std::map<int,int>::iterator t = fds.find(fd);
76         if (t != fds.end())
77         {
78                 fds.erase(t);
79                 ServerInstance->Log(DEBUG,"Deleted fd %d",fd);
80         }
81
82         CurrentSetSize--;
83         ref[fd] = NULL;
84         return true;
85 }
86
87 int SelectEngine::GetMaxFds()
88 {
89         return FD_SETSIZE;
90 }
91
92 int SelectEngine::GetRemainingFds()
93 {
94         return FD_SETSIZE - CurrentSetSize;
95 }
96
97 int SelectEngine::DispatchEvents()
98 {
99         int result = 0;
100         timeval tval;
101         int sresult = 0;
102         EventHandler* ev[MAX_DESCRIPTORS];
103
104         FD_ZERO(&wfdset);
105         FD_ZERO(&rfdset);
106         FD_ZERO(&errfdset);
107
108         for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
109         {
110                 if (ref[a->second]->Readable())
111                         FD_SET (a->second, &rfdset);
112                 else
113                         FD_SET (a->second, &wfdset);
114                 if (writeable[a->second])
115                         FD_SET (a->second, &wfdset);
116
117                 FD_SET (a->second, &errfdset);
118         }
119         tval.tv_sec = 0;
120         tval.tv_usec = 50L;
121         sresult = select(FD_SETSIZE, &rfdset, &wfdset, &errfdset, &tval);
122         if (sresult > 0)
123         {
124                 for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
125                 {
126                         if ((FD_ISSET (a->second, &rfdset)) || (FD_ISSET (a->second, &wfdset)) || FD_ISSET (a->second, &errfdset))
127                         {
128                                 ev[result++] = ref[a->second];
129                         }
130                 }
131         }
132
133         /** An event handler may remove its own descriptor from the list, therefore it is not
134          * safe to directly iterate over the list and dispatch events there with STL iterators.
135          * Thats a shame because it makes this code slower and more resource intensive, but maybe
136          * the user should stop using select(), as select() smells anyway.
137          */
138         for (int i = 0; i < result; i++)
139         {
140                 if (ev[i])
141                 {
142                         if (FD_ISSET (a->second, &errfdset))
143                         {
144                                 if (ev[i])
145                                         ev[i]->HandleEvent(EVENT_ERROR, 0);
146                                 continue;
147                         }
148                         ServerInstance->Log(DEBUG,"Handle %s event on fd %d",writeable[ev[i]->GetFd()] || !ev[i]->Readable() ? "write" : "read", ev[i]->GetFd());
149                         if (writeable[ev[i]->GetFd()])
150                         {
151                                 if (ev[i])
152                                         ev[i]->HandleEvent(EVENT_WRITE);
153                                 writeable[ev[i]->GetFd()] = false;
154         
155                         }
156                         else
157                         {
158                                 if (ev[i])
159                                         ev[i]->HandleEvent(ev[i]->Readable() ? EVENT_READ : EVENT_WRITE);
160                         }
161                 }
162         }
163
164         return result;
165 }
166
167 std::string SelectEngine::GetName()
168 {
169         return "select";
170 }