X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fsocketengine.cpp;h=ec34bf74e6c1c1fa81fc0daf98a2678bc22fbc86;hb=59b1a8955142935b02af6446005ab47fc7c3fc8c;hp=16f303b3dc90f88bdbddaca47e2a19e2e3652ef1;hpb=30484cf401cb508e6707fba6f016df97699396b6;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/socketengine.cpp b/src/socketengine.cpp index 16f303b3d..ec34bf74e 100644 --- a/src/socketengine.cpp +++ b/src/socketengine.cpp @@ -1,51 +1,93 @@ +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev. + * E-mail: + * + * + * + * Written by Craig Edwards, Craig McLure, and others. + * This program is free but copyrighted software; see + * the file COPYING for details. + * + * --------------------------------------------------- + */ + #include "inspircd_config.h" #include "globals.h" #include "inspircd.h" #ifdef USE_EPOLL #include -#define EP_DELAY 5 #endif #ifdef USE_KQUEUE #include #include #include #endif +#include +#include +#include "socketengine.h" -char ref[65535]; - -const char X_LISTEN = 0; -const char X_ESTAB_CLIENT = 1; -const char X_ESTAB_MODULE = 2; -const char X_ESTAB_DNS = 3; - -const char X_READBIT = 0x80; +char ref[MAX_DESCRIPTORS]; SocketEngine::SocketEngine() { + log(DEBUG,"SocketEngine::SocketEngine()"); #ifdef USE_EPOLL - EngineHandle = epoll_create(65535); + EngineHandle = epoll_create(MAX_DESCRIPTORS); #endif #ifdef USE_KQUEUE EngineHandle = kqueue(); #endif + CurrentSetSize = 0; } SocketEngine::~SocketEngine() { -#ifdef USE_EPOLL || USE_KQUEUE + log(DEBUG,"SocketEngine::~SocketEngine()"); +#ifdef USE_EPOLL + close(EngineHandle); +#endif +#ifdef USE_KQUEUE close(EngineHandle); #endif } +char SocketEngine::GetType(int fd) +{ + if ((fd < 0) || (fd > MAX_DESCRIPTORS)) + return X_EMPTY_SLOT; + /* Mask off the top bit used for 'read/write' state */ + return (ref[fd] & ~0x80); +} + bool SocketEngine::AddFd(int fd, bool readable, char type) { - this->fds.push_back(fd); + if ((fd < 0) || (fd > MAX_DESCRIPTORS)) + { + log(DEFAULT,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS); + return false; + } + if (GetRemainingFds() <= 1) + { + log(DEFAULT,"ERROR: System out of file descriptors!"); + return false; + } +#ifdef USE_SELECT + fds[fd] = fd; +#endif + ref[fd] = type; if (readable) + { + log(DEBUG,"Set readbit"); ref[fd] |= X_READBIT; + } + log(DEBUG,"Add socket %d",fd); #ifdef USE_EPOLL struct epoll_event ev; - log(DEBUG,"epoll: Adduser to events, ep=%d socket=%d",EngineHandle,fd); - readable ? ev.events = EPOLLIN | EPOLLET : ev.events = EPOLLOUT | EPOLLET; + log(DEBUG,"epoll: Add socket to events, ep=%d socket=%d",EngineHandle,fd); + readable ? ev.events = EPOLLIN : ev.events = EPOLLOUT; ev.data.fd = fd; int i = epoll_ctl(EngineHandle, EPOLL_CTL_ADD, fd, &ev); if (i < 0) @@ -56,8 +98,8 @@ bool SocketEngine::AddFd(int fd, bool readable, char type) #endif #ifdef USE_KQUEUE struct kevent ke; - log(DEBUG,"kqueue: Add user to events, kq=%d socket=%d",EngineHandle,fd); - EV_SET(&ke, socket, readable ? EVFILT_READ : EVFILT_WRITE, EV_ADD, 0, 0, NULL); + log(DEBUG,"kqueue: Add socket to events, kq=%d socket=%d",EngineHandle,fd); + EV_SET(&ke, fd, readable ? EVFILT_READ : EVFILT_WRITE, EV_ADD, 0, 0, NULL); int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL); if (i == -1) { @@ -65,27 +107,38 @@ bool SocketEngine::AddFd(int fd, bool readable, char type) return false; } #endif -return true; + CurrentSetSize++; + return true; } bool SocketEngine::DelFd(int fd) { - std::vector::iterator i = this->fds.find(fd); - if (i != this->fds.end()) - this->fds.erase(i); + log(DEBUG,"SocketEngine::DelFd(%d)",fd); + + if ((fd < 0) || (fd > MAX_DESCRIPTORS)) + return false; + +#ifdef USE_SELECT + std::map::iterator t = fds.find(fd); + if (t != fds.end()) + { + fds.erase(t); + log(DEBUG,"Deleted fd %d",fd); + } +#endif #ifdef USE_KQUEUE struct kevent ke; - EV_SET(&ke, fd, ref[fd] && X_READBIT ? EVFILT_READ : EVFILT_WRITE, EV_DELETE, 0, 0, NULL); + EV_SET(&ke, fd, ref[fd] & X_READBIT ? EVFILT_READ : EVFILT_WRITE, EV_DELETE, 0, 0, NULL); int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL); if (i == -1) { - log(DEBUG,"kqueue: Failed to remove user from queue!"); + log(DEBUG,"kqueue: Failed to remove socket from queue!"); return false; } #endif #ifdef USE_EPOLL struct epoll_event ev; - ref[fd] && X_READBIT ? ev.events = EPOLLIN | EPOLLET : ev.events = EPOLLOUT | EPOLLET; + ref[fd] && X_READBIT ? ev.events = EPOLLIN : ev.events = EPOLLOUT; ev.data.fd = fd; int i = epoll_ctl(EngineHandle, EPOLL_CTL_DEL, fd, &ev); if (i < 0) @@ -94,54 +147,82 @@ bool SocketEngine::DelFd(int fd) return false; } #endif + CurrentSetSize--; ref[fd] = 0; - return (i != this->fds.end()); + return true; +} + +int SocketEngine::GetMaxFds() +{ +#ifdef USE_SELECT + return FD_SETSIZE; +#endif +#ifdef USE_KQUEUE + return MAX_DESCRIPTORS; +#endif +#ifdef USE_EPOLL + return MAX_DESCRIPTORS; +#endif +} + +int SocketEngine::GetRemainingFds() +{ +#ifdef USE_SELECT + return FD_SETSIZE - CurrentSetSize; +#endif +#ifdef USE_KQUEUE + return MAX_DESCRIPTORS - CurrentSetSize; +#endif +#ifdef USE_EPOLL + return MAX_DESCRIPTORS - CurrentSetSize; +#endif } -bool SocketEngine::Wait(unsigned long millisecs, std::vector &fdlist) +int SocketEngine::Wait(int* fdlist) { - fdlist.clear(); + int result = 0; #ifdef USE_SELECT - int highest_fd = 0; + FD_ZERO(&wfdset); + FD_ZERO(&rfdset); timeval tval; int sresult; - for (unsigned int a = 0; a < fds.size(); a++) + for (std::map::iterator a = fds.begin(); a != fds.end(); a++) { - if (ref[fds[a]] && X_READBIT) + if (ref[a->second] & X_READBIT) { - FD_SET (fds[a], &rfdset); + FD_SET (a->second, &rfdset); } else { - FD_SET (fds[a], &wfdset); + FD_SET (a->second, &wfdset); } } tval.tv_sec = 0; - tval.tv_usec = 1000L; + tval.tv_usec = 100L; sresult = select(FD_SETSIZE, &rfdset, &wfdset, NULL, &tval); if (sresult > 0) { - for (unsigned int a = 0; a < fds.size(); a++) + for (std::map::iterator a = fds.begin(); a != fds.end(); a++) { - if ((FD_ISSET (fds[a], &rfdset)) || (FD_ISSET (fds[a], &wfdset))) - fdlist.push_back(fds[a]); + if ((FD_ISSET (a->second, &rfdset)) || (FD_ISSET (a->second, &wfdset))) + fdlist[result++] = a->second; } } #endif #ifdef USE_KQUEUE - ts.tv_nsec = 1000L; + ts.tv_nsec = 10000L; ts.tv_sec = 0; - int i = kevent(EngineHandle, NULL, 0, &ke_list, 65535, &ts); + int i = kevent(EngineHandle, NULL, 0, &ke_list[0], MAX_DESCRIPTORS, &ts); for (int j = 0; j < i; j++) - fdlist.push_back(ke_list[j].ident); + fdlist[result++] = ke_list[j].ident; #endif #ifdef USE_EPOLL - int i = epoll_wait(EngineHandle, events, 65535, 1); + int i = epoll_wait(EngineHandle, events, MAX_DESCRIPTORS, 100); for (int j = 0; j < i; j++) - fdlist.push_back(event[0].data.fd); + fdlist[result++] = events[j].data.fd; #endif - return true; + return result; } std::string SocketEngine::GetName()