]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/socketengine_iocp.h
Make nt the default channelmode if <options:defaultmodes> is not present.
[user/henk/code/inspircd.git] / include / socketengine_iocp.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef __SOCKETENGINE_IOCP__
15 #define __SOCKETENGINE_IOCP__
16
17 #define READ_BUFFER_SIZE 500
18 #define USING_IOCP 1
19
20 #include "inspircd_config.h"
21 #include "inspircd_win32wrapper.h"
22 #include "globals.h"
23 #include "inspircd.h"
24 #include "socketengine.h"
25
26 enum SocketIOEvent
27 {
28         SOCKET_IO_EVENT_READ_READY                      = 0,
29         SOCKET_IO_EVENT_WRITE_READY                     = 1,
30         SOCKET_IO_EVENT_ACCEPT                          = 2,
31         SOCKET_IO_EVENT_ERROR                           = 3,
32         NUM_SOCKET_IO_EVENTS                            = 4,
33 };
34
35 class Overlapped
36 {
37  public:
38         OVERLAPPED m_overlap;
39         SocketIOEvent m_event;
40         int m_params;
41
42         Overlapped(SocketIOEvent ev, int params) : m_event(ev), m_params(params)
43         {
44                 memset(&m_overlap, 0, sizeof(OVERLAPPED));
45         }
46 };
47
48 struct accept_overlap
49 {
50         int socket;
51         char buf[1024];
52 };
53
54 class IOCPEngine : public SocketEngine
55 {
56         /** Creates a "fake" file descriptor for use with an IOCP socket.
57          * This is a little slow, but it isnt called too much. We'll fix it
58          * in a future release.
59          * @return -1 if there are no free slots, and an integer if it finds one.
60          */
61         __inline int GenerateFd(int RealFd)
62         {
63                 int index_hash = RealFd % MAX_DESCRIPTORS;
64                 if(ref[index_hash] == 0)
65                         return index_hash;
66                 else
67                 {
68                         register int i = 0;
69                         for(; i < MAX_DESCRIPTORS; ++i)
70                                 if(ref[i] == 0)
71                                         return i;
72                 }
73                 return -1;
74         }
75         
76         /** Global I/O completion port that sockets attach to.
77          */
78         HANDLE m_completionPort;
79
80         /** This is kinda shitty... :/ for getting an address from a real fd. 
81          */
82         map<int, EventHandler*> m_binding;
83
84 public:
85         /** Creates an IOCP Socket Engine
86          * @param Instance The creator of this object
87          */
88         IOCPEngine(InspIRCd* Instance);
89
90         /** Deletes an IOCP socket engine and all the attached sockets
91          */
92         ~IOCPEngine();
93
94         /** Adds an event handler to the completion port, and sets up initial events.
95          * @param eh EventHandler to add
96          * @return True if success, false if no room
97          */
98         bool AddFd(EventHandler* eh);
99
100         /** Gets the maximum number of file descriptors that this engine can handle.
101          * @return The number of file descriptors
102          */
103         __inline int GetMaxFds() { return MAX_DESCRIPTORS; }
104
105         /** Gets the number of free/remaining file descriptors under this engine.
106          * @return Remaining count
107          */
108         __inline int GetRemainingFds()
109         {
110                 register int count = 0;
111                 register int i = 0;                     
112                 for(; i < MAX_DESCRIPTORS; ++i)
113                         if(ref[i] == 0)
114                                 ++count;
115                 return count;
116         }
117
118         /** Removes a file descriptor from the set, preventing it from receiving any more events
119          * @return True if remove was successful, false otherwise
120          */
121         bool DelFd(EventHandler* eh, bool force = false);
122
123         /** Called every loop to handle input/output events for all sockets under this engine
124          * @return The number of "changed" sockets.
125          */
126         int DispatchEvents();
127
128         /** Gets the name of this socket engine as a string.
129          * @return string of socket engine name
130          */
131         std::string GetName();
132
133         /** Queues a Write event on the specified event handler.
134          * @param eh EventHandler that needs data sent on
135          */
136         void WantWrite(EventHandler* eh);
137
138         /** Posts a completion event on the specified socket.
139          * @param eh EventHandler for message
140          * @param type Event Type
141          * @param param Event Parameter
142          * @return True if added, false if not
143          */
144         bool PostCompletionEvent(EventHandler* eh, SocketIOEvent type, int param);
145
146         /** Posts a read event on the specified socket
147          * @param eh EventHandler (socket)
148          */
149         void PostReadEvent(EventHandler* eh);
150
151         /** Posts an accept event on the specified socket
152          * @param eh EventHandler (socket)
153          */
154         void PostAcceptEvent(EventHandler* eh);
155
156         /** Returns the EventHandler attached to a specific fd.
157          * If the fd isnt in the socketengine, returns NULL.
158          * @param fd The event handler to look for
159          * @return A pointer to the event handler, or NULL
160          */
161         EventHandler* GetRef(int fd);
162
163         /** Returns true if a file descriptor exists in
164          * the socket engine's list.
165          * @param fd The event handler to look for
166          * @return True if this fd has an event handler
167          */
168         bool HasFd(int fd);
169
170         /** Returns the EventHandler attached to a specific fd.
171          * If the fd isnt in the socketengine, returns NULL.
172          * @param fd The event handler to look for
173          * @return A pointer to the event handler, or NULL
174          */
175         EventHandler* GetIntRef(int fd);
176 };
177
178 //typedef void(*OpHandler)(EventHandler)
179 /** Event Handler Array
180  */
181
182 /** Creates a SocketEngine
183  */
184 class SocketEngineFactory
185 {
186 public:
187         /** Create a new instance of SocketEngine based on IOCPEngine
188          */
189         SocketEngine* Create(InspIRCd* Instance) { return new IOCPEngine(Instance); }
190 };
191
192 #endif
193