1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 #ifndef __SOCKETENGINE__
18 #define __SOCKETENGINE__
23 #include "inspircd_config.h"
28 * Each of these values represents a socket
29 * type in our reference table (the reference
30 * table itself is only accessible to
33 const char X_EMPTY_SLOT = 0;
34 const char X_LISTEN = 1;
35 const char X_ESTAB_CLIENT = 2;
36 const char X_ESTAB_MODULE = 3;
37 const char X_ESTAB_DNS = 4;
40 * To indicate that a socket is readable, we
41 * mask its top bit with this X_READBIT value.
42 * The socket engine can handle two types of
43 * socket, readable and writeable (;error sockets
44 * are dealt with when read() and write() return
45 * negative or zero values).
47 const char X_READBIT = 0x80;
52 * The actual socketengine class presents the
53 * same interface on all operating systems, but
54 * its private members and internal behaviour
55 * should be treated as blackboxed, and vary
56 * from system to system and upon the config
57 * settings chosen by the server admin. The current
58 * version supports select, epoll and kqueue.
59 * The configure script will enable a socket engine
60 * based upon what OS is detected, and will derive
61 * a class from SocketEngine based upon what it finds.
62 * The derived classes file will also implement a
63 * classfactory, SocketEngineFactory, which will
64 * create a derived instance of SocketEngine using
65 * polymorphism so that the core and modules do not
66 * have to be aware of which SocketEngine derived
67 * class they are using.
69 class SocketEngine : public Extensible
72 InspIRCd* ServerInstance;
73 int EngineHandle; /* Handle to the socket engine if needed */
74 int CurrentSetSize; /* Current number of descriptors in the engine */
75 char ref[MAX_DESCRIPTORS]; /* Reference table */
79 * The constructor transparently initializes
80 * the socket engine which the ircd is using.
81 * Please note that if there is a catastrophic
82 * failure (for example, you try and enable
83 * epoll on a 2.4 linux kernel) then this
84 * function may bail back to the shell.
86 SocketEngine(InspIRCd* Instance);
89 * The destructor transparently tidies up
90 * any resources used by the socket engine.
92 virtual ~SocketEngine();
94 /** Add a file descriptor to the engine
95 * Use AddFd to add a file descriptor to the
96 * engine and have the socket engine monitor
97 * it. You must provide a type (see the consts
98 * in socketengine.h) and a boolean flag to
99 * indicate wether to watch this fd for read
100 * or write events (there is currently no
101 * need for support of both).
103 virtual bool AddFd(int fd, bool readable, char type);
105 /** Returns the type value for this file descriptor
106 * This function masks off the X_READBIT value
107 * so that the type of the socket can be obtained.
108 * The core uses this to decide where to dispatch
109 * the event to. Please note that some engines
110 * such as select() have an upper limit of 1024
111 * descriptors which may be active at any one time,
112 * where others such as kqueue have no practical
115 char GetType(int fd);
117 /** Returns the maximum number of file descriptors
118 * you may store in the socket engine at any one time.
120 virtual int GetMaxFds();
122 /** Returns the number of file descriptor slots
123 * which are available for storing fds.
125 virtual int GetRemainingFds();
127 /** Delete a file descriptor from the engine
128 * This function call deletes a file descriptor
129 * from the engine, returning true if it succeeded
130 * and false if it failed.
132 virtual bool DelFd(int fd);
134 /** Returns true if a socket exists in the socket
139 /** Waits for an event.
140 * Please note that this doesnt wait long, only
141 * a couple of milliseconds. It returns a list
142 * of active file descriptors in the vector
143 * fdlist which the core may then act upon.
145 virtual int Wait(int* fdlist);
147 /** Returns the socket engines name
148 * This returns the name of the engine for use
149 * in /VERSION responses.
151 virtual std::string GetName();