]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/socketengine.cpp
Remove an extern, partly because it's unused, partly because it then gets shadowed...
[user/henk/code/inspircd.git] / src / socketengine.cpp
index d482fd6d67a7bd05b7d2412d3463a426e5191329..bcc8e23af32e7567545a1a135dc943004af01e2d 100644 (file)
@@ -2,7 +2,7 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  Inspire is copyright (C) 2002-2005 ChatSpike-Dev.
+ *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
  *                       E-mail:
  *                <brain@chatspike.net>
  *                <Craig@chatspike.net>
@@ -19,7 +19,6 @@
 #include "inspircd.h"
 #ifdef USE_EPOLL
 #include <sys/epoll.h>
-#define EP_DELAY 5
 #endif
 #ifdef USE_KQUEUE
 #include <sys/types.h>
 #include <string>
 #include "socketengine.h"
 
-char ref[65535];
+char ref[MAX_DESCRIPTORS];
 
 SocketEngine::SocketEngine()
 {
        log(DEBUG,"SocketEngine::SocketEngine()");
 #ifdef USE_EPOLL
-       EngineHandle = epoll_create(65535);
+       EngineHandle = epoll_create(MAX_DESCRIPTORS);
 #endif
 #ifdef USE_KQUEUE
        EngineHandle = kqueue();
 #endif
+       CurrentSetSize = 0;
 }
 
 SocketEngine::~SocketEngine()
@@ -56,7 +56,7 @@ SocketEngine::~SocketEngine()
 
 char SocketEngine::GetType(int fd)
 {
-       if ((fd < 0) || (fd > 65535))
+       if ((fd < 0) || (fd > MAX_DESCRIPTORS))
                return X_EMPTY_SLOT;
        /* Mask off the top bit used for 'read/write' state */
        return (ref[fd] & ~0x80);
@@ -64,9 +64,19 @@ char SocketEngine::GetType(int fd)
 
 bool SocketEngine::AddFd(int fd, bool readable, char type)
 {
-       if ((fd < 0) || (fd > 65535))
+       if ((fd < 0) || (fd > MAX_DESCRIPTORS))
+       {
+               log(DEFAULT,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
                return false;
-       this->fds.push_back(fd);
+       }
+       if (GetRemainingFds() <= 1)
+       {
+               log(DEFAULT,"ERROR: System out of file descriptors!");
+               return false;
+       }
+#ifdef USE_SELECT
+       fds[fd] = fd;
+#endif
        ref[fd] = type;
        if (readable)
        {
@@ -76,8 +86,9 @@ bool SocketEngine::AddFd(int fd, bool readable, char type)
        log(DEBUG,"Add socket %d",fd);
 #ifdef USE_EPOLL
        struct epoll_event ev;
+       memset(&ev,0,sizeof(struct epoll_event));
        log(DEBUG,"epoll: Add socket to events, ep=%d socket=%d",EngineHandle,fd);
-       readable ? ev.events = EPOLLIN | EPOLLET : ev.events = EPOLLOUT | EPOLLET;
+       readable ? ev.events = EPOLLIN : ev.events = EPOLLOUT;
        ev.data.fd = fd;
        int i = epoll_ctl(EngineHandle, EPOLL_CTL_ADD, fd, &ev);
        if (i < 0)
@@ -97,30 +108,35 @@ bool SocketEngine::AddFd(int fd, bool readable, char type)
                return false;
        }
 #endif
-return true;
+       CurrentSetSize++;
+       return true;
+}
+
+bool SocketEngine::HasFd(int fd)
+{
+       if ((fd < 0) || (fd > MAX_DESCRIPTORS))
+               return false;
+       return (ref[fd] != 0);
 }
 
 bool SocketEngine::DelFd(int fd)
 {
        log(DEBUG,"SocketEngine::DelFd(%d)",fd);
 
-       if ((fd < 0) || (fd > 65535))
+       if ((fd < 0) || (fd > MAX_DESCRIPTORS))
                return false;
 
-       bool found = false;
-       for (std::vector<int>::iterator i = fds.begin(); i != fds.end(); i++)
+#ifdef USE_SELECT
+       std::map<int,int>::iterator t = fds.find(fd);
+       if (t != fds.end())
        {
-               if (*i == fd)
-               {
-                       fds.erase(i);
-                       log(DEBUG,"Deleted fd %d",fd);
-                       found = true;
-                       break;
-               }
+               fds.erase(t);
+               log(DEBUG,"Deleted fd %d",fd);
        }
+#endif
 #ifdef USE_KQUEUE
        struct kevent ke;
-       EV_SET(&ke, fd, ref[fd] && X_READBIT ? EVFILT_READ : EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
+       EV_SET(&ke, fd, ref[fd] & X_READBIT ? EVFILT_READ : EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
        int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
        if (i == -1)
        {
@@ -130,7 +146,8 @@ bool SocketEngine::DelFd(int fd)
 #endif
 #ifdef USE_EPOLL
        struct epoll_event ev;
-       ref[fd] && X_READBIT ? ev.events = EPOLLIN | EPOLLET : ev.events = EPOLLOUT | EPOLLET;
+       memset(&ev,0,sizeof(struct epoll_event));
+       ref[fd] && X_READBIT ? ev.events = EPOLLIN : ev.events = EPOLLOUT;
        ev.data.fd = fd;
        int i = epoll_ctl(EngineHandle, EPOLL_CTL_DEL, fd, &ev);
        if (i < 0)
@@ -139,58 +156,82 @@ bool SocketEngine::DelFd(int fd)
                return false;
        }
 #endif
+       CurrentSetSize--;
        ref[fd] = 0;
-       return found;
+       return true;
+}
+
+int SocketEngine::GetMaxFds()
+{
+#ifdef USE_SELECT
+       return FD_SETSIZE;
+#endif
+#ifdef USE_KQUEUE
+       return MAX_DESCRIPTORS;
+#endif
+#ifdef USE_EPOLL
+       return MAX_DESCRIPTORS;
+#endif
+}
+
+int SocketEngine::GetRemainingFds()
+{
+#ifdef USE_SELECT
+       return FD_SETSIZE - CurrentSetSize;
+#endif
+#ifdef USE_KQUEUE
+       return MAX_DESCRIPTORS - CurrentSetSize;
+#endif
+#ifdef USE_EPOLL
+       return MAX_DESCRIPTORS - CurrentSetSize;
+#endif
 }
 
-bool SocketEngine::Wait(std::vector<int> &fdlist)
+int SocketEngine::Wait(int* fdlist)
 {
-       fdlist.clear();
+       int result = 0;
 #ifdef USE_SELECT
        FD_ZERO(&wfdset);
        FD_ZERO(&rfdset);
        timeval tval;
        int sresult;
-       for (unsigned int a = 0; a < fds.size(); a++)
+       for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
        {
-               if (ref[fds[a]] & X_READBIT)
+               if (ref[a->second] & X_READBIT)
                {
-                       FD_SET (fds[a], &rfdset);
+                       FD_SET (a->second, &rfdset);
                }
                else
                {
-                       FD_SET (fds[a], &wfdset);
+                       FD_SET (a->second, &wfdset);
                }
                
        }
        tval.tv_sec = 0;
-       tval.tv_usec = 10000L;
+       tval.tv_usec = 50L;
        sresult = select(FD_SETSIZE, &rfdset, &wfdset, NULL, &tval);
        if (sresult > 0)
        {
-               for (unsigned int a = 0; a < fds.size(); a++)
+               for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
                {
-                       if ((FD_ISSET (fds[a], &rfdset)) || (FD_ISSET (fds[a], &wfdset)))
-                       {
-                               log(DEBUG,"...Adding active %d",fds[a]);
-                               fdlist.push_back(fds[a]);
-                       }
+                       if ((FD_ISSET (a->second, &rfdset)) || (FD_ISSET (a->second, &wfdset)))
+                               fdlist[result++] = a->second;
                }
        }
 #endif
 #ifdef USE_KQUEUE
-       ts.tv_nsec = 10000L;
+       ts.tv_nsec = 5000L;
        ts.tv_sec = 0;
-       int i = kevent(EngineHandle, NULL, 0, &ke_list[0], 65535, &ts);
+       int i = kevent(EngineHandle, NULL, 0, &ke_list[0], MAX_DESCRIPTORS, &ts);
        for (int j = 0; j < i; j++)
-               fdlist.push_back(ke_list[j].ident);
+               fdlist[result++] = ke_list[j].ident;
 #endif
 #ifdef USE_EPOLL
-       int i = epoll_wait(EngineHandle, events, 65535, 100);
+       int i = epoll_wait(EngineHandle, events, MAX_DESCRIPTORS, 50);
        for (int j = 0; j < i; j++)
-               fdlist.push_back(events[j].data.fd);
+               fdlist[result++] = events[j].data.fd;
 #endif
-       return true;
+       return result;
 }
 
 std::string SocketEngine::GetName()