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