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 void DelFd(EventHandler* eh);
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 void SelectEngine::DelFd(EventHandler* eh)
76 if ((fd < 0) || (fd > GetMaxFds() - 1))
82 ServerInstance->Logs->Log("SOCKET",DEBUG,"Remove file descriptor: %d", fd);
85 void SelectEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
90 int SelectEngine::DispatchEvents()
94 socklen_t codesize = sizeof(int);
97 fd_set wfdset, rfdset, errfdset;
102 /* Populate the select FD sets (this is why select sucks compared to epoll, kqueue) */
103 for (unsigned int i = 0; i < FD_SETSIZE; i++)
105 EventHandler* eh = ref[i];
108 int state = eh->GetEventMask();
109 if (state & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
111 if (state & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
113 FD_SET (i, &errfdset);
116 /* One second wait */
120 sresult = select(FD_SETSIZE, &rfdset, &wfdset, &errfdset, &tval);
121 ServerInstance->UpdateTime();
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);
156 if (FD_ISSET (i, &wfdset))
159 SetEventMask(ev, ev->GetEventMask() & ~(FD_WRITE_WILL_BLOCK | FD_WANT_SINGLE_WRITE));
160 ev->HandleEvent(EVENT_WRITE);
169 std::string SelectEngine::GetName()
174 SocketEngine* CreateSocketEngine()
176 return new SelectEngine;