]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/socketengine.h
Add #include <sys/select.h> to select engine, we probably need it
[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 "globals.h"
25 #include "inspircd.h"
26
27 /**
28  * Each of these values represents a socket
29  * type in our reference table (the reference
30  * table itself is only accessible to
31  * socketengine.cpp)
32  */
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;
38 const char X_ESTAB_CLASSDNS     = 5;
39
40 /**
41  * To indicate that a socket is readable, we
42  * mask its top bit with this X_READBIT value.
43  * The socket engine can handle two types of
44  * socket, readable and writeable (error sockets
45  * are dealt with when read() and write() return
46  * negative or zero values).
47  */
48 const char X_READBIT            = 0x80;
49
50 /**
51  * The actual socketengine class presents the
52  * same interface on all operating systems, but
53  * its private members and internal behaviour
54  * should be treated as blackboxed, and vary
55  * from system to system and upon the config
56  * settings chosen by the server admin. The current
57  * version supports select, epoll and kqueue.
58  * The configure script will enable a socket engine
59  * based upon what OS is detected, and will derive
60  * a class from SocketEngine based upon what it finds.
61  * The derived classes file will also implement a
62  * classfactory, SocketEngineFactory, which will
63  * create a derived instance of SocketEngine using
64  * polymorphism so that the core and modules do not
65  * have to be aware of which SocketEngine derived
66  * class they are using.
67  */
68 class SocketEngine : public Extensible
69 {
70 protected:
71         int EngineHandle;                       /* Handle to the socket engine if needed */
72         int CurrentSetSize;                     /* Current number of descriptors in the engine */
73         char ref[MAX_DESCRIPTORS];              /* Reference table */
74 public:
75
76         /** Constructor
77          * The constructor transparently initializes
78          * the socket engine which the ircd is using.
79          * Please note that if there is a catastrophic
80          * failure (for example, you try and enable
81          * epoll on a 2.4 linux kernel) then this
82          * function may bail back to the shell.
83          */
84         SocketEngine();
85
86         /** Destructor
87          * The destructor transparently tidies up
88          * any resources used by the socket engine.
89          */
90         virtual ~SocketEngine();
91
92         /** Add a file descriptor to the engine
93          * Use AddFd to add a file descriptor to the
94          * engine and have the socket engine monitor
95          * it. You must provide a type (see the consts
96          * in socketengine.h) and a boolean flag to
97          * indicate wether to watch this fd for read
98          * or write events (there is currently no
99          * need for support of both).
100          */
101         virtual bool AddFd(int fd, bool readable, char type);
102
103         /** Returns the type value for this file descriptor
104          * This function masks off the X_READBIT value
105          * so that the type of the socket can be obtained.
106          * The core uses this to decide where to dispatch
107          * the event to. Please note that some engines
108          * such as select() have an upper limit of 1024
109          * descriptors which may be active at any one time,
110          * where others such as kqueue have no practical
111          * limits at all.
112          */
113         char GetType(int fd);
114
115         /** Returns the maximum number of file descriptors
116          * you may store in the socket engine at any one time.
117          */
118         virtual int GetMaxFds();
119
120         /** Returns the number of file descriptor slots
121          * which are available for storing fds.
122          */
123         virtual int GetRemainingFds();
124
125         /** Delete a file descriptor from the engine
126          * This function call deletes a file descriptor
127          * from the engine, returning true if it succeeded
128          * and false if it failed.
129          */
130         virtual bool DelFd(int fd);
131
132         /** Returns true if a socket exists in the socket
133          * engine's list.
134          */
135         bool HasFd(int fd);
136
137         /** Waits for an event.
138          * Please note that this doesnt wait long, only
139          * a couple of milliseconds. It returns a list
140          * of active file descriptors in the vector
141          * fdlist which the core may then act upon.
142          */
143         virtual int Wait(int* fdlist);
144
145         /** Returns the socket engines name
146          * This returns the name of the engine for use
147          * in /VERSION responses.
148          */
149         virtual std::string GetName();
150 };
151
152 #endif