1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 #include "inspircd_config.h"
22 #include <sys/select.h>
23 #include "socketengine_select.h"
26 SelectEngine::SelectEngine(InspIRCd* Instance) : SocketEngine(Instance)
28 ServerInstance->Log(DEBUG,"SelectEngine::SelectEngine()");
33 SelectEngine::~SelectEngine()
35 ServerInstance->Log(DEBUG,"SelectEngine::~SelectEngine()");
38 bool SelectEngine::AddFd(EventHandler* eh)
41 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
43 ServerInstance->Log(DEFAULT,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
46 if (GetRemainingFds() <= 1)
48 ServerInstance->Log(DEFAULT,"ERROR: System out of file descriptors!");
58 ServerInstance->Log(DEBUG,"Add socket %d",fd);
64 bool SelectEngine::DelFd(EventHandler* eh)
68 ServerInstance->Log(DEBUG,"SelectEngine::DelFd(%d)",fd);
70 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
73 std::map<int,int>::iterator t = fds.find(fd);
77 ServerInstance->Log(DEBUG,"Deleted fd %d",fd);
85 int SelectEngine::GetMaxFds()
90 int SelectEngine::GetRemainingFds()
92 return FD_SETSIZE - CurrentSetSize;
95 int SelectEngine::DispatchEvents()
100 EventHandler* ev[MAX_DESCRIPTOR];
105 for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
107 if (ref[a->second]->Readable())
109 FD_SET (a->second, &rfdset);
113 FD_SET (a->second, &wfdset);
119 sresult = select(FD_SETSIZE, &rfdset, &wfdset, NULL, &tval);
122 for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
124 if ((FD_ISSET (a->second, &rfdset)) || (FD_ISSET (a->second, &wfdset)))
126 ev[result++] = ref[a->second];
131 /** An event handler may remove its own descriptor from the list, therefore it is not
132 * safe to directly iterate over the list and dispatch events there with STL iterators.
133 * Thats a shame because it makes this code slower and more resource intensive, but maybe
134 * the user should stop using select(), as select() smells anyway.
136 for (int i = 0; i < result; i++)
138 ServerInstance->Log(DEBUG,"Handle %s event on fd %d",ev[i]->Readable() ? "read" : "write", ev[i]->GetFd());
139 ev[i]->HandleEvent(ev[i]->Readable() ? EVENT_READ : EVENT_WRITE);
145 std::string SelectEngine::GetName()