1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2005 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__
22 #include "inspircd_config.h"
26 #include <sys/epoll.h>
30 #include <sys/types.h>
31 #include <sys/event.h>
36 * Each of these values represents a socket
37 * type in our reference table (the reference
38 * table itself is only accessible to
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;
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).
55 const char X_READBIT = 0x80;
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.
68 std::vector<int> fds; /* List of file descriptors being monitored */
69 int EngineHandle; /* Handle to the socket engine if needed */
71 fd_set wfdset, rfdset; /* Readable and writeable sets for select() */
74 struct kevent ke_list[65535]; /* Up to 64k sockets for kqueue */
75 struct timespec ts; /* kqueue delay value */
78 struct epoll_event events[65535]; /* Up to 64k sockets for epoll */
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.
94 * The destructor transparently tidies up
95 * any resources used by the socket engine.
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).
108 bool AddFd(int fd, bool readable, char type);
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
120 char GetType(int fd);
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.
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.
135 bool Wait(std::vector<int> &fdlist);
137 /** Returns the socket engines name
138 * This returns the name of the engine for use
139 * in /VERSION responses.
141 std::string GetName();