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 * ---------------------------------------------------
18 #include <sys/epoll.h>
19 #include "socketengine_epoll.h"
21 EPollEngine::EPollEngine(InspIRCd* Instance) : SocketEngine(Instance)
23 EngineHandle = epoll_create(MAX_DESCRIPTORS);
25 if (EngineHandle == -1)
27 ServerInstance->Log(SPARSE,"ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
28 ServerInstance->Log(SPARSE,"ERROR: this is a fatal error, exiting now.");
29 printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
30 printf("ERROR: this is a fatal error, exiting now.");
31 InspIRCd::Exit(ERROR);
36 EPollEngine::~EPollEngine()
41 bool EPollEngine::AddFd(EventHandler* eh)
44 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
46 ServerInstance->Log(DEFAULT,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
49 if (GetRemainingFds() <= 1)
51 ServerInstance->Log(DEFAULT,"ERROR: System out of file descriptors!");
59 ServerInstance->Log(DEBUG,"***** Add socket %d",fd);
61 struct epoll_event ev;
62 memset(&ev,0,sizeof(struct epoll_event));
63 ServerInstance->Log(DEBUG,"epoll: Add socket to events, ep=%d socket=%d",EngineHandle,fd);
64 eh->Readable() ? ev.events = EPOLLIN : ev.events = EPOLLOUT;
66 int i = epoll_ctl(EngineHandle, EPOLL_CTL_ADD, fd, &ev);
69 ServerInstance->Log(DEBUG,"epoll: List insertion failure!");
77 bool EPollEngine::DelFd(EventHandler* eh)
79 ServerInstance->Log(DEBUG,"EPollEngine::DelFd(%d)",eh->GetFd());
82 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
85 struct epoll_event ev;
86 memset(&ev,0,sizeof(struct epoll_event));
87 ref[fd]->Readable() ? ev.events = EPOLLIN : ev.events = EPOLLOUT;
89 int i = epoll_ctl(EngineHandle, EPOLL_CTL_DEL, fd, &ev);
92 ServerInstance->Log(DEBUG,"epoll: List deletion failure!");
101 int EPollEngine::GetMaxFds()
103 return MAX_DESCRIPTORS;
106 int EPollEngine::GetRemainingFds()
108 return MAX_DESCRIPTORS - CurrentSetSize;
111 int EPollEngine::DispatchEvents()
113 int i = epoll_wait(EngineHandle, events, MAX_DESCRIPTORS, 50);
114 for (int j = 0; j < i; j++)
116 ServerInstance->Log(DEBUG,"Handle %s event on fd %d",ref[events[j].data.fd]->Readable() ? "read" : "write", ref[events[j].data.fd]->GetFd());
117 ref[events[j].data.fd]->HandleEvent(ref[events[j].data.fd]->Readable() ? EVENT_READ : EVENT_WRITE);
123 std::string EPollEngine::GetName()