2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
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.
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
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/>.
22 #include "exitcodes.h"
25 # error You need Solaris 10 or later to make use of this code.
32 #include "socketengine.h"
37 /** A specialisation of the SocketEngine class, designed to use solaris 10 I/O completion ports
39 class PortsEngine : public SocketEngine
42 /** These are used by ports to hold socket events
44 std::vector<port_event_t> events;
47 /** Create a new PortsEngine
50 /** Delete a PortsEngine
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();
59 PortsEngine::PortsEngine() : events(1)
61 int max = ulimit(4, 0);
64 MAX_DESCRIPTORS = max;
68 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Can't determine maximum number of open sockets!");
69 std::cout << "ERROR: Can't determine maximum number of open sockets!" << std::endl;
70 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
72 EngineHandle = port_create();
74 if (EngineHandle == -1)
76 ServerInstance->Logs->Log("SOCKET", LOG_SPARSE, "ERROR: Could not initialize socket engine: %s", strerror(errno));
77 ServerInstance->Logs->Log("SOCKET", LOG_SPARSE, "ERROR: This is a fatal error, exiting now.");
78 std::cout << "ERROR: Could not initialize socket engine: " << strerror(errno) << std::endl;
79 std::cout << "ERROR: This is a fatal error, exiting now." << std::endl;
80 ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE);
84 PortsEngine::~PortsEngine()
86 this->Close(EngineHandle);
89 static int mask_to_events(int event_mask)
92 if (event_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
94 if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
99 bool PortsEngine::AddFd(EventHandler* eh, int event_mask)
101 int fd = eh->GetFd();
102 if ((fd < 0) || (fd > GetMaxFds() - 1))
105 if (!SocketEngine::AddFdRef(eh))
108 SocketEngine::SetEventMask(eh, event_mask);
109 port_associate(EngineHandle, PORT_SOURCE_FD, fd, mask_to_events(event_mask), eh);
111 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
112 ResizeDouble(events);
117 void PortsEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
119 if (mask_to_events(new_mask) != mask_to_events(old_mask))
120 port_associate(EngineHandle, PORT_SOURCE_FD, eh->GetFd(), mask_to_events(new_mask), eh);
123 void PortsEngine::DelFd(EventHandler* eh)
125 int fd = eh->GetFd();
126 if ((fd < 0) || (fd > GetMaxFds() - 1))
129 port_dissociate(EngineHandle, PORT_SOURCE_FD, fd);
131 SocketEngine::DelFdRef(eh);
133 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd);
136 int PortsEngine::DispatchEvents()
138 struct timespec poll_time;
140 poll_time.tv_sec = 1;
141 poll_time.tv_nsec = 0;
143 unsigned int nget = 1; // used to denote a retrieve request.
144 int ret = port_getn(EngineHandle, &events[0], events.size(), &nget, &poll_time);
145 ServerInstance->UpdateTime();
147 // first handle an error condition
151 stats.TotalEvents += nget;
154 for (i = 0; i < nget; i++)
156 port_event_t& ev = events[i];
158 if (ev.portev_source != PORT_SOURCE_FD)
161 // Copy these in case the vector gets resized and ev invalidated
162 const int fd = ev.portev_object;
163 const int portev_events = ev.portev_events;
164 EventHandler* eh = GetRef(fd);
168 int mask = eh->GetEventMask();
169 if (portev_events & POLLWRNORM)
170 mask &= ~(FD_WRITE_WILL_BLOCK | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE);
171 if (portev_events & POLLRDNORM)
172 mask &= ~FD_READ_WILL_BLOCK;
173 // reinsert port for next time around, pretending to be one-shot for writes
174 SetEventMask(eh, mask);
175 port_associate(EngineHandle, PORT_SOURCE_FD, fd, mask_to_events(mask), eh);
176 if (portev_events & POLLRDNORM)
179 eh->HandleEvent(EVENT_READ);
180 if (eh != GetRef(fd))
183 if (portev_events & POLLWRNORM)
186 eh->HandleEvent(EVENT_WRITE);
193 SocketEngine* CreateSocketEngine()
195 return new PortsEngine;