#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
-#include "socketengine.h"
#include <iostream>
#include <sys/sysctl.h>
mib[1] = KERN_MAXFILES;
#endif
len = sizeof(MAX_DESCRIPTORS);
+ // MAX_DESCRIPTORS is mainly used for display purposes, no problem if the sysctl() below fails
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);
- }
RecoverFromFork();
}
{
int fd = eh->GetFd();
- if ((fd < 0) || (fd > GetMaxFds() - 1))
+ if (fd < 0)
return false;
if (!SocketEngine::AddFdRef(eh))
// 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);
{
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;
{
// 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))
{
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));
}
}
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)
{
stats.ErrorEvents++;
- eh->HandleEvent(EVENT_ERROR, kev.fflags);
+ eh->OnEventHandlerError(kev.fflags);
continue;
}
if (filter == EVFILT_WRITE)
*/
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();
}
}