]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/socketengine.h
Added comments
[user/henk/code/inspircd.git] / include / socketengine.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2005 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 "inspircd_config.h"
23 #include "globals.h"
24 #include "inspircd.h"
25 #ifdef USE_EPOLL
26 #include <sys/epoll.h>
27 #define EP_DELAY 5
28 #endif
29 #ifdef USE_KQUEUE
30 #include <sys/types.h>
31 #include <sys/event.h>
32 #include <sys/time.h>
33 #endif
34
35 /**
36  * Each of these values represents a socket
37  * type in our reference table (the reference
38  * table itself is only accessible to
39  * socketengine.cpp)
40  */
41 const char X_EMPTY_SLOT         = 0;
42 const char X_LISTEN             = 1;
43 const char X_ESTAB_CLIENT       = 2;
44 const char X_ESTAB_MODULE       = 3;
45 const char X_ESTAB_DNS          = 4;
46
47 /**
48  * To indicate that a socket is readable, we
49  * mask its top bit with this X_READBIT value.
50  * The socket engine can handle two types of
51  * socket, readable and writeable (error sockets
52  * are dealt with when read() and write() return
53  * negative or zero values).
54  */
55 const char X_READBIT            = 0x80;
56
57 /**
58  * The actual socketengine class presents the
59  * same interface on all operating systems, but
60  * its private members and internal behaviour
61  * should be treated as blackboxed, and vary
62  * from system to system and upon the config
63  * settings chosen by the server admin. The current
64  * version supports select, epoll and kqueue.
65  */
66 class SocketEngine {
67
68         std::vector<int> fds;                   /* List of file descriptors being monitored */
69         int EngineHandle;                       /* Handle to the socket engine if needed */
70 #ifdef USE_SELECT
71         fd_set wfdset, rfdset;                  /* Readable and writeable sets for select() */
72 #endif
73 #ifdef USE_KQUEUE
74         struct kevent ke_list[65535];           /* Up to 64k sockets for kqueue */
75         struct timespec ts;                     /* kqueue delay value */
76 #endif
77 #ifdef USE_EPOLL
78         struct epoll_event events[65535];       /* Up to 64k sockets for epoll */
79 #endif
80
81 public:
82
83         /** Constructor
84          * The constructor transparently initializes
85          * the socket engine which the ircd is using.
86          * Please note that if there is a catastrophic
87          * failure (for example, you try and enable
88          * epoll on a 2.4 linux kernel) then this
89          * function may bail back to the shell.
90          */
91         SocketEngine();
92
93         /** Destructor
94          * The destructor transparently tidies up
95          * any resources used by the socket engine.
96          */
97         ~SocketEngine();
98
99         /** Add a file descriptor to the engine
100          * Use AddFd to add a file descriptor to the
101          * engine and have the socket engine monitor
102          * it. You must provide a type (see the consts
103          * in socketengine.h) and a boolean flag to
104          * indicate wether to watch this fd for read
105          * or write events (there is currently no
106          * need for support of both).
107          */
108         bool AddFd(int fd, bool readable, char type);
109
110         /** Returns the type value for this file descriptor
111          * This function masks off the X_READBIT value
112          * so that the type of the socket can be obtained.
113          * The core uses this to decide where to dispatch
114          * the event to. Please note that some engines
115          * such as select() have an upper limit of 1024
116          * descriptors which may be active at any one time,
117          * where others such as kqueue have no practical
118          * limits at all.
119          */
120         char GetType(int fd);
121
122         /** Delete a file descriptor f rom the engine
123          * This function call deletes a file descriptor
124          * from the engine, returning true if it succeeded
125          * and false if it failed.
126          */
127         bool DelFd(int fd);
128
129         /** Waits for an event.
130          * Please note that this doesnt wait long, only
131          * a couple of milliseconds. It returns a list
132          * of active file descriptors in the vector
133          * fdlist which the core may then act upon.
134          */
135         bool Wait(std::vector<int> &fdlist);
136
137         /** Returns the socket engines name
138          * This returns the name of the engine for use
139          * in /VERSION responses.
140          */
141         std::string GetName();
142 };
143
144 #endif