summaryrefslogtreecommitdiff
path: root/src/core/m_socketengine_epoll.cpp
diff options
context:
space:
mode:
authorAdam <Adam@anope.org>2010-07-08 22:19:13 -0400
committerAdam <Adam@anope.org>2010-07-08 22:19:13 -0400
commit1cf4ebb231f2f7770b717a5e176d7bb5cbc66284 (patch)
tree16094a36484e2764c5f541c4324e1d2a6300f61b /src/core/m_socketengine_epoll.cpp
parent8f8b1e46d670f45bafdc5c888bec3f005cc06c1f (diff)
Added an epoll socket engine
Diffstat (limited to 'src/core/m_socketengine_epoll.cpp')
-rw-r--r--src/core/m_socketengine_epoll.cpp155
1 files changed, 155 insertions, 0 deletions
diff --git a/src/core/m_socketengine_epoll.cpp b/src/core/m_socketengine_epoll.cpp
new file mode 100644
index 000000000..95bc60926
--- /dev/null
+++ b/src/core/m_socketengine_epoll.cpp
@@ -0,0 +1,155 @@
+#include "module.h"
+#include <sys/epoll.h>
+#include <ulimit.h>
+
+class SocketEngineEPoll : public SocketEngineBase
+{
+ private:
+ long max;
+ int EngineHandle;
+ epoll_event *events;
+ unsigned SocketCount;
+
+ public:
+ SocketEngineEPoll()
+ {
+ SocketCount = 0;
+ max = ulimit(4, 0);
+
+ if (max <= 0)
+ {
+ Alog() << "Can't determine maximum number of open sockets";
+ throw ModuleException("Can't determine maximum number of open sockets");
+ }
+
+ EngineHandle = epoll_create(max / 4);
+
+ if (EngineHandle == -1)
+ {
+ Alog() << "Could not initialize epoll socket engine: " << strerror(errno);
+ throw ModuleException("Could not initialize epoll socket engine: " + std::string(strerror(errno)));
+ }
+
+ events = new epoll_event[max];
+ memset(events, 0, sizeof(epoll_event) * max);
+ }
+
+ ~SocketEngineEPoll()
+ {
+ delete [] events;
+ }
+
+ void AddSocket(Socket *s)
+ {
+ epoll_event ev;
+
+ memset(&ev, 0, sizeof(ev));
+
+ ev.events = EPOLLIN | EPOLLOUT;
+ ev.data.fd = s->GetSock();
+
+ if (epoll_ctl(EngineHandle, EPOLL_CTL_ADD, ev.data.fd, &ev) == -1)
+ {
+ Alog() << "Unable to add fd " << ev.data.fd << " to socketengine epoll: " << strerror(errno);
+ return;
+ }
+
+ Sockets.insert(std::make_pair(ev.data.fd, s));
+
+ ++SocketCount;
+ }
+
+ void DelSocket(Socket *s)
+ {
+ epoll_event ev;
+
+ memset(&ev, 0, sizeof(ev));
+
+ ev.data.fd = s->GetSock();
+
+ if (epoll_ctl(EngineHandle, EPOLL_CTL_DEL, ev.data.fd, &ev) == -1)
+ {
+ Alog() << "Unable to delete fd " << ev.data.fd << " from socketengine epoll: " << strerror(errno);
+ return;
+ }
+
+ Sockets.erase(ev.data.fd);
+
+ --SocketCount;
+ }
+
+ void Process()
+ {
+ int total = epoll_wait(EngineHandle, events, max - 1, (Config.ReadTimeout * 1000));
+
+ if (total == -1)
+ {
+ Alog() << "SockEngine::Process(): error " << strerror(errno);
+ return;
+ }
+
+ for (int i = 0; i < total; ++i)
+ {
+ epoll_event *ev = &events[i];
+ Socket *s = Sockets[ev->data.fd];
+
+ if (ev->events & (EPOLLHUP | EPOLLERR))
+ {
+ s->ProcessError();
+ s->SetFlag(SF_DEAD);
+ continue;
+ }
+
+ if (ev->events & EPOLLIN)
+ {
+ if (!s->ProcessRead())
+ {
+ s->SetFlag(SF_DEAD);
+ }
+ }
+
+ if (ev->events & EPOLLOUT)
+ {
+ if (!s->ProcessWrite())
+ {
+ s->SetFlag(SF_DEAD);
+ }
+ }
+ }
+
+ for (std::map<int, Socket *>::iterator it = Sockets.begin(), it_end = Sockets.end(); it != it_end;)
+ {
+ Socket *s = it->second;
+ ++it;
+
+ if (s->HasFlag(SF_DEAD))
+ {
+ delete s;
+ }
+ }
+ }
+};
+
+class ModuleSocketEngineEPoll : public Module
+{
+ SocketEngineEPoll *engine;
+
+ public:
+ ModuleSocketEngineEPoll(const std::string &modname, const std::string &creator) : Module(modname, creator)
+ {
+ this->SetPermanent(true);
+ this->SetType(SOCKETENGINE);
+
+ engine = new SocketEngineEPoll();
+ SocketEngine = engine;
+ }
+
+ ~ModuleSocketEngineEPoll()
+ {
+ delete engine;
+ SocketEngine = NULL;
+ }
+};
+
+MODULE_INIT(ModuleSocketEngineEPoll)
+