]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengine_select.cpp
ec242b76d79f5dda13e4c270cb8994d6091871d9
[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::Wait(EventHandler** fdlist)
96 {
97         int result = 0;
98
99         FD_ZERO(&wfdset);
100         FD_ZERO(&rfdset);
101         timeval tval;
102         int sresult;
103         for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
104         {
105                 if (ref[a->second]->Readable())
106                 {
107                         FD_SET (a->second, &rfdset);
108                 }
109                 else
110                 {
111                         FD_SET (a->second, &wfdset);
112                 }
113                 
114         }
115         tval.tv_sec = 0;
116         tval.tv_usec = 50L;
117         sresult = select(FD_SETSIZE, &rfdset, &wfdset, NULL, &tval);
118         if (sresult > 0)
119         {
120                 for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
121                 {
122                         if ((FD_ISSET (a->second, &rfdset)) || (FD_ISSET (a->second, &wfdset)))
123                                 fdlist[result++] = ref[a->second];
124                 }
125         }
126
127         return result;
128 }
129
130 std::string SelectEngine::GetName()
131 {
132         return "select";
133 }