]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengine_select.cpp
8d77da46126706ecb86e622a30936d7ccd4667aa
[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 "socketengine_select.h"
23 #include "helperfuncs.h"
24
25 SelectEngine::SelectEngine()
26 {
27         log(DEBUG,"SelectEngine::SelectEngine()");
28         EngineHandle = 0;
29         CurrentSetSize = 0;
30 }
31
32 SelectEngine::~SelectEngine()
33 {
34         log(DEBUG,"SelectEngine::~SelectEngine()");
35 }
36
37 bool SelectEngine::AddFd(int fd, bool readable, char type)
38 {
39         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
40         {
41                 log(DEFAULT,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
42                 return false;
43         }
44         if (GetRemainingFds() <= 1)
45         {
46                 log(DEFAULT,"ERROR: System out of file descriptors!");
47                 return false;
48         }
49
50         fds[fd] = fd;
51
52         ref[fd] = type;
53         if (readable)
54         {
55                 log(DEBUG,"Set readbit");
56                 ref[fd] |= X_READBIT;
57         }
58         log(DEBUG,"Add socket %d",fd);
59
60         CurrentSetSize++;
61         return true;
62 }
63
64 bool SelectEngine::DelFd(int fd)
65 {
66         log(DEBUG,"SelectEngine::DelFd(%d)",fd);
67
68         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
69                 return false;
70
71         std::map<int,int>::iterator t = fds.find(fd);
72         if (t != fds.end())
73         {
74                 fds.erase(t);
75                 log(DEBUG,"Deleted fd %d",fd);
76         }
77
78         CurrentSetSize--;
79         ref[fd] = 0;
80         return true;
81 }
82
83 int SelectEngine::GetMaxFds()
84 {
85         return FD_SETSIZE;
86 }
87
88 int SelectEngine::GetRemainingFds()
89 {
90         return FD_SETSIZE - CurrentSetSize;
91 }
92
93 int SelectEngine::Wait(int* fdlist)
94 {
95         int result = 0;
96
97         FD_ZERO(&wfdset);
98         FD_ZERO(&rfdset);
99         timeval tval;
100         int sresult;
101         for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
102         {
103                 if (ref[a->second] & X_READBIT)
104                 {
105                         FD_SET (a->second, &rfdset);
106                 }
107                 else
108                 {
109                         FD_SET (a->second, &wfdset);
110                 }
111                 
112         }
113         tval.tv_sec = 0;
114         tval.tv_usec = 50L;
115         sresult = select(FD_SETSIZE, &rfdset, &wfdset, NULL, &tval);
116         if (sresult > 0)
117         {
118                 for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
119                 {
120                         if ((FD_ISSET (a->second, &rfdset)) || (FD_ISSET (a->second, &wfdset)))
121                                 fdlist[result++] = a->second;
122                 }
123         }
124
125         return result;
126 }
127
128 std::string SelectEngine::GetName()
129 {
130         return "select";
131 }