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"
27 #include <sys/epoll.h>
31 #include <sys/types.h>
32 #include <sys/event.h>
37 * Each of these values represents a socket
38 * type in our reference table (the reference
39 * table itself is only accessible to
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;
49 * To indicate that a socket is readable, we
50 * mask its top bit with this X_READBIT value.
51 * The socket engine can handle two types of
52 * socket, readable and writeable (error sockets
53 * are dealt with when read() and write() return
54 * negative or zero values).
56 const char X_READBIT = 0x80;
59 * The actual socketengine class presents the
60 * same interface on all operating systems, but
61 * its private members and internal behaviour
62 * should be treated as blackboxed, and vary
63 * from system to system and upon the config
64 * settings chosen by the server admin. The current
65 * version supports select, epoll and kqueue.
67 class SocketEngine : public Extensible
70 int EngineHandle; /* Handle to the socket engine if needed */
71 int CurrentSetSize; /* Current number of descriptors in the engine */
73 std::map<int,int> fds; /* List of file descriptors being monitored */
74 fd_set wfdset, rfdset; /* Readable and writeable sets for select() */
77 struct kevent ke_list[MAX_DESCRIPTORS]; /* Up to 64k sockets for kqueue */
78 struct timespec ts; /* kqueue delay value */
81 struct epoll_event events[MAX_DESCRIPTORS]; /* Up to 64k sockets for epoll */
87 * The constructor transparently initializes
88 * the socket engine which the ircd is using.
89 * Please note that if there is a catastrophic
90 * failure (for example, you try and enable
91 * epoll on a 2.4 linux kernel) then this
92 * function may bail back to the shell.
97 * The destructor transparently tidies up
98 * any resources used by the socket engine.
102 /** Add a file descriptor to the engine
103 * Use AddFd to add a file descriptor to the
104 * engine and have the socket engine monitor
105 * it. You must provide a type (see the consts
106 * in socketengine.h) and a boolean flag to
107 * indicate wether to watch this fd for read
108 * or write events (there is currently no
109 * need for support of both).
111 bool AddFd(int fd, bool readable, char type);
113 /** Returns the type value for this file descriptor
114 * This function masks off the X_READBIT value
115 * so that the type of the socket can be obtained.
116 * The core uses this to decide where to dispatch
117 * the event to. Please note that some engines
118 * such as select() have an upper limit of 1024
119 * descriptors which may be active at any one time,
120 * where others such as kqueue have no practical
123 char GetType(int fd);
125 /** Returns the maximum number of file descriptors
126 * you may store in the socket engine at any one time.
130 /** Returns the number of file descriptor slots
131 * which are available for storing fds.
133 int GetRemainingFds();
135 /** Delete a file descriptor from the engine
136 * This function call deletes a file descriptor
137 * from the engine, returning true if it succeeded
138 * and false if it failed.
142 /** Returns true if a socket exists in the socket
147 /** Waits for an event.
148 * Please note that this doesnt wait long, only
149 * a couple of milliseconds. It returns a list
150 * of active file descriptors in the vector
151 * fdlist which the core may then act upon.
153 int Wait(int* fdlist);
155 /** Returns the socket engines name
156 * This returns the name of the engine for use
157 * in /VERSION responses.
159 std::string GetName();