]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengines/socketengine_ports.cpp
New socketengine stuff:
[user/henk/code/inspircd.git] / src / socketengines / socketengine_ports.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "exitcodes.h"
23 #include <port.h>
24
25 #ifndef SOCKETENGINE_PORTS
26 #define SOCKETENGINE_PORTS
27
28 #ifndef __sun
29 # error You need Solaris 10 or later to make use of this code.
30 #endif
31
32 #include <vector>
33 #include <string>
34 #include <map>
35 #include "inspircd.h"
36 #include "socketengine.h"
37 #include <port.h>
38 #include <iostream>
39
40 /** A specialisation of the SocketEngine class, designed to use solaris 10 I/O completion ports
41  */
42 class PortsEngine : public SocketEngine
43 {
44 private:
45         /** These are used by ports to hold socket events
46          */
47         std::vector<port_event_t> events;
48         int EngineHandle;
49 public:
50         /** Create a new PortsEngine
51          */
52         PortsEngine();
53         /** Delete a PortsEngine
54          */
55         virtual ~PortsEngine();
56         virtual bool AddFd(EventHandler* eh, int event_mask);
57         virtual void OnSetEvent(EventHandler* eh, int old_mask, int new_mask);
58         virtual void DelFd(EventHandler* eh);
59         virtual int DispatchEvents();
60         virtual std::string GetName();
61 };
62
63 #endif
64
65
66 #include <ulimit.h>
67
68 PortsEngine::PortsEngine() : events(1)
69 {
70         int max = ulimit(4, 0);
71         if (max > 0)
72         {
73                 MAX_DESCRIPTORS = max;
74         }
75         else
76         {
77                 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Can't determine maximum number of open sockets!");
78                 std::cout << "ERROR: Can't determine maximum number of open sockets!" << std::endl;
79                 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
80         }
81         EngineHandle = port_create();
82
83         if (EngineHandle == -1)
84         {
85                 ServerInstance->Logs->Log("SOCKET", LOG_SPARSE, "ERROR: Could not initialize socket engine: %s", strerror(errno));
86                 ServerInstance->Logs->Log("SOCKET", LOG_SPARSE, "ERROR: This is a fatal error, exiting now.");
87                 std::cout << "ERROR: Could not initialize socket engine: " << strerror(errno) << std::endl;
88                 std::cout << "ERROR: This is a fatal error, exiting now." << std::endl;
89                 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
90         }
91         CurrentSetSize = 0;
92 }
93
94 PortsEngine::~PortsEngine()
95 {
96         this->Close(EngineHandle);
97 }
98
99 static int mask_to_events(int event_mask)
100 {
101         int rv = 0;
102         if (event_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
103                 rv |= POLLRDNORM;
104         if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
105                 rv |= POLLWRNORM;
106         return rv;
107 }
108
109 bool PortsEngine::AddFd(EventHandler* eh, int event_mask)
110 {
111         int fd = eh->GetFd();
112         if ((fd < 0) || (fd > GetMaxFds() - 1))
113                 return false;
114
115         if (!SocketEngine::AddFd(eh))
116                 return false;
117
118         SocketEngine::SetEventMask(eh, event_mask);
119         port_associate(EngineHandle, PORT_SOURCE_FD, fd, mask_to_events(event_mask), eh);
120
121         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
122         CurrentSetSize++;
123         ResizeDouble(events);
124
125         return true;
126 }
127
128 void PortsEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
129 {
130         if (mask_to_events(new_mask) != mask_to_events(old_mask))
131                 port_associate(EngineHandle, PORT_SOURCE_FD, eh->GetFd(), mask_to_events(new_mask), eh);
132 }
133
134 void PortsEngine::DelFd(EventHandler* eh)
135 {
136         int fd = eh->GetFd();
137         if ((fd < 0) || (fd > GetMaxFds() - 1))
138                 return;
139
140         port_dissociate(EngineHandle, PORT_SOURCE_FD, fd);
141
142         CurrentSetSize--;
143         SocketEngine::DelFd(eh);
144
145         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd);
146 }
147
148 int PortsEngine::DispatchEvents()
149 {
150         struct timespec poll_time;
151
152         poll_time.tv_sec = 1;
153         poll_time.tv_nsec = 0;
154
155         unsigned int nget = 1; // used to denote a retrieve request.
156         int ret = port_getn(EngineHandle, &events[0], events.size(), &nget, &poll_time);
157         ServerInstance->UpdateTime();
158
159         // first handle an error condition
160         if (ret == -1)
161                 return -1;
162
163         TotalEvents += nget;
164
165         unsigned int i;
166         for (i = 0; i < nget; i++)
167         {
168                 port_event_t& ev = events[i];
169
170                 if (ev.portev_source != PORT_SOURCE_FD)
171                         continue;
172
173                 int fd = ev.portev_object;
174                 EventHandler* eh = GetRef(fd);
175                 if (!eh)
176                         continue;
177
178                 int mask = eh->GetEventMask();
179                 if (ev.portev_events & POLLWRNORM)
180                         mask &= ~(FD_WRITE_WILL_BLOCK | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE);
181                 if (ev.portev_events & POLLRDNORM)
182                         mask &= ~FD_READ_WILL_BLOCK;
183                 // reinsert port for next time around, pretending to be one-shot for writes
184                 SetEventMask(eh, mask);
185                 port_associate(EngineHandle, PORT_SOURCE_FD, fd, mask_to_events(mask), eh);
186                 if (ev.portev_events & POLLRDNORM)
187                 {
188                         ReadEvents++;
189                         eh->HandleEvent(EVENT_READ);
190                         if (eh != GetRef(fd))
191                                 continue;
192                 }
193                 if (ev.portev_events & POLLWRNORM)
194                 {
195                         WriteEvents++;
196                         eh->HandleEvent(EVENT_WRITE);
197                 }
198         }
199
200         return (int)i;
201 }
202
203 std::string PortsEngine::GetName()
204 {
205         return "ports";
206 }
207
208 SocketEngine* CreateSocketEngine()
209 {
210         return new PortsEngine;
211 }