]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/socketengine_iocp.h
formatting and ^M
[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 600
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 /** Socket overlapped event types
27  */
28 enum SocketIOEvent
29 {
30         /** Read ready */
31         SOCKET_IO_EVENT_READ_READY                      = 0,
32         /** Write ready */
33         SOCKET_IO_EVENT_WRITE_READY                     = 1,
34         /** Accept ready */
35         SOCKET_IO_EVENT_ACCEPT                          = 2,
36         /** Error occured */
37         SOCKET_IO_EVENT_ERROR                           = 3,
38         /** Number of events */
39         NUM_SOCKET_IO_EVENTS                            = 4,
40 };
41
42 /** Represents a windows overlapped IO event
43  */
44 class Overlapped
45 {
46  public:
47         /** Overlap event */
48         OVERLAPPED m_overlap;
49         /** Type of event */
50         SocketIOEvent m_event;
51 #ifdef WIN64
52         /** Parameters */
53         unsigned __int64 m_params;
54 #else
55         /** Parameters */
56         unsigned long m_params;
57 #endif
58         /** Create an overlapped event
59          */
60         Overlapped(SocketIOEvent ev, int params) : m_event(ev), m_params(params)
61         {
62                 memset(&m_overlap, 0, sizeof(OVERLAPPED));
63         }
64 };
65
66 /** Specific to UDP sockets with overlapped IO
67  */
68 struct udp_overlap
69 {
70         unsigned char udp_buffer[600];
71         unsigned long udp_len;
72         sockaddr udp_sockaddr[2];
73         unsigned long udp_sockaddr_len;
74 };
75
76 /** Specific to accepting sockets with overlapped IO
77  */
78 struct accept_overlap
79 {
80         int socket;
81         char buf[1024];
82 };
83
84 /** Implementation of SocketEngine that implements windows IO Completion Ports
85  */
86 class IOCPEngine : public SocketEngine
87 {
88         /** Creates a "fake" file descriptor for use with an IOCP socket.
89          * This is a little slow, but it isnt called too much. We'll fix it
90          * in a future release.
91          * @return -1 if there are no free slots, and an integer if it finds one.
92          */
93         __inline int GenerateFd(int RealFd)
94         {
95                 int index_hash = RealFd % MAX_DESCRIPTORS;
96                 if(ref[index_hash] == 0)
97                         return index_hash;
98                 else
99                 {
100                         register int i = 0;
101                         for(; i < MAX_DESCRIPTORS; ++i)
102                                 if(ref[i] == 0)
103                                         return i;
104                 }
105                 return -1;
106         }
107         
108         /** Global I/O completion port that sockets attach to.
109          */
110         HANDLE m_completionPort;
111
112         /** This is kinda shitty... :/ for getting an address from a real fd. 
113          */
114         map<int, EventHandler*> m_binding;
115
116 public:
117         /** Creates an IOCP Socket Engine
118          * @param Instance The creator of this object
119          */
120         IOCPEngine(InspIRCd* Instance);
121
122         /** Deletes an IOCP socket engine and all the attached sockets
123          */
124         ~IOCPEngine();
125
126         /** Adds an event handler to the completion port, and sets up initial events.
127          * @param eh EventHandler to add
128          * @return True if success, false if no room
129          */
130         bool AddFd(EventHandler* eh);
131
132         /** Gets the maximum number of file descriptors that this engine can handle.
133          * @return The number of file descriptors
134          */
135         __inline int GetMaxFds() { return MAX_DESCRIPTORS; }
136
137         /** Gets the number of free/remaining file descriptors under this engine.
138          * @return Remaining count
139          */
140         __inline int GetRemainingFds()
141         {
142                 register int count = 0;
143                 register int i = 0;                     
144                 for(; i < MAX_DESCRIPTORS; ++i)
145                         if(ref[i] == 0)
146                                 ++count;
147                 return count;
148         }
149
150         /** Removes a file descriptor from the set, preventing it from receiving any more events
151          * @return True if remove was successful, false otherwise
152          */
153         bool DelFd(EventHandler* eh, bool force = false);
154
155         /** Called every loop to handle input/output events for all sockets under this engine
156          * @return The number of "changed" sockets.
157          */
158         int DispatchEvents();
159
160         /** Gets the name of this socket engine as a string.
161          * @return string of socket engine name
162          */
163         std::string GetName();
164
165         /** Queues a Write event on the specified event handler.
166          * @param eh EventHandler that needs data sent on
167          */
168         void WantWrite(EventHandler* eh);
169
170         /** Posts a completion event on the specified socket.
171          * @param eh EventHandler for message
172          * @param type Event Type
173          * @param param Event Parameter
174          * @return True if added, false if not
175          */
176         bool PostCompletionEvent(EventHandler* eh, SocketIOEvent type, int param);
177
178         /** Posts a read event on the specified socket
179          * @param eh EventHandler (socket)
180          */
181         void PostReadEvent(EventHandler* eh);
182
183         /** Posts an accept event on the specified socket
184          * @param eh EventHandler (socket)
185          */
186         void PostAcceptEvent(EventHandler* eh);
187
188         /** Returns the EventHandler attached to a specific fd.
189          * If the fd isnt in the socketengine, returns NULL.
190          * @param fd The event handler to look for
191          * @return A pointer to the event handler, or NULL
192          */
193         EventHandler* GetRef(int fd);
194
195         /** Returns true if a file descriptor exists in
196          * the socket engine's list.
197          * @param fd The event handler to look for
198          * @return True if this fd has an event handler
199          */
200         bool HasFd(int fd);
201
202         /** Returns the EventHandler attached to a specific fd.
203          * If the fd isnt in the socketengine, returns NULL.
204          * @param fd The event handler to look for
205          * @return A pointer to the event handler, or NULL
206          */
207         EventHandler* GetIntRef(int fd);
208
209         /** Holds the preallocated buffer passed to WSARecvFrom
210          * function. Yes, I know, it's a dirty hack.
211          */
212         udp_overlap * udp_ov;
213 };
214
215 /** Creates a SocketEngine
216  */
217 class SocketEngineFactory
218 {
219 public:
220         /** Create a new instance of SocketEngine based on IOCPEngine
221          */
222         SocketEngine* Create(InspIRCd* Instance) { return new IOCPEngine(Instance); }
223 };
224
225 #endif
226