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()
41 WSAStartup(MAKEWORD(2,2), &wsadata);
44 MaxSetSize = FD_SETSIZE;
51 void SocketEngine::Deinit()
55 void SocketEngine::RecoverFromFork()
59 bool SocketEngine::AddFd(EventHandler* eh, int event_mask)
66 if (static_cast<size_t>(fd) >= GetMaxFds())
69 if (!SocketEngine::AddFdRef(eh))
72 eh->SetEventMask(event_mask);
73 OnSetEvent(eh, 0, event_mask);
78 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd);
82 void SocketEngine::DelFd(EventHandler* eh)
89 if (static_cast<size_t>(fd) >= GetMaxFds())
92 SocketEngine::DelFdRef(eh);
95 FD_CLR(fd, &WriteSet);
100 ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Remove file descriptor: %d", fd);
103 void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
105 int fd = eh->GetFd();
106 int diff = old_mask ^ new_mask;
108 if (diff & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
110 if (new_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
111 FD_SET(fd, &ReadSet);
113 FD_CLR(fd, &ReadSet);
115 if (diff & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
117 if (new_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
118 FD_SET(fd, &WriteSet);
120 FD_CLR(fd, &WriteSet);
124 int SocketEngine::DispatchEvents()
130 fd_set rfdset = ReadSet, wfdset = WriteSet, errfdset = ErrSet;
132 int sresult = select(MaxFD + 1, &rfdset, &wfdset, &errfdset, &tval);
133 ServerInstance->UpdateTime();
135 for (int i = 0, j = sresult; i <= MaxFD && j > 0; i++)
137 int has_read = FD_ISSET(i, &rfdset), has_write = FD_ISSET(i, &wfdset), has_error = FD_ISSET(i, &errfdset);
139 if (!(has_read || has_write || has_error))
144 EventHandler* ev = GetRef(i);
152 socklen_t codesize = sizeof(int);
154 if (getsockopt(i, SOL_SOCKET, SO_ERROR, (char*)&errcode, &codesize) < 0)
157 ev->OnEventHandlerError(errcode);
163 ev->SetEventMask(ev->GetEventMask() & ~FD_READ_WILL_BLOCK);
164 ev->OnEventHandlerRead();
171 int newmask = (ev->GetEventMask() & ~(FD_WRITE_WILL_BLOCK | FD_WANT_SINGLE_WRITE));
172 SocketEngine::OnSetEvent(ev, ev->GetEventMask(), newmask);
173 ev->SetEventMask(newmask);
174 ev->OnEventHandlerWrite();