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