]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/socketengine_iocp.h
Metaphorically add a metaphorical comment to the metaphorical code. Thank god its...
[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()
62         {
63                 register int i = 0;
64                 for(; i < MAX_DESCRIPTORS; ++i)
65                         if(ref[i] == 0)
66                                 return i;
67                 return -1;
68         }
69         
70         /** Global I/O completion port that sockets attach to.
71          */
72         HANDLE m_completionPort;
73
74         /** This is kinda shitty... :/ for getting an address from a real fd. 
75          */
76         map<int, EventHandler*> m_binding;
77
78 public:
79         /** Creates an IOCP Socket Engine
80          * @param Instance The creator of this object
81          */
82         IOCPEngine(InspIRCd* Instance);
83
84         /** Deletes an IOCP socket engine and all the attached sockets
85          */
86         ~IOCPEngine();
87
88         /** Adds an event handler to the completion port, and sets up initial events.
89          * @param eh EventHandler to add
90          * @return True if success, false if no room
91          */
92         bool AddFd(EventHandler* eh);
93
94         /** Gets the maximum number of file descriptors that this engine can handle.
95          * @return The number of file descriptors
96          */
97         __inline int GetMaxFds() { return MAX_DESCRIPTORS; }
98
99         /** Gets the number of free/remaining file descriptors under this engine.
100          * @return Remaining count
101          */
102         __inline int GetRemainingFds()
103         {
104                 register int count = 0;
105                 register int i = 0;                     
106                 for(; i < MAX_DESCRIPTORS; ++i)
107                         if(ref[i] == 0)
108                                 ++count;
109                 return count;
110         }
111
112         /** Removes a file descriptor from the set, preventing it from receiving any more events
113          * @return True if remove was successful, false otherwise
114          */
115         bool DelFd(EventHandler* eh, bool force = false);
116
117         /** Called every loop to handle input/output events for all sockets under this engine
118          * @return The number of "changed" sockets.
119          */
120         int DispatchEvents();
121
122         /** Gets the name of this socket engine as a string.
123          * @return string of socket engine name
124          */
125         std::string GetName();
126
127         /** Queues a Write event on the specified event handler.
128          * @param eh EventHandler that needs data sent on
129          */
130         void WantWrite(EventHandler* eh);
131
132         /** Posts a completion event on the specified socket.
133          * @param eh EventHandler for message
134          * @param type Event Type
135          * @param param Event Parameter
136          * @return True if added, false if not
137          */
138         bool PostCompletionEvent(EventHandler* eh, SocketIOEvent type, int param);
139
140         /** Posts a read event on the specified socket
141          * @param eh EventHandler (socket)
142          */
143         void PostReadEvent(EventHandler* eh);
144
145         /** Posts an accept event on the specified socket
146          * @param eh EventHandler (socket)
147          */
148         void PostAcceptEvent(EventHandler* eh);
149
150         /** Returns the EventHandler attached to a specific fd.
151          * If the fd isnt in the socketengine, returns NULL.
152          * @param fd The event handler to look for
153          * @return A pointer to the event handler, or NULL
154          */
155         EventHandler* GetRef(int fd);
156
157         /** Returns true if a file descriptor exists in
158          * the socket engine's list.
159          * @param fd The event handler to look for
160          * @return True if this fd has an event handler
161          */
162         bool HasFd(int fd);
163
164         /** Returns the EventHandler attached to a specific fd.
165          * If the fd isnt in the socketengine, returns NULL.
166          * @param fd The event handler to look for
167          * @return A pointer to the event handler, or NULL
168          */
169         EventHandler* GetIntRef(int fd);
170 };
171
172 //typedef void(*OpHandler)(EventHandler)
173 /** Event Handler Array
174  */
175
176 /** Creates a SocketEngine
177  */
178 class SocketEngineFactory
179 {
180 public:
181         /** Create a new instance of SocketEngine based on IOCPEngine
182          */
183         SocketEngine* Create(InspIRCd* Instance) { return new IOCPEngine(Instance); }
184 };
185
186 #endif
187