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