]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/socketengines/socketengine_poll.cpp
Windows: In-depth cleanup (see details)
[user/henk/code/inspircd.git] / src / socketengines / socketengine_poll.cpp
index d32d31cc4cc7b75c0412faa64460d31945aaedd0..dd38c32b978c200c6ef980819dc3d4b27e190262 100644 (file)
-/*       +------------------------------------+
- *       | Inspire Internet Relay Chat Daemon |
- *       +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
  *
- *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
- * See: http://www.inspircd.org/wiki/index.php/Credits
+ *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
+ *   Copyright (C) 2009 Uli Schlachter <psychon@znc.in>
+ *   Copyright (C) 2009 Craig Edwards <craigedwards@brainbox.cc>
+ *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
  *
- * This program is free but copyrighted software; see
- *    the file COPYING for details.
+ * This file is part of InspIRCd.  InspIRCd is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, version 2.
  *
- * ---------------------------------------------------
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+
 #include "inspircd.h"
 #include "exitcodes.h"
-#include "socketengines/socketengine_poll.h"
+
+#ifndef SOCKETENGINE_POLL
+#define SOCKETENGINE_POLL
+
+#include <vector>
+#include <string>
+#include <map>
+#include "inspircd_config.h"
+#include "inspircd.h"
+#include "socketengine.h"
+
+#ifndef _WIN32
+       #ifndef __USE_XOPEN
+           #define __USE_XOPEN /* fuck every fucking OS ever made. needed by poll.h to work.*/
+       #endif
+       #include <poll.h>
+       #include <sys/poll.h>
+#else
+       /* *grumble* */
+       #define struct pollfd WSAPOLLFD
+       #define poll WSAPoll
+#endif
+
+class InspIRCd;
+
+/** A specialisation of the SocketEngine class, designed to use poll().
+ */
+class PollEngine : public SocketEngine
+{
+private:
+       /** These are used by poll() to hold socket events
+        */
+       struct pollfd *events;
+       /** This map maps fds to an index in the events array.
+        */
+       std::map<int, unsigned int> fd_mappings;
+public:
+       /** Create a new PollEngine
+        */
+       PollEngine();
+       /** Delete a PollEngine
+        */
+       virtual ~PollEngine();
+       virtual bool AddFd(EventHandler* eh, int event_mask);
+       virtual void OnSetEvent(EventHandler* eh, int old_mask, int new_mask);
+       virtual EventHandler* GetRef(int fd);
+       virtual void DelFd(EventHandler* eh);
+       virtual int DispatchEvents();
+       virtual std::string GetName();
+};
+
+#endif
+
 #include <ulimit.h>
+#ifdef __FreeBSD__
+       #include <sys/sysctl.h>
+#endif
 
-PollEngine::PollEngine(InspIRCd* Instance) : SocketEngine(Instance)
+PollEngine::PollEngine()
 {
-       // Poll requires no special setup (which is nice).
        CurrentSetSize = 0;
-       MAX_DESCRIPTORS = 0;
+#ifndef __FreeBSD__
+       int max = ulimit(4, 0);
+       if (max > 0)
+       {
+               MAX_DESCRIPTORS = max;
+       }
+       else
+       {
+               ServerInstance->Logs->Log("SOCKET", DEFAULT, "ERROR: Can't determine maximum number of open sockets: %s", strerror(errno));
+               printf("ERROR: Can't determine maximum number of open sockets: %s\n", strerror(errno));
+               ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
+       }
+#else
+       int mib[2];
+       size_t len;
+
+       mib[0] = CTL_KERN;
+       mib[1] = KERN_MAXFILES;
+       len = sizeof(MAX_DESCRIPTORS);
+       sysctl(mib, 2, &MAX_DESCRIPTORS, &len, NULL, 0);
+#endif
 
        ref = new EventHandler* [GetMaxFds()];
        events = new struct pollfd[GetMaxFds()];
@@ -36,7 +119,17 @@ PollEngine::~PollEngine()
        delete[] events;
 }
 
-bool PollEngine::AddFd(EventHandler* eh)
+static int mask_to_poll(int event_mask)
+{
+       int rv = 0;
+       if (event_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
+               rv |= POLLIN;
+       if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
+               rv |= POLLOUT;
+       return rv;
+}
+
+bool PollEngine::AddFd(EventHandler* eh, int event_mask)
 {
        int fd = eh->GetFd();
        if ((fd < 0) || (fd > GetMaxFds() - 1))
@@ -45,148 +138,145 @@ bool PollEngine::AddFd(EventHandler* eh)
                return false;
        }
 
-       if (GetRemainingFds() <= 1)
-       {
-               ServerInstance->Logs->Log("SOCKET",DEBUG,"No remaining FDs cannot add fd: %d", fd);
-               return false;
-       }
-
-       if (ref[fd])
+       if (fd_mappings.find(fd) != fd_mappings.end())
        {
                ServerInstance->Logs->Log("SOCKET",DEBUG,"Attempt to add duplicate fd: %d", fd);
                return false;
        }
 
-       ref[fd] = eh;
-       events[fd].fd = fd;
-       if (eh->Readable())
-       {
-               events[fd].events = POLLIN;
-       }
-       else
-       {
-               events[fd].events = POLLOUT;
-       }
+       unsigned int index = CurrentSetSize;
 
-       ServerInstance->Logs->Log("SOCKET", DEBUG,"New file descriptor: %d (%d)", fd, events[fd].events);
+       fd_mappings[fd] = index;
+       ref[index] = eh;
+       events[index].fd = fd;
+       events[index].events = mask_to_poll(event_mask);
+
+       ServerInstance->Logs->Log("SOCKET", DEBUG,"New file descriptor: %d (%d; index %d)", fd, events[fd].events, index);
+       SocketEngine::SetEventMask(eh, event_mask);
        CurrentSetSize++;
        return true;
 }
 
-void PollEngine::WantWrite(EventHandler* eh)
+EventHandler* PollEngine::GetRef(int fd)
 {
-       events[eh->GetFd()].events = POLLIN | POLLOUT;
+       std::map<int, unsigned int>::iterator it = fd_mappings.find(fd);
+       if (it == fd_mappings.end())
+               return NULL;
+       return ref[it->second];
 }
 
-bool PollEngine::DelFd(EventHandler* eh, bool force)
+void PollEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
 {
-       int fd = eh->GetFd();
-       if ((fd < 0) || (fd > MAX_DESCRIPTORS))
+       std::map<int, unsigned int>::iterator it = fd_mappings.find(eh->GetFd());
+       if (it == fd_mappings.end())
        {
-               ServerInstance->Logs->Log("SOCKET", DEBUG, "DelFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
-               return false;
+               ServerInstance->Logs->Log("SOCKET",DEBUG,"SetEvents() on unknown fd: %d", eh->GetFd());
+               return;
        }
 
-       events[fd].fd = -1;
-       events[fd].events = 0;
-
-       CurrentSetSize--;
-       ref[fd] = NULL;
-
-       ServerInstance->Logs->Log("SOCKET", DEBUG, "Remove file descriptor: %d", fd);
-       return true;
+       events[it->second].events = mask_to_poll(new_mask);
 }
 
-int PollEngine::GetMaxFds()
+void PollEngine::DelFd(EventHandler* eh)
 {
-#ifdef LINUX
-       if (MAX_DESCRIPTORS)
-               return MAX_DESCRIPTORS;
-
-       int max = ulimit(4, 0);
-       if (max > 0)
+       int fd = eh->GetFd();
+       if ((fd < 0) || (fd > MAX_DESCRIPTORS))
        {
-               MAX_DESCRIPTORS = max;
-               return max;
+               ServerInstance->Logs->Log("SOCKET", DEBUG, "DelFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
+               return;
        }
-       else
+
+       std::map<int, unsigned int>::iterator it = fd_mappings.find(fd);
+       if (it == fd_mappings.end())
        {
-               MAX_DESCRIPTORS = 0;
-               ServerInstance->Logs->Log("SOCKET", DEFAULT, "ERROR: Can't determine maximum number of open sockets: %s", strerror(errno));
-               printf("ERROR: Can't determine maximum number of open sockets: %s\n", strerror(errno)));
-               ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
+               ServerInstance->Logs->Log("SOCKET",DEBUG,"DelFd() on unknown fd: %d", fd);
+               return;
        }
-       return 0;
-#endif
-#ifdef FREEBSD
-       if (!MAX_DESCRIPTORS)
+
+       unsigned int index = it->second;
+       unsigned int last_index = CurrentSetSize - 1;
+       int last_fd = events[last_index].fd;
+
+       if (index != last_index)
        {
-               int mib[2], maxfiles;
-               size_t len;
-
-               mib[0] = CTL_KERN;
-               mib[1] = KERN_MAXFILES;
-               len = sizeof(maxfiles);
-               sysctl(mib, 2, &maxfiles, &len, NULL, 0);
-               MAX_DESCRIPTORS = maxfiles;
-               return maxfiles;
+               // We need to move the last fd we got into this gap (gaps are evil!)
+
+               // So update the mapping for the last fd to its new position
+               fd_mappings[last_fd] = index;
+
+               // move last_fd from last_index into index
+               events[index].fd = last_fd;
+               events[index].events = events[last_index].events;
+
+               ref[index] = ref[last_index];
        }
-       return MAX_DESCRIPTORS;
-#endif
-}
 
-int PollEngine::GetRemainingFds()
-{
-       return MAX_DESCRIPTORS - CurrentSetSize;
+       // Now remove all data for the last fd we got into out list.
+       // Above code made sure this always is right
+       fd_mappings.erase(it);
+       events[last_index].fd = 0;
+       events[last_index].events = 0;
+       ref[last_index] = NULL;
+
+       CurrentSetSize--;
+
+       ServerInstance->Logs->Log("SOCKET", DEBUG, "Remove file descriptor: %d (index: %d) "
+                       "(Filled gap with: %d (index: %d))", fd, index, last_fd, last_index);
 }
 
 int PollEngine::DispatchEvents()
 {
-       int i = poll(events, GetMaxFds() - 1, 1000);
-       int fd = 0;
+       int i = poll(events, CurrentSetSize, 1000);
+       int index;
        socklen_t codesize = sizeof(int);
        int errcode;
        int processed = 0;
+       ServerInstance->UpdateTime();
 
        if (i > 0)
        {
-               for (fd = 0; fd < GetMaxFds() - 1 && processed != i; fd++)
+               for (index = 0; index < CurrentSetSize && processed != i; index++)
                {
-                       if (events[fd].revents)
+                       if (events[index].revents)
                                processed++;
+                       EventHandler* eh = ref[index];
+                       if (!eh)
+                               continue;
 
-                       if (events[fd].revents & POLLHUP)
+                       if (events[index].revents & POLLHUP)
                        {
-                               if (ref[fd])
-                                       ref[fd]->HandleEvent(EVENT_ERROR, 0);
+                               eh->HandleEvent(EVENT_ERROR, 0);
                                continue;
                        }
 
-                       if (events[fd].revents & POLLERR)
+                       if (events[index].revents & POLLERR)
                        {
+                               // Get fd
+                               int fd = events[index].fd;
+
                                // Get error number
                                if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0)
                                        errcode = errno;
-                               if (ref[fd])
-                                       ref[fd]->HandleEvent(EVENT_ERROR, errcode);
+                               eh->HandleEvent(EVENT_ERROR, errcode);
                                continue;
                        }
 
-                       if (events[fd].revents & POLLOUT)
+                       if (events[index].revents & POLLIN)
                        {
-                               // Switch to wanting read again
-                               // event handlers have to request to write again if they need it
-                               events[fd].events = POLLIN;
-
-
-                               if (ref[fd])
-                                       ref[fd]->HandleEvent(EVENT_WRITE);
+                               SetEventMask(eh, eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
+                               eh->HandleEvent(EVENT_READ);
+                               if (eh != ref[index])
+                                       // whoops, deleted out from under us
+                                       continue;
                        }
-
-                       if (events[fd].revents & POLLIN)
+                       
+                       if (events[index].revents & POLLOUT)
                        {
-                               if (ref[fd])
-                                       ref[fd]->HandleEvent(EVENT_READ);
+                               int mask = eh->GetEventMask();
+                               mask &= ~(FD_WRITE_WILL_BLOCK | FD_WANT_SINGLE_WRITE);
+                               SetEventMask(eh, mask);
+                               events[index].events = mask_to_poll(mask);
+                               eh->HandleEvent(EVENT_WRITE);
                        }
                }
        }
@@ -199,3 +289,7 @@ std::string PollEngine::GetName()
        return "poll";
 }
 
+SocketEngine* CreateSocketEngine()
+{
+       return new PollEngine;
+}