diff options
-rwxr-xr-x | configure | 43 | ||||
-rw-r--r-- | include/aes.h | 1 | ||||
-rw-r--r-- | include/inspsocket.h | 2 | ||||
-rw-r--r-- | include/socketengine.h | 38 | ||||
-rw-r--r-- | include/socketengine_epoll.h | 51 | ||||
-rw-r--r-- | include/socketengine_kqueue.h | 53 | ||||
-rw-r--r-- | include/socketengine_select.h | 50 | ||||
-rw-r--r-- | src/dns.cpp | 1 | ||||
-rw-r--r-- | src/inspircd.cpp | 5 | ||||
-rw-r--r-- | src/socketengine.cpp | 186 | ||||
-rw-r--r-- | src/socketengine_epoll.cpp | 129 | ||||
-rw-r--r-- | src/socketengine_kqueue.cpp | 130 | ||||
-rw-r--r-- | src/socketengine_select.cpp | 131 |
13 files changed, 605 insertions, 215 deletions
@@ -1022,21 +1022,37 @@ EOF my $use_hiperf = 0; if (($has_kqueue) && ($config{USE_KQUEUE} eq "y")) { print FILEHANDLE "#define USE_KQUEUE\n"; + $se = "socketengine_kqueue"; $use_hiperf = 1; } if (($has_epoll) && ($config{USE_EPOLL} eq "y")) { print FILEHANDLE "#define USE_EPOLL\n"; + $se = "socketengine_epoll"; $use_hiperf = 1; } # user didn't choose either epoll or select for their OS. # default them to USE_SELECT (ewwy puke puke) if (!$use_hiperf) { print FILEHANDLE "#define USE_SELECT\n"; + $se = "socketengine_select"; } print FILEHANDLE "\n#endif\n"; close(FILEHANDLE); } +open(FILEHANDLE, ">include/inspircd_se_config.h"); + print FILEHANDLE <<EOF; +/* Auto generated by configure, do not modify or commit to svn! */ +#ifndef __CONFIGURATION_SOCKETENGINE__ +#define __CONFIGURATION_SOCKETENGINE__ + +#include "$se.h" + +#endif +EOF +close(FILEHANDLE); + + # Create a Modules List.. my $modules = ""; foreach $i (@modlist) @@ -1351,6 +1367,15 @@ CPPFILES = \$(shell /bin/ls -l modes/ | grep '\\.cpp' | sed 's/^.* //' | grep -v RELCPPFILES = \$(shell /bin/ls -l modes/ | grep '\\.cpp' | sed 's/^.* /modes\\//' | grep -v svn) EOM + +$se = "socketengine_select"; +if (($has_kqueue) && ($config{USE_KQUEUE} eq "y")) { + $se = "socketengine_kqueue"; +} +elsif (($has_epoll) && ($config{USE_EPOLL} eq "y")) { + $se = "socketengine_epoll"; +} + ### # This next section is for cygwin dynamic module builds. # Basically, what we do, is build the inspircd core as a library @@ -1393,8 +1418,8 @@ command_parse.o: command_parse.cpp ../include/base.h ../include/hashcomp.h ../in userprocess.o: userprocess.cpp ../include/base.h ../include/hashcomp.h ../include/globals.h ../include/inspircd_config.h \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c userprocess.cpp -socketengine.o: socketengine.cpp ../include/base.h ../include/hashcomp.h ../include/globals.h ../include/inspircd_config.h - \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c socketengine.cpp +socketengine.o: $se.cpp socketengine.cpp ../include/base.h ../include/hashcomp.h ../include/globals.h ../include/inspircd_config.h ../include/$se.h + \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c socketengine.cpp $se.cpp hashcomp.o: hashcomp.cpp ../include/base.h ../include/hashcomp.h ../include/inspircd.h ../include/users.h ../include/globals.h ../include/inspircd_config.h \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c hashcomp.cpp @@ -1492,6 +1517,14 @@ if (($config{OSNAME} !~ /Linux/i) && ($config{HAS_EXECINFO} eq "1")) { $extra = ""; } +$se = "socketengine_select"; +if (($has_kqueue) && ($config{USE_KQUEUE} eq "y")) { + $se = "socketengine_kqueue"; +} +elsif (($has_epoll) && ($config{USE_EPOLL} eq "y")) { + $se = "socketengine_epoll"; +} + open(FH,">src/Makefile") or die("Could not write src/Makefile"); print FH <<EOM; # Insp Makefile :p @@ -1512,9 +1545,9 @@ all: libIRCDtimer.so libIRCDaes.so libIRCDcull_list.so libIRCDuserprocess.so lib inspircd: inspircd.cpp ../include/base.h ../include/channels.h ../include/inspircd.h ../include/channels.h ../include/globals.h ../include/inspircd_config.h ../include/socket.h libIRCDtimer.so libIRCDaes.so libIRCDcull_list.so libIRCDuserprocess.so libIRCDsocketengine.so libIRCDsocket.so libIRCDhash.so libIRCDchannels.so libIRCDmode.so libIRCDxline.so libIRCDstring.so libIRCDasyncdns.so libIRCDbase.so libIRCDconfigreader.so libIRCDinspsocket.so libIRCDmessage.so $cmdobjs libIRCDsnomasks.so libIRCDcommands.so libIRCDdnsqueue.so libIRCDdynamic.so libIRCDusers.so libIRCDmodules.so libIRCDwildcard.so libIRCDhelper.so libIRCDcommand_parse.so \$(CC) -I../include $extra -Wl,--rpath -Wl,/usr/local/lib -Wl,--rpath -Wl,$config{LIBRARY_DIR} \$(FLAGS) -rdynamic -L. inspircd.cpp -o inspircd \$(LDLIBS) libIRCDchannels.so libIRCDmode.so libIRCDxline.so libIRCDstring.so libIRCDasyncdns.so libIRCDbase.so libIRCDconfigreader.so libIRCDinspsocket.so libIRCDmessage.so libIRCDcommands.so libIRCDdnsqueue.so libIRCDdynamic.so libIRCDusers.so libIRCDmodules.so libIRCDwildcard.so libIRCDhelper.so libIRCDhash.so libIRCDsocket.so libIRCDsocketengine.so libIRCDuserprocess.so libIRCDcull_list.so libIRCDcommand_parse.so libIRCDaes.so libIRCDtimer.so libIRCDsnomasks.so -libIRCDsocketengine.so: socketengine.cpp ../include/base.h ../include/hashcomp.h ../include/globals.h ../include/inspircd_config.h - \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c socketengine.cpp - \$(CC) -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDsocketengine.so socketengine.o +libIRCDsocketengine.so: $se.cpp socketengine.cpp ../include/base.h ../include/hashcomp.h ../include/globals.h ../include/inspircd_config.h ../include/$se.h + \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c socketengine.cpp $se.cpp + \$(CC) -Wl,--rpath -Wl,$config{LIBRARY_DIR} -shared -o libIRCDsocketengine.so socketengine.o $se.o libIRCDsnomasks.so: snomasks.cpp ../include/base.h ../include/hashcomp.h ../include/inspircd.h ../include/users.h ../include/globals.h ../include/inspircd_config.h ../include/channels.h \$(CC) -pipe -I../include \$(FLAGS) -export-dynamic -c snomasks.cpp diff --git a/include/aes.h b/include/aes.h index 53e4bd181..b14338e7a 100644 --- a/include/aes.h +++ b/include/aes.h @@ -2,6 +2,7 @@ #define __AES_H__
#include <cstring>
+#include "inspircd_config.h"
#include "base.h"
using namespace std;
diff --git a/include/inspsocket.h b/include/inspsocket.h index 56c04373d..4bd76788c 100644 --- a/include/inspsocket.h +++ b/include/inspsocket.h @@ -20,8 +20,8 @@ #include <sstream> #include <string> #include <deque> -#include "inspircd_config.h" #include "dns.h" +#include "inspircd_config.h" #include "socket.h" /** diff --git a/include/socketengine.h b/include/socketengine.h index e07642fa1..734aa1938 100644 --- a/include/socketengine.h +++ b/include/socketengine.h @@ -23,15 +23,6 @@ #include "inspircd_config.h" #include "globals.h" #include "inspircd.h" -#ifdef USE_EPOLL -#include <sys/epoll.h> -#define EP_DELAY 5 -#endif -#ifdef USE_KQUEUE -#include <sys/types.h> -#include <sys/event.h> -#include <sys/time.h> -#endif /** * Each of these values represents a socket @@ -67,21 +58,10 @@ const char X_READBIT = 0x80; */ class SocketEngine : public Extensible { - +protected: int EngineHandle; /* Handle to the socket engine if needed */ int CurrentSetSize; /* Current number of descriptors in the engine */ -#ifdef USE_SELECT - std::map<int,int> fds; /* List of file descriptors being monitored */ - fd_set wfdset, rfdset; /* Readable and writeable sets for select() */ -#endif -#ifdef USE_KQUEUE - struct kevent ke_list[MAX_DESCRIPTORS]; /* Up to 64k sockets for kqueue */ - struct timespec ts; /* kqueue delay value */ -#endif -#ifdef USE_EPOLL - struct epoll_event events[MAX_DESCRIPTORS]; /* Up to 64k sockets for epoll */ -#endif - + char ref[MAX_DESCRIPTORS]; /* Reference table */ public: /** Constructor @@ -98,7 +78,7 @@ public: * The destructor transparently tidies up * any resources used by the socket engine. */ - ~SocketEngine(); + virtual ~SocketEngine(); /** Add a file descriptor to the engine * Use AddFd to add a file descriptor to the @@ -109,7 +89,7 @@ public: * or write events (there is currently no * need for support of both). */ - bool AddFd(int fd, bool readable, char type); + virtual bool AddFd(int fd, bool readable, char type); /** Returns the type value for this file descriptor * This function masks off the X_READBIT value @@ -126,19 +106,19 @@ public: /** Returns the maximum number of file descriptors * you may store in the socket engine at any one time. */ - int GetMaxFds(); + virtual int GetMaxFds(); /** Returns the number of file descriptor slots * which are available for storing fds. */ - int GetRemainingFds(); + virtual int GetRemainingFds(); /** Delete a file descriptor from the engine * This function call deletes a file descriptor * from the engine, returning true if it succeeded * and false if it failed. */ - bool DelFd(int fd); + virtual bool DelFd(int fd); /** Returns true if a socket exists in the socket * engine's list. @@ -151,13 +131,13 @@ public: * of active file descriptors in the vector * fdlist which the core may then act upon. */ - int Wait(int* fdlist); + virtual int Wait(int* fdlist); /** Returns the socket engines name * This returns the name of the engine for use * in /VERSION responses. */ - std::string GetName(); + virtual std::string GetName(); }; #endif diff --git a/include/socketengine_epoll.h b/include/socketengine_epoll.h new file mode 100644 index 000000000..ddbeb6f40 --- /dev/null +++ b/include/socketengine_epoll.h @@ -0,0 +1,51 @@ +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev. + * E-mail: + * <brain@chatspike.net> + * <Craig@chatspike.net> + * + * Written by Craig Edwards, Craig McLure, and others. + * This program is free but copyrighted software; see + * the file COPYING for details. + * + * --------------------------------------------------- +*/ + +#ifndef __SOCKETENGINE_EPOLL__ +#define __SOCKETENGINE_EPOLL__ + +#include <vector> +#include <string> +#include <map> +#include "inspircd_config.h" +#include "globals.h" +#include "inspircd.h" +#include "socketengine.h" +#include <sys/epoll.h> +#define EP_DELAY 5 + +class EPollEngine : public SocketEngine +{ +private: + struct epoll_event events[MAX_DESCRIPTORS]; /* Up to 64k sockets for epoll */ +public: + EPollEngine(); + virtual ~EPollEngine(); + virtual bool AddFd(int fd, bool readable, char type); + virtual int GetMaxFds(); + virtual int GetRemainingFds(); + virtual bool DelFd(int fd); + virtual int Wait(int* fdlist); + virtual std::string GetName(); +}; + +class SocketEngineFactory +{ +public: + SocketEngine* Create() { return new EPollEngine(); } +}; + +#endif diff --git a/include/socketengine_kqueue.h b/include/socketengine_kqueue.h new file mode 100644 index 000000000..6de6a0552 --- /dev/null +++ b/include/socketengine_kqueue.h @@ -0,0 +1,53 @@ +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev. + * E-mail: + * <brain@chatspike.net> + * <Craig@chatspike.net> + * + * Written by Craig Edwards, Craig McLure, and others. + * This program is free but copyrighted software; see + * the file COPYING for details. + * + * --------------------------------------------------- +*/ + +#ifndef __SOCKETENGINE_KQUEUE__ +#define __SOCKETENGINE_KQUEUE__ + +#include <vector> +#include <string> +#include <map> +#include "inspircd_config.h" +#include "globals.h" +#include "inspircd.h" +#include <sys/types.h> +#include <sys/event.h> +#include <sys/time.h> +#include "socketengine.h" + +class KQueueEngine : public SocketEngine +{ +private: + struct kevent ke_list[MAX_DESCRIPTORS]; /* Up to 64k sockets for kqueue */ + struct timespec ts; /* kqueue delay value */ +public: + KQueueEngineEngine(); + virtual ~KQueueEngine(); + virtual bool AddFd(int fd, bool readable, char type); + virtual int GetMaxFds(); + virtual int GetRemainingFds(); + virtual bool DelFd(int fd); + virtual int Wait(int* fdlist); + virtual std::string GetName(); +}; + +class SocketEngineFactory +{ + public: + SocketEngine* Create() { return new KQueueEngine(); } +}; + +#endif diff --git a/include/socketengine_select.h b/include/socketengine_select.h new file mode 100644 index 000000000..bbd9b78dd --- /dev/null +++ b/include/socketengine_select.h @@ -0,0 +1,50 @@ +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev. + * E-mail: + * <brain@chatspike.net> + * <Craig@chatspike.net> + * + * Written by Craig Edwards, Craig McLure, and others. + * This program is free but copyrighted software; see + * the file COPYING for details. + * + * --------------------------------------------------- +*/ + +#ifndef __SOCKETENGINE_SELECT__ +#define __SOCKETENGINE_SELECT__ + +#include <vector> +#include <string> +#include <map> +#include "inspircd_config.h" +#include "globals.h" +#include "inspircd.h" +#include "socketengine.h" + +class SelectEngine : public SocketEngine +{ +private: + std::map<int,int> fds; /* List of file descriptors being monitored */ + fd_set wfdset, rfdset; /* Readable and writeable sets for select() */ +public: + SelectEngine(); + virtual ~SelectEngine(); + virtual bool AddFd(int fd, bool readable, char type); + virtual int GetMaxFds(); + virtual int GetRemainingFds(); + virtual bool DelFd(int fd); + virtual int Wait(int* fdlist); + virtual std::string GetName(); +}; + +class SocketEngineFactory +{ +public: + SocketEngine* Create() { return new SelectEngine(); } +}; + +#endif diff --git a/src/dns.cpp b/src/dns.cpp index 0347caaae..d67815b2c 100644 --- a/src/dns.cpp +++ b/src/dns.cpp @@ -49,6 +49,7 @@ using namespace std; #include "dns.h" #include "inspircd.h" #include "helperfuncs.h" +#include "inspircd_config.h" #include "socketengine.h" #include "configreader.h" diff --git a/src/inspircd.cpp b/src/inspircd.cpp index a6106a787..2f08750bd 100644 --- a/src/inspircd.cpp +++ b/src/inspircd.cpp @@ -63,6 +63,7 @@ #include "helperfuncs.h" #include "hashcomp.h" #include "socketengine.h" +#include "inspircd_se_config.h" #include "userprocess.h" #include "socket.h" #include "typedefs.h" @@ -323,7 +324,9 @@ InspIRCd::InspIRCd(int argc, char** argv) /* Because of limitations in kqueue on freebsd, we must fork BEFORE we * initialize the socket engine. */ - SE = new SocketEngine(); + SocketEngineFactory* SEF = new SocketEngineFactory(); + SE = SEF->Create(); + delete SEF; /* We must load the modules AFTER initializing the socket engine, now */ diff --git a/src/socketengine.cpp b/src/socketengine.cpp index 886e2b0d0..4c164f31e 100644 --- a/src/socketengine.cpp +++ b/src/socketengine.cpp @@ -30,40 +30,17 @@ #include "socketengine.h" #include "helperfuncs.h" -char ref[MAX_DESCRIPTORS]; - SocketEngine::SocketEngine() { - log(DEBUG,"SocketEngine::SocketEngine()"); -#ifdef USE_EPOLL - EngineHandle = epoll_create(MAX_DESCRIPTORS); -#endif -#ifdef USE_KQUEUE - EngineHandle = kqueue(); -#endif -#ifdef USE_SELECT - EngineHandle = 0; -#endif - if (EngineHandle == -1) - { - log(SPARSE,"ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features."); - log(SPARSE,"ERROR: this is a fatal error, exiting now."); - printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features."); - printf("ERROR: this is a fatal error, exiting now."); - Exit(0); - } - CurrentSetSize = 0; + printf("Something blew up in your face. class SocketEngine is an abstract class and configure must\n"); + printf("select a socket engine to derive from it such as EPollEngine, KQueueEngine or SelectEngine.\n"); + printf("Rerun configure, and try again.\n"); + exit(0); } SocketEngine::~SocketEngine() { log(DEBUG,"SocketEngine::~SocketEngine()"); -#ifdef USE_EPOLL - close(EngineHandle); -#endif -#ifdef USE_KQUEUE - close(EngineHandle); -#endif } char SocketEngine::GetType(int fd) @@ -76,51 +53,6 @@ char SocketEngine::GetType(int fd) bool SocketEngine::AddFd(int fd, bool readable, char type) { - 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; - memset(&ev,0,sizeof(struct epoll_event)); - 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) - { - log(DEBUG,"epoll: List insertion failure!"); - return false; - } -#endif -#ifdef USE_KQUEUE - struct kevent ke; - 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) - { - log(DEBUG,"kqueue: List insertion failure!"); - return false; - } -#endif - CurrentSetSize++; return true; } @@ -133,129 +65,25 @@ bool SocketEngine::HasFd(int fd) bool SocketEngine::DelFd(int fd) { - log(DEBUG,"SocketEngine::DelFd(%d)",fd); - - if ((fd < 0) || (fd > MAX_DESCRIPTORS)) - return false; - -#ifdef USE_SELECT - std::map<int,int>::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); - int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL); - if (i == -1) - { - log(DEBUG,"kqueue: Failed to remove socket from queue!"); - return false; - } -#endif -#ifdef USE_EPOLL - struct epoll_event ev; - memset(&ev,0,sizeof(struct epoll_event)); - 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) - { - log(DEBUG,"epoll: List deletion failure!"); - return false; - } -#endif - CurrentSetSize--; - ref[fd] = 0; 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 + return 0; } 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 + return 0; } int SocketEngine::Wait(int* fdlist) { - int result = 0; -#ifdef USE_SELECT - FD_ZERO(&wfdset); - FD_ZERO(&rfdset); - timeval tval; - int sresult; - for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++) - { - if (ref[a->second] & X_READBIT) - { - FD_SET (a->second, &rfdset); - } - else - { - FD_SET (a->second, &wfdset); - } - - } - tval.tv_sec = 0; - tval.tv_usec = 50L; - sresult = select(FD_SETSIZE, &rfdset, &wfdset, NULL, &tval); - if (sresult > 0) - { - for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++) - { - if ((FD_ISSET (a->second, &rfdset)) || (FD_ISSET (a->second, &wfdset))) - fdlist[result++] = a->second; - } - } -#endif -#ifdef USE_KQUEUE - ts.tv_nsec = 5000L; - ts.tv_sec = 0; - int i = kevent(EngineHandle, NULL, 0, &ke_list[0], MAX_DESCRIPTORS, &ts); - for (int j = 0; j < i; j++) - fdlist[result++] = ke_list[j].ident; -#endif -#ifdef USE_EPOLL - int i = epoll_wait(EngineHandle, events, MAX_DESCRIPTORS, 50); - for (int j = 0; j < i; j++) - fdlist[result++] = events[j].data.fd; -#endif - return result; + return 0; } std::string SocketEngine::GetName() { -#ifdef USE_SELECT - return "select"; -#endif -#ifdef USE_KQUEUE - return "kqueue"; -#endif -#ifdef USE_EPOLL - return "epoll"; -#endif return "misconfigured"; } diff --git a/src/socketengine_epoll.cpp b/src/socketengine_epoll.cpp new file mode 100644 index 000000000..d1a761d5b --- /dev/null +++ b/src/socketengine_epoll.cpp @@ -0,0 +1,129 @@ +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev. + * E-mail: + * <brain@chatspike.net> + * <Craig@chatspike.net> + * + * 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" +#include <sys/epoll.h> +#include <vector> +#include <string> +#include "socketengine_epoll.h" +#include "helperfuncs.h" + +EPollEngine::EPollEngine() +{ + EngineHandle = epoll_create(MAX_DESCRIPTORS); + + if (EngineHandle == -1) + { + log(SPARSE,"ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features."); + log(SPARSE,"ERROR: this is a fatal error, exiting now."); + printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features."); + printf("ERROR: this is a fatal error, exiting now."); + Exit(0); + } + CurrentSetSize = 0; +} + +EPollEngine::~EPollEngine() +{ + close(EngineHandle); +} + +bool EPollEngine::AddFd(int fd, bool readable, char type) +{ + 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; + } + ref[fd] = type; + if (readable) + { + log(DEBUG,"Set readbit"); + ref[fd] |= X_READBIT; + } + log(DEBUG,"Add socket %d",fd); + + struct epoll_event ev; + memset(&ev,0,sizeof(struct epoll_event)); + 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) + { + log(DEBUG,"epoll: List insertion failure!"); + return false; + } + + CurrentSetSize++; + return true; +} + +bool EPollEngine::DelFd(int fd) +{ + log(DEBUG,"EPollEngine::DelFd(%d)",fd); + + if ((fd < 0) || (fd > MAX_DESCRIPTORS)) + return false; + + struct epoll_event ev; + memset(&ev,0,sizeof(struct epoll_event)); + 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) + { + log(DEBUG,"epoll: List deletion failure!"); + return false; + } + + CurrentSetSize--; + ref[fd] = 0; + return true; +} + +int EPollEngine::GetMaxFds() +{ + return MAX_DESCRIPTORS; +} + +int EPollEngine::GetRemainingFds() +{ + return MAX_DESCRIPTORS - CurrentSetSize; +} + +int EPollEngine::Wait(int* fdlist) +{ + int result = 0; + + int i = epoll_wait(EngineHandle, events, MAX_DESCRIPTORS, 50); + for (int j = 0; j < i; j++) + fdlist[result++] = events[j].data.fd; + + return result; +} + +std::string EPollEngine::GetName() +{ + return "epoll"; +} diff --git a/src/socketengine_kqueue.cpp b/src/socketengine_kqueue.cpp new file mode 100644 index 000000000..388a84f29 --- /dev/null +++ b/src/socketengine_kqueue.cpp @@ -0,0 +1,130 @@ +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev. + * E-mail: + * <brain@chatspike.net> + * <Craig@chatspike.net> + * + * 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" +#include <sys/types.h> +#include <sys/event.h> +#include <sys/time.h> +#include <vector> +#include <string> +#include "socketengine_kqueue.h" +#include "helperfuncs.h" + +KQueueEngine::KQueueEngine() +{ + EngineHandle = kqueue(); + if (EngineHandle == -1) + { + log(SPARSE,"ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features."); + log(SPARSE,"ERROR: this is a fatal error, exiting now."); + printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features."); + printf("ERROR: this is a fatal error, exiting now."); + Exit(0); + } + CurrentSetSize = 0; +} + +KQueueEngine::~KQueueEngine() +{ + log(DEBUG,"KQueueEngine::~KQueueEngine()"); + close(EngineHandle); +} + +bool KQueueEngine::AddFd(int fd, bool readable, char type) +{ + 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; + } + + ref[fd] = type; + if (readable) + { + log(DEBUG,"Set readbit"); + ref[fd] |= X_READBIT; + } + log(DEBUG,"Add socket %d",fd); + + struct kevent ke; + 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) + { + log(DEBUG,"kqueue: List insertion failure!"); + return false; + } + + CurrentSetSize++; + return true; +} + +bool KQueueEngine::DelFd(int fd) +{ + log(DEBUG,"KQueueEngine::DelFd(%d)",fd); + + if ((fd < 0) || (fd > MAX_DESCRIPTORS)) + return false; + + struct kevent ke; + 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 socket from queue!"); + return false; + } + + CurrentSetSize--; + ref[fd] = 0; + return true; +} + +int KQueueEngine::GetMaxFds() +{ + return MAX_DESCRIPTORS; +} + +int KQueueEngine::GetRemainingFds() +{ + return MAX_DESCRIPTORS - CurrentSetSize; +} + +int KQueueEngine::Wait(int* fdlist) +{ + int result = 0; + + ts.tv_nsec = 5000L; + ts.tv_sec = 0; + int i = kevent(EngineHandle, NULL, 0, &ke_list[0], MAX_DESCRIPTORS, &ts); + for (int j = 0; j < i; j++) + fdlist[result++] = ke_list[j].ident; + + return result; +} + +std::string KQueueEngine::GetName() +{ + return "kqueue"; +} diff --git a/src/socketengine_select.cpp b/src/socketengine_select.cpp new file mode 100644 index 000000000..8d77da461 --- /dev/null +++ b/src/socketengine_select.cpp @@ -0,0 +1,131 @@ +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev. + * E-mail: + * <brain@chatspike.net> + * <Craig@chatspike.net> + * + * 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" +#include <vector> +#include <string> +#include "socketengine_select.h" +#include "helperfuncs.h" + +SelectEngine::SelectEngine() +{ + log(DEBUG,"SelectEngine::SelectEngine()"); + EngineHandle = 0; + CurrentSetSize = 0; +} + +SelectEngine::~SelectEngine() +{ + log(DEBUG,"SelectEngine::~SelectEngine()"); +} + +bool SelectEngine::AddFd(int fd, bool readable, char type) +{ + 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; + } + + fds[fd] = fd; + + ref[fd] = type; + if (readable) + { + log(DEBUG,"Set readbit"); + ref[fd] |= X_READBIT; + } + log(DEBUG,"Add socket %d",fd); + + CurrentSetSize++; + return true; +} + +bool SelectEngine::DelFd(int fd) +{ + log(DEBUG,"SelectEngine::DelFd(%d)",fd); + + if ((fd < 0) || (fd > MAX_DESCRIPTORS)) + return false; + + std::map<int,int>::iterator t = fds.find(fd); + if (t != fds.end()) + { + fds.erase(t); + log(DEBUG,"Deleted fd %d",fd); + } + + CurrentSetSize--; + ref[fd] = 0; + return true; +} + +int SelectEngine::GetMaxFds() +{ + return FD_SETSIZE; +} + +int SelectEngine::GetRemainingFds() +{ + return FD_SETSIZE - CurrentSetSize; +} + +int SelectEngine::Wait(int* fdlist) +{ + int result = 0; + + FD_ZERO(&wfdset); + FD_ZERO(&rfdset); + timeval tval; + int sresult; + for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++) + { + if (ref[a->second] & X_READBIT) + { + FD_SET (a->second, &rfdset); + } + else + { + FD_SET (a->second, &wfdset); + } + + } + tval.tv_sec = 0; + tval.tv_usec = 50L; + sresult = select(FD_SETSIZE, &rfdset, &wfdset, NULL, &tval); + if (sresult > 0) + { + for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++) + { + if ((FD_ISSET (a->second, &rfdset)) || (FD_ISSET (a->second, &wfdset))) + fdlist[result++] = a->second; + } + } + + return result; +} + +std::string SelectEngine::GetName() +{ + return "select"; +} |