]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/socketengine.h
EventHandler class, an abstraction for raw i/o
[user/henk/code/inspircd.git] / include / socketengine.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15 */
16
17 #ifndef __SOCKETENGINE__
18 #define __SOCKETENGINE__
19
20 #include <vector>
21 #include <string>
22 #include <map>
23 #include "inspircd_config.h"
24 #include "base.h"
25
26 /** Types of event an EventHandler may receive.
27  * EVENT_READ is a readable file descriptor,
28  * and EVENT_WRITE is a writeable file descriptor.
29  */
30 enum EventType
31 {
32         EVENT_READ      =       0,
33         EVENT_WRITE     =       1
34 };
35
36 class InspIRCd;
37
38 /** This class is a basic I/O handler class.
39  * Any object which wishes to receive basic I/O events
40  * from the socketengine must derive from this class and
41  * implement the HandleEvent() method. The derived class
42  * must then be added to SocketEngine using the method
43  * SocketEngine::AddFd(), after which point the derived
44  * class will receive events to its HandleEvent() method.
45  * The derived class should also implement one of Readable()
46  * and Writeable(). In the current implementation, only
47  * Readable() is used. If this returns true, the socketengine
48  * inserts a readable socket. If it is false, the socketengine
49  * inserts a writeable socket. The derived class should never
50  * change the value this function returns without first
51  * deleting the socket from the socket engine. The only
52  * requirement beyond this for an event handler is that it
53  * must have a file descriptor. What this file descriptor
54  * is actually attached to is completely up to you.
55  */
56 class EventHandler : public Extensible
57 {
58  protected:
59         /** File descriptor.
60          * All events which can be handled
61          * must have a file descriptor.
62          * This allows you to add events for
63          * sockets, fifo's, pipes, and various
64          * other forms of IPC.
65          */
66         int fd;
67  public:
68         /** Get the current file descriptor
69          * @return The file descriptor of this handler
70          */
71         int GetFd();
72
73         /** Set a new file desciptor
74          * @param FD The new file descriptor. Do not
75          * call this method without first deleting the
76          * object from the SocketEngine if you have
77          * added it to a SocketEngine instance.
78          */
79         void SetFd(int FD);
80
81         /** Constructor
82          */
83         EventHandler() {}
84
85         /** Destructor
86          */
87         virtual ~EventHandler() {}
88
89         /** Override this function to indicate readability.
90          * @return This should return true if the function
91          * wishes to receive EVENT_READ events. Do not change
92          * what this function returns while the event handler
93          * is still added to a SocketEngine instance!
94          * If this function is unimplemented, the base class
95          * will return true.
96          */
97         virtual bool Readable();
98
99         /** Override this function to indicate writeability.
100          * @return This should return true if the function
101          * wishes to receive EVENT_WRITE events. Do not change
102          * what this function returns while the event handler
103          * is still added to a SocketEngine instance!
104          * If this function is unimplemented, the base class
105          * will return false.
106          */
107         virtual bool Writeable();
108
109         /** Process an I/O event.
110          * You MUST implement this function in your derived
111          * class, and it will be called whenever read or write
112          * events are received, depending on what your functions
113          * Readable() and Writeable() returns.
114          * @param et either one of EVENT_READ for read events,
115          * and EVENT_WRITE for write events.
116          */
117         virtual void HandleEvent(EventType et) = 0;
118 };
119
120 /** Provides basic file-descriptor-based I/O support.
121  * The actual socketengine class presents the
122  * same interface on all operating systems, but
123  * its private members and internal behaviour
124  * should be treated as blackboxed, and vary
125  * from system to system and upon the config
126  * settings chosen by the server admin. The current
127  * version supports select, epoll and kqueue.
128  * The configure script will enable a socket engine
129  * based upon what OS is detected, and will derive
130  * a class from SocketEngine based upon what it finds.
131  * The derived classes file will also implement a
132  * classfactory, SocketEngineFactory, which will
133  * create a derived instance of SocketEngine using
134  * polymorphism so that the core and modules do not
135  * have to be aware of which SocketEngine derived
136  * class they are using.
137  */
138 class SocketEngine : public Extensible
139 {
140 protected:
141         /** Owner/Creator
142          */
143         InspIRCd* ServerInstance;
144         /** Handle to socket engine, where needed.
145          */
146         int EngineHandle;
147         /** Current number of descriptors in the engine
148          */
149         int CurrentSetSize;
150         /** Reference table, contains all current handlers
151          */
152         EventHandler* ref[MAX_DESCRIPTORS];
153 public:
154
155         /** Constructor.
156          * The constructor transparently initializes
157          * the socket engine which the ircd is using.
158          * Please note that if there is a catastrophic
159          * failure (for example, you try and enable
160          * epoll on a 2.4 linux kernel) then this
161          * function may bail back to the shell.
162          * @param Instance The creator/owner of this object
163          */
164         SocketEngine(InspIRCd* Instance);
165
166         /** Destructor.
167          * The destructor transparently tidies up
168          * any resources used by the socket engine.
169          */
170         virtual ~SocketEngine();
171
172         /** Add an EventHandler object to the engine.
173          * Use AddFd to add a file descriptor to the
174          * engine and have the socket engine monitor
175          * it. You must provide an object derived from
176          * EventHandler which implements HandleEvent()
177          * and optionally Readable() and Writeable().
178          * @param eh An event handling object to add
179          */
180         virtual bool AddFd(EventHandler* eh);
181
182         /** Returns the maximum number of file descriptors
183          * you may store in the socket engine at any one time.
184          * @return The maximum fd value
185          */
186         virtual int GetMaxFds();
187
188         /** Returns the number of file descriptor slots
189          * which are available for storing fds.
190          * @return The number of remaining fd's
191          */
192         virtual int GetRemainingFds();
193
194         /** Delete an event handler from the engine.
195          * This function call deletes an EventHandler
196          * from the engine, returning true if it succeeded
197          * and false if it failed. This does not free the
198          * EventHandler pointer using delete, if this is
199          * required you must do this yourself.
200          * @param eh The event handler object to remove
201          * @return True if the event handler was removed
202          */
203         virtual bool DelFd(EventHandler* eh);
204
205         /** Returns true if a file descriptor exists in
206          * the socket engine's list.
207          * @param fd The event handler to look for
208          * @return True if this fd has an event handler
209          */
210         bool HasFd(int fd);
211
212         /** Returns the EventHandler attached to a specific fd.
213          * If the fd isnt in the socketengine, returns NULL.
214          * @param fd The event handler to look for
215          * @return A pointer to the event handler, or NULL
216          */
217         EventHandler* GetRef(int fd);
218
219         /** Waits for an event.
220          * Please note that this doesnt wait long, only
221          * a couple of milliseconds. It returns a list
222          * of active EventHandlers in the array fdlist
223          * which the core will then dispatch events to
224          * by calling their EventHandler::HandleEvent()
225          * methods with the neccessary EventType value.
226          * @param fdlist A pointer to a set of EventHandler
227          * classes. You should ensure that the array you pass
228          * is at least MAX_DESCRIPTORS in size, to accomodate
229          * for the maximum number of events which can occur.
230          * @return The number of events which have occured.
231          */
232         virtual int Wait(EventHandler** fdlist);
233
234         /** Returns the socket engines name.
235          * This returns the name of the engine for use
236          * in /VERSION responses.
237          * @return The socket engine name
238          */
239         virtual std::string GetName();
240 };
241
242 #endif