1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #include "inspircd_config.h"
17 #include "socketengine.h"
20 #include <sys/select.h>
23 /** A specialisation of the SocketEngine class, designed to use traditional select().
25 class SelectEngine : public SocketEngine
28 /** Create a new SelectEngine
31 /** Delete a SelectEngine
33 virtual ~SelectEngine();
34 virtual bool AddFd(EventHandler* eh, int event_mask);
35 virtual bool DelFd(EventHandler* eh, bool force = false);
36 void OnSetEvent(EventHandler* eh, int, int);
37 virtual int DispatchEvents();
38 virtual std::string GetName();
41 SelectEngine::SelectEngine()
43 MAX_DESCRIPTORS = FD_SETSIZE;
46 ref = new EventHandler* [GetMaxFds()];
47 memset(ref, 0, GetMaxFds() * sizeof(EventHandler*));
50 SelectEngine::~SelectEngine()
55 bool SelectEngine::AddFd(EventHandler* eh, int event_mask)
58 if ((fd < 0) || (fd > GetMaxFds() - 1))
65 SocketEngine::SetEventMask(eh, event_mask);
68 ServerInstance->Logs->Log("SOCKET",DEBUG,"New file descriptor: %d", fd);
72 bool SelectEngine::DelFd(EventHandler* eh, bool force)
76 if ((fd < 0) || (fd > GetMaxFds() - 1))
82 ServerInstance->Logs->Log("SOCKET",DEBUG,"Remove file descriptor: %d", fd);
86 void SelectEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
91 int SelectEngine::DispatchEvents()
95 socklen_t codesize = sizeof(int);
98 fd_set wfdset, rfdset, errfdset;
103 /* Populate the select FD sets (this is why select sucks compared to epoll, kqueue) */
104 for (unsigned int i = 0; i < FD_SETSIZE; i++)
106 EventHandler* eh = ref[i];
109 int state = eh->GetEventMask();
110 if (state & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
112 if (state & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
114 FD_SET (i, &errfdset);
117 /* One second wait */
121 sresult = select(FD_SETSIZE, &rfdset, &wfdset, &errfdset, &tval);
123 /* Nothing to process this time around */
127 for (int i = 0; i < FD_SETSIZE; i++)
129 EventHandler* ev = ref[i];
132 if (FD_ISSET (i, &errfdset))
135 if (getsockopt(i, SOL_SOCKET, SO_ERROR, (char*)&errcode, &codesize) < 0)
138 ev->HandleEvent(EVENT_ERROR, errcode);
143 /* NOTE: This is a pair of seperate if statements as the socket
144 * may be in both read and writeable state at the same time.
145 * If an error event occurs above it is not worth processing the
146 * read and write states even if set.
148 if (FD_ISSET (i, &rfdset))
151 SetEventMask(ev, ev->GetEventMask() & ~FD_READ_WILL_BLOCK);
152 ev->HandleEvent(EVENT_READ);
154 if (FD_ISSET (i, &wfdset))
157 SetEventMask(ev, ev->GetEventMask() & ~(FD_WRITE_WILL_BLOCK | FD_WANT_SINGLE_WRITE));
158 ev->HandleEvent(EVENT_WRITE);
167 std::string SelectEngine::GetName()
172 SocketEngine* CreateSocketEngine()
174 return new SelectEngine;