From e381b06561228aaea752deda20a62c6dc99a560e Mon Sep 17 00:00:00 2001 From: brain Date: Fri, 18 Aug 2006 01:08:14 +0000 Subject: EventHandler class, an abstraction for raw i/o git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@4941 e03df62e-2008-0410-955e-edbf42e46eb7 --- include/configreader.h | 4 +- include/connection.h | 15 +-- include/dns.h | 18 +--- include/inspircd.h | 20 ++-- include/inspsocket.h | 12 +-- include/modules.h | 6 +- include/socket.h | 11 ++- include/socketengine.h | 206 ++++++++++++++++++++++++++++++------------ include/socketengine_epoll.h | 6 +- include/socketengine_kqueue.h | 6 +- include/socketengine_select.h | 6 +- include/users.h | 5 + 12 files changed, 196 insertions(+), 119 deletions(-) (limited to 'include') diff --git a/include/configreader.h b/include/configreader.h index a63e31d71..467b5bde3 100644 --- a/include/configreader.h +++ b/include/configreader.h @@ -25,6 +25,8 @@ #include "inspircd.h" #include "globals.h" #include "modules.h" +#include "socketengine.h" +#include "socket.h" class ServerConfig; class InspIRCd; @@ -310,7 +312,7 @@ class ServerConfig : public Extensible /** A list of the file descriptors for the listening client ports */ - int openSockfd[MAX_DESCRIPTORS]; + ListenSocket* openSockfd[255]; /** Boolean sets of which modules implement which functions */ diff --git a/include/connection.h b/include/connection.h index bb9f7a580..55d6204a1 100644 --- a/include/connection.h +++ b/include/connection.h @@ -20,18 +20,14 @@ #include #include "inspircd_config.h" #include "base.h" +#include "socketengine.h" /** connection is the base class of userrec, and holds basic user properties. * This can be extended for holding other user-like objects in the future. */ -class connection : public Extensible +class connection : public EventHandler { public: - /** File descriptor of the connection. - * For a remote connection, this will have a negative value. - */ - int fd; - /** Hostname of connection. * This should be valid as per RFC1035. */ @@ -80,13 +76,6 @@ class connection : public Extensible /** Used by PING checking code */ time_t nping; - - /** Default constructor, creates the user as remote. - */ - connection() - { - this->fd = -1; - } }; diff --git a/include/dns.h b/include/dns.h index c3b74fdca..6c690dc87 100644 --- a/include/dns.h +++ b/include/dns.h @@ -37,8 +37,9 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include #include "inspircd_config.h" -#include "socket.h" #include "base.h" +#include "socketengine.h" +#include "socket.h" using namespace std; using irc::sockets::insp_aton; @@ -225,7 +226,7 @@ class Resolver : public Extensible * back to Resolver objects, based upon the request ID. You * should never use this class yourself. */ -class DNS : public Extensible +class DNS : public EventHandler { private: @@ -247,11 +248,6 @@ class DNS : public Extensible */ insp_inaddr myserver; - /** - * File descriptor being used to perform queries - */ - static int MasterSocket; - /** * A counter used to form part of the pseudo-random id */ @@ -294,11 +290,6 @@ class DNS : public Extensible * Empty out a header into a data stream ready for transmission "on the wire" */ static void EmptyHeader(unsigned char *output, const DNSHeader *header, const int length); - /** - * Get the master socket fd, used internally - */ - static int GetMasterSocket(); - /** * Start the lookup of an ipv4 from a hostname */ @@ -337,8 +328,9 @@ class DNS : public Extensible /** * Handle a SocketEngine read event + * Inherited from EventHandler */ - void MarshallReads(int fd); + void HandleEvent(EventType et); /** * Add a Resolver* to the list of active classes diff --git a/include/inspircd.h b/include/inspircd.h index f404cce80..77a1e45ac 100644 --- a/include/inspircd.h +++ b/include/inspircd.h @@ -215,12 +215,6 @@ class InspIRCd : public classbase */ void MoveBefore(std::string modulename, std::string before); - /** Process a user whos socket has been flagged as active - * @param cu The user to process - * @return There is no actual return value, however upon exit, the user 'cu' may have been deleted - */ - void ProcessUser(userrec* cu); - /** Iterate the list of InspSocket objects, removing ones which have timed out * @param TIME the current time */ @@ -322,14 +316,6 @@ class InspIRCd : public classbase */ std::vector module_sockets; - /** Socket reference table, provides fast lookup of fd to InspSocket* - */ - InspSocket* socket_ref[MAX_DESCRIPTORS]; - - /** user reference table, provides fast lookup of fd to userrec* - */ - userrec* fd_ref_table[MAX_DESCRIPTORS]; - /** Client list, a hash_map containing all clients, local and remote */ user_hash clientlist; @@ -381,6 +367,12 @@ class InspIRCd : public classbase */ time_t Time(); + /** Process a user whos socket has been flagged as active + * @param cu The user to process + * @return There is no actual return value, however upon exit, the user 'cu' may have been deleted + */ + void ProcessUser(userrec* cu); + /** Get the total number of currently loaded modules * @return The number of loaded modules */ diff --git a/include/inspsocket.h b/include/inspsocket.h index ddb0baedb..3791aa3e0 100644 --- a/include/inspsocket.h +++ b/include/inspsocket.h @@ -23,6 +23,7 @@ #include "dns.h" #include "inspircd_config.h" #include "socket.h" +#include "inspsocket.h" /** * States which a socket may be in @@ -53,18 +54,13 @@ using irc::sockets::insp_aton; * and use the InspSocket constructors to establish connections * and bindings. */ -class InspSocket : public Extensible +class InspSocket : public EventHandler { public: InspIRCd* Instance; std::deque outbuffer; - /** - * The file descriptor of this socket - */ - int fd; - /** * The hostname connected to */ @@ -365,6 +361,10 @@ class InspSocket : public Extensible * memory reclaimed. */ void MarkAsClosed(); + + void HandleEvent(EventType et); + + bool Readable(); }; #endif diff --git a/include/modules.h b/include/modules.h index aa36bed3a..87d76e668 100644 --- a/include/modules.h +++ b/include/modules.h @@ -166,9 +166,9 @@ typedef std::map featurelist; // useful macros -#define IS_LOCAL(x) ((x->fd > -1) && (x->fd <= MAX_DESCRIPTORS)) -#define IS_REMOTE(x) (x->fd < 0) -#define IS_MODULE_CREATED(x) (x->fd == FD_MAGIC_NUMBER) +#define IS_LOCAL(x) ((x->GetFd() > -1) && (x->GetFd() <= MAX_DESCRIPTORS)) +#define IS_REMOTE(x) (x->GetFd() < 0) +#define IS_MODULE_CREATED(x) (x->GetFd() == FD_MAGIC_NUMBER) /** Holds a module's Version information * The four members (set by the constructor only) indicate details as to the version number diff --git a/include/socket.h b/include/socket.h index 8fd2df0df..c00c7f131 100644 --- a/include/socket.h +++ b/include/socket.h @@ -26,10 +26,10 @@ #include #include #include -#include #include #include #include "inspircd_config.h" +#include "socketengine.h" namespace irc { @@ -138,4 +138,13 @@ namespace irc }; }; +class ListenSocket : public EventHandler +{ + protected: + InspIRCd* ServerInstance; + public: + ListenSocket(InspIRCd* Instance, int sockfd, irc::sockets::insp_sockaddr client, irc::sockets::insp_sockaddr server, int port, char* addr); + void HandleEvent(EventType et); +}; + #endif diff --git a/include/socketengine.h b/include/socketengine.h index 5ccfa801e..fd01b4cf8 100644 --- a/include/socketengine.h +++ b/include/socketengine.h @@ -21,34 +21,103 @@ #include #include #include "inspircd_config.h" -#include "globals.h" -#include "inspircd.h" - -/** - * Each of these values represents a socket - * type in our reference table (the reference - * table itself is only accessible to - * socketengine.cpp) - */ -const char X_EMPTY_SLOT = 0; -const char X_LISTEN = 1; -const char X_ESTAB_CLIENT = 2; -const char X_ESTAB_MODULE = 3; -const char X_ESTAB_DNS = 4; - -/** - * To indicate that a socket is readable, we - * mask its top bit with this X_READBIT value. - * The socket engine can handle two types of - * socket, readable and writeable (;error sockets - * are dealt with when read() and write() return - * negative or zero values). +#include "base.h" + +/** Types of event an EventHandler may receive. + * EVENT_READ is a readable file descriptor, + * and EVENT_WRITE is a writeable file descriptor. */ -const char X_READBIT = 0x80; +enum EventType +{ + EVENT_READ = 0, + EVENT_WRITE = 1 +}; class InspIRCd; -/** +/** This class is a basic I/O handler class. + * Any object which wishes to receive basic I/O events + * from the socketengine must derive from this class and + * implement the HandleEvent() method. The derived class + * must then be added to SocketEngine using the method + * SocketEngine::AddFd(), after which point the derived + * class will receive events to its HandleEvent() method. + * The derived class should also implement one of Readable() + * and Writeable(). In the current implementation, only + * Readable() is used. If this returns true, the socketengine + * inserts a readable socket. If it is false, the socketengine + * inserts a writeable socket. The derived class should never + * change the value this function returns without first + * deleting the socket from the socket engine. The only + * requirement beyond this for an event handler is that it + * must have a file descriptor. What this file descriptor + * is actually attached to is completely up to you. + */ +class EventHandler : public Extensible +{ + protected: + /** File descriptor. + * All events which can be handled + * must have a file descriptor. + * This allows you to add events for + * sockets, fifo's, pipes, and various + * other forms of IPC. + */ + int fd; + public: + /** Get the current file descriptor + * @return The file descriptor of this handler + */ + int GetFd(); + + /** Set a new file desciptor + * @param FD The new file descriptor. Do not + * call this method without first deleting the + * object from the SocketEngine if you have + * added it to a SocketEngine instance. + */ + void SetFd(int FD); + + /** Constructor + */ + EventHandler() {} + + /** Destructor + */ + virtual ~EventHandler() {} + + /** Override this function to indicate readability. + * @return This should return true if the function + * wishes to receive EVENT_READ events. Do not change + * what this function returns while the event handler + * is still added to a SocketEngine instance! + * If this function is unimplemented, the base class + * will return true. + */ + virtual bool Readable(); + + /** Override this function to indicate writeability. + * @return This should return true if the function + * wishes to receive EVENT_WRITE events. Do not change + * what this function returns while the event handler + * is still added to a SocketEngine instance! + * If this function is unimplemented, the base class + * will return false. + */ + virtual bool Writeable(); + + /** Process an I/O event. + * You MUST implement this function in your derived + * class, and it will be called whenever read or write + * events are received, depending on what your functions + * Readable() and Writeable() returns. + * @param et either one of EVENT_READ for read events, + * and EVENT_WRITE for write events. + */ + virtual void HandleEvent(EventType et) = 0; +}; + +/** Provides basic file-descriptor-based I/O support. * The actual socketengine class presents the * same interface on all operating systems, but * its private members and internal behaviour @@ -69,84 +138,103 @@ class InspIRCd; class SocketEngine : public Extensible { protected: + /** Owner/Creator + */ InspIRCd* ServerInstance; - int EngineHandle; /* Handle to the socket engine if needed */ - int CurrentSetSize; /* Current number of descriptors in the engine */ - char ref[MAX_DESCRIPTORS]; /* Reference table */ + /** Handle to socket engine, where needed. + */ + int EngineHandle; + /** Current number of descriptors in the engine + */ + int CurrentSetSize; + /** Reference table, contains all current handlers + */ + EventHandler* ref[MAX_DESCRIPTORS]; public: - /** Constructor + /** Constructor. * The constructor transparently initializes * the socket engine which the ircd is using. * Please note that if there is a catastrophic * failure (for example, you try and enable * epoll on a 2.4 linux kernel) then this * function may bail back to the shell. + * @param Instance The creator/owner of this object */ SocketEngine(InspIRCd* Instance); - /** Destructor + /** Destructor. * The destructor transparently tidies up * any resources used by the socket engine. */ virtual ~SocketEngine(); - /** Add a file descriptor to the engine + /** Add an EventHandler object to the engine. * Use AddFd to add a file descriptor to the * engine and have the socket engine monitor - * it. You must provide a type (see the consts - * in socketengine.h) and a boolean flag to - * indicate wether to watch this fd for read - * or write events (there is currently no - * need for support of both). - */ - 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 - * so that the type of the socket can be obtained. - * The core uses this to decide where to dispatch - * the event to. Please note that some engines - * such as select() have an upper limit of 1024 - * descriptors which may be active at any one time, - * where others such as kqueue have no practical - * limits at all. - */ - char GetType(int fd); + * it. You must provide an object derived from + * EventHandler which implements HandleEvent() + * and optionally Readable() and Writeable(). + * @param eh An event handling object to add + */ + virtual bool AddFd(EventHandler* eh); /** Returns the maximum number of file descriptors * you may store in the socket engine at any one time. + * @return The maximum fd value */ virtual int GetMaxFds(); /** Returns the number of file descriptor slots * which are available for storing fds. + * @return The number of remaining fd's */ virtual int GetRemainingFds(); - /** Delete a file descriptor from the engine - * This function call deletes a file descriptor + /** Delete an event handler from the engine. + * This function call deletes an EventHandler * from the engine, returning true if it succeeded - * and false if it failed. + * and false if it failed. This does not free the + * EventHandler pointer using delete, if this is + * required you must do this yourself. + * @param eh The event handler object to remove + * @return True if the event handler was removed */ - virtual bool DelFd(int fd); + virtual bool DelFd(EventHandler* eh); - /** Returns true if a socket exists in the socket - * engine's list. + /** 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* GetRef(int fd); + /** Waits for an event. * Please note that this doesnt wait long, only * a couple of milliseconds. It returns a list - * of active file descriptors in the vector - * fdlist which the core may then act upon. + * of active EventHandlers in the array fdlist + * which the core will then dispatch events to + * by calling their EventHandler::HandleEvent() + * methods with the neccessary EventType value. + * @param fdlist A pointer to a set of EventHandler + * classes. You should ensure that the array you pass + * is at least MAX_DESCRIPTORS in size, to accomodate + * for the maximum number of events which can occur. + * @return The number of events which have occured. */ - virtual int Wait(int* fdlist); + virtual int Wait(EventHandler** fdlist); - /** Returns the socket engines name + /** Returns the socket engines name. * This returns the name of the engine for use * in /VERSION responses. + * @return The socket engine name */ virtual std::string GetName(); }; diff --git a/include/socketengine_epoll.h b/include/socketengine_epoll.h index f675671e2..04ede734d 100644 --- a/include/socketengine_epoll.h +++ b/include/socketengine_epoll.h @@ -45,11 +45,11 @@ public: /** Delete an EPollEngine */ virtual ~EPollEngine(); - virtual bool AddFd(int fd, bool readable, char type); + virtual bool AddFd(EventHandler* eh); virtual int GetMaxFds(); virtual int GetRemainingFds(); - virtual bool DelFd(int fd); - virtual int Wait(int* fdlist); + virtual bool DelFd(EventHandler* eh); + virtual int Wait(EventHandler** fdlist); virtual std::string GetName(); }; diff --git a/include/socketengine_kqueue.h b/include/socketengine_kqueue.h index 2010949ed..392843697 100644 --- a/include/socketengine_kqueue.h +++ b/include/socketengine_kqueue.h @@ -49,11 +49,11 @@ public: /** Delete a KQueueEngine */ virtual ~KQueueEngine(); - virtual bool AddFd(int fd, bool readable, char type); + virtual bool AddFd(EventHandler* eh); virtual int GetMaxFds(); virtual int GetRemainingFds(); - virtual bool DelFd(int fd); - virtual int Wait(int* fdlist); + virtual bool DelFd(EventHandler* eh); + virtual int Wait(EventHandler** fdlist); virtual std::string GetName(); }; diff --git a/include/socketengine_select.h b/include/socketengine_select.h index a6db96ee5..d4b366b21 100644 --- a/include/socketengine_select.h +++ b/include/socketengine_select.h @@ -47,11 +47,11 @@ public: /** Delete a SelectEngine */ virtual ~SelectEngine(); - virtual bool AddFd(int fd, bool readable, char type); + virtual bool AddFd(EventHandler* eh); virtual int GetMaxFds(); virtual int GetRemainingFds(); - virtual bool DelFd(int fd); - virtual int Wait(int* fdlist); + virtual bool DelFd(EventHandler* eh); + virtual int Wait(EventHandler** fdlist); virtual std::string GetName(); }; diff --git a/include/users.h b/include/users.h index b292cd998..3b1c2a3ec 100644 --- a/include/users.h +++ b/include/users.h @@ -743,6 +743,11 @@ class userrec : public connection */ void ShowRULES(); + /** Handle socket event. + * From EventHandler class. + */ + void HandleEvent(EventType et); + /** Default destructor */ virtual ~userrec(); -- cgit v1.2.3