]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengine.cpp
Keep count of the number of events in total, and seperate read, write and error event...
[user/henk/code/inspircd.git] / src / socketengine.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core: libIRCDsocketengine */
15
16 /*********        DEFAULTS       **********/
17 /* $ExtraSources: socketengines/socketengine_select.cpp */
18 /* $ExtraObjects: socketengine_select.o */
19
20 /* $If: USE_KQUEUE */
21 /* $ExtraSources: socketengines/socketengine_kqueue.cpp */
22 /* $ExtraObjects: socketengine_kqueue.o */
23 /* $EndIf */
24
25 /* $If: USE_EPOLL */
26 /* $ExtraSources: socketengines/socketengine_epoll.cpp */
27 /* $ExtraObjects: socketengine_epoll.o */
28 /* $EndIf */
29
30 /* $If: USE_PORTS */
31 /* $ExtraSources: socketengines/socketengine_ports.cpp */
32 /* $ExtraObjects: socketengine_ports.o */
33 /* $EndIf */
34
35 #include "inspircd.h"
36 #include "socketengine.h"
37
38 int EventHandler::GetFd()
39 {
40         return this->fd;
41 }
42
43 void EventHandler::SetFd(int FD)
44 {
45         this->fd = FD;
46 }
47
48 bool EventHandler::Readable()
49 {
50         return true;
51 }
52
53 bool EventHandler::Writeable()
54 {
55         return false;
56 }
57
58 void SocketEngine::WantWrite(EventHandler* eh)
59 {
60 }
61
62 SocketEngine::SocketEngine(InspIRCd* Instance) : ServerInstance(Instance)
63 {
64         memset(ref, 0, sizeof(ref));
65         TotalEvents = WriteEvents = ReadEvents = ErrorEvents = 0;
66 }
67
68 SocketEngine::~SocketEngine()
69 {
70 }
71
72 bool SocketEngine::AddFd(EventHandler* eh)
73 {
74         return true;
75 }
76
77 bool SocketEngine::HasFd(int fd)
78 {
79         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
80                 return false;
81         return ref[fd];
82 }
83
84 EventHandler* SocketEngine::GetRef(int fd)
85 {
86         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
87                 return 0;
88         return ref[fd];
89 }
90
91 bool SocketEngine::DelFd(EventHandler* eh, bool force)
92 {
93         return true;
94 }
95
96 int SocketEngine::GetMaxFds()
97 {
98         return 0;
99 }
100
101 int SocketEngine::GetRemainingFds()
102 {
103         return 0;
104 }
105
106 int SocketEngine::DispatchEvents()
107 {
108         return 0;
109 }
110
111 std::string SocketEngine::GetName()
112 {
113         return "misconfigured";
114 }
115
116 bool SocketEngine::BoundsCheckFd(EventHandler* eh)
117 {       
118         if (!eh)
119                 return false;
120         if ((eh->GetFd() < 0) || (eh->GetFd() > MAX_DESCRIPTORS))
121                 return false;
122         return true;
123 }
124
125 #ifdef WINDOWS
126
127 int SocketEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen) { return -1; }
128 int SocketEngine::Close(int fd) { return -1; }
129 int SocketEngine::Close(EventHandler* fd) { return -1; }
130 int SocketEngine::Blocking(int fd) { return -1; }
131 int SocketEngine::NonBlocking(int fd) { return -1; }
132 int SocketEngine::GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen) { return -1; }
133 int SocketEngine::RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen) { return -1; }
134
135 #else
136
137 int SocketEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen)
138 {
139         return accept(fd->GetFd(), addr, addrlen);
140 }
141
142 int SocketEngine::Close(EventHandler* fd)
143 {
144         return close(fd->GetFd());
145 }
146
147 int SocketEngine::Close(int fd)
148 {
149         return close(fd);
150 }
151
152 int SocketEngine::Blocking(int fd)
153 {
154         int flags = fcntl(fd, F_GETFL, 0);
155         return fcntl(fd, F_SETFL, flags ^ O_NONBLOCK);
156 }
157
158 int SocketEngine::NonBlocking(int fd)
159 {
160         int flags = fcntl(fd, F_GETFL, 0);
161         return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
162 }
163
164 int SocketEngine::GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen)
165 {
166         return getsockname(fd->GetFd(), name, namelen);
167 }
168
169 int SocketEngine::RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen)
170 {
171         return recvfrom(fd->GetFd(), buf, len, flags, from, fromlen);
172 }
173
174 #endif
175
176 int SocketEngine::Send(EventHandler* fd, const void *buf, size_t len, int flags)
177 {
178         return send(fd->GetFd(), (const char*)buf, len, flags);
179 }
180
181 int SocketEngine::Recv(EventHandler* fd, void *buf, size_t len, int flags)
182 {
183         return recv(fd->GetFd(), (char*)buf, len, flags);
184 }
185
186 int SocketEngine::SendTo(EventHandler* fd, const void *buf, size_t len, int flags, const sockaddr *to, socklen_t tolen)
187 {
188         return sendto(fd->GetFd(), (const char*)buf, len, flags, to, tolen);
189 }
190
191 int SocketEngine::Connect(EventHandler* fd, const sockaddr *serv_addr, socklen_t addrlen)
192 {
193         return connect(fd->GetFd(), serv_addr, addrlen);
194 }
195
196 int SocketEngine::Shutdown(EventHandler* fd, int how)
197 {
198         return shutdown(fd->GetFd(), how);
199 }
200
201 int SocketEngine::Bind(int fd, const sockaddr *my_addr, socklen_t addrlen)
202 {
203         return bind(fd, my_addr, addrlen);
204 }
205
206 int SocketEngine::Listen(int sockfd, int backlog)
207 {
208         return listen(sockfd, backlog);
209 }
210
211 int SocketEngine::Shutdown(int fd, int how)
212 {
213         return shutdown(fd, how);
214 }
215
216 void SocketEngine::RecoverFromFork()
217 {
218 }
219