]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengine_select.cpp
Passing invalid instance to dns isnt a good idea
[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 #include "helperfuncs.h"
25
26 SelectEngine::SelectEngine()
27 {
28         log(DEBUG,"SelectEngine::SelectEngine()");
29         EngineHandle = 0;
30         CurrentSetSize = 0;
31 }
32
33 SelectEngine::~SelectEngine()
34 {
35         log(DEBUG,"SelectEngine::~SelectEngine()");
36 }
37
38 bool SelectEngine::AddFd(int fd, bool readable, char type)
39 {
40         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
41         {
42                 log(DEFAULT,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
43                 return false;
44         }
45         if (GetRemainingFds() <= 1)
46         {
47                 log(DEFAULT,"ERROR: System out of file descriptors!");
48                 return false;
49         }
50
51         fds[fd] = fd;
52
53         if (ref[fd])
54                 return false;
55
56         ref[fd] = type;
57         if (readable)
58         {
59                 log(DEBUG,"Set readbit");
60                 ref[fd] |= X_READBIT;
61         }
62         log(DEBUG,"Add socket %d",fd);
63
64         CurrentSetSize++;
65         return true;
66 }
67
68 bool SelectEngine::DelFd(int fd)
69 {
70         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                 log(DEBUG,"Deleted fd %d",fd);
80         }
81
82         CurrentSetSize--;
83         ref[fd] = 0;
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::Wait(int* fdlist)
98 {
99         int result = 0;
100
101         FD_ZERO(&wfdset);
102         FD_ZERO(&rfdset);
103         timeval tval;
104         int sresult;
105         for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
106         {
107                 if (ref[a->second] & X_READBIT)
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                                 fdlist[result++] = a->second;
126                 }
127         }
128
129         return result;
130 }
131
132 std::string SelectEngine::GetName()
133 {
134         return "select";
135 }