X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fsocketengines%2Fsocketengine_kqueue.cpp;h=b23cfbd9dcf2b6af86a415bac1c9132d7c4ac92a;hb=33180223e318c304892b3fa8640f90f1ddf6f4b4;hp=96def4cca81b828be325fd30512bba2d37c7ddf7;hpb=080bb7c0b3d4cc3fcd06d04621ae4780ebbf3f2c;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/socketengines/socketengine_kqueue.cpp b/src/socketengines/socketengine_kqueue.cpp index 96def4cca..b23cfbd9d 100644 --- a/src/socketengines/socketengine_kqueue.cpp +++ b/src/socketengines/socketengine_kqueue.cpp @@ -20,12 +20,10 @@ #include "inspircd.h" -#include "exitcodes.h" + #include #include #include -#include "socketengine.h" -#include #include /** A specialisation of the SocketEngine class, designed to use BSD kqueue(). @@ -47,25 +45,7 @@ namespace */ void SocketEngine::Init() { - MAX_DESCRIPTORS = 0; - int mib[2]; - size_t len; - - mib[0] = CTL_KERN; -#ifdef KERN_MAXFILESPERPROC - mib[1] = KERN_MAXFILESPERPROC; -#else - mib[1] = KERN_MAXFILES; -#endif - len = sizeof(MAX_DESCRIPTORS); - sysctl(mib, 2, &MAX_DESCRIPTORS, &len, NULL, 0); - if (MAX_DESCRIPTORS <= 0) - { - ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Can't determine maximum number of open sockets!"); - std::cout << "ERROR: Can't determine maximum number of open sockets!" << std::endl; - ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE); - } - + LookupMaxFds(); RecoverFromFork(); } @@ -78,13 +58,7 @@ void SocketEngine::RecoverFromFork() */ EngineHandle = kqueue(); if (EngineHandle == -1) - { - ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features."); - ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR: this is a fatal error, exiting now."); - std::cout << "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features." << std::endl; - std::cout << "ERROR: this is a fatal error, exiting now." << std::endl; - ServerInstance->QuickExit(EXIT_STATUS_SOCKETENGINE); - } + InitError(); } /** Shutdown the kqueue engine @@ -105,7 +79,7 @@ bool SocketEngine::AddFd(EventHandler* eh, int event_mask) { int fd = eh->GetFd(); - if ((fd < 0) || (fd > GetMaxFds() - 1)) + if (fd < 0) return false; if (!SocketEngine::AddFdRef(eh)) @@ -113,7 +87,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(eh)); ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "New file descriptor: %d", fd); @@ -128,7 +102,7 @@ void SocketEngine::DelFd(EventHandler* eh) { int fd = eh->GetFd(); - if ((fd < 0) || (fd > GetMaxFds() - 1)) + if (fd < 0) { ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "DelFd() on invalid fd: %d", fd); return; @@ -154,7 +128,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(eh)); } else if ((old_mask & FD_WANT_POLL_WRITE) && !(new_mask & FD_WANT_POLL_WRITE)) { @@ -165,7 +139,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(eh)); } } @@ -187,37 +161,36 @@ int SocketEngine::DispatchEvents() for (int j = 0; j < i; j++) { struct kevent& kev = ke_list[j]; + EventHandler* eh = static_cast(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) { stats.ErrorEvents++; - eh->HandleEvent(EVENT_ERROR, kev.fflags); + eh->OnEventHandlerError(kev.fflags); continue; } if (filter == EVFILT_WRITE) { - stats.WriteEvents++; /* When mask is FD_WANT_FAST_WRITE or FD_WANT_SINGLE_WRITE, * we set a one-shot write, so we need to clear that bit * to detect when it set again. */ const int bits_to_clr = FD_WANT_SINGLE_WRITE | FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK; eh->SetEventMask(eh->GetEventMask() & ~bits_to_clr); - eh->HandleEvent(EVENT_WRITE); + eh->OnEventHandlerWrite(); } else if (filter == EVFILT_READ) { - stats.ReadEvents++; eh->SetEventMask(eh->GetEventMask() & ~FD_READ_WILL_BLOCK); - eh->HandleEvent(EVENT_READ); + eh->OnEventHandlerRead(); } }