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/>.
25 #include <sys/select.h>
28 /** A specialisation of the SocketEngine class, designed to use traditional select().
32 fd_set ReadSet, WriteSet, ErrSet;
36 void SocketEngine::Init()
38 MAX_DESCRIPTORS = FD_SETSIZE;
45 void SocketEngine::Deinit()
49 void SocketEngine::RecoverFromFork()
53 bool SocketEngine::AddFd(EventHandler* eh, int event_mask)
56 if ((fd < 0) || (fd > GetMaxFds() - 1))
59 if (!SocketEngine::AddFdRef(eh))
62 eh->SetEventMask(event_mask);
63 OnSetEvent(eh, 0, event_mask);
68 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
72 void SocketEngine::DelFd(EventHandler* eh)
76 if ((fd < 0) || (fd > GetMaxFds() - 1))
79 SocketEngine::DelFdRef(eh);
82 FD_CLR(fd, &WriteSet);
87 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd);
90 void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
93 int diff = old_mask ^ new_mask;
95 if (diff & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
97 if (new_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
100 FD_CLR(fd, &ReadSet);
102 if (diff & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
104 if (new_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
105 FD_SET(fd, &WriteSet);
107 FD_CLR(fd, &WriteSet);
111 int SocketEngine::DispatchEvents()
117 fd_set rfdset = ReadSet, wfdset = WriteSet, errfdset = ErrSet;
119 int sresult = select(MaxFD + 1, &rfdset, &wfdset, &errfdset, &tval);
120 ServerInstance->UpdateTime();
122 for (int i = 0, j = sresult; i <= MaxFD && j > 0; i++)
124 int has_read = FD_ISSET(i, &rfdset), has_write = FD_ISSET(i, &wfdset), has_error = FD_ISSET(i, &errfdset);
126 if (!(has_read || has_write || has_error))
131 EventHandler* ev = GetRef(i);
139 socklen_t codesize = sizeof(int);
141 if (getsockopt(i, SOL_SOCKET, SO_ERROR, (char*)&errcode, &codesize) < 0)
144 ev->OnEventHandlerError(errcode);
151 ev->SetEventMask(ev->GetEventMask() & ~FD_READ_WILL_BLOCK);
152 ev->OnEventHandlerRead();
160 int newmask = (ev->GetEventMask() & ~(FD_WRITE_WILL_BLOCK | FD_WANT_SINGLE_WRITE));
161 SocketEngine::OnSetEvent(ev, ev->GetEventMask(), newmask);
162 ev->SetEventMask(newmask);
163 ev->OnEventHandlerWrite();