]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/socketengines/socketengine_poll.cpp
Replace SocketEngine::GetName() with INSPIRCD_SOCKETENGINE_NAME define
[user/henk/code/inspircd.git] / src / socketengines / socketengine_poll.cpp
index 0112b7d2fb5c431e7a512ba585f02653021b7bc6..53953626f9dc5bac798ac546a470945069d5eb63 100644 (file)
@@ -21,9 +21,6 @@
  */
 
 
-#ifndef SOCKETENGINE_POLL
-#define SOCKETENGINE_POLL
-
 #include <iostream>
 #include <vector>
 #include <string>
@@ -44,8 +41,6 @@
 # define poll WSAPoll
 #endif
 
-class InspIRCd;
-
 /** A specialisation of the SocketEngine class, designed to use poll().
  */
 class PollEngine : public SocketEngine
@@ -65,11 +60,8 @@ public:
        virtual void OnSetEvent(EventHandler* eh, int old_mask, int new_mask);
        virtual void DelFd(EventHandler* eh);
        virtual int DispatchEvents();
-       virtual std::string GetName();
 };
 
-#endif
-
 PollEngine::PollEngine() : events(1), fd_mappings(1)
 {
        CurrentSetSize = 0;
@@ -203,43 +195,49 @@ int PollEngine::DispatchEvents()
        {
                struct pollfd& pfd = events[index];
 
-               if (pfd.revents)
+               // Copy these in case the vector gets resized and pfd invalidated
+               const int fd = pfd.fd;
+               const short revents = pfd.revents;
+
+               if (revents)
                        processed++;
 
-               EventHandler* eh = GetRef(pfd.fd);
+               EventHandler* eh = GetRef(fd);
                if (!eh)
                        continue;
 
-               if (pfd.revents & POLLHUP)
+               if (revents & POLLHUP)
                {
                        eh->HandleEvent(EVENT_ERROR, 0);
                        continue;
                }
 
-               if (pfd.revents & POLLERR)
+               if (revents & POLLERR)
                {
                        // Get error number
-                       if (getsockopt(pfd.fd, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0)
+                       if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0)
                                errcode = errno;
                        eh->HandleEvent(EVENT_ERROR, errcode);
                        continue;
                }
 
-               if (pfd.revents & POLLIN)
+               if (revents & POLLIN)
                {
                        SetEventMask(eh, eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
                        eh->HandleEvent(EVENT_READ);
-                       if (eh != GetRef(pfd.fd))
+                       if (eh != GetRef(fd))
                                // whoops, deleted out from under us
                                continue;
                }
 
-               if (pfd.revents & POLLOUT)
+               if (revents & POLLOUT)
                {
                        int mask = eh->GetEventMask();
                        mask &= ~(FD_WRITE_WILL_BLOCK | FD_WANT_SINGLE_WRITE);
                        SetEventMask(eh, mask);
-                       pfd.events = mask_to_poll(mask);
+
+                       // The vector could've been resized, reference can be invalid by now; don't use it
+                       events[index].events = mask_to_poll(mask);
                        eh->HandleEvent(EVENT_WRITE);
                }
        }
@@ -247,11 +245,6 @@ int PollEngine::DispatchEvents()
        return i;
 }
 
-std::string PollEngine::GetName()
-{
-       return "poll";
-}
-
 SocketEngine* CreateSocketEngine()
 {
        return new PollEngine;