]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengine_select.cpp
Fixed to dynamically allocate the fieldlist
[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         ref[fd] = type;
54         if (readable)
55         {
56                 log(DEBUG,"Set readbit");
57                 ref[fd] |= X_READBIT;
58         }
59         log(DEBUG,"Add socket %d",fd);
60
61         CurrentSetSize++;
62         return true;
63 }
64
65 bool SelectEngine::DelFd(int fd)
66 {
67         log(DEBUG,"SelectEngine::DelFd(%d)",fd);
68
69         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
70                 return false;
71
72         std::map<int,int>::iterator t = fds.find(fd);
73         if (t != fds.end())
74         {
75                 fds.erase(t);
76                 log(DEBUG,"Deleted fd %d",fd);
77         }
78
79         CurrentSetSize--;
80         ref[fd] = 0;
81         return true;
82 }
83
84 int SelectEngine::GetMaxFds()
85 {
86         return FD_SETSIZE;
87 }
88
89 int SelectEngine::GetRemainingFds()
90 {
91         return FD_SETSIZE - CurrentSetSize;
92 }
93
94 int SelectEngine::Wait(int* fdlist)
95 {
96         int result = 0;
97
98         FD_ZERO(&wfdset);
99         FD_ZERO(&rfdset);
100         timeval tval;
101         int sresult;
102         for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
103         {
104                 if (ref[a->second] & X_READBIT)
105                 {
106                         FD_SET (a->second, &rfdset);
107                 }
108                 else
109                 {
110                         FD_SET (a->second, &wfdset);
111                 }
112                 
113         }
114         tval.tv_sec = 0;
115         tval.tv_usec = 50L;
116         sresult = select(FD_SETSIZE, &rfdset, &wfdset, NULL, &tval);
117         if (sresult > 0)
118         {
119                 for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
120                 {
121                         if ((FD_ISSET (a->second, &rfdset)) || (FD_ISSET (a->second, &wfdset)))
122                                 fdlist[result++] = a->second;
123                 }
124         }
125
126         return result;
127 }
128
129 std::string SelectEngine::GetName()
130 {
131         return "select";
132 }