]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengines/socketengine_ports.cpp
098e7511662ecc343751516ee404f7f9007b5a18
[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
24 #ifndef __sun
25 # error You need Solaris 10 or later to make use of this code.
26 #endif
27
28 #include <vector>
29 #include <string>
30 #include <map>
31 #include "inspircd.h"
32 #include "socketengine.h"
33 #include <port.h>
34 #include <iostream>
35 #include <ulimit.h>
36
37 /** A specialisation of the SocketEngine class, designed to use solaris 10 I/O completion ports
38  */
39 class PortsEngine : public SocketEngine
40 {
41 private:
42         /** These are used by ports to hold socket events
43          */
44         std::vector<port_event_t> events;
45         int EngineHandle;
46 public:
47         /** Create a new PortsEngine
48          */
49         PortsEngine();
50         /** Delete a PortsEngine
51          */
52         virtual ~PortsEngine();
53         virtual bool AddFd(EventHandler* eh, int event_mask);
54         virtual void OnSetEvent(EventHandler* eh, int old_mask, int new_mask);
55         virtual void DelFd(EventHandler* eh);
56         virtual int DispatchEvents();
57         virtual std::string GetName();
58 };
59
60 PortsEngine::PortsEngine() : events(1)
61 {
62         int max = ulimit(4, 0);
63         if (max > 0)
64         {
65                 MAX_DESCRIPTORS = max;
66         }
67         else
68         {
69                 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Can't determine maximum number of open sockets!");
70                 std::cout << "ERROR: Can't determine maximum number of open sockets!" << std::endl;
71                 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
72         }
73         EngineHandle = port_create();
74
75         if (EngineHandle == -1)
76         {
77                 ServerInstance->Logs->Log("SOCKET", LOG_SPARSE, "ERROR: Could not initialize socket engine: %s", strerror(errno));
78                 ServerInstance->Logs->Log("SOCKET", LOG_SPARSE, "ERROR: This is a fatal error, exiting now.");
79                 std::cout << "ERROR: Could not initialize socket engine: " << strerror(errno) << std::endl;
80                 std::cout << "ERROR: This is a fatal error, exiting now." << std::endl;
81                 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
82         }
83         CurrentSetSize = 0;
84 }
85
86 PortsEngine::~PortsEngine()
87 {
88         this->Close(EngineHandle);
89 }
90
91 static int mask_to_events(int event_mask)
92 {
93         int rv = 0;
94         if (event_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
95                 rv |= POLLRDNORM;
96         if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
97                 rv |= POLLWRNORM;
98         return rv;
99 }
100
101 bool PortsEngine::AddFd(EventHandler* eh, int event_mask)
102 {
103         int fd = eh->GetFd();
104         if ((fd < 0) || (fd > GetMaxFds() - 1))
105                 return false;
106
107         if (!SocketEngine::AddFd(eh))
108                 return false;
109
110         SocketEngine::SetEventMask(eh, event_mask);
111         port_associate(EngineHandle, PORT_SOURCE_FD, fd, mask_to_events(event_mask), eh);
112
113         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
114         CurrentSetSize++;
115         ResizeDouble(events);
116
117         return true;
118 }
119
120 void PortsEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
121 {
122         if (mask_to_events(new_mask) != mask_to_events(old_mask))
123                 port_associate(EngineHandle, PORT_SOURCE_FD, eh->GetFd(), mask_to_events(new_mask), eh);
124 }
125
126 void PortsEngine::DelFd(EventHandler* eh)
127 {
128         int fd = eh->GetFd();
129         if ((fd < 0) || (fd > GetMaxFds() - 1))
130                 return;
131
132         port_dissociate(EngineHandle, PORT_SOURCE_FD, fd);
133
134         CurrentSetSize--;
135         SocketEngine::DelFd(eh);
136
137         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd);
138 }
139
140 int PortsEngine::DispatchEvents()
141 {
142         struct timespec poll_time;
143
144         poll_time.tv_sec = 1;
145         poll_time.tv_nsec = 0;
146
147         unsigned int nget = 1; // used to denote a retrieve request.
148         int ret = port_getn(EngineHandle, &events[0], events.size(), &nget, &poll_time);
149         ServerInstance->UpdateTime();
150
151         // first handle an error condition
152         if (ret == -1)
153                 return -1;
154
155         TotalEvents += nget;
156
157         unsigned int i;
158         for (i = 0; i < nget; i++)
159         {
160                 port_event_t& ev = events[i];
161
162                 if (ev.portev_source != PORT_SOURCE_FD)
163                         continue;
164
165                 // Copy these in case the vector gets resized and ev invalidated
166                 const int fd = ev.portev_object;
167                 const int portev_events = ev.portev_events;
168                 EventHandler* eh = GetRef(fd);
169                 if (!eh)
170                         continue;
171
172                 int mask = eh->GetEventMask();
173                 if (portev_events & POLLWRNORM)
174                         mask &= ~(FD_WRITE_WILL_BLOCK | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE);
175                 if (portev_events & POLLRDNORM)
176                         mask &= ~FD_READ_WILL_BLOCK;
177                 // reinsert port for next time around, pretending to be one-shot for writes
178                 SetEventMask(eh, mask);
179                 port_associate(EngineHandle, PORT_SOURCE_FD, fd, mask_to_events(mask), eh);
180                 if (portev_events & POLLRDNORM)
181                 {
182                         ReadEvents++;
183                         eh->HandleEvent(EVENT_READ);
184                         if (eh != GetRef(fd))
185                                 continue;
186                 }
187                 if (portev_events & POLLWRNORM)
188                 {
189                         WriteEvents++;
190                         eh->HandleEvent(EVENT_WRITE);
191                 }
192         }
193
194         return (int)i;
195 }
196
197 std::string PortsEngine::GetName()
198 {
199         return "ports";
200 }
201
202 SocketEngine* CreateSocketEngine()
203 {
204         return new PortsEngine;
205 }