]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengine_epoll.cpp
Made SANICK not collide the user (theres no need to in the new 1.1 now we have return...
[user/henk/code/inspircd.git] / src / socketengine_epoll.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include "inspircd.h"
18 #include <sys/epoll.h>
19 #include "socketengine_epoll.h"
20
21 EPollEngine::EPollEngine(InspIRCd* Instance) : SocketEngine(Instance)
22 {
23         EngineHandle = epoll_create(MAX_DESCRIPTORS);
24
25         if (EngineHandle == -1)
26         {
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);
32         }
33         CurrentSetSize = 0;
34 }
35
36 EPollEngine::~EPollEngine()
37 {
38         close(EngineHandle);
39 }
40
41 bool EPollEngine::AddFd(EventHandler* eh)
42 {
43         int fd = eh->GetFd();
44         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
45         {
46                 ServerInstance->Log(DEFAULT,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
47                 return false;
48         }
49         if (GetRemainingFds() <= 1)
50         {
51                 ServerInstance->Log(DEFAULT,"ERROR: System out of file descriptors!");
52                 return false;
53         }
54         if (ref[fd])
55                 return false;
56
57         ref[fd] = eh;
58
59         ServerInstance->Log(DEBUG,"***** Add socket %d",fd);
60
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;
65         ev.data.fd = fd;
66         int i = epoll_ctl(EngineHandle, EPOLL_CTL_ADD, fd, &ev);
67         if (i < 0)
68         {
69                 ServerInstance->Log(DEBUG,"epoll: List insertion failure!");
70                 return false;
71         }
72
73         CurrentSetSize++;
74         return true;
75 }
76
77 bool EPollEngine::DelFd(EventHandler* eh)
78 {
79         ServerInstance->Log(DEBUG,"EPollEngine::DelFd(%d)",eh->GetFd());
80
81         int fd = eh->GetFd();
82         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
83                 return false;
84
85         struct epoll_event ev;
86         memset(&ev,0,sizeof(struct epoll_event));
87         ref[fd]->Readable() ? ev.events = EPOLLIN : ev.events = EPOLLOUT;
88         ev.data.fd = fd;
89         int i = epoll_ctl(EngineHandle, EPOLL_CTL_DEL, fd, &ev);
90         if (i < 0)
91         {
92                 ServerInstance->Log(DEBUG,"epoll: List deletion failure!");
93                 return false;
94         }
95
96         CurrentSetSize--;
97         ref[fd] = NULL;
98         return true;
99 }
100
101 int EPollEngine::GetMaxFds()
102 {
103         return MAX_DESCRIPTORS;
104 }
105
106 int EPollEngine::GetRemainingFds()
107 {
108         return MAX_DESCRIPTORS - CurrentSetSize;
109 }
110
111 int EPollEngine::DispatchEvents()
112 {
113         int i = epoll_wait(EngineHandle, events, MAX_DESCRIPTORS, 50);
114         for (int j = 0; j < i; j++)
115         {
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);
118         }
119
120         return i;
121 }
122
123 std::string EPollEngine::GetName()
124 {
125         return "epoll";
126 }
127