diff options
author | Attila Molnar <attilamolnar@hush.com> | 2014-02-09 17:31:57 +0100 |
---|---|---|
committer | Attila Molnar <attilamolnar@hush.com> | 2014-02-09 17:31:57 +0100 |
commit | 034488fc4aca4fb85c923d881f4b95675eba37b6 (patch) | |
tree | 8a6eb0328b49258f667ed346406da949db8bbdb2 /src/socketengines | |
parent | 4ec65c6231df9fcb38210f9b885cdf73b72cc176 (diff) |
Store the EventHandler* in the kevent/epoll_event/portev struct
This removes the need to call GetRef() for every socket in DispatchEvents()
Diffstat (limited to 'src/socketengines')
-rw-r--r-- | src/socketengines/socketengine_epoll.cpp | 17 | ||||
-rw-r--r-- | src/socketengines/socketengine_kqueue.cpp | 15 | ||||
-rw-r--r-- | src/socketengines/socketengine_ports.cpp | 4 |
3 files changed, 17 insertions, 19 deletions
diff --git a/src/socketengines/socketengine_epoll.cpp b/src/socketengines/socketengine_epoll.cpp index 961dc8b49..26dfc8f2f 100644 --- a/src/socketengines/socketengine_epoll.cpp +++ b/src/socketengines/socketengine_epoll.cpp @@ -117,7 +117,7 @@ bool SocketEngine::AddFd(EventHandler* eh, int event_mask) struct epoll_event ev; memset(&ev, 0, sizeof(ev)); ev.events = mask_to_epoll(event_mask); - ev.data.fd = fd; + ev.data.ptr = static_cast<void*>(eh); int i = epoll_ctl(EngineHandle, EPOLL_CTL_ADD, fd, &ev); if (i < 0) { @@ -143,7 +143,7 @@ void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask) struct epoll_event ev; memset(&ev, 0, sizeof(ev)); ev.events = new_events; - ev.data.fd = eh->GetFd(); + ev.data.ptr = static_cast<void*>(eh); epoll_ctl(EngineHandle, EPOLL_CTL_MOD, eh->GetFd(), &ev); } } @@ -185,13 +185,10 @@ int SocketEngine::DispatchEvents() // Copy these in case the vector gets resized and ev invalidated const epoll_event ev = events[j]; - EventHandler* eh = GetRef(ev.data.fd); - if (!eh) - { - ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "Got event on unknown fd: %d", events[j].data.fd); - epoll_ctl(EngineHandle, EPOLL_CTL_DEL, events[j].data.fd, &events[j]); + EventHandler* const eh = static_cast<EventHandler*>(ev.data.ptr); + const int fd = eh->GetFd(); + if (fd < 0) continue; - } if (ev.events & EPOLLHUP) { @@ -206,7 +203,7 @@ int SocketEngine::DispatchEvents() /* Get error number */ socklen_t codesize = sizeof(int); int errcode; - if (getsockopt(ev.data.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; @@ -230,7 +227,7 @@ int SocketEngine::DispatchEvents() { stats.ReadEvents++; eh->HandleEvent(EVENT_READ); - if (eh != GetRef(ev.data.fd)) + if (eh != GetRef(fd)) // whoa! we got deleted, better not give out the write event continue; } diff --git a/src/socketengines/socketengine_kqueue.cpp b/src/socketengines/socketengine_kqueue.cpp index 96def4cca..b7797a82f 100644 --- a/src/socketengines/socketengine_kqueue.cpp +++ b/src/socketengines/socketengine_kqueue.cpp @@ -113,7 +113,7 @@ bool SocketEngine::AddFd(EventHandler* eh, int event_mask) // We always want to read from the socket... struct kevent* ke = GetChangeKE(); - EV_SET(ke, fd, EVFILT_READ, EV_ADD, 0, 0, NULL); + EV_SET(ke, fd, EVFILT_READ, EV_ADD, 0, 0, static_cast<void*>(eh)); ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd); @@ -154,7 +154,7 @@ void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask) { // new poll-style write struct kevent* ke = GetChangeKE(); - EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_ADD, 0, 0, NULL); + EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_ADD, 0, 0, static_cast<void*>(eh)); } else if ((old_mask & FD_WANT_POLL_WRITE) && !(new_mask & FD_WANT_POLL_WRITE)) { @@ -165,7 +165,7 @@ void SocketEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask) if ((new_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)) && !(old_mask & (FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))) { struct kevent* ke = GetChangeKE(); - EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL); + EV_SET(ke, eh->GetFd(), EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, static_cast<void*>(eh)); } } @@ -187,13 +187,14 @@ int SocketEngine::DispatchEvents() for (int j = 0; j < i; j++) { struct kevent& kev = ke_list[j]; + EventHandler* eh = static_cast<EventHandler*>(kev.udata); + if (!eh) + continue; // Copy these in case the vector gets resized and kev invalidated - const int fd = kev.ident; + const int fd = eh->GetFd(); const short filter = kev.filter; - - EventHandler* eh = GetRef(fd); - if (!eh) + if (fd < 0) continue; if (kev.flags & EV_EOF) diff --git a/src/socketengines/socketengine_ports.cpp b/src/socketengines/socketengine_ports.cpp index d7425813e..c30400107 100644 --- a/src/socketengines/socketengine_ports.cpp +++ b/src/socketengines/socketengine_ports.cpp @@ -157,8 +157,8 @@ int SocketEngine::DispatchEvents() // Copy these in case the vector gets resized and ev invalidated const int fd = ev.portev_object; const int portev_events = ev.portev_events; - EventHandler* eh = GetRef(fd); - if (!eh) + EventHandler* eh = static_cast<EventHandler*>(ev.portev_user); + if (eh->GetFd() < 0) continue; int mask = eh->GetEventMask(); |