]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/socketengine_select.cpp
Move whowas containers into whowas class to avoid all cpp files including cmd_whowas...
[user/henk/code/inspircd.git] / src / socketengine_select.cpp
index 8b0379152b0d6a8f30d913b4129847ff5cd20982..2be16b282d01ff9f742fd11d897782f86eb08fca 100644 (file)
@@ -2,12 +2,9 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
- *                       E-mail:
- *                <brain@chatspike.net>
- *                <Craig@chatspike.net>
+ *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
  *
- * Written by Craig Edwards, Craig McLure, and others.
  * This program is free but copyrighted software; see
  *            the file COPYING for details.
  *
@@ -37,12 +34,12 @@ bool SelectEngine::AddFd(EventHandler* eh)
        int fd = eh->GetFd();
        if ((fd < 0) || (fd > MAX_DESCRIPTORS))
        {
-               ServerInstance->Log(DEFAULT,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
+               ServerInstance->Log(DEBUG,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
                return false;
        }
        if (GetRemainingFds() <= 1)
        {
-               ServerInstance->Log(DEFAULT,"ERROR: System out of file descriptors!");
+               ServerInstance->Log(DEBUG,"ERROR: System out of file descriptors!");
                return false;
        }
 
@@ -67,17 +64,12 @@ bool SelectEngine::DelFd(EventHandler* eh)
 {
        int fd = eh->GetFd();
 
-       ServerInstance->Log(DEBUG,"SelectEngine::DelFd(%d)",fd);
-
        if ((fd < 0) || (fd > MAX_DESCRIPTORS))
                return false;
 
        std::map<int,int>::iterator t = fds.find(fd);
        if (t != fds.end())
-       {
                fds.erase(t);
-               ServerInstance->Log(DEBUG,"Deleted fd %d",fd);
-       }
 
        CurrentSetSize--;
        ref[fd] = NULL;
@@ -100,9 +92,12 @@ int SelectEngine::DispatchEvents()
        timeval tval;
        int sresult = 0;
        EventHandler* ev[MAX_DESCRIPTORS];
+       socklen_t codesize;
+       int errcode;
 
        FD_ZERO(&wfdset);
        FD_ZERO(&rfdset);
+       FD_ZERO(&errfdset);
 
        for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
        {
@@ -112,15 +107,17 @@ int SelectEngine::DispatchEvents()
                        FD_SET (a->second, &wfdset);
                if (writeable[a->second])
                        FD_SET (a->second, &wfdset);
+
+               FD_SET (a->second, &errfdset);
        }
-       tval.tv_sec = 0;
-       tval.tv_usec = 50L;
-       sresult = select(FD_SETSIZE, &rfdset, &wfdset, NULL, &tval);
+       tval.tv_sec = 1;
+       tval.tv_usec = 0;
+       sresult = select(FD_SETSIZE, &rfdset, &wfdset, &errfdset, &tval);
        if (sresult > 0)
        {
                for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
                {
-                       if ((FD_ISSET (a->second, &rfdset)) || (FD_ISSET (a->second, &wfdset)))
+                       if ((FD_ISSET (a->second, &rfdset)) || (FD_ISSET (a->second, &wfdset)) || FD_ISSET (a->second, &errfdset))
                        {
                                ev[result++] = ref[a->second];
                        }
@@ -134,16 +131,34 @@ int SelectEngine::DispatchEvents()
         */
        for (int i = 0; i < result; i++)
        {
-               ServerInstance->Log(DEBUG,"Handle %s event on fd %d",writeable[ev[i]->GetFd()] || !ev[i]->Readable() ? "write" : "read", ev[i]->GetFd());
-               if (writeable[ev[i]->GetFd()])
-               {
-                       ev[i]->HandleEvent(EVENT_WRITE);
-                       writeable[ev[i]->GetFd()] = false;
-
-               }
-               else
+               if (ev[i])
                {
-                       ev[i]->HandleEvent(ev[i]->Readable() ? EVENT_READ : EVENT_WRITE);
+                       if (FD_ISSET (ev[i]->GetFd(), &errfdset))
+                       {
+                               if (ev[i])
+                               {
+                                       if (getsockopt(ev[i]->GetFd(), SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0)
+                                               errcode = errno;
+
+                                       ev[i]->HandleEvent(EVENT_ERROR, errcode);
+                               }
+                               continue;
+                       }
+                       if (ev[i])
+                       {
+                               if (writeable[ev[i]->GetFd()])
+                               {
+                                       if (ev[i])
+                                               ev[i]->HandleEvent(EVENT_WRITE);
+                                       writeable[ev[i]->GetFd()] = false;
+               
+                               }
+                               else
+                               {
+                                       if (ev[i])
+                                               ev[i]->HandleEvent(ev[i]->Readable() ? EVENT_READ : EVENT_WRITE);
+                               }
+                       }
                }
        }