1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #ifndef __SOCKETENGINE_IOCP__
15 #define __SOCKETENGINE_IOCP__
17 #define READ_BUFFER_SIZE 500
20 #include "inspircd_config.h"
21 #include "inspircd_win32wrapper.h"
24 #include "socketengine.h"
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,
39 SocketIOEvent m_event;
42 Overlapped(SocketIOEvent ev, int params) : m_event(ev), m_params(params)
44 memset(&m_overlap, 0, sizeof(OVERLAPPED));
54 class IOCPEngine : public SocketEngine
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.
61 __inline int GenerateFd(int RealFd)
63 int index_hash = RealFd % MAX_DESCRIPTORS;
64 if(ref[index_hash] == 0)
69 for(; i < MAX_DESCRIPTORS; ++i)
76 /** Global I/O completion port that sockets attach to.
78 HANDLE m_completionPort;
80 /** This is kinda shitty... :/ for getting an address from a real fd.
82 map<int, EventHandler*> m_binding;
85 /** Creates an IOCP Socket Engine
86 * @param Instance The creator of this object
88 IOCPEngine(InspIRCd* Instance);
90 /** Deletes an IOCP socket engine and all the attached sockets
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
98 bool AddFd(EventHandler* eh);
100 /** Gets the maximum number of file descriptors that this engine can handle.
101 * @return The number of file descriptors
103 __inline int GetMaxFds() { return MAX_DESCRIPTORS; }
105 /** Gets the number of free/remaining file descriptors under this engine.
106 * @return Remaining count
108 __inline int GetRemainingFds()
110 register int count = 0;
112 for(; i < MAX_DESCRIPTORS; ++i)
118 /** Removes a file descriptor from the set, preventing it from receiving any more events
119 * @return True if remove was successful, false otherwise
121 bool DelFd(EventHandler* eh, bool force = false);
123 /** Called every loop to handle input/output events for all sockets under this engine
124 * @return The number of "changed" sockets.
126 int DispatchEvents();
128 /** Gets the name of this socket engine as a string.
129 * @return string of socket engine name
131 std::string GetName();
133 /** Queues a Write event on the specified event handler.
134 * @param eh EventHandler that needs data sent on
136 void WantWrite(EventHandler* eh);
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
144 bool PostCompletionEvent(EventHandler* eh, SocketIOEvent type, int param);
146 /** Posts a read event on the specified socket
147 * @param eh EventHandler (socket)
149 void PostReadEvent(EventHandler* eh);
151 /** Posts an accept event on the specified socket
152 * @param eh EventHandler (socket)
154 void PostAcceptEvent(EventHandler* eh);
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
161 EventHandler* GetRef(int fd);
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
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
175 EventHandler* GetIntRef(int fd);
178 //typedef void(*OpHandler)(EventHandler)
179 /** Event Handler Array
182 /** Creates a SocketEngine
184 class SocketEngineFactory
187 /** Create a new instance of SocketEngine based on IOCPEngine
189 SocketEngine* Create(InspIRCd* Instance) { return new IOCPEngine(Instance); }