2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2014 Adam <Adam@anope.org>
5 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
6 * Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
8 * This file is part of InspIRCd. InspIRCd is free software: you can
9 * redistribute it and/or modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation, version 2.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "socketengine.h"
26 #include <sys/select.h>
29 /** A specialisation of the SocketEngine class, designed to use traditional select().
31 class SelectEngine : public SocketEngine
33 fd_set ReadSet, WriteSet, ErrSet;
37 /** Create a new SelectEngine
40 virtual bool AddFd(EventHandler* eh, int event_mask);
41 virtual void DelFd(EventHandler* eh);
42 void OnSetEvent(EventHandler* eh, int, int);
43 virtual int DispatchEvents();
44 virtual std::string GetName();
47 SelectEngine::SelectEngine()
49 MAX_DESCRIPTORS = FD_SETSIZE;
58 bool SelectEngine::AddFd(EventHandler* eh, int event_mask)
61 if ((fd < 0) || (fd > GetMaxFds() - 1))
64 if (!SocketEngine::AddFd(eh))
67 SocketEngine::SetEventMask(eh, event_mask);
68 OnSetEvent(eh, 0, event_mask);
75 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
79 void SelectEngine::DelFd(EventHandler* eh)
83 if ((fd < 0) || (fd > GetMaxFds() - 1))
87 SocketEngine::DelFd(eh);
90 FD_CLR(fd, &WriteSet);
95 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd);
98 void SelectEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
100 int fd = eh->GetFd();
101 int diff = old_mask ^ new_mask;
103 if (diff & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
105 if (new_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
106 FD_SET(fd, &ReadSet);
108 FD_CLR(fd, &ReadSet);
110 if (diff & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
112 if (new_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
113 FD_SET(fd, &WriteSet);
115 FD_CLR(fd, &WriteSet);
119 int SelectEngine::DispatchEvents()
121 static timeval tval = { 1, 0 };
123 fd_set rfdset = ReadSet, wfdset = WriteSet, errfdset = ErrSet;
125 int sresult = select(MaxFD + 1, &rfdset, &wfdset, &errfdset, &tval);
126 ServerInstance->UpdateTime();
128 /* Nothing to process this time around */
132 for (int i = 0, j = sresult; i <= MaxFD && j > 0; i++)
134 int has_read = FD_ISSET(i, &rfdset), has_write = FD_ISSET(i, &wfdset), has_error = FD_ISSET(i, &errfdset);
136 if (has_read || has_write || has_error)
140 EventHandler* ev = GetRef(i);
148 socklen_t codesize = sizeof(int);
150 if (getsockopt(i, SOL_SOCKET, SO_ERROR, (char*)&errcode, &codesize) < 0)
153 ev->HandleEvent(EVENT_ERROR, errcode);
160 SetEventMask(ev, ev->GetEventMask() & ~FD_READ_WILL_BLOCK);
161 ev->HandleEvent(EVENT_READ);
168 int newmask = (ev->GetEventMask() & ~(FD_WRITE_WILL_BLOCK | FD_WANT_SINGLE_WRITE));
169 this->OnSetEvent(ev, ev->GetEventMask(), newmask);
170 SetEventMask(ev, newmask);
171 ev->HandleEvent(EVENT_WRITE);
179 std::string SelectEngine::GetName()
184 SocketEngine* CreateSocketEngine()
186 return new SelectEngine;