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/>.
21 #include "inspircd_config.h"
24 #include "socketengine.h"
27 #include <sys/select.h>
30 /** A specialisation of the SocketEngine class, designed to use traditional select().
32 class SelectEngine : public SocketEngine
34 fd_set ReadSet, WriteSet, ErrSet;
38 /** Create a new SelectEngine
41 /** Delete a SelectEngine
43 virtual ~SelectEngine();
44 virtual bool AddFd(EventHandler* eh, int event_mask);
45 virtual void DelFd(EventHandler* eh);
46 void OnSetEvent(EventHandler* eh, int, int);
47 virtual int DispatchEvents();
48 virtual std::string GetName();
51 SelectEngine::SelectEngine()
53 MAX_DESCRIPTORS = FD_SETSIZE;
56 ref = new EventHandler* [GetMaxFds()];
57 memset(ref, 0, GetMaxFds() * sizeof(EventHandler*));
65 SelectEngine::~SelectEngine()
70 bool SelectEngine::AddFd(EventHandler* eh, int event_mask)
73 if ((fd < 0) || (fd > GetMaxFds() - 1))
81 SocketEngine::SetEventMask(eh, event_mask);
82 OnSetEvent(eh, 0, event_mask);
89 ServerInstance->Logs->Log("SOCKET",DEBUG,"New file descriptor: %d", fd);
93 void SelectEngine::DelFd(EventHandler* eh)
97 if ((fd < 0) || (fd > GetMaxFds() - 1))
103 FD_CLR(fd, &ReadSet);
104 FD_CLR(fd, &WriteSet);
109 ServerInstance->Logs->Log("SOCKET",DEBUG,"Remove file descriptor: %d", fd);
112 void SelectEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
114 int fd = eh->GetFd();
115 int diff = old_mask ^ new_mask;
117 if (diff & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
119 if (new_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
120 FD_SET(fd, &ReadSet);
122 FD_CLR(fd, &ReadSet);
124 if (diff & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
126 if (new_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
127 FD_SET(fd, &WriteSet);
129 FD_CLR(fd, &WriteSet);
133 int SelectEngine::DispatchEvents()
135 static timeval tval = { 1, 0 };
137 fd_set rfdset = ReadSet, wfdset = WriteSet, errfdset = ErrSet;
139 int sresult = select(MaxFD + 1, &rfdset, &wfdset, &errfdset, &tval);
140 ServerInstance->UpdateTime();
142 /* Nothing to process this time around */
146 for (int i = 0, j = sresult; i <= MaxFD && j > 0; i++)
148 int has_read = FD_ISSET(i, &rfdset), has_write = FD_ISSET(i, &wfdset), has_error = FD_ISSET(i, &errfdset);
150 if (has_read || has_write || has_error)
154 EventHandler* ev = ref[i];
162 socklen_t codesize = sizeof(int);
164 if (getsockopt(i, SOL_SOCKET, SO_ERROR, (char*)&errcode, &codesize) < 0)
167 ev->HandleEvent(EVENT_ERROR, errcode);
174 SetEventMask(ev, ev->GetEventMask() & ~FD_READ_WILL_BLOCK);
175 ev->HandleEvent(EVENT_READ);
182 SetEventMask(ev, ev->GetEventMask() & ~(FD_WRITE_WILL_BLOCK | FD_WANT_SINGLE_WRITE));
183 ev->HandleEvent(EVENT_WRITE);
191 std::string SelectEngine::GetName()
196 SocketEngine* CreateSocketEngine()
198 return new SelectEngine;