From 7aa5e059a8f66d91bd8b69c58c657ceb70b4baff Mon Sep 17 00:00:00 2001 From: danieldg Date: Wed, 30 Sep 2009 18:31:05 +0000 Subject: Remove include/inspircd_se_config.h and socketengine-specific headers git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@11784 e03df62e-2008-0410-955e-edbf42e46eb7 --- .Makefile.inc | 17 +- .gitignore | 1 - configure | 14 +- include/socketengine.h | 2 + include/socketengines/socketengine_epoll.h | 59 ------- include/socketengines/socketengine_iocp.h | 237 ---------------------------- include/socketengines/socketengine_kqueue.h | 64 -------- include/socketengines/socketengine_poll.h | 74 --------- include/socketengines/socketengine_ports.h | 64 -------- include/socketengines/socketengine_select.h | 55 ------- make/calcdep.pl | 3 +- src/dns.cpp | 1 - src/filelogger.cpp | 1 - src/inspircd.cpp | 4 +- src/socketengine.cpp | 28 ---- src/socketengines/socketengine_epoll.cpp | 34 +++- src/socketengines/socketengine_iocp.cpp | 234 ++++++++++++++++++++++++++- src/socketengines/socketengine_kqueue.cpp | 61 ++++++- src/socketengines/socketengine_poll.cpp | 70 +++++++- src/socketengines/socketengine_ports.cpp | 60 ++++++- src/socketengines/socketengine_select.cpp | 52 +++++- 21 files changed, 519 insertions(+), 616 deletions(-) delete mode 100644 include/socketengines/socketengine_epoll.h delete mode 100644 include/socketengines/socketengine_iocp.h delete mode 100644 include/socketengines/socketengine_kqueue.h delete mode 100644 include/socketengines/socketengine_poll.h delete mode 100644 include/socketengines/socketengine_ports.h delete mode 100644 include/socketengines/socketengine_select.h diff --git a/.Makefile.inc b/.Makefile.inc index ef1610ab5..c801dd068 100644 --- a/.Makefile.inc +++ b/.Makefile.inc @@ -163,14 +163,14 @@ install: target@EXTRA_DIR@ clean: @echo Cleaning... - @rm -f src/inspircd src/modes/modeclasses.a - @rm -f src/*.so src/modules/*.so src/commands/*.so - @rm -f src/*.o src/*/*.o src/modules/*/*.o - @rm -f src/.*.d src/*/.*.d src/modules/*/.*.d - @rm -f $(BUILDPATH)/bin/inspircd $(BUILDPATH)/include $(BUILDPATH)/real.mk - @rm -rf $(BUILDPATH)/obj $(BUILDPATH)/modules - @-rmdir $(BUILDPATH)/bin - @-rmdir $(BUILDPATH) + @-rm -f src/inspircd src/modes/modeclasses.a include/inspircd_se_config.h + @-rm -f src/*.so src/modules/*.so src/commands/*.so + @-rm -f src/*.o src/*/*.o src/modules/*/*.o + @-rm -f src/.*.d src/*/.*.d src/modules/*/.*.d + @-rm -f $(BUILDPATH)/bin/inspircd $(BUILDPATH)/include $(BUILDPATH)/real.mk + @-rm -rf $(BUILDPATH)/obj $(BUILDPATH)/modules + @-rmdir $(BUILDPATH)/bin 2>/dev/null + @-rmdir $(BUILDPATH) 2>/dev/null @echo Completed. deinstall: @@ -193,7 +193,6 @@ configureclean: rm -f GNUmakefile rm -f include/inspircd_config.h rm -f include/inspircd_version.h - rm -f include/inspircd_se_config.h distclean: clean configureclean diff --git a/.gitignore b/.gitignore index df667074d..ede2efae1 100644 --- a/.gitignore +++ b/.gitignore @@ -17,7 +17,6 @@ /include/inspircd_config.h /include/inspircd_version.h -/include/inspircd_se_config.h /src/modules/m_geoip.cpp /src/modules/m_ldapauth.cpp diff --git a/configure b/configure index 72764eec0..cd21375b5 100755 --- a/configure +++ b/configure @@ -997,18 +997,6 @@ print FILEHANDLE "#define MAXBUF " . ($config{MAXBUF}+2) . "\n"; print FILEHANDLE "\n#include \"threadengines/threadengine_pthread.h\"\n\n#endif\n"; close(FILEHANDLE); - open(FILEHANDLE, ">include/inspircd_se_config.h.tmp"); - print FILEHANDLE <include/inspircd_version.h.tmp"); print FILEHANDLE < -#include -#include -#include "inspircd_config.h" -#include "inspircd.h" -#include "socketengine.h" -#include -#define EP_DELAY 5 - -/** A specialisation of the SocketEngine class, designed to use linux 2.6 epoll(). - */ -class EPollEngine : public SocketEngine -{ -private: - /** These are used by epoll() to hold socket events - */ - struct epoll_event* events; - int EngineHandle; -public: - /** Create a new EPollEngine - */ - EPollEngine(); - /** Delete an EPollEngine - */ - virtual ~EPollEngine(); - virtual bool AddFd(EventHandler* eh, int event_mask); - virtual void OnSetEvent(EventHandler* eh, int old_mask, int new_mask); - virtual bool DelFd(EventHandler* eh, bool force = false); - virtual int DispatchEvents(); - virtual std::string GetName(); -}; - -/** Creates a SocketEngine - */ -class SocketEngineFactory -{ -public: - /** Create a new instance of SocketEngine based on EpollEngine - */ - SocketEngine* Create() { return new EPollEngine; } -}; - -#endif diff --git a/include/socketengines/socketengine_iocp.h b/include/socketengines/socketengine_iocp.h deleted file mode 100644 index 0f4f6bdf5..000000000 --- a/include/socketengines/socketengine_iocp.h +++ /dev/null @@ -1,237 +0,0 @@ -/* +------------------------------------+ - * | Inspire Internet Relay Chat Daemon | - * +------------------------------------+ - * - * InspIRCd: (C) 2002-2009 InspIRCd Development Team - * See: http://wiki.inspircd.org/Credits - * - * This program is free but copyrighted software; see - * the file COPYING for details. - * - * --------------------------------------------------- - */ - -#ifndef __SOCKETENGINE_IOCP__ -#define __SOCKETENGINE_IOCP__ - -#define READ_BUFFER_SIZE 600 -#define USING_IOCP 1 - -#include "inspircd_config.h" -#include "inspircd_win32wrapper.h" -#include "inspircd.h" -#include "socketengine.h" - -/** Socket overlapped event types - */ -enum SocketIOEvent -{ - /** Read ready */ - SOCKET_IO_EVENT_READ_READY = 0, - /** Write ready */ - SOCKET_IO_EVENT_WRITE_READY = 1, - /** Accept ready */ - SOCKET_IO_EVENT_ACCEPT = 2, - /** Error occured */ - SOCKET_IO_EVENT_ERROR = 3, - /** Number of events */ - NUM_SOCKET_IO_EVENTS = 4, -}; - -/** Represents a windows overlapped IO event - */ -class Overlapped -{ - public: - /** Overlap event */ - OVERLAPPED m_overlap; - /** Type of event */ - SocketIOEvent m_event; -#ifdef WIN64 - /** Parameters */ - unsigned __int64 m_params; -#else - /** Parameters */ - unsigned long m_params; -#endif - /** Create an overlapped event - */ - Overlapped(SocketIOEvent ev, int params) : m_event(ev), m_params(params) - { - memset(&m_overlap, 0, sizeof(OVERLAPPED)); - } -}; - -/** Specific to UDP sockets with overlapped IO - */ -struct udp_overlap -{ - unsigned char udp_buffer[600]; - unsigned long udp_len; - sockaddr udp_sockaddr[2]; - unsigned long udp_sockaddr_len; -}; - -/** Specific to accepting sockets with overlapped IO - */ -struct accept_overlap -{ - int socket; - char buf[1024]; -}; - -/** Implementation of SocketEngine that implements windows IO Completion Ports - */ -class IOCPEngine : public SocketEngine -{ - /** Creates a "fake" file descriptor for use with an IOCP socket. - * This is a little slow, but it isnt called too much. We'll fix it - * in a future release. - * @return -1 if there are no free slots, and an integer if it finds one. - */ - __inline int GenerateFd(int RealFd) - { - int index_hash = RealFd % MAX_DESCRIPTORS; - if(ref[index_hash] == 0) - return index_hash; - else - { - register int i = 0; - for(; i < MAX_DESCRIPTORS; ++i) - if(ref[i] == 0) - return i; - } - return -1; - } - - /** Global I/O completion port that sockets attach to. - */ - HANDLE m_completionPort; - - /** This is kinda shitty... :/ for getting an address from a real fd. - */ - std::map m_binding; - -public: - /** Holds the preallocated buffer passed to WSARecvFrom - * function. Yes, I know, it's a dirty hack. - */ - udp_overlap * udp_ov; - - /** Creates an IOCP Socket Engine - * @param Instance The creator of this object - */ - IOCPEngine(); - - /** Deletes an IOCP socket engine and all the attached sockets - */ - ~IOCPEngine(); - - /** Adds an event handler to the completion port, and sets up initial events. - * @param eh EventHandler to add - * @return True if success, false if no room - */ - bool AddFd(EventHandler* eh, int event_mask); - - /** Gets the maximum number of file descriptors that this engine can handle. - * @return The number of file descriptors - */ - __inline int GetMaxFds() { return MAX_DESCRIPTORS; } - - /** Gets the number of free/remaining file descriptors under this engine. - * @return Remaining count - */ - __inline int GetRemainingFds() - { - register int count = 0; - register int i = 0; - for(; i < MAX_DESCRIPTORS; ++i) - if(ref[i] == 0) - ++count; - return count; - } - - /** Removes a file descriptor from the set, preventing it from receiving any more events - * @return True if remove was successful, false otherwise - */ - bool DelFd(EventHandler* eh, bool force = false); - - /** Called every loop to handle input/output events for all sockets under this engine - * @return The number of "changed" sockets. - */ - int DispatchEvents(); - - /** Gets the name of this socket engine as a string. - * @return string of socket engine name - */ - std::string GetName(); - - void OnSetEvent(EventHandler* eh, int old_mask, int new_mask); - - /** Posts a completion event on the specified socket. - * @param eh EventHandler for message - * @param type Event Type - * @param param Event Parameter - * @return True if added, false if not - */ - bool PostCompletionEvent(EventHandler* eh, SocketIOEvent type, int param); - - /** Posts a read event on the specified socket - * @param eh EventHandler (socket) - */ - void PostReadEvent(EventHandler* eh); - - /** Posts an accept event on the specified socket - * @param eh EventHandler (socket) - */ - void PostAcceptEvent(EventHandler* eh); - - /** Returns the EventHandler attached to a specific fd. - * If the fd isnt in the socketengine, returns NULL. - * @param fd The event handler to look for - * @return A pointer to the event handler, or NULL - */ - EventHandler* GetRef(int fd); - - /** Returns true if a file descriptor exists in - * the socket engine's list. - * @param fd The event handler to look for - * @return True if this fd has an event handler - */ - bool HasFd(int fd); - - /** Returns the EventHandler attached to a specific fd. - * If the fd isnt in the socketengine, returns NULL. - * @param fd The event handler to look for - * @return A pointer to the event handler, or NULL - */ - EventHandler* GetIntRef(int fd); - - bool BoundsCheckFd(EventHandler* eh); - - virtual int Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen); - - virtual int RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen); - - virtual int Blocking(int fd); - - virtual int NonBlocking(int fd); - - virtual int GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen); - - virtual int Close(int fd); - - virtual int Close(EventHandler* fd); -}; - -/** Creates a SocketEngine - */ -class SocketEngineFactory -{ -public: - /** Create a new instance of SocketEngine based on IOCPEngine - */ - SocketEngine* Create() { return new IOCPEngine; } -}; - -#endif diff --git a/include/socketengines/socketengine_kqueue.h b/include/socketengines/socketengine_kqueue.h deleted file mode 100644 index 01621c1de..000000000 --- a/include/socketengines/socketengine_kqueue.h +++ /dev/null @@ -1,64 +0,0 @@ -/* +------------------------------------+ - * | Inspire Internet Relay Chat Daemon | - * +------------------------------------+ - * - * InspIRCd: (C) 2002-2009 InspIRCd Development Team - * See: http://wiki.inspircd.org/Credits - * - * This program is free but copyrighted software; see - * the file COPYING for details. - * - * --------------------------------------------------- - */ - -#ifndef __SOCKETENGINE_KQUEUE__ -#define __SOCKETENGINE_KQUEUE__ - -#include -#include -#include -#include "inspircd_config.h" -#include "inspircd.h" -#include -#include -#include -#include "socketengine.h" - -/** A specialisation of the SocketEngine class, designed to use FreeBSD kqueue(). - */ -class KQueueEngine : public SocketEngine -{ -private: - int EngineHandle; - /** These are used by kqueue() to hold socket events - */ - struct kevent* ke_list; - /** This is a specialised time value used by kqueue() - */ - struct timespec ts; -public: - /** Create a new KQueueEngine - */ - KQueueEngine(); - /** Delete a KQueueEngine - */ - virtual ~KQueueEngine(); - bool AddFd(EventHandler* eh, int event_mask); - void OnSetEvent(EventHandler* eh, int old_mask, int new_mask); - virtual bool DelFd(EventHandler* eh, bool force = false); - virtual int DispatchEvents(); - virtual std::string GetName(); - virtual void RecoverFromFork(); -}; - -/** Creates a SocketEngine - */ -class SocketEngineFactory -{ - public: - /** Create a new instance of SocketEngine based on KQueueEngine - */ - SocketEngine* Create() { return new KQueueEngine; } -}; - -#endif diff --git a/include/socketengines/socketengine_poll.h b/include/socketengines/socketengine_poll.h deleted file mode 100644 index a99ce915c..000000000 --- a/include/socketengines/socketengine_poll.h +++ /dev/null @@ -1,74 +0,0 @@ -/* +------------------------------------+ - * | Inspire Internet Relay Chat Daemon | - * +------------------------------------+ - * - * InspIRCd: (C) 2002-2009 InspIRCd Development Team - * See: http://wiki.inspircd.org/Credits - * - * This program is free but copyrighted software; see - * the file COPYING for details. - * - * --------------------------------------------------- - */ - -#ifndef __SOCKETENGINE_POLL__ -#define __SOCKETENGINE_POLL__ - -#include -#include -#include -#include "inspircd_config.h" -#include "inspircd.h" -#include "socketengine.h" - -#ifndef WINDOWS - #ifndef __USE_XOPEN - #define __USE_XOPEN /* fuck every fucking OS ever made. needed by poll.h to work.*/ - #endif - #include - #include -#else - /* *grumble* */ - #define struct pollfd WSAPOLLFD - #define poll WSAPoll -#endif - -class InspIRCd; - -/** A specialisation of the SocketEngine class, designed to use poll(). - */ -class PollEngine : public SocketEngine -{ -private: - /** These are used by poll() to hold socket events - */ - struct pollfd *events; - /** This map maps fds to an index in the events array. - */ - std::map fd_mappings; -public: - /** Create a new PollEngine - */ - PollEngine(); - /** Delete a PollEngine - */ - virtual ~PollEngine(); - virtual bool AddFd(EventHandler* eh, int event_mask); - virtual void OnSetEvent(EventHandler* eh, int old_mask, int new_mask); - virtual EventHandler* GetRef(int fd); - virtual bool DelFd(EventHandler* eh, bool force = false); - virtual int DispatchEvents(); - virtual std::string GetName(); -}; - -/** Creates a SocketEngine - */ -class SocketEngineFactory -{ -public: - /** Create a new instance of SocketEngine based on PollEngine - */ - SocketEngine* Create() { return new PollEngine; } -}; - -#endif diff --git a/include/socketengines/socketengine_ports.h b/include/socketengines/socketengine_ports.h deleted file mode 100644 index be8c4bad2..000000000 --- a/include/socketengines/socketengine_ports.h +++ /dev/null @@ -1,64 +0,0 @@ -/* +------------------------------------+ - * | Inspire Internet Relay Chat Daemon | - * +------------------------------------+ - * - * InspIRCd: (C) 2002-2009 InspIRCd Development Team - * See: http://wiki.inspircd.org/Credits - * - * This program is free but copyrighted software; see - * the file COPYING for details. - * - * --------------------------------------------------- - */ - -#ifndef __SOCKETENGINE_PORTS__ -#define __SOCKETENGINE_PORTS__ - -#ifndef __sun -# error You need Solaris 10 or later to make use of this code. -#endif - -#include -#include -#include -#include "inspircd_config.h" -#include "inspircd.h" -#include "socketengine.h" -#include - -/** A specialisation of the SocketEngine class, designed to use solaris 10 I/O completion ports - */ -class PortsEngine : public SocketEngine -{ -private: - /** These are used by epoll() to hold socket events - */ - port_event_t* events; -public: - /** Create a new PortsEngine - * @param Instance The creator of this object - */ - PortsEngine(); - /** Delete a PortsEngine - */ - virtual ~PortsEngine(); - virtual bool AddFd(EventHandler* eh, int event_mask); - void OnSetEvent(EventHandler* eh, int old_event, int new_event); - virtual bool DelFd(EventHandler* eh, bool force = false); - virtual int DispatchEvents(); - virtual std::string GetName(); - virtual void WantWrite(EventHandler* eh); -}; - -/** Creates a SocketEngine - */ -class SocketEngineFactory -{ -public: - /** Create a new instance of SocketEngine based on PortsEngine - */ - SocketEngine* Create() { return new PortsEngine; } -}; - -#endif - diff --git a/include/socketengines/socketengine_select.h b/include/socketengines/socketengine_select.h deleted file mode 100644 index 5a4aee86b..000000000 --- a/include/socketengines/socketengine_select.h +++ /dev/null @@ -1,55 +0,0 @@ -/* +------------------------------------+ - * | Inspire Internet Relay Chat Daemon | - * +------------------------------------+ - * - * InspIRCd: (C) 2002-2009 InspIRCd Development Team - * See: http://wiki.inspircd.org/Credits - * - * This program is free but copyrighted software; see - * the file COPYING for details. - * - * --------------------------------------------------- - */ - -#ifndef __SOCKETENGINE_SELECT__ -#define __SOCKETENGINE_SELECT__ - -#include -#include -#include -#ifndef WINDOWS -#include -#endif // WINDOWS -#include "inspircd_config.h" -#include "inspircd.h" -#include "socketengine.h" - -/** A specialisation of the SocketEngine class, designed to use traditional select(). - */ -class SelectEngine : public SocketEngine -{ -public: - /** Create a new SelectEngine - */ - SelectEngine(); - /** Delete a SelectEngine - */ - virtual ~SelectEngine(); - virtual bool AddFd(EventHandler* eh, int event_mask); - virtual bool DelFd(EventHandler* eh, bool force = false); - void OnSetEvent(EventHandler* eh, int, int); - virtual int DispatchEvents(); - virtual std::string GetName(); -}; - -/** Creates a SocketEngine - */ -class SocketEngineFactory -{ -public: - /** Create a new instance of SocketEngine based on SelectEngine - */ - SocketEngine* Create() { return new SelectEngine; } -}; - -#endif diff --git a/make/calcdep.pl b/make/calcdep.pl index d9242b098..890d8d265 100755 --- a/make/calcdep.pl +++ b/make/calcdep.pl @@ -37,9 +37,10 @@ all: bin/inspircd modules END my @core_deps; - for my $file (<*.cpp>, , "socketengines/$ENV{SOCKETENGINE}.cpp", "threadengines/threadengine_pthread.cpp") { + for my $file (<*.cpp>, , , "threadengines/threadengine_pthread.cpp") { my $out = find_output $file; dep_cpp $file, $out; + next if $file =~ m#^socketengines/# && $file ne "socketengines/$ENV{SOCKETENGINE}.cpp"; push @core_deps, $out; } diff --git a/src/dns.cpp b/src/dns.cpp index a28881ad2..718a1698f 100644 --- a/src/dns.cpp +++ b/src/dns.cpp @@ -31,7 +31,6 @@ looks like this, walks like this or tastes like this. #include #else #include "inspircd_win32wrapper.h" -#include "inspircd_se_config.h" #endif #include "inspircd.h" diff --git a/src/filelogger.cpp b/src/filelogger.cpp index 1a88823c4..f442b8b7a 100644 --- a/src/filelogger.cpp +++ b/src/filelogger.cpp @@ -16,7 +16,6 @@ #include "inspircd.h" #include #include "socketengine.h" -#include "inspircd_se_config.h" #include "filelogger.h" FileLogStream::FileLogStream(int loglevel, FileWriter *fw) diff --git a/src/inspircd.cpp b/src/inspircd.cpp index 300d01650..b815ce4ac 100644 --- a/src/inspircd.cpp +++ b/src/inspircd.cpp @@ -36,7 +36,6 @@ #include "xline.h" #include "bancache.h" #include "socketengine.h" -#include "inspircd_se_config.h" #include "socket.h" #include "command_parse.h" #include "exitcodes.h" @@ -374,8 +373,7 @@ InspIRCd::InspIRCd(int argc, char** argv) : // This must be created first, so other parts of Insp can use it while starting up this->Logs = new LogManager; - SocketEngineFactory SEF; - SE = SEF.Create(); + SE = CreateSocketEngine(); this->Threads = new ThreadEngine; diff --git a/src/socketengine.cpp b/src/socketengine.cpp index 49624481f..3108ee705 100644 --- a/src/socketengine.cpp +++ b/src/socketengine.cpp @@ -11,35 +11,7 @@ * --------------------------------------------------- */ -/* $Core */ - -/********* DEFAULTS **********/ - -/* $ExtraSources: socketengines/socketengine_select.cpp */ -/* $ExtraObjects: socketengine_select.o */ - -/* $If: USE_POLL */ -/* $ExtraSources: socketengines/socketengine_poll.cpp */ -/* $ExtraObjects: socketengine_poll.o */ -/* $EndIf */ - -/* $If: USE_KQUEUE */ -/* $ExtraSources: socketengines/socketengine_kqueue.cpp */ -/* $ExtraObjects: socketengine_kqueue.o */ -/* $EndIf */ - -/* $If: USE_EPOLL */ -/* $ExtraSources: socketengines/socketengine_epoll.cpp */ -/* $ExtraObjects: socketengine_epoll.o */ -/* $EndIf */ - -/* $If: USE_PORTS */ -/* $ExtraSources: socketengines/socketengine_ports.cpp */ -/* $ExtraObjects: socketengine_ports.o */ -/* $EndIf */ - #include "inspircd.h" -#include "socketengine.h" EventHandler::EventHandler() { diff --git a/src/socketengines/socketengine_epoll.cpp b/src/socketengines/socketengine_epoll.cpp index 059798d25..c1b22ffba 100644 --- a/src/socketengines/socketengine_epoll.cpp +++ b/src/socketengines/socketengine_epoll.cpp @@ -11,10 +11,38 @@ * --------------------------------------------------- */ +#include +#include +#include #include "inspircd.h" #include "exitcodes.h" -#include "socketengines/socketengine_epoll.h" +#include "socketengine.h" +#include #include +#define EP_DELAY 5 + +/** A specialisation of the SocketEngine class, designed to use linux 2.6 epoll(). + */ +class EPollEngine : public SocketEngine +{ +private: + /** These are used by epoll() to hold socket events + */ + struct epoll_event* events; + int EngineHandle; +public: + /** Create a new EPollEngine + */ + EPollEngine(); + /** Delete an EPollEngine + */ + virtual ~EPollEngine(); + virtual bool AddFd(EventHandler* eh, int event_mask); + virtual void OnSetEvent(EventHandler* eh, int old_mask, int new_mask); + virtual bool DelFd(EventHandler* eh, bool force = false); + virtual int DispatchEvents(); + virtual std::string GetName(); +}; EPollEngine::EPollEngine() { @@ -216,3 +244,7 @@ std::string EPollEngine::GetName() return "epoll"; } +SocketEngine* CreateSocketEngine() +{ + return new EPollEngine; +} diff --git a/src/socketengines/socketengine_iocp.cpp b/src/socketengines/socketengine_iocp.cpp index c253bf0a6..ad45ce5ce 100644 --- a/src/socketengines/socketengine_iocp.cpp +++ b/src/socketengines/socketengine_iocp.cpp @@ -11,7 +11,234 @@ * --------------------------------------------------- */ -#include "socketengines/socketengine_iocp.h" +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * InspIRCd: (C) 2002-2009 InspIRCd Development Team + * See: http://wiki.inspircd.org/Credits + * + * This program is free but copyrighted software; see + * the file COPYING for details. + * + * --------------------------------------------------- + */ + +#ifndef __SOCKETENGINE_IOCP__ +#define __SOCKETENGINE_IOCP__ + +#define READ_BUFFER_SIZE 600 +#define USING_IOCP 1 + +#include "inspircd_config.h" +#include "inspircd_win32wrapper.h" +#include "inspircd.h" +#include "socketengine.h" + +/** Socket overlapped event types + */ +enum SocketIOEvent +{ + /** Read ready */ + SOCKET_IO_EVENT_READ_READY = 0, + /** Write ready */ + SOCKET_IO_EVENT_WRITE_READY = 1, + /** Accept ready */ + SOCKET_IO_EVENT_ACCEPT = 2, + /** Error occured */ + SOCKET_IO_EVENT_ERROR = 3, + /** Number of events */ + NUM_SOCKET_IO_EVENTS = 4, +}; + +/** Represents a windows overlapped IO event + */ +class Overlapped +{ + public: + /** Overlap event */ + OVERLAPPED m_overlap; + /** Type of event */ + SocketIOEvent m_event; +#ifdef WIN64 + /** Parameters */ + unsigned __int64 m_params; +#else + /** Parameters */ + unsigned long m_params; +#endif + /** Create an overlapped event + */ + Overlapped(SocketIOEvent ev, int params) : m_event(ev), m_params(params) + { + memset(&m_overlap, 0, sizeof(OVERLAPPED)); + } +}; + +/** Specific to UDP sockets with overlapped IO + */ +struct udp_overlap +{ + unsigned char udp_buffer[600]; + unsigned long udp_len; + sockaddr udp_sockaddr[2]; + unsigned long udp_sockaddr_len; +}; + +/** Specific to accepting sockets with overlapped IO + */ +struct accept_overlap +{ + int socket; + char buf[1024]; +}; + +/** Implementation of SocketEngine that implements windows IO Completion Ports + */ +class IOCPEngine : public SocketEngine +{ + /** Creates a "fake" file descriptor for use with an IOCP socket. + * This is a little slow, but it isnt called too much. We'll fix it + * in a future release. + * @return -1 if there are no free slots, and an integer if it finds one. + */ + __inline int GenerateFd(int RealFd) + { + int index_hash = RealFd % MAX_DESCRIPTORS; + if(ref[index_hash] == 0) + return index_hash; + else + { + register int i = 0; + for(; i < MAX_DESCRIPTORS; ++i) + if(ref[i] == 0) + return i; + } + return -1; + } + + /** Global I/O completion port that sockets attach to. + */ + HANDLE m_completionPort; + + /** This is kinda shitty... :/ for getting an address from a real fd. + */ + std::map m_binding; + +public: + /** Holds the preallocated buffer passed to WSARecvFrom + * function. Yes, I know, it's a dirty hack. + */ + udp_overlap * udp_ov; + + /** Creates an IOCP Socket Engine + * @param Instance The creator of this object + */ + IOCPEngine(); + + /** Deletes an IOCP socket engine and all the attached sockets + */ + ~IOCPEngine(); + + /** Adds an event handler to the completion port, and sets up initial events. + * @param eh EventHandler to add + * @return True if success, false if no room + */ + bool AddFd(EventHandler* eh, int event_mask); + + /** Gets the maximum number of file descriptors that this engine can handle. + * @return The number of file descriptors + */ + __inline int GetMaxFds() { return MAX_DESCRIPTORS; } + + /** Gets the number of free/remaining file descriptors under this engine. + * @return Remaining count + */ + __inline int GetRemainingFds() + { + register int count = 0; + register int i = 0; + for(; i < MAX_DESCRIPTORS; ++i) + if(ref[i] == 0) + ++count; + return count; + } + + /** Removes a file descriptor from the set, preventing it from receiving any more events + * @return True if remove was successful, false otherwise + */ + bool DelFd(EventHandler* eh, bool force = false); + + /** Called every loop to handle input/output events for all sockets under this engine + * @return The number of "changed" sockets. + */ + int DispatchEvents(); + + /** Gets the name of this socket engine as a string. + * @return string of socket engine name + */ + std::string GetName(); + + void OnSetEvent(EventHandler* eh, int old_mask, int new_mask); + + /** Posts a completion event on the specified socket. + * @param eh EventHandler for message + * @param type Event Type + * @param param Event Parameter + * @return True if added, false if not + */ + bool PostCompletionEvent(EventHandler* eh, SocketIOEvent type, int param); + + /** Posts a read event on the specified socket + * @param eh EventHandler (socket) + */ + void PostReadEvent(EventHandler* eh); + + /** Posts an accept event on the specified socket + * @param eh EventHandler (socket) + */ + void PostAcceptEvent(EventHandler* eh); + + /** Returns the EventHandler attached to a specific fd. + * If the fd isnt in the socketengine, returns NULL. + * @param fd The event handler to look for + * @return A pointer to the event handler, or NULL + */ + EventHandler* GetRef(int fd); + + /** Returns true if a file descriptor exists in + * the socket engine's list. + * @param fd The event handler to look for + * @return True if this fd has an event handler + */ + bool HasFd(int fd); + + /** Returns the EventHandler attached to a specific fd. + * If the fd isnt in the socketengine, returns NULL. + * @param fd The event handler to look for + * @return A pointer to the event handler, or NULL + */ + EventHandler* GetIntRef(int fd); + + bool BoundsCheckFd(EventHandler* eh); + + virtual int Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen); + + virtual int RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen); + + virtual int Blocking(int fd); + + virtual int NonBlocking(int fd); + + virtual int GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen); + + virtual int Close(int fd); + + virtual int Close(EventHandler* fd); +}; + +#endif + #include "exitcodes.h" #include @@ -526,3 +753,8 @@ int IOCPEngine::Close(EventHandler* fd) { return this->Close(fd->GetFd()); } + +SocketEngine* CreateSocketEngine() +{ + return new IOCPEngine; +} diff --git a/src/socketengines/socketengine_kqueue.cpp b/src/socketengines/socketengine_kqueue.cpp index d05ee438d..0550ae051 100644 --- a/src/socketengines/socketengine_kqueue.cpp +++ b/src/socketengines/socketengine_kqueue.cpp @@ -16,7 +16,61 @@ #include #include #include -#include "socketengines/socketengine_kqueue.h" +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * InspIRCd: (C) 2002-2009 InspIRCd Development Team + * See: http://wiki.inspircd.org/Credits + * + * This program is free but copyrighted software; see + * the file COPYING for details. + * + * --------------------------------------------------- + */ + +#ifndef __SOCKETENGINE_KQUEUE__ +#define __SOCKETENGINE_KQUEUE__ + +#include +#include +#include +#include "inspircd_config.h" +#include "inspircd.h" +#include +#include +#include +#include "socketengine.h" + +/** A specialisation of the SocketEngine class, designed to use FreeBSD kqueue(). + */ +class KQueueEngine : public SocketEngine +{ +private: + int EngineHandle; + /** These are used by kqueue() to hold socket events + */ + struct kevent* ke_list; + /** This is a specialised time value used by kqueue() + */ + struct timespec ts; +public: + /** Create a new KQueueEngine + */ + KQueueEngine(); + /** Delete a KQueueEngine + */ + virtual ~KQueueEngine(); + bool AddFd(EventHandler* eh, int event_mask); + void OnSetEvent(EventHandler* eh, int old_mask, int new_mask); + virtual bool DelFd(EventHandler* eh, bool force = false); + virtual int DispatchEvents(); + virtual std::string GetName(); + virtual void RecoverFromFork(); +}; + +#endif + #include KQueueEngine::KQueueEngine() @@ -203,3 +257,8 @@ std::string KQueueEngine::GetName() { return "kqueue"; } + +SocketEngine* CreateSocketEngine() +{ + return new KQueueEngine; +} diff --git a/src/socketengines/socketengine_poll.cpp b/src/socketengines/socketengine_poll.cpp index a72d21d1f..16917ffbc 100644 --- a/src/socketengines/socketengine_poll.cpp +++ b/src/socketengines/socketengine_poll.cpp @@ -13,7 +13,71 @@ #include "inspircd.h" #include "exitcodes.h" -#include "socketengines/socketengine_poll.h" +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * InspIRCd: (C) 2002-2009 InspIRCd Development Team + * See: http://wiki.inspircd.org/Credits + * + * This program is free but copyrighted software; see + * the file COPYING for details. + * + * --------------------------------------------------- + */ + +#ifndef __SOCKETENGINE_POLL__ +#define __SOCKETENGINE_POLL__ + +#include +#include +#include +#include "inspircd_config.h" +#include "inspircd.h" +#include "socketengine.h" + +#ifndef WINDOWS + #ifndef __USE_XOPEN + #define __USE_XOPEN /* fuck every fucking OS ever made. needed by poll.h to work.*/ + #endif + #include + #include +#else + /* *grumble* */ + #define struct pollfd WSAPOLLFD + #define poll WSAPoll +#endif + +class InspIRCd; + +/** A specialisation of the SocketEngine class, designed to use poll(). + */ +class PollEngine : public SocketEngine +{ +private: + /** These are used by poll() to hold socket events + */ + struct pollfd *events; + /** This map maps fds to an index in the events array. + */ + std::map fd_mappings; +public: + /** Create a new PollEngine + */ + PollEngine(); + /** Delete a PollEngine + */ + virtual ~PollEngine(); + virtual bool AddFd(EventHandler* eh, int event_mask); + virtual void OnSetEvent(EventHandler* eh, int old_mask, int new_mask); + virtual EventHandler* GetRef(int fd); + virtual bool DelFd(EventHandler* eh, bool force = false); + virtual int DispatchEvents(); + virtual std::string GetName(); +}; + +#endif + #include #ifdef __FreeBSD__ #include @@ -225,3 +289,7 @@ std::string PollEngine::GetName() return "poll"; } +SocketEngine* CreateSocketEngine() +{ + return new PollEngine; +} diff --git a/src/socketengines/socketengine_ports.cpp b/src/socketengines/socketengine_ports.cpp index d23857f50..28dfdf4e4 100644 --- a/src/socketengines/socketengine_ports.cpp +++ b/src/socketengines/socketengine_ports.cpp @@ -14,7 +14,61 @@ #include "inspircd.h" #include "exitcodes.h" #include -#include "socketengines/socketengine_ports.h" +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * InspIRCd: (C) 2002-2009 InspIRCd Development Team + * See: http://wiki.inspircd.org/Credits + * + * This program is free but copyrighted software; see + * the file COPYING for details. + * + * --------------------------------------------------- + */ + +#ifndef __SOCKETENGINE_PORTS__ +#define __SOCKETENGINE_PORTS__ + +#ifndef __sun +# error You need Solaris 10 or later to make use of this code. +#endif + +#include +#include +#include +#include "inspircd_config.h" +#include "inspircd.h" +#include "socketengine.h" +#include + +/** A specialisation of the SocketEngine class, designed to use solaris 10 I/O completion ports + */ +class PortsEngine : public SocketEngine +{ +private: + /** These are used by epoll() to hold socket events + */ + port_event_t* events; +public: + /** Create a new PortsEngine + * @param Instance The creator of this object + */ + PortsEngine(); + /** Delete a PortsEngine + */ + virtual ~PortsEngine(); + virtual bool AddFd(EventHandler* eh, int event_mask); + void OnSetEvent(EventHandler* eh, int old_event, int new_event); + virtual bool DelFd(EventHandler* eh, bool force = false); + virtual int DispatchEvents(); + virtual std::string GetName(); + virtual void WantWrite(EventHandler* eh); +}; + +#endif + + #include PortsEngine::PortsEngine() @@ -163,3 +217,7 @@ std::string PortsEngine::GetName() return "ports"; } +SocketEngine* CreateSocketEngine() +{ + return new PortsEngine; +} diff --git a/src/socketengines/socketengine_select.cpp b/src/socketengines/socketengine_select.cpp index b35558a70..f0668aafb 100644 --- a/src/socketengines/socketengine_select.cpp +++ b/src/socketengines/socketengine_select.cpp @@ -15,7 +15,52 @@ #ifndef WINDOWS #include #endif // WINDOWS -#include "socketengines/socketengine_select.h" +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * InspIRCd: (C) 2002-2009 InspIRCd Development Team + * See: http://wiki.inspircd.org/Credits + * + * This program is free but copyrighted software; see + * the file COPYING for details. + * + * --------------------------------------------------- + */ + +#ifndef __SOCKETENGINE_SELECT__ +#define __SOCKETENGINE_SELECT__ + +#include +#include +#include +#ifndef WINDOWS +#include +#endif // WINDOWS +#include "inspircd_config.h" +#include "inspircd.h" +#include "socketengine.h" + +/** A specialisation of the SocketEngine class, designed to use traditional select(). + */ +class SelectEngine : public SocketEngine +{ +public: + /** Create a new SelectEngine + */ + SelectEngine(); + /** Delete a SelectEngine + */ + virtual ~SelectEngine(); + virtual bool AddFd(EventHandler* eh, int event_mask); + virtual bool DelFd(EventHandler* eh, bool force = false); + void OnSetEvent(EventHandler* eh, int, int); + virtual int DispatchEvents(); + virtual std::string GetName(); +}; + +#endif + SelectEngine::SelectEngine() @@ -148,3 +193,8 @@ std::string SelectEngine::GetName() { return "select"; } + +SocketEngine* CreateSocketEngine() +{ + return new SelectEngine; +} -- cgit v1.2.3